Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,49 +4,32 @@ from transformers import pipeline
|
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
-
from nltk.corpus import wordnet
|
| 8 |
-
from nltk.corpus import stopwords
|
| 9 |
-
from nltk.tokenize import word_tokenize
|
| 10 |
from spellchecker import SpellChecker
|
| 11 |
import re
|
| 12 |
-
import string
|
| 13 |
import random
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
nltk.download('
|
| 19 |
-
nltk.download('
|
| 20 |
-
|
| 21 |
-
nltk.download('wordnet')
|
| 22 |
-
nltk.download('omw-1.4')
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Initialize stopwords
|
| 27 |
-
stop_words = set(stopwords.words("english"))
|
| 28 |
-
|
| 29 |
-
# Words we don't want to replace
|
| 30 |
-
exclude_tags = {'PRP', 'PRP$', 'MD', 'VBZ', 'VBP', 'VBD', 'VBG', 'VBN', 'TO', 'IN', 'DT', 'CC'}
|
| 31 |
-
exclude_words = {'is', 'am', 'are', 'was', 'were', 'have', 'has', 'do', 'does', 'did', 'will', 'shall', 'should', 'would', 'could', 'can', 'may', 'might'}
|
| 32 |
-
|
| 33 |
-
# Initialize the English text classification pipeline for AI detection
|
| 34 |
-
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
try:
|
| 41 |
-
nlp = spacy.load("en_core_web_sm")
|
| 42 |
-
except OSError:
|
| 43 |
-
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 44 |
-
nlp = spacy.load("en_core_web_sm")
|
| 45 |
|
| 46 |
def plagiarism_removal(text):
|
| 47 |
def plagiarism_remover(word):
|
| 48 |
# Handle stopwords, punctuation, and excluded words
|
| 49 |
-
if word.lower() in
|
| 50 |
return word
|
| 51 |
|
| 52 |
# Find synonyms
|
|
@@ -79,7 +62,7 @@ def plagiarism_removal(text):
|
|
| 79 |
return synonym_choice
|
| 80 |
|
| 81 |
# Tokenize, replace words, and join them back
|
| 82 |
-
para_split = word_tokenize(text)
|
| 83 |
final_text = [plagiarism_remover(word) for word in para_split]
|
| 84 |
|
| 85 |
# Handle spacing around punctuation correctly
|
|
@@ -92,6 +75,23 @@ def plagiarism_removal(text):
|
|
| 92 |
|
| 93 |
return " ".join(corrected_text)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Function to predict the label and score for English text (AI Detection)
|
| 96 |
def predict_en(text):
|
| 97 |
res = pipeline_en(text)[0]
|
|
@@ -205,43 +205,37 @@ def correct_spelling(text):
|
|
| 205 |
corrected_words = []
|
| 206 |
for word in words:
|
| 207 |
corrected_word = spell.correction(word)
|
| 208 |
-
|
| 209 |
-
corrected_words.append(corrected_word)
|
| 210 |
-
else:
|
| 211 |
-
corrected_words.append(word)
|
| 212 |
return ' '.join(corrected_words)
|
| 213 |
|
| 214 |
-
# Main function for paraphrasing and grammar correction
|
| 215 |
def paraphrase_and_correct(text):
|
| 216 |
-
# Add synonym replacement here
|
| 217 |
cleaned_text = remove_redundant_words(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
plag_removed = plagiarism_removal(cleaned_text)
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 223 |
-
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 224 |
-
paraphrased_text = fix_possessives(paraphrased_text)
|
| 225 |
-
paraphrased_text = correct_spelling(paraphrased_text)
|
| 226 |
-
paraphrased_text = fix_punctuation_spacing(paraphrased_text)
|
| 227 |
-
|
| 228 |
-
return paraphrased_text
|
| 229 |
-
|
| 230 |
-
# Gradio app setup
|
| 231 |
with gr.Blocks() as demo:
|
|
|
|
| 232 |
with gr.Tab("AI Detection"):
|
| 233 |
-
t1 = gr.Textbox(lines=5, label='Text')
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 239 |
|
| 240 |
-
with gr.Tab("Paraphrasing
|
| 241 |
-
t2 = gr.Textbox(lines=5, label='
|
| 242 |
-
button2 = gr.Button("
|
| 243 |
-
|
| 244 |
|
| 245 |
-
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=
|
| 246 |
|
| 247 |
-
demo.launch(
|
|
|
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
+
from nltk.corpus import wordnet, stopwords # Import stopwords here
|
|
|
|
|
|
|
| 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 top_words or word.lower() in exclude_words or word in string.punctuation:
|
| 33 |
return word
|
| 34 |
|
| 35 |
# Find synonyms
|
|
|
|
| 62 |
return synonym_choice
|
| 63 |
|
| 64 |
# Tokenize, replace words, and join them back
|
| 65 |
+
para_split = nltk.word_tokenize(text)
|
| 66 |
final_text = [plagiarism_remover(word) for word in para_split]
|
| 67 |
|
| 68 |
# Handle spacing around punctuation correctly
|
|
|
|
| 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'}
|
| 81 |
+
|
| 82 |
+
# Initialize the English text classification pipeline for AI detection
|
| 83 |
+
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
| 84 |
+
|
| 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")
|
| 91 |
+
except OSError:
|
| 92 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 93 |
+
nlp = spacy.load("en_core_web_sm")
|
| 94 |
+
|
| 95 |
# Function to predict the label and score for English text (AI Detection)
|
| 96 |
def predict_en(text):
|
| 97 |
res = pipeline_en(text)[0]
|
|
|
|
| 205 |
corrected_words = []
|
| 206 |
for word in words:
|
| 207 |
corrected_word = spell.correction(word)
|
| 208 |
+
corrected_words.append(corrected_word)
|
|
|
|
|
|
|
|
|
|
| 209 |
return ' '.join(corrected_words)
|
| 210 |
|
| 211 |
+
# Main processing function for paraphrasing and grammar correction
|
| 212 |
def paraphrase_and_correct(text):
|
|
|
|
| 213 |
cleaned_text = remove_redundant_words(text)
|
| 214 |
+
cleaned_text = fix_punctuation_spacing(cleaned_text)
|
| 215 |
+
cleaned_text = fix_possessives(cleaned_text)
|
| 216 |
+
cleaned_text = capitalize_sentences_and_nouns(cleaned_text)
|
| 217 |
+
cleaned_text = force_first_letter_capital(cleaned_text)
|
| 218 |
+
cleaned_text = correct_tense_errors(cleaned_text)
|
| 219 |
+
cleaned_text = correct_article_errors(cleaned_text)
|
| 220 |
+
cleaned_text = ensure_subject_verb_agreement(cleaned_text)
|
| 221 |
+
cleaned_text = correct_spelling(cleaned_text)
|
| 222 |
plag_removed = plagiarism_removal(cleaned_text)
|
| 223 |
+
return plag_removed
|
| 224 |
+
|
| 225 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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='Input Text')
|
| 230 |
+
output1 = gr.Label()
|
| 231 |
+
button1 = gr.Button("π Process!")
|
| 232 |
+
button1.click(fn=predict_en, inputs=t1, outputs=output1)
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
with gr.Tab("Paraphrasing and Grammar Correction"):
|
| 235 |
+
t2 = gr.Textbox(lines=5, label='Input Text')
|
| 236 |
+
button2 = gr.Button("π Process!")
|
| 237 |
+
output2 = gr.Textbox(lines=5, label='Processed Text')
|
| 238 |
|
| 239 |
+
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=output2)
|
| 240 |
|
| 241 |
+
demo.launch()
|