Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import subprocess
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
|
|
|
| 9 |
|
| 10 |
# Initialize the English text classification pipeline for AI detection
|
| 11 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
@@ -29,14 +30,6 @@ def predict_en(text):
|
|
| 29 |
res = pipeline_en(text)[0]
|
| 30 |
return res['label'], res['score']
|
| 31 |
|
| 32 |
-
# Function to get synonyms using NLTK WordNet
|
| 33 |
-
def get_synonyms_nltk(word, pos):
|
| 34 |
-
synsets = wordnet.synsets(word, pos=pos)
|
| 35 |
-
if synsets:
|
| 36 |
-
lemmas = synsets[0].lemmas()
|
| 37 |
-
return [lemma.name() for lemma in lemmas]
|
| 38 |
-
return []
|
| 39 |
-
|
| 40 |
# Function to remove redundant and meaningless words
|
| 41 |
def remove_redundant_words(text):
|
| 42 |
doc = nlp(text)
|
|
@@ -44,15 +37,20 @@ def remove_redundant_words(text):
|
|
| 44 |
filtered_text = [token.text for token in doc if token.text.lower() not in meaningless_words]
|
| 45 |
return ' '.join(filtered_text)
|
| 46 |
|
| 47 |
-
# Function to fix spacing
|
| 48 |
def fix_punctuation_spacing(text):
|
| 49 |
-
|
| 50 |
-
text =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return text
|
| 52 |
|
| 53 |
-
#
|
| 54 |
def capitalize_sentences_and_nouns(text):
|
| 55 |
-
text = fix_punctuation_spacing(text)
|
| 56 |
doc = nlp(text)
|
| 57 |
corrected_text = []
|
| 58 |
|
|
@@ -69,18 +67,11 @@ def capitalize_sentences_and_nouns(text):
|
|
| 69 |
|
| 70 |
return ' '.join(corrected_text)
|
| 71 |
|
| 72 |
-
# Function to
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
for token in doc:
|
| 78 |
-
if token.dep_ == 'poss' and token.head.pos_ == 'NOUN':
|
| 79 |
-
corrected_text.append(f"{token.text}'s")
|
| 80 |
-
else:
|
| 81 |
-
corrected_text.append(token.text)
|
| 82 |
-
|
| 83 |
-
return ' '.join(corrected_text)
|
| 84 |
|
| 85 |
# Function to correct tense errors in a sentence
|
| 86 |
def correct_tense_errors(text):
|
|
@@ -94,28 +85,6 @@ def correct_tense_errors(text):
|
|
| 94 |
corrected_text.append(token.text)
|
| 95 |
return ' '.join(corrected_text)
|
| 96 |
|
| 97 |
-
# Function to correct singular/plural errors
|
| 98 |
-
def correct_singular_plural_errors(text):
|
| 99 |
-
doc = nlp(text)
|
| 100 |
-
corrected_text = []
|
| 101 |
-
|
| 102 |
-
for token in doc:
|
| 103 |
-
if token.pos_ == "NOUN":
|
| 104 |
-
if token.tag_ == "NN": # Singular noun
|
| 105 |
-
if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
|
| 106 |
-
corrected_text.append(token.lemma_ + 's')
|
| 107 |
-
else:
|
| 108 |
-
corrected_text.append(token.text)
|
| 109 |
-
elif token.tag_ == "NNS": # Plural noun
|
| 110 |
-
if any(child.text.lower() in ['a', 'one'] for child in token.head.children):
|
| 111 |
-
corrected_text.append(token.lemma_)
|
| 112 |
-
else:
|
| 113 |
-
corrected_text.append(token.text)
|
| 114 |
-
else:
|
| 115 |
-
corrected_text.append(token.text)
|
| 116 |
-
|
| 117 |
-
return ' '.join(corrected_text)
|
| 118 |
-
|
| 119 |
# Function to check and correct article errors
|
| 120 |
def correct_article_errors(text):
|
| 121 |
doc = nlp(text)
|
|
@@ -133,42 +102,6 @@ def correct_article_errors(text):
|
|
| 133 |
corrected_text.append(token.text)
|
| 134 |
return ' '.join(corrected_text)
|
| 135 |
|
| 136 |
-
# Function to get the correct synonym while maintaining verb form
|
| 137 |
-
def replace_with_synonym(token):
|
| 138 |
-
pos = None
|
| 139 |
-
if token.pos_ == "VERB":
|
| 140 |
-
pos = wordnet.VERB
|
| 141 |
-
elif token.pos_ == "NOUN":
|
| 142 |
-
pos = wordnet.NOUN
|
| 143 |
-
elif token.pos_ == "ADJ":
|
| 144 |
-
pos = wordnet.ADJ
|
| 145 |
-
elif token.pos_ == "ADV":
|
| 146 |
-
pos = wordnet.ADV
|
| 147 |
-
|
| 148 |
-
synonyms = get_synonyms_nltk(token.lemma_, pos)
|
| 149 |
-
|
| 150 |
-
if synonyms:
|
| 151 |
-
synonym = synonyms[0]
|
| 152 |
-
if token.tag_ == "VBG": # Present participle (e.g., running)
|
| 153 |
-
synonym += 'ing'
|
| 154 |
-
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
| 155 |
-
synonym += 'ed'
|
| 156 |
-
elif token.tag_ == "VBZ": # Third-person singular present
|
| 157 |
-
synonym += 's'
|
| 158 |
-
return synonym
|
| 159 |
-
return token.text
|
| 160 |
-
|
| 161 |
-
# Function to check for and avoid double negatives
|
| 162 |
-
def correct_double_negatives(text):
|
| 163 |
-
doc = nlp(text)
|
| 164 |
-
corrected_text = []
|
| 165 |
-
for token in doc:
|
| 166 |
-
if token.text.lower() == "not" and any(child.text.lower() == "never" for child in token.head.children):
|
| 167 |
-
corrected_text.append("always")
|
| 168 |
-
else:
|
| 169 |
-
corrected_text.append(token.text)
|
| 170 |
-
return ' '.join(corrected_text)
|
| 171 |
-
|
| 172 |
# Function to ensure subject-verb agreement
|
| 173 |
def ensure_subject_verb_agreement(text):
|
| 174 |
doc = nlp(text)
|
|
@@ -194,44 +127,7 @@ def correct_spelling(text):
|
|
| 194 |
corrected_words.append(word) # Keep the original word if correction is None
|
| 195 |
return ' '.join(corrected_words)
|
| 196 |
|
| 197 |
-
#
|
| 198 |
-
def rephrase_with_synonyms(text):
|
| 199 |
-
doc = nlp(text)
|
| 200 |
-
rephrased_text = []
|
| 201 |
-
|
| 202 |
-
for token in doc:
|
| 203 |
-
pos_tag = None
|
| 204 |
-
if token.pos_ == "NOUN":
|
| 205 |
-
pos_tag = wordnet.NOUN
|
| 206 |
-
elif token.pos_ == "VERB":
|
| 207 |
-
pos_tag = wordnet.VERB
|
| 208 |
-
elif token.pos_ == "ADJ":
|
| 209 |
-
pos_tag = wordnet.ADJ
|
| 210 |
-
elif token.pos_ == "ADV":
|
| 211 |
-
pos_tag = wordnet.ADV
|
| 212 |
-
|
| 213 |
-
if pos_tag:
|
| 214 |
-
synonyms = get_synonyms_nltk(token.text, pos_tag)
|
| 215 |
-
if synonyms:
|
| 216 |
-
synonym = synonyms[0] # Just using the first synonym for simplicity
|
| 217 |
-
if token.pos_ == "VERB":
|
| 218 |
-
if token.tag_ == "VBG": # Present participle (e.g., running)
|
| 219 |
-
synonym += 'ing'
|
| 220 |
-
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
| 221 |
-
synonym += 'ed'
|
| 222 |
-
elif token.tag_ == "VBZ": # Third-person singular present
|
| 223 |
-
synonym += 's'
|
| 224 |
-
elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
|
| 225 |
-
synonym += 's' if not synonym.endswith('s') else ""
|
| 226 |
-
rephrased_text.append(synonym)
|
| 227 |
-
else:
|
| 228 |
-
rephrased_text.append(token.text)
|
| 229 |
-
else:
|
| 230 |
-
rephrased_text.append(token.text)
|
| 231 |
-
|
| 232 |
-
return ' '.join(rephrased_text)
|
| 233 |
-
|
| 234 |
-
# Function to paraphrase and correct grammar with enhanced accuracy
|
| 235 |
def paraphrase_and_correct(text):
|
| 236 |
# Remove meaningless or redundant words first
|
| 237 |
cleaned_text = remove_redundant_words(text)
|
|
@@ -239,25 +135,24 @@ def paraphrase_and_correct(text):
|
|
| 239 |
# Capitalize sentences and nouns
|
| 240 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
| 241 |
|
| 242 |
-
# Ensure first letter of each sentence is capitalized
|
| 243 |
-
paraphrased_text =
|
| 244 |
|
| 245 |
# Apply grammatical corrections
|
| 246 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 247 |
-
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
| 248 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 249 |
-
paraphrased_text = correct_double_negatives(paraphrased_text)
|
| 250 |
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
paraphrased_text =
|
|
|
|
| 254 |
|
| 255 |
# Correct spelling errors
|
| 256 |
paraphrased_text = correct_spelling(paraphrased_text)
|
| 257 |
|
| 258 |
return paraphrased_text
|
| 259 |
|
| 260 |
-
# Gradio app setup
|
| 261 |
with gr.Blocks() as demo:
|
| 262 |
with gr.Tab("AI Detection"):
|
| 263 |
t1 = gr.Textbox(lines=5, label='Text')
|
|
@@ -265,7 +160,6 @@ with gr.Blocks() as demo:
|
|
| 265 |
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
| 266 |
score1 = gr.Textbox(lines=1, label='Prob')
|
| 267 |
|
| 268 |
-
# Connect the prediction function to the button
|
| 269 |
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 270 |
|
| 271 |
with gr.Tab("Paraphrasing & Grammar Correction"):
|
|
@@ -273,7 +167,6 @@ with gr.Blocks() as demo:
|
|
| 273 |
button2 = gr.Button("🔄 Paraphrase and Correct")
|
| 274 |
result2 = gr.Textbox(lines=5, label='Corrected Text')
|
| 275 |
|
| 276 |
-
# Connect the paraphrasing and correction function to the button
|
| 277 |
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 278 |
|
| 279 |
-
demo.launch(share=True)
|
|
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
+
import re
|
| 10 |
|
| 11 |
# Initialize the English text classification pipeline for AI detection
|
| 12 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
|
|
| 30 |
res = pipeline_en(text)[0]
|
| 31 |
return res['label'], res['score']
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Function to remove redundant and meaningless words
|
| 34 |
def remove_redundant_words(text):
|
| 35 |
doc = nlp(text)
|
|
|
|
| 37 |
filtered_text = [token.text for token in doc if token.text.lower() not in meaningless_words]
|
| 38 |
return ' '.join(filtered_text)
|
| 39 |
|
| 40 |
+
# Function to fix spacing before punctuation
|
| 41 |
def fix_punctuation_spacing(text):
|
| 42 |
+
# Remove spaces before commas, periods, question marks, etc.
|
| 43 |
+
text = re.sub(r'\s+([,.\'!?:])', r'\1', text)
|
| 44 |
+
return text
|
| 45 |
+
|
| 46 |
+
# Function to fix possessives like "Earth's"
|
| 47 |
+
def fix_possessives(text):
|
| 48 |
+
# Simple rule to catch possessives and correct spacing
|
| 49 |
+
text = re.sub(r'(\w)\s\'\s?s', r"\1's", text)
|
| 50 |
return text
|
| 51 |
|
| 52 |
+
# Function to capitalize the first letter of sentences and proper nouns
|
| 53 |
def capitalize_sentences_and_nouns(text):
|
|
|
|
| 54 |
doc = nlp(text)
|
| 55 |
corrected_text = []
|
| 56 |
|
|
|
|
| 67 |
|
| 68 |
return ' '.join(corrected_text)
|
| 69 |
|
| 70 |
+
# Function to force capitalization of the first letter of every sentence
|
| 71 |
+
def force_first_letter_capital(text):
|
| 72 |
+
sentences = text.split(". ")
|
| 73 |
+
capitalized_sentences = [sentence[0].capitalize() + sentence[1:] if sentence else "" for sentence in sentences]
|
| 74 |
+
return ". ".join(capitalized_sentences)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Function to correct tense errors in a sentence
|
| 77 |
def correct_tense_errors(text):
|
|
|
|
| 85 |
corrected_text.append(token.text)
|
| 86 |
return ' '.join(corrected_text)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Function to check and correct article errors
|
| 89 |
def correct_article_errors(text):
|
| 90 |
doc = nlp(text)
|
|
|
|
| 102 |
corrected_text.append(token.text)
|
| 103 |
return ' '.join(corrected_text)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Function to ensure subject-verb agreement
|
| 106 |
def ensure_subject_verb_agreement(text):
|
| 107 |
doc = nlp(text)
|
|
|
|
| 127 |
corrected_words.append(word) # Keep the original word if correction is None
|
| 128 |
return ' '.join(corrected_words)
|
| 129 |
|
| 130 |
+
# Main function for paraphrasing and grammar correction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def paraphrase_and_correct(text):
|
| 132 |
# Remove meaningless or redundant words first
|
| 133 |
cleaned_text = remove_redundant_words(text)
|
|
|
|
| 135 |
# Capitalize sentences and nouns
|
| 136 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
| 137 |
|
| 138 |
+
# Ensure first letter of each sentence is capitalized
|
| 139 |
+
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
| 140 |
|
| 141 |
# Apply grammatical corrections
|
| 142 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
|
|
|
| 143 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
|
|
|
| 144 |
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 145 |
|
| 146 |
+
# Fix punctuation spacing and possessives
|
| 147 |
+
paraphrased_text = fix_punctuation_spacing(paraphrased_text)
|
| 148 |
+
paraphrased_text = fix_possessives(paraphrased_text)
|
| 149 |
|
| 150 |
# Correct spelling errors
|
| 151 |
paraphrased_text = correct_spelling(paraphrased_text)
|
| 152 |
|
| 153 |
return paraphrased_text
|
| 154 |
|
| 155 |
+
# Gradio app setup
|
| 156 |
with gr.Blocks() as demo:
|
| 157 |
with gr.Tab("AI Detection"):
|
| 158 |
t1 = gr.Textbox(lines=5, label='Text')
|
|
|
|
| 160 |
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
| 161 |
score1 = gr.Textbox(lines=1, label='Prob')
|
| 162 |
|
|
|
|
| 163 |
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 164 |
|
| 165 |
with gr.Tab("Paraphrasing & Grammar Correction"):
|
|
|
|
| 167 |
button2 = gr.Button("🔄 Paraphrase and Correct")
|
| 168 |
result2 = gr.Textbox(lines=5, label='Corrected Text')
|
| 169 |
|
|
|
|
| 170 |
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 171 |
|
| 172 |
+
demo.launch(share=True)
|