sashtech commited on
Commit
01902a2
·
verified ·
1 Parent(s): 33af001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -83
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
- app = FastAPI()
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
- # Define the input model for FastAPI (for validation)
34
- class TextInput(BaseModel):
 
 
 
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 rephrase text and replace words with their synonyms while maintaining form
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
- # [Place your processing logic here, such as removing redundant words, correcting grammar, etc.]
73
- return rephrase_with_synonyms(text)
74
-
75
- # Define FastAPI route for AI detection
76
- @app.post("/ai-detect")
77
- async def ai_detect(input: TextInput):
78
- label, score = predict_en(input.text)
79
- return {"label": label, "score": score}
80
-
81
- # Define FastAPI route for paraphrasing and grammar correction
82
- @app.post("/paraphrase")
83
- async def paraphrase(input: TextInput):
84
- corrected_text = paraphrase_and_correct(input.text)
85
- return {"corrected_text": corrected_text}
86
-
87
- # Function to get synonyms using NLTK WordNet
88
- def get_synonyms_nltk(word, pos):
89
- synsets = wordnet.synsets(word, pos=pos)
90
- if synsets:
91
- lemmas = synsets[0].lemmas()
92
- return [lemma.name() for lemma in lemmas]
93
- return []
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
- with gr.Tab("Paraphrasing & Grammar Correction"):
108
- t2 = gr.Textbox(lines=5, label='Text for Paraphrasing and Grammar Correction')
 
109
  button2 = gr.Button("🔄 Paraphrase and Correct")
110
- result2 = gr.Textbox(lines=10, label='Corrected Text', placeholder="Corrected and paraphrased text will appear here")
111
 
112
- # Connect the paraphrasing and correction function to the Gradio UI
113
  button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2)
114
 
115
- # Start Gradio on port 7860 and share the app publicly
116
- demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
117
 
118
- # Run both FastAPI and Gradio concurrently
119
- if __name__ == "__main__":
120
- import multiprocessing
121
 
122
- # Run FastAPI server in one process
123
- fastapi_process = multiprocessing.Process(target=uvicorn.run, args=(app,), kwargs={"host": "0.0.0.0", "port": 8000})
124
- fastapi_process.start()
125
 
126
- # Run Gradio interface in another process
127
- gradio_ui()
 
 
 
 
128
 
129
- # When done, stop both processes
130
- fastapi_process.join()
 
 
 
 
 
 
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)