Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,13 @@ import subprocess
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
-
from fastapi import FastAPI
|
| 10 |
from pydantic import BaseModel
|
| 11 |
import uvicorn
|
|
|
|
| 12 |
|
| 13 |
-
# Initialize FastAPI
|
| 14 |
-
|
| 15 |
|
| 16 |
# Initialize the English text classification pipeline for AI detection
|
| 17 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
@@ -30,8 +31,11 @@ except OSError:
|
|
| 30 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 31 |
nlp = spacy.load("en_core_web_sm")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
text: str
|
| 36 |
|
| 37 |
# Function to predict the label and score for English text (AI Detection)
|
|
@@ -39,92 +43,61 @@ def predict_en(text):
|
|
| 39 |
res = pipeline_en(text)[0]
|
| 40 |
return res['label'], res['score']
|
| 41 |
|
| 42 |
-
# Function to
|
| 43 |
-
def rephrase_with_synonyms(text):
|
| 44 |
-
doc = nlp(text)
|
| 45 |
-
rephrased_text = []
|
| 46 |
-
|
| 47 |
-
for token in doc:
|
| 48 |
-
pos_tag = None
|
| 49 |
-
if token.pos_ == "NOUN":
|
| 50 |
-
pos_tag = wordnet.NOUN
|
| 51 |
-
elif token.pos_ == "VERB":
|
| 52 |
-
pos_tag = wordnet.VERB
|
| 53 |
-
elif token.pos_ == "ADJ":
|
| 54 |
-
pos_tag = wordnet.ADJ
|
| 55 |
-
elif token.pos_ == "ADV":
|
| 56 |
-
pos_tag = wordnet.ADV
|
| 57 |
-
|
| 58 |
-
if pos_tag:
|
| 59 |
-
synonyms = get_synonyms_nltk(token.text, pos_tag)
|
| 60 |
-
if synonyms:
|
| 61 |
-
synonym = synonyms[0]
|
| 62 |
-
rephrased_text.append(synonym)
|
| 63 |
-
else:
|
| 64 |
-
rephrased_text.append(token.text)
|
| 65 |
-
else:
|
| 66 |
-
rephrased_text.append(token.text)
|
| 67 |
-
|
| 68 |
-
return ' '.join(rephrased_text)
|
| 69 |
-
|
| 70 |
-
# Function to paraphrase and correct text
|
| 71 |
def paraphrase_and_correct(text):
|
| 72 |
-
#
|
| 73 |
-
return
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
@
|
| 77 |
-
async def
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# Set up Gradio UI
|
| 96 |
-
def gradio_ui():
|
| 97 |
-
with gr.Blocks() as demo:
|
| 98 |
-
with gr.Tab("AI Detection"):
|
| 99 |
-
t1 = gr.Textbox(lines=5, label='Text for AI Detection')
|
| 100 |
-
button1 = gr.Button("🤖 Predict AI Detection")
|
| 101 |
-
label1 = gr.Textbox(lines=1, label='Predicted Label')
|
| 102 |
-
score1 = gr.Textbox(lines=1, label='Prediction Score')
|
| 103 |
-
|
| 104 |
-
# Connect the prediction function to the Gradio UI
|
| 105 |
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
button2 = gr.Button("🔄 Paraphrase and Correct")
|
| 110 |
-
result2 = gr.Textbox(lines=10, label='Corrected Text', placeholder="
|
| 111 |
|
| 112 |
-
# Connect the paraphrasing and correction function to the
|
| 113 |
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
demo.launch(
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
import multiprocessing
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
fastapi_process.start()
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
+
from fastapi import FastAPI, HTTPException
|
| 10 |
from pydantic import BaseModel
|
| 11 |
import uvicorn
|
| 12 |
+
import uuid # To generate unique link IDs
|
| 13 |
|
| 14 |
+
# Initialize FastAPI app
|
| 15 |
+
api_app = FastAPI()
|
| 16 |
|
| 17 |
# Initialize the English text classification pipeline for AI detection
|
| 18 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
|
|
| 31 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 32 |
nlp = spacy.load("en_core_web_sm")
|
| 33 |
|
| 34 |
+
# Generate temporary link storage (could be database or in-memory store)
|
| 35 |
+
temporary_links = {}
|
| 36 |
+
|
| 37 |
+
# Define request models for FastAPI
|
| 38 |
+
class TextRequest(BaseModel):
|
| 39 |
text: str
|
| 40 |
|
| 41 |
# Function to predict the label and score for English text (AI Detection)
|
|
|
|
| 43 |
res = pipeline_en(text)[0]
|
| 44 |
return res['label'], res['score']
|
| 45 |
|
| 46 |
+
# Function to paraphrase and correct grammar with enhanced accuracy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def paraphrase_and_correct(text):
|
| 48 |
+
# Here should go all the paraphrasing and grammar correction logic.
|
| 49 |
+
return text # For now just return the input
|
| 50 |
+
|
| 51 |
+
# API Endpoint to create a new temporary link for Gradio interface
|
| 52 |
+
@api_app.post("/generate-link/")
|
| 53 |
+
async def generate_temporary_link(task: str):
|
| 54 |
+
# Check if the task is either 'ai-detection' or 'paraphrase'
|
| 55 |
+
if task not in ["ai-detection", "paraphrase"]:
|
| 56 |
+
raise HTTPException(status_code=400, detail="Invalid task type.")
|
| 57 |
+
|
| 58 |
+
# Create a unique link using UUID
|
| 59 |
+
link_id = str(uuid.uuid4())
|
| 60 |
+
|
| 61 |
+
# Set up Gradio interface based on task
|
| 62 |
+
if task == "ai-detection":
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
t1 = gr.Textbox(lines=5, label='Text')
|
| 65 |
+
button1 = gr.Button("🤖 Predict!")
|
| 66 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
| 67 |
+
score1 = gr.Textbox(lines=1, label='Prob')
|
| 68 |
+
|
| 69 |
+
# Connect the prediction function to the button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1])
|
| 71 |
|
| 72 |
+
elif task == "paraphrase":
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction')
|
| 75 |
button2 = gr.Button("🔄 Paraphrase and Correct")
|
| 76 |
+
result2 = gr.Textbox(lines=10, label='Corrected Text', placeholder="The corrected text will appear here...")
|
| 77 |
|
| 78 |
+
# Connect the paraphrasing and correction function to the button
|
| 79 |
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
|
| 80 |
|
| 81 |
+
# Launch Gradio and get the link
|
| 82 |
+
demo_url = demo.launch(share=True, prevent_thread_lock=True)
|
| 83 |
|
| 84 |
+
# Save the generated link in memory (temporary)
|
| 85 |
+
temporary_links[link_id] = {"task": task, "url": demo_url}
|
|
|
|
| 86 |
|
| 87 |
+
# Return the link to the user
|
| 88 |
+
return {"link_id": link_id, "url": demo_url}
|
|
|
|
| 89 |
|
| 90 |
+
# API Endpoint to get the status or result via the generated link
|
| 91 |
+
@api_app.get("/get-link/{link_id}")
|
| 92 |
+
async def get_temporary_link(link_id: str):
|
| 93 |
+
# Check if the link exists
|
| 94 |
+
if link_id not in temporary_links:
|
| 95 |
+
raise HTTPException(status_code=404, detail="Link not found.")
|
| 96 |
|
| 97 |
+
# Retrieve the link details
|
| 98 |
+
link_details = temporary_links[link_id]
|
| 99 |
+
return {"link": link_details["url"]}
|
| 100 |
+
|
| 101 |
+
# Run the FastAPI app
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
uvicorn.run(api_app, host="0.0.0.0", port=8000)
|