Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
|
|
|
| 3 |
from app.main import app as fastapi_app, moderate_text, TextRequest
|
| 4 |
|
| 5 |
@spaces.GPU
|
| 6 |
def _satisfy_zerogpu_startup_check():
|
| 7 |
return "GPU available"
|
| 8 |
|
|
|
|
| 9 |
async def moderate_ui(text):
|
| 10 |
if not text.strip():
|
| 11 |
return "Please enter some text."
|
| 12 |
try:
|
| 13 |
request = TextRequest(text=text)
|
| 14 |
response = await moderate_text(request)
|
|
|
|
| 15 |
result = f"π¨ Flagged: {response.is_flagged}\n\n"
|
| 16 |
result += f"π Original: {response.original_text}\n\n"
|
| 17 |
if response.is_flagged and response.rewritten_text:
|
|
@@ -19,22 +24,24 @@ async def moderate_ui(text):
|
|
| 19 |
for i, suggestion in enumerate(response.rewritten_text, 1):
|
| 20 |
result += f"{i}. {suggestion}\n"
|
| 21 |
elif response.is_flagged:
|
| 22 |
-
result += "β
|
| 23 |
else:
|
| 24 |
result += "β
This text is clean. No proper sentence needed."
|
| 25 |
return result
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
|
|
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=moderate_ui,
|
| 31 |
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
|
| 32 |
outputs=gr.Textbox(lines=12, label="Moderation Result"),
|
| 33 |
title="Multilingual Moderation API",
|
| 34 |
-
description="Enter text to detect abuse/threats and get suggested proper sentences."
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
+
|
| 5 |
+
# Import your FastAPI app and logic
|
| 6 |
from app.main import app as fastapi_app, moderate_text, TextRequest
|
| 7 |
|
| 8 |
@spaces.GPU
|
| 9 |
def _satisfy_zerogpu_startup_check():
|
| 10 |
return "GPU available"
|
| 11 |
|
| 12 |
+
# The UI function
|
| 13 |
async def moderate_ui(text):
|
| 14 |
if not text.strip():
|
| 15 |
return "Please enter some text."
|
| 16 |
try:
|
| 17 |
request = TextRequest(text=text)
|
| 18 |
response = await moderate_text(request)
|
| 19 |
+
|
| 20 |
result = f"π¨ Flagged: {response.is_flagged}\n\n"
|
| 21 |
result += f"π Original: {response.original_text}\n\n"
|
| 22 |
if response.is_flagged and response.rewritten_text:
|
|
|
|
| 24 |
for i, suggestion in enumerate(response.rewritten_text, 1):
|
| 25 |
result += f"{i}. {suggestion}\n"
|
| 26 |
elif response.is_flagged:
|
| 27 |
+
result += "β
(Model could not generate a rewrite)"
|
| 28 |
else:
|
| 29 |
result += "β
This text is clean. No proper sentence needed."
|
| 30 |
return result
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error: {str(e)}"
|
| 33 |
|
| 34 |
+
# Create Gradio UI
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=moderate_ui,
|
| 37 |
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
|
| 38 |
outputs=gr.Textbox(lines=12, label="Moderation Result"),
|
| 39 |
title="Multilingual Moderation API",
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# π₯ THE MAGIC FIX: Mount Gradio INSIDE FastAPI, at the "/ui" path
|
| 43 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/ui")
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
# Run FastAPI (which now contains Gradio)
|
| 47 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|