sashdev commited on
Commit
a30dc01
Β·
verified Β·
1 Parent(s): 7fff3d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -52
app.py CHANGED
@@ -46,6 +46,7 @@ def plagiarism_removal(text):
46
  if word.lower() in stop_words or word.lower() in exclude_words or word in string.punctuation:
47
  return word
48
 
 
49
  synonyms = set()
50
  for syn in wordnet.synsets(word):
51
  for lemma in syn.lemmas():
@@ -53,7 +54,7 @@ def plagiarism_removal(text):
53
  synonyms.add(lemma.name())
54
 
55
  pos_tag_word = nltk.pos_tag([word])[0]
56
-
57
  if pos_tag_word[1] in exclude_tags:
58
  return word
59
 
@@ -80,37 +81,6 @@ def plagiarism_removal(text):
80
 
81
  return " ".join(corrected_text)
82
 
83
- def change_voice(text):
84
- doc = nlp(text)
85
- sentences = list(doc.sents)
86
- modified_sentences = []
87
-
88
- for sent in sentences:
89
- # Randomly choose to convert to active or passive voice
90
- if random.choice([True, False]): # Change voice
91
- subject = None
92
- verb = None
93
- obj = None
94
-
95
- for token in sent:
96
- if token.dep_ == 'nsubj': # Subject
97
- subject = token
98
- elif token.dep_ == 'ROOT': # Main verb
99
- verb = token
100
- elif token.dep_ == 'dobj': # Object
101
- obj = token
102
-
103
- if subject and verb and obj:
104
- # Active to Passive
105
- passive_sentence = f"{obj.text} was {verb.lemma_} by {subject.text}"
106
- modified_sentences.append(passive_sentence)
107
- else:
108
- modified_sentences.append(sent.text) # No change if structure doesn't match
109
- else:
110
- modified_sentences.append(sent.text) # No change
111
-
112
- return ' '.join(modified_sentences)
113
-
114
  def predict_en(text):
115
  res = pipeline_en(text)[0]
116
  return res['label'], res['score']
@@ -219,24 +189,41 @@ def correct_spelling(text):
219
  corrected_words.append(word)
220
  return ' '.join(corrected_words)
221
 
222
- # Main function for paraphrasing and grammar correction
223
  def paraphrase_and_correct(text):
224
- # Change narrative voice randomly
225
- voice_changed_text = change_voice(text)
226
-
227
- cleaned_text = remove_redundant_words(voice_changed_text)
228
- plag_removed = plagiarism_removal(cleaned_text)
229
- paraphrased_text = capitalize_sentences_and_nouns(plag_removed)
230
- paraphrased_text = force_first_letter_capital(paraphrased_text)
231
- paraphrased_text = correct_article_errors(paraphrased_text)
232
- paraphrased_text = correct_tense_errors(paraphrased_text)
233
- paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
234
- paraphrased_text = fix_possessives(paraphrased_text)
235
- paraphrased_text = fix_punctuation_spacing(paraphrased_text)
236
- paraphrased_text = correct_spelling(paraphrased_text)
237
-
238
- return paraphrased_text
239
-
240
- # Gradio interface setup
241
- iface = gr.Interface(fn=paraphrase_and_correct, inputs="text", outputs="text", title="Text Paraphraser and Corrector", description="A tool to paraphrase and correct grammar in your text.")
242
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  if word.lower() in stop_words or word.lower() in exclude_words or word in string.punctuation:
47
  return word
48
 
49
+ # Find synonyms
50
  synonyms = set()
51
  for syn in wordnet.synsets(word):
52
  for lemma in syn.lemmas():
 
54
  synonyms.add(lemma.name())
55
 
56
  pos_tag_word = nltk.pos_tag([word])[0]
57
+
58
  if pos_tag_word[1] in exclude_tags:
59
  return word
60
 
 
81
 
82
  return " ".join(corrected_text)
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  def predict_en(text):
85
  res = pipeline_en(text)[0]
86
  return res['label'], res['score']
 
189
  corrected_words.append(word)
190
  return ' '.join(corrected_words)
191
 
 
192
  def paraphrase_and_correct(text):
193
+ paragraphs = text.split("\n\n") # Split by paragraphs
194
+
195
+ # Process each paragraph separately
196
+ processed_paragraphs = []
197
+ for paragraph in paragraphs:
198
+ cleaned_text = remove_redundant_words(paragraph)
199
+ plag_removed = plagiarism_removal(cleaned_text)
200
+ paraphrased_text = capitalize_sentences_and_nouns(plag_removed)
201
+ paraphrased_text = force_first_letter_capital(paraphrased_text)
202
+ paraphrased_text = correct_article_errors(paraphrased_text)
203
+ paraphrased_text = correct_tense_errors(paraphrased_text)
204
+ paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
205
+ paraphrased_text = fix_possessives(paraphrased_text)
206
+ paraphrased_text = correct_spelling(paraphrased_text)
207
+ paraphrased_text = fix_punctuation_spacing(paraphrased_text)
208
+ processed_paragraphs.append(paraphrased_text)
209
+
210
+ return "\n\n".join(processed_paragraphs) # Reassemble the text with paragraphs
211
+
212
+ # Gradio app setup
213
+ with gr.Blocks() as demo:
214
+ with gr.Tab("AI Detection"):
215
+ t1 = gr.Textbox(lines=5, label='Text')
216
+ button1 = gr.Button("πŸ€– Predict!")
217
+ label1 = gr.Textbox(lines=1, label='Predicted Label πŸŽƒ')
218
+ score1 = gr.Textbox(lines=1, label='Prob')
219
+
220
+ button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
221
+
222
+ with gr.Tab("Paraphrasing & Grammar Correction"):
223
+ t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
224
+ button2 = gr.Button("πŸ”„ Paraphrase and Correct")
225
+ result2 = gr.Textbox(lines=5, label='Corrected Text')
226
+
227
+ button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
228
+
229
+ demo.launch(share=True)