sashtech commited on
Commit
a1d4c88
·
verified ·
1 Parent(s): bf4908b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -136,12 +136,12 @@ def replace_with_synonym(token):
136
 
137
  if synonyms:
138
  synonym = synonyms[0]
139
- if token.tag_ == "VBG": # Present participle
140
- synonym += 'ing'
141
  elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
142
- synonym += 'ed'
143
  elif token.tag_ == "VBZ": # Third-person singular present
144
- synonym += 's'
145
  return synonym
146
  return token.text
147
 
@@ -175,15 +175,18 @@ def correct_spelling(text):
175
  corrected_words = []
176
  for word in words:
177
  corrected_word = spell.correction(word)
178
- corrected_words.append(corrected_word if corrected_word else word)
179
  return ' '.join(corrected_words)
180
 
181
  # Function to correct punctuation issues
182
  def correct_punctuation(text):
183
- # Fix spacing before punctuation
184
- text = re.sub(r'\s([?.!,";:])', r'\1', text)
185
- # Add missing commas in compound sentences
186
- text = re.sub(r'(\w+)\s+and\s+(\w+)', r'\1, and \2', text)
 
 
 
187
  return text
188
 
189
  # Function to rephrase text and replace words with their synonyms while maintaining form
@@ -207,12 +210,12 @@ def rephrase_with_synonyms(text):
207
  if synonyms:
208
  synonym = synonyms[0] # Just using the first synonym for simplicity
209
  if token.pos_ == "VERB":
210
- if token.tag_ == "VBG": # Present participle
211
- synonym += 'ing'
212
  elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
213
- synonym += 'ed'
214
  elif token.tag_ == "VBZ": # Third-person singular present
215
- synonym += 's'
216
  elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
217
  synonym += 's' if not synonym.endswith('s') else ""
218
  rephrased_text.append(synonym)
@@ -241,6 +244,7 @@ def paraphrase_and_correct(text):
241
  # Correct spelling and punctuation
242
  paraphrased_text = correct_spelling(paraphrased_text)
243
  paraphrased_text = correct_punctuation(paraphrased_text)
 
244
 
245
  # Rephrase with synonyms
246
  paraphrased_text = rephrase_with_synonyms(paraphrased_text)
@@ -250,13 +254,21 @@ def paraphrase_and_correct(text):
250
 
251
  return final_text
252
 
253
- # Gradio interface
 
 
 
 
 
 
254
  iface = gr.Interface(
255
- fn=paraphrase_and_correct,
256
- inputs=gr.Textbox(label="Input Text", lines=5, placeholder="Enter text here..."),
257
- outputs=gr.Textbox(label="Output Text", lines=5),
258
- title="Text Paraphraser and Grammar Corrector",
259
- description="This tool paraphrases the input text, corrects grammar, improves punctuation, and enhances overall clarity."
260
  )
261
 
262
- iface.launch()
 
 
 
136
 
137
  if synonyms:
138
  synonym = synonyms[0]
139
+ if token.tag_ == "VBG": # Present participle (e.g., running)
140
+ synonym = synonym + 'ing'
141
  elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
142
+ synonym = synonym + 'ed'
143
  elif token.tag_ == "VBZ": # Third-person singular present
144
+ synonym = synonym + 's'
145
  return synonym
146
  return token.text
147
 
 
175
  corrected_words = []
176
  for word in words:
177
  corrected_word = spell.correction(word)
178
+ corrected_words.append(corrected_word if corrected_word else word) # Keep original if correction is None
179
  return ' '.join(corrected_words)
180
 
181
  # Function to correct punctuation issues
182
  def correct_punctuation(text):
183
+ text = re.sub(r'\s+([?.!,";:])', r'\1', text) # Remove space before punctuation
184
+ text = re.sub(r'([?.!,";:])\s+', r'\1 ', text) # Ensure a single space after punctuation
185
+ return text
186
+
187
+ # Function to ensure correct handling of possessive forms
188
+ def handle_possessives(text):
189
+ text = re.sub(r"\b(\w+)'s\b", r"\1's", text) # Preserve possessive forms
190
  return text
191
 
192
  # Function to rephrase text and replace words with their synonyms while maintaining form
 
210
  if synonyms:
211
  synonym = synonyms[0] # Just using the first synonym for simplicity
212
  if token.pos_ == "VERB":
213
+ if token.tag_ == "VBG": # Present participle (e.g., running)
214
+ synonym = synonym + 'ing'
215
  elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
216
+ synonym = synonym + 'ed'
217
  elif token.tag_ == "VBZ": # Third-person singular present
218
+ synonym = synonym + 's'
219
  elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
220
  synonym += 's' if not synonym.endswith('s') else ""
221
  rephrased_text.append(synonym)
 
244
  # Correct spelling and punctuation
245
  paraphrased_text = correct_spelling(paraphrased_text)
246
  paraphrased_text = correct_punctuation(paraphrased_text)
247
+ paraphrased_text = handle_possessives(paraphrased_text) # Handle possessives
248
 
249
  # Rephrase with synonyms
250
  paraphrased_text = rephrase_with_synonyms(paraphrased_text)
 
254
 
255
  return final_text
256
 
257
+ # Gradio Interface
258
+ def process_text(input_text):
259
+ ai_label, ai_score = predict_en(input_text)
260
+ corrected_text = paraphrase_and_correct(input_text)
261
+ return ai_label, ai_score, corrected_text
262
+
263
+ # Create Gradio interface
264
  iface = gr.Interface(
265
+ fn=process_text,
266
+ inputs="text",
267
+ outputs=["text", "number", "text"],
268
+ title="AI Content Detection and Grammar Correction",
269
+ description="Enter text to detect AI-generated content and correct grammar."
270
  )
271
 
272
+ # Launch the Gradio app
273
+ if __name__ == "__main__":
274
+ iface.launch()