Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
import uvicorn
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
@spaces.GPU
|
| 7 |
def _satisfy_zerogpu_startup_check():
|
| 8 |
-
"""Dummy function to bypass ZeroGPU startup requirement."""
|
| 9 |
return "GPU available"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
demo = gr.Interface(
|
| 18 |
fn=moderate_ui,
|
| 19 |
-
inputs="
|
| 20 |
-
outputs="
|
| 21 |
-
title="Multilingual Moderation API"
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
# This keeps your /moderate and /health endpoints accessible.
|
| 26 |
-
# We mount it under the "/api" path to avoid conflicts.
|
| 27 |
-
demo.app.mount("/api", fastapi_app)
|
| 28 |
-
|
| 29 |
-
# 3. Launch Gradio (required for ZeroGPU)
|
| 30 |
if __name__ == "__main__":
|
| 31 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Import the logic and models from your FastAPI app
|
| 5 |
+
from app.main import moderate_text, TextRequest
|
| 6 |
+
|
| 7 |
+
# 2. ZeroGPU Startup Check
|
| 8 |
@spaces.GPU
|
| 9 |
def _satisfy_zerogpu_startup_check():
|
|
|
|
| 10 |
return "GPU available"
|
| 11 |
|
| 12 |
+
# 3. The Real UI Function
|
| 13 |
+
async def moderate_ui(text):
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return "Please enter some text."
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Create the request object expected by your FastAPI route
|
| 19 |
+
request = TextRequest(text=text)
|
| 20 |
+
|
| 21 |
+
# Call your actual FastAPI function directly
|
| 22 |
+
response = await moderate_text(request)
|
| 23 |
+
|
| 24 |
+
# Format the result nicely for the UI
|
| 25 |
+
result = f"🚨 Flagged: {response.is_flagged}\n\n"
|
| 26 |
+
result += f"📝 Original: {response.original_text}\n\n"
|
| 27 |
+
|
| 28 |
+
if response.is_flagged and response.rewritten_text:
|
| 29 |
+
result += f"✅ Suggested Proper Sentence:\n\"{response.rewritten_text}\""
|
| 30 |
+
elif response.is_flagged:
|
| 31 |
+
result += "✅ Suggested Proper Sentence: (Model could not generate a rewrite)"
|
| 32 |
+
else:
|
| 33 |
+
result += "✅ This text is clean. No proper sentence needed."
|
| 34 |
+
|
| 35 |
+
return result
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
|
| 39 |
+
# 4. Build the Interface
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=moderate_ui,
|
| 42 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here (e.g., 'I will kill you')..."),
|
| 43 |
+
outputs=gr.Textbox(lines=6, label="Moderation Result"),
|
| 44 |
+
title="Multilingual Moderation API",
|
| 45 |
+
description="Enter text to detect abuse/threats and get a suggested proper sentence."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# 5. Launch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if __name__ == "__main__":
|
| 50 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|