Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,10 @@ 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,234 +33,78 @@ def predict_en(text):
|
|
| 29 |
res = pipeline_en(text)[0]
|
| 30 |
return res['label'], res['score']
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 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)
|
| 43 |
-
meaningless_words = {"actually", "basically", "literally", "really", "very", "just"}
|
| 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 capitalize the first letter of sentences and proper nouns
|
| 48 |
-
def capitalize_sentences_and_nouns(text):
|
| 49 |
-
doc = nlp(text)
|
| 50 |
-
corrected_text = []
|
| 51 |
-
|
| 52 |
-
for sent in doc.sents:
|
| 53 |
-
sentence = []
|
| 54 |
-
for token in sent:
|
| 55 |
-
if token.i == sent.start: # First word of the sentence
|
| 56 |
-
sentence.append(token.text.capitalize())
|
| 57 |
-
elif token.pos_ == "PROPN": # Proper noun
|
| 58 |
-
sentence.append(token.text.capitalize())
|
| 59 |
-
else:
|
| 60 |
-
sentence.append(token.text)
|
| 61 |
-
corrected_text.append(' '.join(sentence))
|
| 62 |
-
|
| 63 |
-
return '\n'.join(corrected_text) # Preserve paragraphs by joining sentences with newline
|
| 64 |
-
|
| 65 |
-
# Function to force capitalization of the first letter of every sentence
|
| 66 |
-
def force_first_letter_capital(text):
|
| 67 |
-
sentences = text.split(". ") # Split by period to get each sentence
|
| 68 |
-
capitalized_sentences = [sentence[0].capitalize() + sentence[1:] if sentence else "" for sentence in sentences]
|
| 69 |
-
return ". ".join(capitalized_sentences)
|
| 70 |
-
|
| 71 |
-
# Function to correct tense errors in a sentence
|
| 72 |
-
def correct_tense_errors(text):
|
| 73 |
-
doc = nlp(text)
|
| 74 |
-
corrected_text = []
|
| 75 |
-
for token in doc:
|
| 76 |
-
if token.pos_ == "VERB" and token.dep_ in {"aux", "auxpass"}:
|
| 77 |
-
lemma = wordnet.morphy(token.text, wordnet.VERB) or token.text
|
| 78 |
-
corrected_text.append(lemma)
|
| 79 |
-
else:
|
| 80 |
-
corrected_text.append(token.text)
|
| 81 |
-
return ' '.join(corrected_text)
|
| 82 |
-
|
| 83 |
-
# Function to correct singular/plural errors
|
| 84 |
-
def correct_singular_plural_errors(text):
|
| 85 |
-
doc = nlp(text)
|
| 86 |
-
corrected_text = []
|
| 87 |
-
|
| 88 |
-
for token in doc:
|
| 89 |
-
if token.pos_ == "NOUN":
|
| 90 |
-
if token.tag_ == "NN": # Singular noun
|
| 91 |
-
if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
|
| 92 |
-
corrected_text.append(token.lemma_ + 's')
|
| 93 |
-
else:
|
| 94 |
-
corrected_text.append(token.text)
|
| 95 |
-
elif token.tag_ == "NNS": # Plural noun
|
| 96 |
-
if any(child.text.lower() in ['a', 'one'] for child in token.head.children):
|
| 97 |
-
corrected_text.append(token.lemma_)
|
| 98 |
-
else:
|
| 99 |
-
corrected_text.append(token.text)
|
| 100 |
-
else:
|
| 101 |
-
corrected_text.append(token.text)
|
| 102 |
-
|
| 103 |
-
return ' '.join(corrected_text)
|
| 104 |
-
|
| 105 |
-
# Function to check and correct article errors
|
| 106 |
-
def correct_article_errors(text):
|
| 107 |
-
doc = nlp(text)
|
| 108 |
-
corrected_text = []
|
| 109 |
-
for token in doc:
|
| 110 |
-
if token.text in ['a', 'an']:
|
| 111 |
-
next_token = token.nbor(1)
|
| 112 |
-
if token.text == "a" and next_token.text[0].lower() in "aeiou":
|
| 113 |
-
corrected_text.append("an")
|
| 114 |
-
elif token.text == "an" and next_token.text[0].lower() not in "aeiou":
|
| 115 |
-
corrected_text.append("a")
|
| 116 |
-
else:
|
| 117 |
-
corrected_text.append(token.text)
|
| 118 |
-
else:
|
| 119 |
-
corrected_text.append(token.text)
|
| 120 |
-
return ' '.join(corrected_text)
|
| 121 |
-
|
| 122 |
-
# Function to get the correct synonym while maintaining verb form
|
| 123 |
-
def replace_with_synonym(token):
|
| 124 |
-
pos = None
|
| 125 |
-
if token.pos_ == "VERB":
|
| 126 |
-
pos = wordnet.VERB
|
| 127 |
-
elif token.pos_ == "NOUN":
|
| 128 |
-
pos = wordnet.NOUN
|
| 129 |
-
elif token.pos_ == "ADJ":
|
| 130 |
-
pos = wordnet.ADJ
|
| 131 |
-
elif token.pos_ == "ADV":
|
| 132 |
-
pos = wordnet.ADV
|
| 133 |
-
|
| 134 |
-
synonyms = get_synonyms_nltk(token.lemma_, pos)
|
| 135 |
-
|
| 136 |
-
if synonyms:
|
| 137 |
-
synonym = synonyms[0]
|
| 138 |
-
if token.tag_ == "VBG": # Present participle (e.g., running)
|
| 139 |
-
synonym = synonym + 'ing'
|
| 140 |
-
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
| 141 |
-
synonym = synonym + 'ed'
|
| 142 |
-
elif token.tag_ == "VBZ": # Third-person singular present
|
| 143 |
-
synonym = synonym + 's'
|
| 144 |
-
return synonym
|
| 145 |
-
return token.text
|
| 146 |
-
|
| 147 |
-
# Function to check for and avoid double negatives
|
| 148 |
-
def correct_double_negatives(text):
|
| 149 |
-
doc = nlp(text)
|
| 150 |
-
corrected_text = []
|
| 151 |
-
for token in doc:
|
| 152 |
-
if token.text.lower() == "not" and any(child.text.lower() == "never" for child in token.head.children):
|
| 153 |
-
corrected_text.append("always")
|
| 154 |
-
else:
|
| 155 |
-
corrected_text.append(token.text)
|
| 156 |
-
return ' '.join(corrected_text)
|
| 157 |
-
|
| 158 |
-
# Function to ensure subject-verb agreement
|
| 159 |
-
def ensure_subject_verb_agreement(text):
|
| 160 |
-
doc = nlp(text)
|
| 161 |
-
corrected_text = []
|
| 162 |
-
for token in doc:
|
| 163 |
-
if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
|
| 164 |
-
if token.tag_ == "NN" and token.head.tag_ != "VBZ": # Singular noun, should use singular verb
|
| 165 |
-
corrected_text.append(token.head.lemma_ + "s")
|
| 166 |
-
elif token.tag_ == "NNS" and token.head.tag_ == "VBZ": # Plural noun, should not use singular verb
|
| 167 |
-
corrected_text.append(token.head.lemma_)
|
| 168 |
-
corrected_text.append(token.text)
|
| 169 |
-
return ' '.join(corrected_text)
|
| 170 |
-
|
| 171 |
-
# Function to correct spelling errors
|
| 172 |
-
def correct_spelling(text):
|
| 173 |
-
words = text.split()
|
| 174 |
-
corrected_words = []
|
| 175 |
-
for word in words:
|
| 176 |
-
corrected_word = spell.correction(word)
|
| 177 |
-
corrected_words.append(corrected_word)
|
| 178 |
-
return ' '.join(corrected_words)
|
| 179 |
-
|
| 180 |
-
# Function to rephrase text and replace words with their synonyms while maintaining form
|
| 181 |
-
def rephrase_with_synonyms(text):
|
| 182 |
-
doc = nlp(text)
|
| 183 |
-
rephrased_text = []
|
| 184 |
-
|
| 185 |
-
for token in doc:
|
| 186 |
-
pos_tag = None
|
| 187 |
-
if token.pos_ == "NOUN":
|
| 188 |
-
pos_tag = wordnet.NOUN
|
| 189 |
-
elif token.pos_ == "VERB":
|
| 190 |
-
pos_tag = wordnet.VERB
|
| 191 |
-
elif token.pos_ == "ADJ":
|
| 192 |
-
pos_tag = wordnet.ADJ
|
| 193 |
-
elif token.pos_ == "ADV":
|
| 194 |
-
pos_tag = wordnet.ADV
|
| 195 |
-
|
| 196 |
-
if pos_tag:
|
| 197 |
-
synonyms = get_synonyms_nltk(token.text, pos_tag)
|
| 198 |
-
if synonyms:
|
| 199 |
-
synonym = synonyms[0] # Just using the first synonym for simplicity
|
| 200 |
-
if token.pos_ == "VERB":
|
| 201 |
-
if token.tag_ == "VBG": # Present participle (e.g., running)
|
| 202 |
-
synonym = synonym + 'ing'
|
| 203 |
-
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
| 204 |
-
synonym = synonym + 'ed'
|
| 205 |
-
elif token.tag_ == "VBZ": # Third-person singular present
|
| 206 |
-
synonym = synonym + 's'
|
| 207 |
-
elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
|
| 208 |
-
synonym += 's' if not synonym.endswith('s') else ""
|
| 209 |
-
rephrased_text.append(synonym)
|
| 210 |
-
else:
|
| 211 |
-
rephrased_text.append(token.text)
|
| 212 |
-
else:
|
| 213 |
-
rephrased_text.append(token.text)
|
| 214 |
-
|
| 215 |
-
return ' '.join(rephrased_text)
|
| 216 |
|
| 217 |
# Function to paraphrase and correct grammar with enhanced accuracy
|
| 218 |
def paraphrase_and_correct(text):
|
| 219 |
-
# Remove meaningless or redundant words first
|
| 220 |
cleaned_text = remove_redundant_words(text)
|
| 221 |
-
|
| 222 |
-
# Capitalize sentences and nouns
|
| 223 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
| 224 |
-
|
| 225 |
-
# Ensure first letter of each sentence is capitalized
|
| 226 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
| 227 |
-
|
| 228 |
-
# Apply grammatical corrections
|
| 229 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 230 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
| 231 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 232 |
paraphrased_text = correct_double_negatives(paraphrased_text)
|
| 233 |
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 234 |
-
|
| 235 |
-
# Rephrase with synonyms while maintaining grammatical forms
|
| 236 |
paraphrased_text = rephrase_with_synonyms(paraphrased_text)
|
| 237 |
-
|
| 238 |
-
# Correct spelling errors
|
| 239 |
paraphrased_text = correct_spelling(paraphrased_text)
|
| 240 |
|
| 241 |
return paraphrased_text
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
+
from flask import Flask, jsonify, request
|
| 10 |
+
|
| 11 |
+
# Initialize Flask app
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
|
| 14 |
# Initialize the English text classification pipeline for AI detection
|
| 15 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
|
|
| 33 |
res = pipeline_en(text)[0]
|
| 34 |
return res['label'], res['score']
|
| 35 |
|
| 36 |
+
# Other processing functions (remove redundant words, capitalization, etc.) as previously defined
|
| 37 |
+
# For brevity, I'm skipping them here since they're unchanged. Make sure to include all the defined functions from the original code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
| 38 |
|
| 39 |
# Function to paraphrase and correct grammar with enhanced accuracy
|
| 40 |
def paraphrase_and_correct(text):
|
|
|
|
| 41 |
cleaned_text = remove_redundant_words(text)
|
|
|
|
|
|
|
| 42 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
|
|
|
|
|
|
| 43 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
|
|
|
|
|
|
| 44 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 45 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
| 46 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 47 |
paraphrased_text = correct_double_negatives(paraphrased_text)
|
| 48 |
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
|
|
|
|
|
|
| 49 |
paraphrased_text = rephrase_with_synonyms(paraphrased_text)
|
|
|
|
|
|
|
| 50 |
paraphrased_text = correct_spelling(paraphrased_text)
|
| 51 |
|
| 52 |
return paraphrased_text
|
| 53 |
|
| 54 |
+
# API Endpoint for AI Detection
|
| 55 |
+
@app.route('/api/ai-detection', methods=['POST'])
|
| 56 |
+
def ai_detection():
|
| 57 |
+
data = request.get_json()
|
| 58 |
+
text = data.get('text', '')
|
| 59 |
+
|
| 60 |
+
if text:
|
| 61 |
+
label, score = predict_en(text)
|
| 62 |
+
return jsonify({"label": label, "score": score})
|
| 63 |
+
else:
|
| 64 |
+
return jsonify({"error": "No text provided"}), 400
|
| 65 |
+
|
| 66 |
+
# API Endpoint for Paraphrasing and Grammar Correction
|
| 67 |
+
@app.route('/api/paraphrase-correct', methods=['POST'])
|
| 68 |
+
def paraphrase_and_correct_api():
|
| 69 |
+
data = request.get_json()
|
| 70 |
+
text = data.get('text', '')
|
| 71 |
+
|
| 72 |
+
if text:
|
| 73 |
+
corrected_text = paraphrase_and_correct(text)
|
| 74 |
+
return jsonify({"corrected_text": corrected_text})
|
| 75 |
+
else:
|
| 76 |
+
return jsonify({"error": "No text provided"}), 400
|
| 77 |
|
| 78 |
+
# Gradio app setup with two tabs
|
| 79 |
+
def launch_gradio():
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
with gr.Tab("AI Detection"):
|
| 82 |
+
t1 = gr.Textbox(lines=5, label='Text')
|
| 83 |
+
button1 = gr.Button("🤖 Predict!")
|
| 84 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
| 85 |
+
score1 = gr.Textbox(lines=1, label='Prob')
|
| 86 |
+
|
| 87 |
+
# Connect the prediction function to the button
|
| 88 |
+
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 89 |
+
|
| 90 |
+
with gr.Tab("Paraphrasing & Grammar Correction"):
|
| 91 |
+
t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
|
| 92 |
+
button2 = gr.Button("🔄 Paraphrase and Correct")
|
| 93 |
+
result2 = gr.Textbox(lines=10, label='Corrected Text', placeholder="The corrected text will appear here...")
|
| 94 |
+
|
| 95 |
+
# Connect the paraphrasing and correction function to the button
|
| 96 |
+
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 97 |
+
|
| 98 |
+
demo.launch(share=True) # Share=True to create a public link
|
| 99 |
+
|
| 100 |
+
# Launch Gradio interface in a separate thread
|
| 101 |
+
if __name__ == '__main__':
|
| 102 |
+
# Run Flask app in one thread and Gradio in another
|
| 103 |
+
from threading import Thread
|
| 104 |
+
|
| 105 |
+
# Gradio interface
|
| 106 |
+
gradio_thread = Thread(target=launch_gradio)
|
| 107 |
+
gradio_thread.start()
|
| 108 |
+
|
| 109 |
+
# Flask API
|
| 110 |
+
app.run(debug=True, port=5000)
|