Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,32 +4,22 @@ from transformers import pipeline
|
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
-
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
import re
|
| 10 |
-
import random
|
| 11 |
-
import string
|
| 12 |
|
| 13 |
-
# Ensure necessary NLTK data is downloaded
|
| 14 |
-
def download_nltk_resources():
|
| 15 |
-
try:
|
| 16 |
-
nltk.download('punkt') # Tokenizer for English text
|
| 17 |
-
nltk.download('stopwords') # Stop words
|
| 18 |
-
nltk.download('averaged_perceptron_tagger') # POS tagger
|
| 19 |
-
nltk.download('wordnet') # WordNet
|
| 20 |
-
nltk.download('omw-1.4') # Open Multilingual Wordnet
|
| 21 |
-
except Exception as e:
|
| 22 |
-
print(f"Error downloading NLTK resources: {e}")
|
| 23 |
-
|
| 24 |
-
# Call the download function
|
| 25 |
-
download_nltk_resources()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
top_words = set(stopwords.words("english")) # More efficient as a set
|
| 28 |
|
| 29 |
def plagiarism_removal(text):
|
| 30 |
def plagiarism_remover(word):
|
| 31 |
# Handle stopwords, punctuation, and excluded words
|
| 32 |
-
if word.lower() in
|
| 33 |
return word
|
| 34 |
|
| 35 |
# Find synonyms
|
|
@@ -62,7 +52,7 @@ def plagiarism_removal(text):
|
|
| 62 |
return synonym_choice
|
| 63 |
|
| 64 |
# Tokenize, replace words, and join them back
|
| 65 |
-
para_split =
|
| 66 |
final_text = [plagiarism_remover(word) for word in para_split]
|
| 67 |
|
| 68 |
# Handle spacing around punctuation correctly
|
|
@@ -75,6 +65,12 @@ def plagiarism_removal(text):
|
|
| 75 |
|
| 76 |
return " ".join(corrected_text)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Words we don't want to replace
|
| 79 |
exclude_tags = {'PRP', 'PRP$', 'MD', 'VBZ', 'VBP', 'VBD', 'VBG', 'VBN', 'TO', 'IN', 'DT', 'CC'}
|
| 80 |
exclude_words = {'is', 'am', 'are', 'was', 'were', 'have', 'has', 'do', 'does', 'did', 'will', 'shall', 'should', 'would', 'could', 'can', 'may', 'might'}
|
|
@@ -85,6 +81,10 @@ pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt
|
|
| 85 |
# Initialize the spell checker
|
| 86 |
spell = SpellChecker()
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Ensure the SpaCy model is installed
|
| 89 |
try:
|
| 90 |
nlp = spacy.load("en_core_web_sm")
|
|
@@ -205,37 +205,46 @@ def correct_spelling(text):
|
|
| 205 |
corrected_words = []
|
| 206 |
for word in words:
|
| 207 |
corrected_word = spell.correction(word)
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
return ' '.join(corrected_words)
|
| 210 |
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
def paraphrase_and_correct(text):
|
|
|
|
| 213 |
cleaned_text = remove_redundant_words(text)
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
with gr.Blocks() as demo:
|
| 227 |
-
gr.Markdown("# AI Text Processor")
|
| 228 |
with gr.Tab("AI Detection"):
|
| 229 |
-
t1 = gr.Textbox(lines=5, label='
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
with gr.Tab("Paraphrasing
|
| 235 |
-
t2 = gr.Textbox(lines=5, label='
|
| 236 |
-
button2 = gr.Button("
|
| 237 |
-
|
| 238 |
|
| 239 |
-
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=
|
| 240 |
|
| 241 |
-
demo.launch()
|
|
|
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
+
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
import re
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
|
| 13 |
+
nltk.download('punkt')
|
| 14 |
+
nltk.download('stopwords')
|
| 15 |
+
nltk.download('averaged_perceptron_tagger')
|
| 16 |
+
nltk.download('wordnet')
|
| 17 |
top_words = set(stopwords.words("english")) # More efficient as a set
|
| 18 |
|
| 19 |
def plagiarism_removal(text):
|
| 20 |
def plagiarism_remover(word):
|
| 21 |
# Handle stopwords, punctuation, and excluded words
|
| 22 |
+
if word.lower() in stop_words or word.lower() in exclude_words or word in string.punctuation:
|
| 23 |
return word
|
| 24 |
|
| 25 |
# Find synonyms
|
|
|
|
| 52 |
return synonym_choice
|
| 53 |
|
| 54 |
# Tokenize, replace words, and join them back
|
| 55 |
+
para_split = word_tokenize(text)
|
| 56 |
final_text = [plagiarism_remover(word) for word in para_split]
|
| 57 |
|
| 58 |
# Handle spacing around punctuation correctly
|
|
|
|
| 65 |
|
| 66 |
return " ".join(corrected_text)
|
| 67 |
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
# Words we don't want to replace
|
| 75 |
exclude_tags = {'PRP', 'PRP$', 'MD', 'VBZ', 'VBP', 'VBD', 'VBG', 'VBN', 'TO', 'IN', 'DT', 'CC'}
|
| 76 |
exclude_words = {'is', 'am', 'are', 'was', 'were', 'have', 'has', 'do', 'does', 'did', 'will', 'shall', 'should', 'would', 'could', 'can', 'may', 'might'}
|
|
|
|
| 81 |
# Initialize the spell checker
|
| 82 |
spell = SpellChecker()
|
| 83 |
|
| 84 |
+
# Ensure necessary NLTK data is downloaded
|
| 85 |
+
nltk.download('wordnet')
|
| 86 |
+
nltk.download('omw-1.4')
|
| 87 |
+
|
| 88 |
# Ensure the SpaCy model is installed
|
| 89 |
try:
|
| 90 |
nlp = spacy.load("en_core_web_sm")
|
|
|
|
| 205 |
corrected_words = []
|
| 206 |
for word in words:
|
| 207 |
corrected_word = spell.correction(word)
|
| 208 |
+
if corrected_word is not None:
|
| 209 |
+
corrected_words.append(corrected_word)
|
| 210 |
+
else:
|
| 211 |
+
corrected_words.append(word)
|
| 212 |
return ' '.join(corrected_words)
|
| 213 |
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
# Main function for paraphrasing and grammar correction
|
| 218 |
def paraphrase_and_correct(text):
|
| 219 |
+
# Add synonym replacement here
|
| 220 |
cleaned_text = remove_redundant_words(text)
|
| 221 |
+
plag_removed=plagiarism_removal(cleaned_text)
|
| 222 |
+
paraphrased_text = capitalize_sentences_and_nouns(plag_removed)
|
| 223 |
+
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
| 224 |
+
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 225 |
+
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 226 |
+
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 227 |
+
paraphrased_text = fix_possessives(paraphrased_text)
|
| 228 |
+
paraphrased_text = correct_spelling(paraphrased_text)
|
| 229 |
+
paraphrased_text = fix_punctuation_spacing(paraphrased_text)
|
| 230 |
+
|
| 231 |
+
return paraphrased_text
|
| 232 |
+
|
| 233 |
+
# Gradio app setup
|
| 234 |
with gr.Blocks() as demo:
|
|
|
|
| 235 |
with gr.Tab("AI Detection"):
|
| 236 |
+
t1 = gr.Textbox(lines=5, label='Text')
|
| 237 |
+
button1 = gr.Button("π€ Predict!")
|
| 238 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label π')
|
| 239 |
+
score1 = gr.Textbox(lines=1, label='Prob')
|
| 240 |
+
|
| 241 |
+
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 242 |
|
| 243 |
+
with gr.Tab("Paraphrasing & Grammar Correction"):
|
| 244 |
+
t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
|
| 245 |
+
button2 = gr.Button("π Paraphrase and Correct")
|
| 246 |
+
result2 = gr.Textbox(lines=5, label='Corrected Text')
|
| 247 |
|
| 248 |
+
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 249 |
|
| 250 |
+
demo.launch(share=True)
|