sashtech commited on
Commit
23b0057
1 Parent(s): 7154049

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -141
app.py CHANGED
@@ -1,144 +1,31 @@
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')
12
- nltk.download('omw-1.4')
13
-
14
- # Ensure the spaCy model is installed
15
- try:
16
- nlp = spacy.load("en_core_web_sm")
17
- except OSError:
18
- subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
19
- nlp = spacy.load("en_core_web_sm")
20
-
21
- # Load a smaller Word2Vec model from Gensim's pre-trained models
22
- word_vectors = api.load("glove-wiki-gigaword-50")
23
-
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)
38
- with torch.no_grad():
39
- outputs = model_ai(**inputs)
40
- probabilities = torch.softmax(outputs.logits, dim=1)
41
- ai_probability = probabilities[0][1].item() # Probability of being AI-generated
42
- return f"AI-Generated Content Probability: {ai_probability * 100:.2f}%"
43
-
44
- # Function to get synonyms using NLTK WordNet
45
- def get_synonyms_nltk(word, pos):
46
- synsets = wordnet.synsets(word, pos=pos)
47
- if synsets:
48
- lemmas = synsets[0].lemmas()
49
- return [lemma.name() for lemma in lemmas]
50
- return []
51
-
52
- # Function to check and correct tenses and verbs using spaCy
53
- def check_tense_and_correct(text):
54
- doc = nlp(text)
55
- corrected_text = []
56
-
57
- for token in doc:
58
- if token.pos_ == 'VERB':
59
- tense = token.tag_
60
- if tense == 'VBZ':
61
- corrected_text.append(token.lemma_)
62
- elif tense == 'VBD':
63
- corrected_text.append(token.text)
64
- else:
65
- corrected_text.append(token.text)
66
- else:
67
- corrected_text.append(token.text)
68
-
69
- return ' '.join(corrected_text)
70
-
71
- # Function to capitalize the first letter of sentences and proper nouns
72
- def capitalize_sentences_and_nouns(text):
73
- doc = nlp(text)
74
- corrected_text = []
75
-
76
- for sent in doc.sents:
77
- sentence = []
78
- for token in sent:
79
- if token.i == sent.start:
80
- sentence.append(token.text.capitalize())
81
- elif token.pos_ == "PROPN":
82
- sentence.append(token.text.capitalize())
83
- else:
84
- sentence.append(token.text)
85
- corrected_text.append(' '.join(sentence))
86
-
87
- return ' '.join(corrected_text)
88
-
89
- # Paraphrasing function using spaCy and NLTK
90
- def paraphrase_with_spacy_nltk(text):
91
- doc = nlp(text)
92
- paraphrased_words = []
93
-
94
- for token in doc:
95
- pos = None
96
- if token.pos_ in {"NOUN"}:
97
- pos = wordnet.NOUN
98
- elif token.pos_ in {"VERB"}:
99
- pos = wordnet.VERB
100
- elif token.pos_ in {"ADJ"}:
101
- pos = wordnet.ADJ
102
- elif token.pos_ in {"ADV"}:
103
- pos = wordnet.ADV
104
-
105
- synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
106
- if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
107
- paraphrased_words.append(synonyms[0])
108
- else:
109
- paraphrased_words.append(token.text)
110
-
111
- paraphrased_sentence = ' '.join(paraphrased_words)
112
- corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
113
 
114
- return corrected_text
115
-
116
- # Function to correct grammar using Gramformer
117
- def correct_grammar(text):
118
- corrected_sentences = gf.correct(text)
119
- return corrected_sentences[0] if corrected_sentences else text
120
-
121
- # Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
122
- def paraphrase_and_correct(text):
123
- paraphrased_text = paraphrase_with_spacy_nltk(text)
124
- tense_checked_text = check_tense_and_correct(paraphrased_text)
125
- capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
126
- final_text = correct_grammar(capitalized_text)
127
-
128
- return final_text
129
-
130
- # Gradio interface definition
131
- with gr.Blocks() as interface:
132
- with gr.Row():
133
- with gr.Column():
134
- text_input = gr.Textbox(lines=5, label="Input Text")
135
- detect_button = gr.Button("AI Detection")
136
- paraphrase_button = gr.Button("Paraphrase & Correct")
137
- with gr.Column():
138
- output_text = gr.Textbox(label="Output")
139
-
140
- detect_button.click(detect_ai_generated, inputs=text_input, outputs=output_text)
141
- paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
142
 
143
- # Launch the Gradio app
144
- interface.launch(debug=False)
 
 
 
1
  import gradio as gr
2
+ from gramformer import Gramformer # Assuming gramformer.py is in the same directory as this app
3
+
4
+ # Initialize Gramformer
5
+ gf = Gramformer(models=1, use_gpu=False) # Set use_gpu=True if using a GPU-enabled space
6
+
7
+ def correct_sentence(input_sentence):
8
+ # Use the correct method from Gramformer
9
+ corrected_sentences = gf.correct(input_sentence, max_candidates=1)
10
+ return "\n".join(corrected_sentences)
11
+
12
+ # Create Gradio interface
13
+ def gradio_interface():
14
+ input_text = gr.inputs.Textbox(lines=2, placeholder="Enter a sentence here...")
15
+ output_text = gr.outputs.Textbox()
16
+
17
+ # Gradio Interface: Takes a sentence, corrects it, and outputs the correction
18
+ iface = gr.Interface(
19
+ fn=correct_sentence,
20
+ inputs=input_text,
21
+ outputs=output_text,
22
+ title="Grammar Correction",
23
+ description="Corrects grammatical errors in the input sentence using the Gramformer model."
24
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ return iface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # Run the Gradio app
29
+ if __name__ == "__main__":
30
+ iface = gradio_interface()
31
+ iface.launch()