Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import spacy
|
| 3 |
import subprocess
|
| 4 |
import nltk
|
| 5 |
from nltk.corpus import wordnet
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
nltk.download('wordnet')
|
| 9 |
nltk.download('omw-1.4')
|
| 10 |
|
| 11 |
-
# Ensure the SpaCy model is installed
|
| 12 |
try:
|
| 13 |
nlp = spacy.load("en_core_web_sm")
|
| 14 |
except OSError:
|
| 15 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 16 |
nlp = spacy.load("en_core_web_sm")
|
| 17 |
|
| 18 |
-
# Function to get synonyms using NLTK WordNet
|
| 19 |
def get_synonyms_nltk(word, pos):
|
| 20 |
synsets = wordnet.synsets(word, pos=pos)
|
| 21 |
if synsets:
|
|
@@ -23,7 +33,7 @@ def get_synonyms_nltk(word, pos):
|
|
| 23 |
return [lemma.name() for lemma in lemmas]
|
| 24 |
return []
|
| 25 |
|
| 26 |
-
# Function to capitalize the first letter of sentences and proper nouns
|
| 27 |
def capitalize_sentences_and_nouns(text):
|
| 28 |
doc = nlp(text)
|
| 29 |
corrected_text = []
|
|
@@ -41,7 +51,7 @@ def capitalize_sentences_and_nouns(text):
|
|
| 41 |
|
| 42 |
return ' '.join(corrected_text)
|
| 43 |
|
| 44 |
-
# Paraphrasing function using SpaCy and NLTK
|
| 45 |
def paraphrase_with_spacy_nltk(text):
|
| 46 |
doc = nlp(text)
|
| 47 |
paraphrased_words = []
|
|
@@ -74,7 +84,7 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 74 |
|
| 75 |
return corrected_text
|
| 76 |
|
| 77 |
-
# Combined function: Paraphrase -> Capitalization
|
| 78 |
def paraphrase_and_correct(text):
|
| 79 |
# Step 1: Paraphrase the text
|
| 80 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
|
@@ -84,16 +94,24 @@ def paraphrase_and_correct(text):
|
|
| 84 |
|
| 85 |
return final_text
|
| 86 |
|
| 87 |
-
# Gradio
|
| 88 |
-
with gr.Blocks() as
|
| 89 |
-
with gr.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
# Launch the
|
| 99 |
-
|
|
|
|
| 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)
|
| 13 |
+
def predict_en(text):
|
| 14 |
+
res = pipeline_en(text)[0]
|
| 15 |
+
return res['label'], res['score']
|
| 16 |
+
|
| 17 |
+
# Ensure necessary NLTK data is downloaded for Humanifier
|
| 18 |
nltk.download('wordnet')
|
| 19 |
nltk.download('omw-1.4')
|
| 20 |
|
| 21 |
+
# Ensure the SpaCy model is installed for Humanifier
|
| 22 |
try:
|
| 23 |
nlp = spacy.load("en_core_web_sm")
|
| 24 |
except OSError:
|
| 25 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 26 |
nlp = spacy.load("en_core_web_sm")
|
| 27 |
|
| 28 |
+
# Function to get synonyms using NLTK WordNet (Humanifier)
|
| 29 |
def get_synonyms_nltk(word, pos):
|
| 30 |
synsets = wordnet.synsets(word, pos=pos)
|
| 31 |
if synsets:
|
|
|
|
| 33 |
return [lemma.name() for lemma in lemmas]
|
| 34 |
return []
|
| 35 |
|
| 36 |
+
# Function to capitalize the first letter of sentences and proper nouns (Humanifier)
|
| 37 |
def capitalize_sentences_and_nouns(text):
|
| 38 |
doc = nlp(text)
|
| 39 |
corrected_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)
|
| 57 |
paraphrased_words = []
|
|
|
|
| 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)
|
|
|
|
| 94 |
|
| 95 |
return final_text
|
| 96 |
|
| 97 |
+
# Gradio app setup with two tabs
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
with gr.Tab("AI Detection"):
|
| 100 |
+
t1 = gr.Textbox(lines=5, label='Text')
|
| 101 |
+
button1 = gr.Button("🤖 Predict!")
|
| 102 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
| 103 |
+
score1 = gr.Textbox(lines=1, label='Prob')
|
| 104 |
+
|
| 105 |
+
# Connect the prediction function to the button
|
| 106 |
+
button1.click(predict_en, inputs=[t1], outputs=[label1, score1], api_name='predict_en')
|
| 107 |
+
|
| 108 |
+
with gr.Tab("Humanifier"):
|
| 109 |
+
text_input = gr.Textbox(lines=5, label="Input Text")
|
| 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 both functionalities
|
| 117 |
+
demo.launch()
|