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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -38
app.py CHANGED
@@ -11,7 +11,6 @@ from spellchecker import SpellChecker
11
  import re
12
  import string
13
  import random
14
- from language_tool_python import LanguageTool
15
 
16
  # Download necessary NLTK data
17
  nltk.download('punkt')
@@ -35,9 +34,6 @@ pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt
35
  # Initialize the spell checker
36
  spell = SpellChecker()
37
 
38
- # Initialize LanguageTool
39
- tool = LanguageTool('en-US')
40
-
41
  # Ensure the SpaCy model is installed
42
  try:
43
  nlp = spacy.load("en_core_web_sm")
@@ -78,12 +74,43 @@ def plagiarism_removal(text):
78
  corrected_text = []
79
  for i in range(len(final_text)):
80
  if final_text[i] in string.punctuation and i > 0:
81
- corrected_text[-1] += final_text[i]
82
  else:
83
  corrected_text.append(final_text[i])
84
 
85
  return " ".join(corrected_text)
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def predict_en(text):
88
  res = pipeline_en(text)[0]
89
  return res['label'], res['score']
@@ -192,13 +219,12 @@ def correct_spelling(text):
192
  corrected_words.append(word)
193
  return ' '.join(corrected_words)
194
 
195
- def grammar_check(text):
196
- matches = tool.check(text)
197
- corrected_text = language_tool_python.utils.correct(text, matches)
198
- return corrected_text
199
-
200
  def paraphrase_and_correct(text):
201
- cleaned_text = remove_redundant_words(text)
 
 
 
202
  plag_removed = plagiarism_removal(cleaned_text)
203
  paraphrased_text = capitalize_sentences_and_nouns(plag_removed)
204
  paraphrased_text = force_first_letter_capital(paraphrased_text)
@@ -206,33 +232,11 @@ def paraphrase_and_correct(text):
206
  paraphrased_text = correct_tense_errors(paraphrased_text)
207
  paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
208
  paraphrased_text = fix_possessives(paraphrased_text)
209
- paraphrased_text = correct_spelling(paraphrased_text)
210
  paraphrased_text = fix_punctuation_spacing(paraphrased_text)
211
- paraphrased_text = grammar_check(paraphrased_text)
212
-
213
  return paraphrased_text
214
 
215
- with gr.Blocks() as demo:
216
- with gr.Tab("AI Detection"):
217
- t1 = gr.Textbox(lines=5, label='Text')
218
- button1 = gr.Button("πŸ€– Predict!")
219
- label1 = gr.Textbox(lines=1, label='Predicted Label πŸŽƒ')
220
- score1 = gr.Textbox(lines=1, label='Prob')
221
-
222
- button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
223
-
224
- with gr.Tab("Paraphrasing & Grammar Correction"):
225
- t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
226
- button2 = gr.Button("πŸ”„ Paraphrase and Correct")
227
- result2 = gr.Textbox(lines=5, label='Corrected Text')
228
-
229
- button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
230
-
231
- if __name__ == "__main__":
232
- try:
233
- subprocess.run(["java", "-version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
234
- except FileNotFoundError:
235
- print("Java is not installed. Please install Java to use LanguageTool.")
236
- exit(1)
237
-
238
- demo.launch(share=True)
 
11
  import re
12
  import string
13
  import random
 
14
 
15
  # Download necessary NLTK data
16
  nltk.download('punkt')
 
34
  # Initialize the spell checker
35
  spell = SpellChecker()
36
 
 
 
 
37
  # Ensure the SpaCy model is installed
38
  try:
39
  nlp = spacy.load("en_core_web_sm")
 
74
  corrected_text = []
75
  for i in range(len(final_text)):
76
  if final_text[i] in string.punctuation and i > 0:
77
+ corrected_text[-1] += final_text[i]
78
  else:
79
  corrected_text.append(final_text[i])
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
  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)
 
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()