Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
-
import gingerit
|
| 8 |
from nltk.corpus import wordnet
|
| 9 |
from gensim import downloader as api
|
| 10 |
-
from gingerit.gingerit import GingerIt # Import GingerIt for grammar correction
|
| 11 |
|
| 12 |
# Ensure necessary NLTK data is downloaded
|
| 13 |
nltk.download('wordnet')
|
|
@@ -30,6 +28,9 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 31 |
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# AI detection function using DistilBERT
|
| 34 |
def detect_ai_generated(text):
|
| 35 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
@@ -47,7 +48,7 @@ def get_synonyms_nltk(word, pos):
|
|
| 47 |
return [lemma.name() for lemma in lemmas]
|
| 48 |
return []
|
| 49 |
|
| 50 |
-
# Paraphrasing function using spaCy and NLTK
|
| 51 |
def paraphrase_with_spacy_nltk(text):
|
| 52 |
doc = nlp(text)
|
| 53 |
paraphrased_words = []
|
|
@@ -77,11 +78,10 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 77 |
|
| 78 |
return paraphrased_sentence
|
| 79 |
|
| 80 |
-
# Grammar correction function using
|
| 81 |
def correct_grammar(text):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return result['result'] # Return the corrected text
|
| 85 |
|
| 86 |
# Combined function: Paraphrase -> Grammar Check
|
| 87 |
def paraphrase_and_correct(text):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
import torch
|
| 4 |
import spacy
|
| 5 |
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 |
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 |
+
# Load grammar correction model from Hugging Face
|
| 32 |
+
grammar_corrector = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction", device=0 if torch.cuda.is_available() else -1)
|
| 33 |
+
|
| 34 |
# AI detection function using DistilBERT
|
| 35 |
def detect_ai_generated(text):
|
| 36 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
|
|
| 48 |
return [lemma.name() for lemma in lemmas]
|
| 49 |
return []
|
| 50 |
|
| 51 |
+
# Paraphrasing function using spaCy and NLTK
|
| 52 |
def paraphrase_with_spacy_nltk(text):
|
| 53 |
doc = nlp(text)
|
| 54 |
paraphrased_words = []
|
|
|
|
| 78 |
|
| 79 |
return paraphrased_sentence
|
| 80 |
|
| 81 |
+
# Grammar correction function using the Hugging Face model
|
| 82 |
def correct_grammar(text):
|
| 83 |
+
corrected_text = grammar_corrector(text)[0]['generated_text']
|
| 84 |
+
return corrected_text
|
|
|
|
| 85 |
|
| 86 |
# Combined function: Paraphrase -> Grammar Check
|
| 87 |
def paraphrase_and_correct(text):
|