Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import spacy
|
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
-
import
|
| 9 |
from gensim import downloader as api
|
| 10 |
|
| 11 |
# Ensure necessary NLTK data is downloaded
|
|
@@ -28,76 +28,3 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 28 |
# Load AI Detector model and tokenizer from Hugging Face (DistilBERT)
|
| 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 language-tool-python
|
| 33 |
-
def correct_grammar_with_language_tool(text):
|
| 34 |
-
tool = language_tool_python.LanguageTool('en-US')
|
| 35 |
-
matches = tool.check(text)
|
| 36 |
-
corrected_text = language_tool_python.utils.correct(text, matches)
|
| 37 |
-
return corrected_text
|
| 38 |
-
|
| 39 |
-
# AI detection function using DistilBERT
|
| 40 |
-
def detect_ai_generated(text):
|
| 41 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 42 |
-
with torch.no_grad():
|
| 43 |
-
outputs = model(**inputs)
|
| 44 |
-
probabilities = torch.softmax(outputs.logits, dim=1)
|
| 45 |
-
ai_probability = probabilities[0][1].item() # Probability of being AI-generated
|
| 46 |
-
return f"AI-Generated Content Probability: {ai_probability:.2f}%"
|
| 47 |
-
|
| 48 |
-
# Function to get synonyms using NLTK WordNet
|
| 49 |
-
def get_synonyms_nltk(word, pos):
|
| 50 |
-
synsets = wordnet.synsets(word, pos=pos)
|
| 51 |
-
if synsets:
|
| 52 |
-
lemmas = synsets[0].lemmas()
|
| 53 |
-
return [lemma.name() for lemma in lemmas]
|
| 54 |
-
return []
|
| 55 |
-
|
| 56 |
-
# Paraphrasing function using spaCy and NLTK with grammar correction
|
| 57 |
-
def paraphrase_with_spacy_nltk(text):
|
| 58 |
-
doc = nlp(text)
|
| 59 |
-
paraphrased_words = []
|
| 60 |
-
|
| 61 |
-
for token in doc:
|
| 62 |
-
# Map spaCy POS tags to WordNet POS tags
|
| 63 |
-
pos = None
|
| 64 |
-
if token.pos_ in {"NOUN"}:
|
| 65 |
-
pos = wordnet.NOUN
|
| 66 |
-
elif token.pos_ in {"VERB"}:
|
| 67 |
-
pos = wordnet.VERB
|
| 68 |
-
elif token.pos_ in {"ADJ"}:
|
| 69 |
-
pos = wordnet.ADJ
|
| 70 |
-
elif token.pos_ in {"ADV"}:
|
| 71 |
-
pos = wordnet.ADV
|
| 72 |
-
|
| 73 |
-
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
| 74 |
-
|
| 75 |
-
# Replace with a synonym only if it makes sense
|
| 76 |
-
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
| 77 |
-
paraphrased_words.append(synonyms[0])
|
| 78 |
-
else:
|
| 79 |
-
paraphrased_words.append(token.text)
|
| 80 |
-
|
| 81 |
-
# Join the words back into a sentence
|
| 82 |
-
paraphrased_sentence = ' '.join(paraphrased_words)
|
| 83 |
-
|
| 84 |
-
# Correct the grammar of the paraphrased sentence using language-tool-python
|
| 85 |
-
corrected_sentence = correct_grammar_with_language_tool(paraphrased_sentence)
|
| 86 |
-
|
| 87 |
-
return corrected_sentence
|
| 88 |
-
|
| 89 |
-
# Gradio interface definition
|
| 90 |
-
with gr.Blocks() as interface:
|
| 91 |
-
with gr.Row():
|
| 92 |
-
with gr.Column():
|
| 93 |
-
text_input = gr.Textbox(lines=5, label="Input Text")
|
| 94 |
-
detect_button = gr.Button("AI Detection")
|
| 95 |
-
paraphrase_button = gr.Button("Paraphrase with spaCy & NLTK (Grammar Corrected)")
|
| 96 |
-
with gr.Column():
|
| 97 |
-
output_text = gr.Textbox(label="Output")
|
| 98 |
-
|
| 99 |
-
detect_button.click(detect_ai_generated, inputs=text_input, outputs=output_text)
|
| 100 |
-
paraphrase_button.click(paraphrase_with_spacy_nltk, inputs=text_input, outputs=output_text)
|
| 101 |
-
|
| 102 |
-
# Launch the Gradio app
|
| 103 |
-
interface.launch(debug=False)
|
|
|
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
+
import language_check # Use language-check instead of language-tool-python
|
| 9 |
from gensim import downloader as api
|
| 10 |
|
| 11 |
# Ensure necessary NLTK data is downloaded
|
|
|
|
| 28 |
# Load AI Detector model and tokenizer from Hugging Face (DistilBERT)
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|