Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ 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")
|
|
@@ -51,6 +53,45 @@ def capitalize_sentences_and_nouns(text):
|
|
| 51 |
|
| 52 |
return ' '.join(corrected_text)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Paraphrasing function using SpaCy and NLTK (Humanifier)
|
| 55 |
def paraphrase_with_spacy_nltk(text):
|
| 56 |
doc = nlp(text)
|
|
@@ -84,13 +125,19 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 84 |
|
| 85 |
return corrected_text
|
| 86 |
|
| 87 |
-
# Combined function: Paraphrase -> Capitalization (Humanifier)
|
| 88 |
def paraphrase_and_correct(text):
|
| 89 |
# Step 1: Paraphrase the text
|
| 90 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
| 91 |
|
| 92 |
-
# Step 2:
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return final_text
|
| 96 |
|
|
@@ -110,7 +157,7 @@ with gr.Blocks() as demo:
|
|
| 110 |
paraphrase_button = gr.Button("Paraphrase & Correct")
|
| 111 |
output_text = gr.Textbox(label="Paraphrased Text")
|
| 112 |
|
| 113 |
-
# Connect the paraphrasing function to the button
|
| 114 |
paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
|
| 115 |
|
| 116 |
# Launch the app with the remaining functionalities
|
|
|
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
+
from grammarChecker.util import check_becauseError, check_butError, check_TenseError, check_articleError
|
| 9 |
+
from pattern.en import conjugate, tenses
|
| 10 |
|
| 11 |
# Initialize the English text classification pipeline for AI detection
|
| 12 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
|
|
| 53 |
|
| 54 |
return ' '.join(corrected_text)
|
| 55 |
|
| 56 |
+
# Function to check and correct tense consistency in sentences using Pattern.en
|
| 57 |
+
def check_tense_consistency(text):
|
| 58 |
+
doc = nlp(text)
|
| 59 |
+
corrected_sentences = []
|
| 60 |
+
|
| 61 |
+
for sent in doc.sents:
|
| 62 |
+
verbs = [token for token in sent if token.pos_ == 'VERB']
|
| 63 |
+
if verbs:
|
| 64 |
+
# Find the most common tense in the sentence
|
| 65 |
+
common_tense = None
|
| 66 |
+
for verb in verbs:
|
| 67 |
+
verb_tense = tenses(verb.text)
|
| 68 |
+
if verb_tense:
|
| 69 |
+
common_tense = verb_tense[0][0]
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
# Conjugate all verbs to the common tense if there's inconsistency
|
| 73 |
+
corrected_sentence = []
|
| 74 |
+
for token in sent:
|
| 75 |
+
if token.pos_ == 'VERB' and common_tense:
|
| 76 |
+
corrected_verb = conjugate(token.text, tense=common_tense)
|
| 77 |
+
corrected_sentence.append(corrected_verb)
|
| 78 |
+
else:
|
| 79 |
+
corrected_sentence.append(token.text)
|
| 80 |
+
corrected_sentences.append(' '.join(corrected_sentence))
|
| 81 |
+
else:
|
| 82 |
+
corrected_sentences.append(sent.text)
|
| 83 |
+
|
| 84 |
+
return ' '.join(corrected_sentences)
|
| 85 |
+
|
| 86 |
+
# Function to perform grammar and structure corrections using external grammar functions
|
| 87 |
+
def grammar_correction(text):
|
| 88 |
+
error_count_because, corrected_text_because = check_becauseError(text)
|
| 89 |
+
error_count_but, corrected_text_but = check_butError(corrected_text_because)
|
| 90 |
+
error_count_tense, corrected_text_tense = check_TenseError(corrected_text_but)
|
| 91 |
+
error_count_article, final_corrected_text = check_articleError(corrected_text_tense)
|
| 92 |
+
|
| 93 |
+
return final_corrected_text
|
| 94 |
+
|
| 95 |
# Paraphrasing function using SpaCy and NLTK (Humanifier)
|
| 96 |
def paraphrase_with_spacy_nltk(text):
|
| 97 |
doc = nlp(text)
|
|
|
|
| 125 |
|
| 126 |
return corrected_text
|
| 127 |
|
| 128 |
+
# Combined function: Paraphrase -> Grammar Correction -> Capitalization -> Tense Consistency (Humanifier)
|
| 129 |
def paraphrase_and_correct(text):
|
| 130 |
# Step 1: Paraphrase the text
|
| 131 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
| 132 |
|
| 133 |
+
# Step 2: Grammar and structure corrections
|
| 134 |
+
grammatically_corrected_text = grammar_correction(paraphrased_text)
|
| 135 |
+
|
| 136 |
+
# Step 3: Capitalize sentences and proper nouns
|
| 137 |
+
capitalized_text = capitalize_sentences_and_nouns(grammatically_corrected_text)
|
| 138 |
+
|
| 139 |
+
# Step 4: Check and correct tense consistency
|
| 140 |
+
final_text = check_tense_consistency(capitalized_text)
|
| 141 |
|
| 142 |
return final_text
|
| 143 |
|
|
|
|
| 157 |
paraphrase_button = gr.Button("Paraphrase & Correct")
|
| 158 |
output_text = gr.Textbox(label="Paraphrased Text")
|
| 159 |
|
| 160 |
+
# Connect the paraphrasing and correction function to the button
|
| 161 |
paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
|
| 162 |
|
| 163 |
# Launch the app with the remaining functionalities
|