sashtech commited on
Commit
e5f0e06
·
verified ·
1 Parent(s): f980998

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, 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
 
10
  # Ensure necessary NLTK data is downloaded
11
  nltk.download('wordnet')
@@ -28,9 +30,11 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
28
  tokenizer_ai = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
29
  model_ai = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
30
 
31
- # Load grammar correction model and tokenizer from Hugging Face
32
- tokenizer_gc = AutoTokenizer.from_pretrained("prithivida/grammar_error_correcter_v1")
33
- model_gc = AutoModelForSeq2SeqLM.from_pretrained("prithivida/grammar_error_correcter_v1").to(device)
 
 
34
 
35
  # AI detection function using DistilBERT
36
  def detect_ai_generated(text):
@@ -123,14 +127,17 @@ def paraphrase_with_spacy_nltk(text):
123
 
124
  return corrected_text
125
 
126
- # Function to correct grammar using Hugging Face model
127
- def correct_grammar(text):
128
- inputs = tokenizer_gc.encode("gec: " + text, return_tensors="pt", truncation=True).to(device)
129
- outputs = model_gc.generate(inputs, max_length=512, num_beams=5, early_stopping=True)
130
- corrected_text = tokenizer_gc.decode(outputs[0], skip_special_tokens=True)
131
- return corrected_text
 
 
 
132
 
133
- # Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
134
  def paraphrase_and_correct(text):
135
  # Step 1: Paraphrase the text
136
  paraphrased_text = paraphrase_with_spacy_nltk(text)
@@ -141,8 +148,8 @@ def paraphrase_and_correct(text):
141
  # Step 3: Capitalize sentences and proper nouns
142
  capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
143
 
144
- # Step 4: Correct grammar using Hugging Face model
145
- final_text = correct_grammar(capitalized_text)
146
 
147
  return final_text
148
 
 
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 autocorrect import Speller # Autocorrect library for spelling correction
10
+ from gingerit.gingerit import GingerIt # GingerIt for grammar correction
11
 
12
  # Ensure necessary NLTK data is downloaded
13
  nltk.download('wordnet')
 
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):
 
127
 
128
  return corrected_text
129
 
130
+ # Function to correct spelling using autocorrect and grammar using GingerIt
131
+ def correct_spelling_and_grammar(text):
132
+ # Step 1: Correct spelling using autocorrect
133
+ corrected_spelling = spell(text)
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 -> Spelling and Grammar Correction
141
  def paraphrase_and_correct(text):
142
  # Step 1: Paraphrase the text
143
  paraphrased_text = paraphrase_with_spacy_nltk(text)
 
148
  # Step 3: Capitalize sentences and proper nouns
149
  capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
150
 
151
+ # Step 4: Correct spelling and grammar
152
+ final_text = correct_spelling_and_grammar(capitalized_text)
153
 
154
  return final_text
155