Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
141 |
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
142 |
-
synonym
|
143 |
elif token.tag_ == "VBZ": # Third-person singular present
|
144 |
-
synonym
|
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 |
-
#
|
184 |
-
text = re.sub(r'
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
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
|
212 |
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
213 |
-
synonym
|
214 |
elif token.tag_ == "VBZ": # Third-person singular present
|
215 |
-
synonym
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
iface = gr.Interface(
|
255 |
-
fn=
|
256 |
-
inputs=
|
257 |
-
outputs=
|
258 |
-
title="
|
259 |
-
description="
|
260 |
)
|
261 |
|
262 |
-
|
|
|
|
|
|
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()
|