sashtech commited on
Commit
aed78e3
Β·
verified Β·
1 Parent(s): dfa5513

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -114
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import os
2
  import gradio as gr
3
  from transformers import pipeline
4
  import spacy
@@ -6,121 +5,33 @@ import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
  from spellchecker import SpellChecker
9
- import language_tool_python
10
 
11
- # Initialize the English text classification pipeline for AI detection
12
- pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
13
 
14
- # Initialize the spell checker
15
- spell = SpellChecker()
16
-
17
- # Initialize the LanguageTool for grammar correction
18
- tool = language_tool_python.LanguageTool('en-US')
19
-
20
- # Ensure necessary NLTK data is downloaded
21
- nltk.download('wordnet')
22
- nltk.download('omw-1.4')
23
-
24
- # Ensure the SpaCy model is installed
25
- try:
26
- nlp = spacy.load("en_core_web_sm")
27
- except OSError:
28
- subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
29
- nlp = spacy.load("en_core_web_sm")
30
-
31
- # Function to predict the label and score for English text (AI Detection)
32
- def predict_en(text):
33
- res = pipeline_en(text)[0]
34
- return res['label'], res['score']
35
-
36
- # Function to remove redundant and meaningless words
37
- def remove_redundant_words(text):
38
- doc = nlp(text)
39
- meaningless_words = {"actually", "basically", "literally", "really", "very", "just"}
40
- filtered_text = [token.text for token in doc if token.text.lower() not in meaningless_words]
41
- return ' '.join(filtered_text)
42
-
43
- # Function to apply grammatical corrections using LanguageTool
44
- def correct_grammar(text):
45
- corrected_text = tool.correct(text)
46
- return corrected_text
47
-
48
- # Function to correct spelling errors
49
- def correct_spelling(text):
50
- words = text.split()
51
- corrected_words = []
52
- for word in words:
53
- corrected_word = spell.correction(word)
54
- corrected_words.append(corrected_word if corrected_word else word) # Keep original word if no correction
55
- return ' '.join(corrected_words)
56
-
57
- # Function to capitalize the first letter of each sentence and proper nouns
58
- def capitalize_sentences_and_nouns(text):
59
- doc = nlp(text)
60
- corrected_text = []
61
-
62
- for sent in doc.sents:
63
- sentence = []
64
- for token in sent:
65
- if token.i == sent.start: # First word of the sentence
66
- sentence.append(token.text.capitalize())
67
- elif token.pos_ == "PROPN": # Proper noun
68
- sentence.append(token.text.capitalize())
69
- else:
70
- sentence.append(token.text)
71
- corrected_text.append(' '.join(sentence))
72
-
73
- return ' '.join(corrected_text)
74
-
75
- # Function to rephrase with contextually appropriate synonyms
76
- def rephrase_with_synonyms(text):
77
- doc = nlp(text)
78
- rephrased_text = []
79
-
80
- for token in doc:
81
- pos_tag = None
82
- if token.pos_ == "NOUN":
83
- pos_tag = wordnet.NOUN
84
- elif token.pos_ == "VERB":
85
- pos_tag = wordnet.VERB
86
- elif token.pos_ == "ADJ":
87
- pos_tag = wordnet.ADJ
88
- elif token.pos_ == "ADV":
89
- pos_tag = wordnet.ADV
90
-
91
- if pos_tag:
92
- synonyms = wordnet.synsets(token.text, pos=pos_tag)
93
- if synonyms:
94
- synonym = synonyms[0].lemmas()[0].name() # Choose the first synonym
95
- rephrased_text.append(synonym)
96
- else:
97
- rephrased_text.append(token.text)
98
- else:
99
- rephrased_text.append(token.text)
100
-
101
- return ' '.join(rephrased_text)
102
-
103
- # Comprehensive function for paraphrasing and grammar correction
104
- def paraphrase_and_correct(text):
105
- # Step 1: Remove meaningless or redundant words
106
- cleaned_text = remove_redundant_words(text)
107
-
108
- # Step 2: Capitalize sentences and proper nouns
109
- paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
110
 
111
- # Step 3: Correct grammar using LanguageTool
112
- paraphrased_text = correct_grammar(paraphrased_text)
 
 
 
113
 
114
- # Step 4: Rephrase with contextually appropriate synonyms
115
- paraphrased_text = rephrase_with_synonyms(paraphrased_text)
116
-
117
- # Step 5: Correct spelling errors
118
- paraphrased_text = correct_spelling(paraphrased_text)
119
-
120
- # Step 6: Correct any remaining grammar issues after rephrasing
121
- paraphrased_text = correct_grammar(paraphrased_text)
122
-
123
- return paraphrased_text
 
 
124
 
125
  # Gradio app setup with two tabs
126
  with gr.Blocks() as demo:
@@ -130,15 +41,15 @@ with gr.Blocks() as demo:
130
  label1 = gr.Textbox(lines=1, label='Predicted Label πŸŽƒ')
131
  score1 = gr.Textbox(lines=1, label='Prob')
132
 
133
- # Connect the prediction function to the button
134
  button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
135
 
136
  with gr.Tab("Paraphrasing & Grammar Correction"):
137
  t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
138
  button2 = gr.Button("πŸ”„ Paraphrase and Correct")
 
139
  result2 = gr.Textbox(lines=5, label='Corrected Text')
140
 
141
- # Connect the paraphrasing and correction function to the button
142
  button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
 
143
 
144
  demo.launch(share=True)
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  import spacy
 
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:
 
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)