Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,126 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Initialize Gramformer
|
5 |
-
gf = Gramformer(models=
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
return
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import torch
|
9 |
+
from gramformer import Gramformer
|
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 Gramformer
|
15 |
+
gf = Gramformer(models=2, use_gpu=False) # 2 = corrector
|
16 |
+
|
17 |
+
# Function to predict the label and score for English text (AI Detection)
|
18 |
+
def predict_en(text):
|
19 |
+
res = pipeline_en(text)[0]
|
20 |
+
return res['label'], res['score']
|
21 |
+
|
22 |
+
# Ensure necessary NLTK data is downloaded for Humanifier
|
23 |
+
nltk.download('wordnet')
|
24 |
+
nltk.download('omw-1.4')
|
25 |
+
|
26 |
+
# Ensure the SpaCy model is installed for Humanifier
|
27 |
+
try:
|
28 |
+
nlp = spacy.load("en_core_web_sm")
|
29 |
+
except OSError:
|
30 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
31 |
+
nlp = spacy.load("en_core_web_sm")
|
32 |
+
|
33 |
+
# Function to get synonyms using NLTK WordNet (Humanifier)
|
34 |
+
def get_synonyms_nltk(word, pos):
|
35 |
+
synsets = wordnet.synsets(word, pos=pos)
|
36 |
+
if synsets:
|
37 |
+
lemmas = synsets[0].lemmas()
|
38 |
+
return [lemma.name() for lemma in lemmas]
|
39 |
+
return []
|
40 |
+
|
41 |
+
# Function to capitalize the first letter of sentences and proper nouns (Humanifier)
|
42 |
+
def capitalize_sentences_and_nouns(text):
|
43 |
+
doc = nlp(text)
|
44 |
+
corrected_text = []
|
45 |
+
|
46 |
+
for sent in doc.sents:
|
47 |
+
sentence = []
|
48 |
+
for token in sent:
|
49 |
+
if token.i == sent.start: # First word of the sentence
|
50 |
+
sentence.append(token.text.capitalize())
|
51 |
+
elif token.pos_ == "PROPN": # Proper noun
|
52 |
+
sentence.append(token.text.capitalize())
|
53 |
+
else:
|
54 |
+
sentence.append(token.text)
|
55 |
+
corrected_text.append(' '.join(sentence))
|
56 |
+
|
57 |
+
return ' '.join(corrected_text)
|
58 |
+
|
59 |
+
# Paraphrasing function using SpaCy and NLTK (Humanifier)
|
60 |
+
def paraphrase_with_spacy_nltk(text):
|
61 |
+
doc = nlp(text)
|
62 |
+
paraphrased_words = []
|
63 |
+
|
64 |
+
for token in doc:
|
65 |
+
# Map SpaCy POS tags to WordNet POS tags
|
66 |
+
pos = None
|
67 |
+
if token.pos_ in {"NOUN"}:
|
68 |
+
pos = wordnet.NOUN
|
69 |
+
elif token.pos_ in {"VERB"}:
|
70 |
+
pos = wordnet.VERB
|
71 |
+
elif token.pos_ in {"ADJ"}:
|
72 |
+
pos = wordnet.ADJ
|
73 |
+
elif token.pos_ in {"ADV"}:
|
74 |
+
pos = wordnet.ADV
|
75 |
+
|
76 |
+
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
77 |
+
|
78 |
+
# Replace with a synonym only if it makes sense
|
79 |
+
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
80 |
+
paraphrased_words.append(synonyms[0])
|
81 |
+
else:
|
82 |
+
paraphrased_words.append(token.text)
|
83 |
+
|
84 |
+
# Join the words back into a sentence
|
85 |
+
paraphrased_sentence = ' '.join(paraphrased_words)
|
86 |
+
|
87 |
+
# Capitalize sentences and proper nouns
|
88 |
+
corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
|
89 |
+
|
90 |
+
return corrected_text
|
91 |
+
|
92 |
+
# Combined function: Paraphrase -> Capitalization -> Grammar Correction (Humanifier)
|
93 |
+
def paraphrase_correct_and_grammar(text):
|
94 |
+
# Step 1: Paraphrase the text
|
95 |
+
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
96 |
+
|
97 |
+
# Step 2: Capitalize sentences and proper nouns
|
98 |
+
capitalized_text = capitalize_sentences_and_nouns(paraphrased_text)
|
99 |
+
|
100 |
+
# Step 3: Grammar correction using Gramformer
|
101 |
+
corrected_sentences = gf.correct(capitalized_text)
|
102 |
+
final_text = corrected_sentences[0] if corrected_sentences else capitalized_text
|
103 |
+
|
104 |
+
return final_text
|
105 |
+
|
106 |
+
# Gradio app setup with two tabs
|
107 |
+
with gr.Blocks() as demo:
|
108 |
+
with gr.Tab("AI Detection"):
|
109 |
+
t1 = gr.Textbox(lines=5, label='Text')
|
110 |
+
button1 = gr.Button("🤖 Predict!")
|
111 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
112 |
+
score1 = gr.Textbox(lines=1, label='Prob')
|
113 |
+
|
114 |
+
# Connect the prediction function to the button
|
115 |
+
button1.click(predict_en, inputs=[t1], outputs=[label1, score1], api_name='predict_en')
|
116 |
+
|
117 |
+
with gr.Tab("Humanifier"):
|
118 |
+
text_input = gr.Textbox(lines=5, label="Input Text")
|
119 |
+
paraphrase_button = gr.Button("Paraphrase, Correct & Grammar Check")
|
120 |
+
output_text = gr.Textbox(label="Processed Text")
|
121 |
+
|
122 |
+
# Connect the paraphrasing and grammar correction function to the button
|
123 |
+
paraphrase_button.click(paraphrase_correct_and_grammar, inputs=text_input, outputs=output_text)
|
124 |
+
|
125 |
+
# Launch the app with both functionalities
|
126 |
+
demo.launch()
|