Spaces:
Sleeping
Sleeping
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 |
from nltk.corpus import wordnet
|
8 |
from gensim import downloader as api
|
9 |
-
from
|
10 |
-
from gingerit.gingerit import GingerIt # GingerIt for grammar correction
|
11 |
|
12 |
# Ensure necessary NLTK data is downloaded
|
13 |
nltk.download('wordnet')
|
@@ -26,16 +24,14 @@ word_vectors = api.load("glove-wiki-gigaword-50")
|
|
26 |
# Check for GPU and set the device accordingly
|
27 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
28 |
|
29 |
-
# Load
|
|
|
|
|
|
|
|
|
30 |
tokenizer_ai = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
31 |
model_ai = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
|
32 |
|
33 |
-
# Initialize Autocorrect for spelling correction
|
34 |
-
spell = Speller()
|
35 |
-
|
36 |
-
# Initialize GingerIt for grammar correction
|
37 |
-
parser = GingerIt()
|
38 |
-
|
39 |
# AI detection function using DistilBERT
|
40 |
def detect_ai_generated(text):
|
41 |
inputs = tokenizer_ai(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
@@ -127,17 +123,12 @@ def paraphrase_with_spacy_nltk(text):
|
|
127 |
|
128 |
return corrected_text
|
129 |
|
130 |
-
# Function to correct
|
131 |
-
def
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
# Step 2: Correct grammar using GingerIt
|
136 |
-
grammar_correction = parser.parse(corrected_spelling)
|
137 |
-
|
138 |
-
return grammar_correction['result']
|
139 |
|
140 |
-
# Combined function: Paraphrase -> Tense Check -> Capitalization ->
|
141 |
def paraphrase_and_correct(text):
|
142 |
# Step 1: Paraphrase the text
|
143 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
@@ -148,8 +139,8 @@ def paraphrase_and_correct(text):
|
|
148 |
# Step 3: Capitalize sentences and proper nouns
|
149 |
capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
|
150 |
|
151 |
-
# Step 4: Correct
|
152 |
-
final_text =
|
153 |
|
154 |
return final_text
|
155 |
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
import spacy
|
4 |
import subprocess
|
5 |
import nltk
|
6 |
from nltk.corpus import wordnet
|
7 |
from gensim import downloader as api
|
8 |
+
from gramformer import Gramformer
|
|
|
9 |
|
10 |
# Ensure necessary NLTK data is downloaded
|
11 |
nltk.download('wordnet')
|
|
|
24 |
# Check for GPU and set the device accordingly
|
25 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
26 |
|
27 |
+
# Load Gramformer for grammar correction (model 2 for correction)
|
28 |
+
gf = Gramformer(models=2, use_gpu=torch.cuda.is_available())
|
29 |
+
|
30 |
+
# AI detection model and tokenizer remain the same as before
|
31 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
32 |
tokenizer_ai = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
33 |
model_ai = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# AI detection function using DistilBERT
|
36 |
def detect_ai_generated(text):
|
37 |
inputs = tokenizer_ai(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
|
123 |
|
124 |
return corrected_text
|
125 |
|
126 |
+
# Function to correct grammar using Gramformer
|
127 |
+
def correct_grammar(text):
|
128 |
+
corrected_sentences = gf.correct(text)
|
129 |
+
return corrected_sentences[0] if corrected_sentences else text
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
+
# Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
|
132 |
def paraphrase_and_correct(text):
|
133 |
# Step 1: Paraphrase the text
|
134 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
|
|
139 |
# Step 3: Capitalize sentences and proper nouns
|
140 |
capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
|
141 |
|
142 |
+
# Step 4: Correct grammar using Gramformer
|
143 |
+
final_text = correct_grammar(capitalized_text)
|
144 |
|
145 |
return final_text
|
146 |
|