sashtech commited on
Commit
19e6d3f
·
verified ·
1 Parent(s): ba62c12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -1,12 +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
- import language_tool_python # LanguageTool for grammar checking
10
 
11
  # Ensure necessary NLTK data is downloaded
12
  nltk.download('wordnet')
@@ -29,8 +28,9 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29
  tokenizer_ai = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
30
  model_ai = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device)
31
 
32
- # Initialize LanguageTool for grammar correction (English)
33
- tool = language_tool_python.LanguageTool('en-US')
 
34
 
35
  # AI detection function using DistilBERT
36
  def detect_ai_generated(text):
@@ -123,10 +123,11 @@ def paraphrase_with_spacy_nltk(text):
123
 
124
  return corrected_text
125
 
126
- # Function to correct grammar using LanguageTool
127
  def correct_grammar(text):
128
- # Apply grammar and spelling suggestions
129
- corrected_text = tool.correct(text)
 
130
  return corrected_text
131
 
132
  # Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
@@ -140,7 +141,7 @@ def paraphrase_and_correct(text):
140
  # Step 3: Capitalize sentences and proper nouns
141
  capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
142
 
143
- # Step 4: Correct grammar
144
  final_text = correct_grammar(capitalized_text)
145
 
146
  return final_text
 
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
  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
 
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
 
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