imseldrith commited on
Commit
17cb555
1 Parent(s): d6a3f78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -8
app.py CHANGED
@@ -6,6 +6,24 @@ import random
6
  import nltk
7
  nltk.download('punkt')
8
  nltk.download('wordnet')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def paraphrase_text(text, synonyms_num=5, random_synonym=True):
10
  # Tokenize the text
11
  tokens = word_tokenize(text)
@@ -38,6 +56,7 @@ def paraphrase_text(text, synonyms_num=5, random_synonym=True):
38
  paraphrased_text = ' '.join(paraphrased_tokens)
39
  return paraphrased_text
40
 
 
41
  def paraphrase_html(html_text, synonyms_num, random_synonym):
42
  # Parse the HTML using BeautifulSoup
43
  soup = BeautifulSoup(html_text, 'html.parser')
@@ -50,14 +69,101 @@ def paraphrase_html(html_text, synonyms_num, random_synonym):
50
  paraphrased_html = str(soup)
51
  return paraphrased_html
52
 
53
- st.set_page_config(page_title="HTML Paraphraser", page_icon=":pencil2:")
54
- st.title("HTML Paraphraser")
55
 
56
- synonyms_num = st.sidebar.slider("Number of Synonyms", min_value=1, max_value=10, value=5, step=1)
57
- random_synonym = st.sidebar.checkbox("Use Random Synonym", value=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- html_text = st.text_area("Enter HTML text to paraphrase", height=300)
 
 
 
 
 
 
 
 
60
 
61
- if st.button("Paraphrase"):
62
- paraphrased_html = paraphrase_html(html_text, synonyms_num, random_synonym)
63
- st.write(paraphrased_html, unsafe_allow_html=True)
 
6
  import nltk
7
  nltk.download('punkt')
8
  nltk.download('wordnet')
9
+
10
+ import streamlit as st
11
+ from bs4 import BeautifulSoup
12
+ from nltk.tokenize import word_tokenize
13
+ from nltk.corpus import wordnet
14
+ from transformers import pipeline
15
+
16
+ # Text analytics
17
+ from textblob import TextBlob
18
+ from textatistic import Textatistic
19
+
20
+ # Integration with other tools
21
+ from grammarbot import GrammarBotClient
22
+
23
+ # Error handling and logging
24
+ import logging
25
+
26
+
27
  def paraphrase_text(text, synonyms_num=5, random_synonym=True):
28
  # Tokenize the text
29
  tokens = word_tokenize(text)
 
56
  paraphrased_text = ' '.join(paraphrased_tokens)
57
  return paraphrased_text
58
 
59
+
60
  def paraphrase_html(html_text, synonyms_num, random_synonym):
61
  # Parse the HTML using BeautifulSoup
62
  soup = BeautifulSoup(html_text, 'html.parser')
 
69
  paraphrased_html = str(soup)
70
  return paraphrased_html
71
 
 
 
72
 
73
+ def analyze_text(text):
74
+ # Text analytics using textblob and textatistic
75
+ blob = TextBlob(text)
76
+ sentiment = blob.sentiment.polarity
77
+ subjectivity = blob.sentiment.subjectivity
78
+ text_stats = Textatistic(text)
79
+ readability_score = text_stats.flesch_score
80
+
81
+ return sentiment, subjectivity, readability_score
82
+
83
+
84
+ def grammar_check(text):
85
+ # Integration with grammarbot tool
86
+ client = GrammarBotClient()
87
+ response = client.check(text)
88
+
89
+ matches = response['matches']
90
+ if not matches:
91
+ return "No grammar errors found."
92
+ else:
93
+ message = "Grammar errors found:\n"
94
+ for match in matches:
95
+ message += f"{match['message']} at line {match['replacements'][0]['startLine']}\n"
96
+ return message
97
+
98
+
99
+ def paraphrase_with_model(text, model):
100
+ # Advanced paraphrasing using pre-trained models
101
+ paraphrase_pipeline = pipeline("text2text-generation", model=model, tokenizer=model, device=0 if st.config.experimental.get_query_params()['device'] == 'cpu' else -1)
102
+ paraphrased_text = paraphrase_pipeline(text, max_length=50, do_sample=True, temperature=0.9)[0]['generated_text']
103
+ return paraphrased_text
104
+
105
+
106
+ st.set_page_config(page_title="HTML Paraphraser and Analyzer", page_icon=":pencil2:")
107
+
108
+ st.sidebar.title("HTML Paraphraser and Analyzer")
109
+
110
+ st.title("HTML Paraphraser and Analyzer")
111
+ html_input = st.text_area("Enter HTML to paraphrase", height=250)
112
+
113
+ synonyms_num = st.slider("Number of synonyms per word", min_value=1, max_value=10, value=5, step=1)
114
+
115
+ random_synonym = st.checkbox("Use random synonym", value=True)
116
+
117
+ #Add a button to paraphrase the HTML
118
+ if st.button("Paraphrase HTML"):
119
+ try:
120
+ paraphrased_html = paraphrase_html(html_input, synonyms_num, random_synonym)
121
+ st.write(paraphrased_html, unsafe_allow_html=True)
122
+ except Exception as e:
123
+ logging.exception("Error occurred while paraphrasing HTML")
124
+ st.error("An error occurred while paraphrasing the HTML. Please try again.")
125
+
126
+ #Add a text area for the user to input the text to analyze
127
+ text_input = st.text_area("Enter text to analyze")
128
+
129
+ #Add a button to analyze the text
130
+ if st.button("Analyze Text"):
131
+ try:
132
+ sentiment, subjectivity, readability_score = analyze_text(text_input)
133
+ st.write(f"Sentiment: {sentiment:.2f}")
134
+ st.write(f"Subjectivity: {subjectivity:.2f}")
135
+ st.write(f"Readability Score: {readability_score:.2f}")
136
+ except Exception as e:
137
+ logging.exception("Error occurred while analyzing text")
138
+ st.error("An error occurred while analyzing the text. Please try again.")
139
+
140
+ #Add a button to check grammar using GrammarBot
141
+ if st.button("Check Grammar"):
142
+ try:
143
+ grammar_check_result = grammar_check(text_input)
144
+ st.write(grammar_check_result)
145
+ except Exception as e:
146
+ logging.exception("Error occurred while checking grammar")
147
+ st.error("An error occurred while checking grammar. Please try again.")
148
+
149
+ #Add a selectbox for choosing the pre-trained model for advanced paraphrasing
150
+ models = {
151
+ "GPT-2": "gpt2",
152
+ "GPT-Neo": "EleutherAI/gpt-neo-1.3B",
153
+ "T5": "t5-base",
154
+ "Pegasus": "google/pegasus-large",
155
+ }
156
+ model_name = st.selectbox("Choose a pre-trained model for advanced paraphrasing", options=list(models.keys()))
157
 
158
+ #Add a button to paraphrase the text using the selected model
159
+ if st.button("Advanced Paraphrasing"):
160
+ try:
161
+ model = models[model_name]
162
+ paraphrased_text = paraphrase_with_model(text_input, model)
163
+ st.write(paraphrased_text)
164
+ except Exception as e:
165
+ logging.exception("Error occurred while performing advanced paraphrasing")
166
+ st.error("An error occurred while performing advanced paraphrasing. Please try again.")
167
 
168
+ #Set up logging to capture any errors
169
+ logging.basicConfig(filename='error.log', level=logging.ERROR, format='%(asctime)s %(message)s')