sashtech commited on
Commit
c3f5d2b
·
verified ·
1 Parent(s): 41941cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  import spacy
5
  import subprocess
@@ -28,6 +28,9 @@ 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
  # AI detection function using DistilBERT
32
  def detect_ai_generated(text):
33
  inputs = tokenizer_ai(text, return_tensors="pt", truncation=True, max_length=512).to(device)
@@ -96,13 +99,16 @@ def paraphrase_with_spacy_nltk(text):
96
 
97
  return corrected_text
98
 
99
- # Combined function: Paraphrase -> Capitalization
100
  def paraphrase_and_correct(text):
101
  # Step 1: Paraphrase the text
102
  paraphrased_text = paraphrase_with_spacy_nltk(text)
103
 
104
- # Step 2: Capitalize sentences and proper nouns
105
- final_text = capitalize_sentences_and_nouns(paraphrased_text)
 
 
 
106
 
107
  return final_text
108
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
3
  import torch
4
  import spacy
5
  import subprocess
 
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='prithivida/grammar-error-correcter')
33
+
34
  # AI detection function using DistilBERT
35
  def detect_ai_generated(text):
36
  inputs = tokenizer_ai(text, return_tensors="pt", truncation=True, max_length=512).to(device)
 
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