sashtech commited on
Commit
062b394
β€’
1 Parent(s): c6565ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -45
app.py CHANGED
@@ -1,55 +1,162 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
3
  import spacy
4
  import subprocess
5
  import nltk
6
  from nltk.corpus import wordnet
7
  from spellchecker import SpellChecker
 
8
 
9
- # Initialize other components (AI detection, NLP, etc.) as before...
 
10
 
11
- # Function to paraphrase and correct grammar using Ginger
12
- def correct_with_ginger(text):
13
- ginger_result = get_ginger_result(text)
14
- if "error" in ginger_result:
15
- return ginger_result["error"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- original_text = text
18
- fixed_text = original_text
19
- color_gap, fixed_gap = 0, 0
20
- if not ginger_result["LightGingerTheTextResult"]:
21
- return "No grammatical issues found!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- for result in ginger_result["LightGingerTheTextResult"]:
24
- if result["Suggestions"]:
25
- from_index = result["From"] + color_gap
26
- to_index = result["To"] + 1 + color_gap
27
- suggest = result["Suggestions"][0]["Text"]
28
-
29
- original_text = original_text[:from_index] + original_text[from_index:to_index] + original_text[to_index:]
30
- fixed_text = fixed_text[:from_index-fixed_gap] + suggest + fixed_text[to_index-fixed_gap:]
31
- color_gap += len(suggest) - (to_index - from_index)
32
- fixed_gap += to_index - from_index - len(suggest)
33
-
34
- return fixed_text
35
-
36
- # Gradio app setup with two tabs
37
- with gr.Blocks() as demo:
38
- with gr.Tab("AI Detection"):
39
- t1 = gr.Textbox(lines=5, label='Text')
40
- button1 = gr.Button("πŸ€– Predict!")
41
- label1 = gr.Textbox(lines=1, label='Predicted Label πŸŽƒ')
42
- score1 = gr.Textbox(lines=1, label='Prob')
43
-
44
- button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
45
-
46
- with gr.Tab("Paraphrasing & Grammar Correction"):
47
- t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
48
- button2 = gr.Button("πŸ”„ Paraphrase and Correct")
49
- ginger_button = gr.Button("πŸ”§ Correct with Ginger")
50
- result2 = gr.Textbox(lines=5, label='Corrected Text')
51
-
52
- button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
53
- ginger_button.click(fn=correct_with_ginger, inputs=t2, outputs=result2)
54
-
55
- demo.launch(share=True)
 
1
+ import os
2
  import gradio as gr
 
3
  import spacy
4
  import subprocess
5
  import nltk
6
  from nltk.corpus import wordnet
7
  from spellchecker import SpellChecker
8
+ from ginger import get_ginger_result # Importing the grammar correction function
9
 
10
+ # Initialize the English text classification pipeline for AI detection
11
+ pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
12
 
13
+ # Initialize the spell checker
14
+ spell = SpellChecker()
15
+
16
+ # Ensure necessary NLTK data is downloaded
17
+ nltk.download('wordnet')
18
+ nltk.download('omw-1.4')
19
+
20
+ # Ensure the SpaCy model is installed
21
+ try:
22
+ nlp = spacy.load("en_core_web_sm")
23
+ except OSError:
24
+ subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
25
+ nlp = spacy.load("en_core_web_sm")
26
+
27
+ # Function to predict the label and score for English text (AI Detection)
28
+ def predict_en(text):
29
+ res = pipeline_en(text)[0]
30
+ return res['label'], res['score']
31
+
32
+ # Function to get synonyms using NLTK WordNet
33
+ def get_synonyms_nltk(word, pos):
34
+ synsets = wordnet.synsets(word, pos=pos)
35
+ if synsets:
36
+ lemmas = synsets[0].lemmas()
37
+ return [lemma.name() for lemma in lemmas]
38
+ return []
39
+
40
+ # Function to remove redundant and meaningless words
41
+ def remove_redundant_words(text):
42
+ doc = nlp(text)
43
+ meaningless_words = {"actually", "basically", "literally", "really", "very", "just"}
44
+ filtered_text = [token.text for token in doc if token.text.lower() not in meaningless_words]
45
+ return ' '.join(filtered_text)
46
+
47
+ # Function to capitalize the first letter of sentences and proper nouns
48
+ def capitalize_sentences_and_nouns(text):
49
+ doc = nlp(text)
50
+ corrected_text = []
51
+
52
+ for sent in doc.sents:
53
+ sentence = []
54
+ for token in sent:
55
+ if token.i == sent.start: # First word of the sentence
56
+ sentence.append(token.text.capitalize())
57
+ elif token.pos_ == "PROPN": # Proper noun
58
+ sentence.append(token.text.capitalize())
59
+ else:
60
+ sentence.append(token.text)
61
+ corrected_text.append(' '.join(sentence))
62
+
63
+ return ' '.join(corrected_text)
64
+
65
+ # Function to force capitalization of the first letter of every sentence
66
+ def force_first_letter_capital(text):
67
+ sentences = text.split(". ") # Split by period to get each sentence
68
+ capitalized_sentences = [sentence[0].capitalize() + sentence[1:] if sentence else "" for sentence in sentences]
69
+ return ". ".join(capitalized_sentences)
70
+
71
+ # Function to correct tense errors in a sentence
72
+ def correct_tense_errors(text):
73
+ doc = nlp(text)
74
+ corrected_text = []
75
+ for token in doc:
76
+ if token.pos_ == "VERB" and token.dep_ in {"aux", "auxpass"}:
77
+ lemma = wordnet.morphy(token.text, wordnet.VERB) or token.text
78
+ corrected_text.append(lemma)
79
+ else:
80
+ corrected_text.append(token.text)
81
+ return ' '.join(corrected_text)
82
+
83
+ # Function to correct singular/plural errors
84
+ def correct_singular_plural_errors(text):
85
+ doc = nlp(text)
86
+ corrected_text = []
87
 
88
+ for token in doc:
89
+ if token.pos_ == "NOUN":
90
+ if token.tag_ == "NN": # Singular noun
91
+ if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
92
+ corrected_text.append(token.lemma_ + 's')
93
+ else:
94
+ corrected_text.append(token.text)
95
+ elif token.tag_ == "NNS": # Plural noun
96
+ if any(child.text.lower() in ['a', 'one'] for child in token.head.children):
97
+ corrected_text.append(token.lemma_)
98
+ else:
99
+ corrected_text.append(token.text)
100
+ else:
101
+ corrected_text.append(token.text)
102
+
103
+ return ' '.join(corrected_text)
104
+
105
+ # Function to check and correct article errors
106
+ def correct_article_errors(text):
107
+ doc = nlp(text)
108
+ corrected_text = []
109
+ for token in doc:
110
+ if token.text in ['a', 'an']:
111
+ next_token = token.nbor(1)
112
+ if token.text == "a" and next_token.text[0].lower() in "aeiou":
113
+ corrected_text.append("an")
114
+ elif token.text == "an" and next_token.text[0].lower() not in "aeiou":
115
+ corrected_text.append("a")
116
+ else:
117
+ corrected_text.append(token.text)
118
+ else:
119
+ corrected_text.append(token.text)
120
+ return ' '.join(corrected_text)
121
+
122
+ # Function to get the correct synonym while maintaining verb form
123
+ def replace_with_synonym(token):
124
+ pos = None
125
+ if token.pos_ == "VERB":
126
+ pos = wordnet.VERB
127
+ elif token.pos_ == "NOUN":
128
+ pos = wordnet.NOUN
129
+ elif token.pos_ == "ADJ":
130
+ pos = wordnet.ADJ
131
+ elif token.pos_ == "ADV":
132
+ pos = wordnet.ADV
133
 
134
+ synonyms = get_synonyms_nltk(token.text, pos)
135
+ if synonyms:
136
+ return synonyms[0]
137
+ return token.text
138
+
139
+ # Function to use Ginger API for grammar correction (NEW)
140
+ def correct_grammar_with_ginger(text):
141
+ result = get_ginger_result(text)
142
+ corrected_text = text
143
+ for suggestion in result["LightGingerTheTextResult"]:
144
+ if suggestion["Suggestions"]:
145
+ from_index = suggestion["From"]
146
+ to_index = suggestion["To"] + 1
147
+ suggested_text = suggestion["Suggestions"][0]["Text"]
148
+ corrected_text = corrected_text[:from_index] + suggested_text + corrected_text[to_index:]
149
+ return corrected_text
150
+
151
+ # Gradio interface
152
+ def process_text(text):
153
+ text = correct_article_errors(text)
154
+ text = correct_singular_plural_errors(text)
155
+ text = correct_tense_errors(text)
156
+ text = capitalize_sentences_and_nouns(text)
157
+ text = remove_redundant_words(text)
158
+ text = correct_grammar_with_ginger(text) # Add grammar correction using Ginger here
159
+ return text
160
+
161
+ iface = gr.Interface(fn=process_text, inputs="text", outputs="text")
162
+ iface.launch()