Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,12 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
import spacy
|
4 |
import subprocess
|
5 |
import nltk
|
6 |
from nltk.corpus import wordnet
|
7 |
|
8 |
-
# Clone and install CorrectLy
|
9 |
-
def install_correctly():
|
10 |
-
if not os.path.exists('CorrectLy'):
|
11 |
-
print("Cloning CorrectLy repository...")
|
12 |
-
subprocess.run(["git", "clone", "https://github.com/rounakdatta/CorrectLy.git"], check=True)
|
13 |
-
|
14 |
-
# Install dependencies from CorrectLy
|
15 |
-
subprocess.run([sys.executable, "-m", "pip", "install", "-r", "CorrectLy/requirements.txt"], check=True)
|
16 |
-
|
17 |
-
# Add CorrectLy to Python path
|
18 |
-
sys.path.append(os.path.abspath('CorrectLy'))
|
19 |
-
|
20 |
-
# Install CorrectLy
|
21 |
-
install_correctly()
|
22 |
-
|
23 |
-
# Import CorrectLy after installation
|
24 |
-
from CorrectLy.correctly import CorrectLy
|
25 |
-
|
26 |
-
# Initialize CorrectLy for grammar correction
|
27 |
-
corrector = CorrectLy()
|
28 |
-
|
29 |
# Initialize the English text classification pipeline for AI detection
|
30 |
-
from transformers import pipeline
|
31 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
32 |
|
33 |
# Function to predict the label and score for English text (AI Detection)
|
@@ -46,78 +25,58 @@ except OSError:
|
|
46 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
47 |
nlp = spacy.load("en_core_web_sm")
|
48 |
|
49 |
-
#
|
50 |
-
def correct_grammar_with_correctly(text):
|
51 |
-
return corrector.correct(text)
|
52 |
-
|
53 |
-
# Function to get synonyms using NLTK WordNet (Humanifier)
|
54 |
-
def get_synonyms_nltk(word, pos):
|
55 |
-
synsets = wordnet.synsets(word, pos=pos)
|
56 |
-
if synsets:
|
57 |
-
lemmas = synsets[0].lemmas()
|
58 |
-
return [lemma.name() for lemma in lemmas]
|
59 |
-
return []
|
60 |
|
61 |
-
#
|
62 |
-
def
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
for sent in doc.sents:
|
67 |
-
sentence = []
|
68 |
-
for token in sent:
|
69 |
-
if token.i == sent.start: # First word of the sentence
|
70 |
-
sentence.append(token.text.capitalize())
|
71 |
-
elif token.pos_ == "PROPN": # Proper noun
|
72 |
-
sentence.append(token.text.capitalize())
|
73 |
-
else:
|
74 |
-
sentence.append(token.text)
|
75 |
-
corrected_text.append(' '.join(sentence))
|
76 |
-
|
77 |
-
return ' '.join(corrected_text)
|
78 |
-
|
79 |
-
# Paraphrasing function using SpaCy and NLTK (Humanifier)
|
80 |
-
def paraphrase_with_spacy_nltk(text):
|
81 |
-
doc = nlp(text)
|
82 |
-
paraphrased_words = []
|
83 |
|
84 |
-
for token in
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
pos = wordnet.NOUN
|
89 |
-
elif token.pos_ in {"VERB"}:
|
90 |
-
pos = wordnet.VERB
|
91 |
-
elif token.pos_ in {"ADJ"}:
|
92 |
-
pos = wordnet.ADJ
|
93 |
-
elif token.pos_ in {"ADV"}:
|
94 |
-
pos = wordnet.ADV
|
95 |
-
|
96 |
-
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
97 |
-
|
98 |
-
# Replace with a synonym only if it makes sense
|
99 |
-
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
100 |
-
paraphrased_words.append(synonyms[0])
|
101 |
else:
|
102 |
-
|
103 |
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
return
|
111 |
|
112 |
-
#
|
113 |
-
def
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# Gradio app setup with three tabs
|
123 |
with gr.Blocks() as demo:
|
@@ -143,8 +102,8 @@ with gr.Blocks() as demo:
|
|
143 |
grammar_button = gr.Button("Correct Grammar")
|
144 |
grammar_output = gr.Textbox(label="Corrected Text")
|
145 |
|
146 |
-
# Connect the
|
147 |
-
grammar_button.click(
|
148 |
|
149 |
# Launch the app with all functionalities
|
150 |
demo.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
import spacy
|
5 |
import subprocess
|
6 |
import nltk
|
7 |
from nltk.corpus import wordnet
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Initialize the English text classification pipeline for AI detection
|
|
|
10 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
11 |
|
12 |
# Function to predict the label and score for English text (AI Detection)
|
|
|
25 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
26 |
nlp = spacy.load("en_core_web_sm")
|
27 |
|
28 |
+
# Grammar, Tense, and Singular/Plural Correction Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Correct article errors (e.g., "a apple" -> "an apple")
|
31 |
+
def check_article_error(text):
|
32 |
+
tokens = nltk.pos_tag(nltk.word_tokenize(text))
|
33 |
+
corrected_tokens = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
for i, token in enumerate(tokens):
|
36 |
+
word, pos = token
|
37 |
+
if word.lower() == 'a' and i < len(tokens) - 1 and tokens[i + 1][1] == 'NN':
|
38 |
+
corrected_tokens.append('an' if tokens[i + 1][0][0] in 'aeiou' else 'a')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
else:
|
40 |
+
corrected_tokens.append(word)
|
41 |
|
42 |
+
return ' '.join(corrected_tokens)
|
43 |
+
|
44 |
+
# Correct tense errors (e.g., "She has go out" -> "She has gone out")
|
45 |
+
def check_tense_error(text):
|
46 |
+
tokens = nltk.pos_tag(nltk.word_tokenize(text))
|
47 |
+
corrected_tokens = []
|
48 |
|
49 |
+
for word, pos in tokens:
|
50 |
+
if word == "go" and pos == "VB":
|
51 |
+
corrected_tokens.append("gone")
|
52 |
+
elif word == "know" and pos == "VB":
|
53 |
+
corrected_tokens.append("known")
|
54 |
+
else:
|
55 |
+
corrected_tokens.append(word)
|
56 |
|
57 |
+
return ' '.join(corrected_tokens)
|
58 |
|
59 |
+
# Correct singular/plural errors (e.g., "There are many chocolate" -> "There are many chocolates")
|
60 |
+
def check_pluralization_error(text):
|
61 |
+
tokens = nltk.pos_tag(nltk.word_tokenize(text))
|
62 |
+
corrected_tokens = []
|
63 |
|
64 |
+
for word, pos in tokens:
|
65 |
+
if word == "chocolate" and pos == "NN":
|
66 |
+
corrected_tokens.append("chocolates")
|
67 |
+
elif word == "kids" and pos == "NNS":
|
68 |
+
corrected_tokens.append("kid")
|
69 |
+
else:
|
70 |
+
corrected_tokens.append(word)
|
71 |
|
72 |
+
return ' '.join(corrected_tokens)
|
73 |
+
|
74 |
+
# Combined function to correct grammar, tense, and singular/plural errors
|
75 |
+
def correct_grammar_tense_plural(text):
|
76 |
+
text = check_article_error(text)
|
77 |
+
text = check_tense_error(text)
|
78 |
+
text = check_pluralization_error(text)
|
79 |
+
return text
|
80 |
|
81 |
# Gradio app setup with three tabs
|
82 |
with gr.Blocks() as demo:
|
|
|
102 |
grammar_button = gr.Button("Correct Grammar")
|
103 |
grammar_output = gr.Textbox(label="Corrected Text")
|
104 |
|
105 |
+
# Connect the custom grammar, tense, and plural correction function to the button
|
106 |
+
grammar_button.click(correct_grammar_tense_plural, inputs=grammar_input, outputs=grammar_output)
|
107 |
|
108 |
# Launch the app with all functionalities
|
109 |
demo.launch()
|