sashtech commited on
Commit
ddf9006
·
verified ·
1 Parent(s): e82df55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -1,11 +1,12 @@
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,8 +29,8 @@ 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 (T5) from Hugging Face
32
- grammar_corrector = pipeline('text2text-generation', model='vennify/t5-base-grammar-correction')
33
 
34
  # AI detection function using DistilBERT
35
  def detect_ai_generated(text):
@@ -99,16 +100,22 @@ def paraphrase_with_spacy_nltk(text):
99
 
100
  return corrected_text
101
 
102
- # Combined function: Paraphrase -> Grammar Correction -> Capitalization
 
 
 
 
 
 
103
  def paraphrase_and_correct(text):
104
  # Step 1: Paraphrase the text
105
  paraphrased_text = paraphrase_with_spacy_nltk(text)
106
 
107
- # Step 2: Correct grammar using T5 model
108
- corrected_text = grammar_corrector(paraphrased_text)[0]['generated_text']
109
 
110
- # Step 3: Capitalize sentences and proper nouns
111
- final_text = capitalize_sentences_and_nouns(corrected_text)
112
 
113
  return final_text
114
 
 
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
10
 
11
  # Ensure necessary NLTK data is downloaded
12
  nltk.download('wordnet')
 
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
33
+ tool = language_tool_python.LanguageTool('en-US')
34
 
35
  # AI detection function using DistilBERT
36
  def detect_ai_generated(text):
 
100
 
101
  return corrected_text
102
 
103
+ # Function to correct grammar using LanguageTool
104
+ def correct_grammar(text):
105
+ matches = tool.check(text)
106
+ corrected_text = language_tool_python.utils.correct(text, matches)
107
+ return corrected_text
108
+
109
+ # Combined function: Paraphrase -> Capitalization -> Grammar Correction
110
  def paraphrase_and_correct(text):
111
  # Step 1: Paraphrase the text
112
  paraphrased_text = paraphrase_with_spacy_nltk(text)
113
 
114
+ # Step 2: Capitalize sentences and proper nouns
115
+ capitalized_text = capitalize_sentences_and_nouns(paraphrased_text)
116
 
117
+ # Step 3: Correct grammar
118
+ final_text = correct_grammar(capitalized_text)
119
 
120
  return final_text
121