Spaces:
Running
Running
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 gensim import downloader as api
|
|
|
|
| 9 |
|
| 10 |
# Ensure necessary NLTK data is downloaded
|
| 11 |
nltk.download('wordnet')
|
|
@@ -28,6 +29,12 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 29 |
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# AI detection function using DistilBERT
|
| 32 |
def detect_ai_generated(text):
|
| 33 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
@@ -45,7 +52,7 @@ def get_synonyms_nltk(word, pos):
|
|
| 45 |
return [lemma.name() for lemma in lemmas]
|
| 46 |
return []
|
| 47 |
|
| 48 |
-
# Paraphrasing function using spaCy and NLTK
|
| 49 |
def paraphrase_with_spacy_nltk(text):
|
| 50 |
doc = nlp(text)
|
| 51 |
paraphrased_words = []
|
|
@@ -73,7 +80,10 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 73 |
# Join the words back into a sentence
|
| 74 |
paraphrased_sentence = ' '.join(paraphrased_words)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Gradio interface definition
|
| 79 |
with gr.Blocks() as interface:
|
|
@@ -81,7 +91,7 @@ with gr.Blocks() as interface:
|
|
| 81 |
with gr.Column():
|
| 82 |
text_input = gr.Textbox(lines=5, label="Input Text")
|
| 83 |
detect_button = gr.Button("AI Detection")
|
| 84 |
-
paraphrase_button = gr.Button("Paraphrase with spaCy & NLTK")
|
| 85 |
with gr.Column():
|
| 86 |
output_text = gr.Textbox(label="Output")
|
| 87 |
|
|
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from gensim import downloader as api
|
| 9 |
+
from textblob import TextBlob # Import TextBlob for grammar correction
|
| 10 |
|
| 11 |
# Ensure necessary NLTK data is downloaded
|
| 12 |
nltk.download('wordnet')
|
|
|
|
| 29 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 30 |
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
|
| 31 |
|
| 32 |
+
# Function to correct grammar using TextBlob
|
| 33 |
+
def correct_grammar_with_textblob(text):
|
| 34 |
+
blob = TextBlob(text)
|
| 35 |
+
corrected_text = str(blob.correct())
|
| 36 |
+
return corrected_text
|
| 37 |
+
|
| 38 |
# AI detection function using DistilBERT
|
| 39 |
def detect_ai_generated(text):
|
| 40 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
|
|
| 52 |
return [lemma.name() for lemma in lemmas]
|
| 53 |
return []
|
| 54 |
|
| 55 |
+
# Paraphrasing function using spaCy and NLTK with TextBlob grammar correction
|
| 56 |
def paraphrase_with_spacy_nltk(text):
|
| 57 |
doc = nlp(text)
|
| 58 |
paraphrased_words = []
|
|
|
|
| 80 |
# Join the words back into a sentence
|
| 81 |
paraphrased_sentence = ' '.join(paraphrased_words)
|
| 82 |
|
| 83 |
+
# Correct the grammar of the paraphrased sentence using TextBlob
|
| 84 |
+
corrected_sentence = correct_grammar_with_textblob(paraphrased_sentence)
|
| 85 |
+
|
| 86 |
+
return corrected_sentence
|
| 87 |
|
| 88 |
# Gradio interface definition
|
| 89 |
with gr.Blocks() as interface:
|
|
|
|
| 91 |
with gr.Column():
|
| 92 |
text_input = gr.Textbox(lines=5, label="Input Text")
|
| 93 |
detect_button = gr.Button("AI Detection")
|
| 94 |
+
paraphrase_button = gr.Button("Paraphrase with spaCy & NLTK (Grammar Corrected with TextBlob)")
|
| 95 |
with gr.Column():
|
| 96 |
output_text = gr.Textbox(label="Output")
|
| 97 |
|