Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from app.main import app as fastapi_app
|
| 4 |
|
| 5 |
# Dummy function to pass ZeroGPU check
|
| 6 |
@spaces.GPU
|
| 7 |
def _satisfy_zerogpu_startup_check():
|
| 8 |
return "GPU available"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def moderate_ui(text):
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
demo = gr.Interface(
|
| 15 |
fn=moderate_ui,
|
| 16 |
-
inputs="
|
| 17 |
-
outputs="
|
| 18 |
-
title="Multilingual Moderation API"
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Mount FastAPI onto Gradio
|
| 22 |
-
# This exposes your API at /api/moderate
|
| 23 |
demo.app.mount("/api", fastapi_app)
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
+
from app.main import app as fastapi_app, moderate_text, TextRequest
|
| 4 |
|
| 5 |
# Dummy function to pass ZeroGPU check
|
| 6 |
@spaces.GPU
|
| 7 |
def _satisfy_zerogpu_startup_check():
|
| 8 |
return "GPU available"
|
| 9 |
|
| 10 |
+
# The real UI function that calls your FastAPI logic
|
| 11 |
+
async def moderate_ui(text):
|
| 12 |
+
if not text.strip():
|
| 13 |
+
return "Please enter some text."
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Create the Pydantic request object
|
| 17 |
+
request = TextRequest(text=text)
|
| 18 |
+
|
| 19 |
+
# Call your FastAPI function directly
|
| 20 |
+
response = await moderate_text(request)
|
| 21 |
+
|
| 22 |
+
# Format the output for the Gradio UI
|
| 23 |
+
result = f"🚨 Flagged: {response.is_flagged}\n\n"
|
| 24 |
+
result += f"📝 Original: {response.original_text}\n\n"
|
| 25 |
+
|
| 26 |
+
if response.is_flagged and response.rewritten_text:
|
| 27 |
+
result += "✅ Suggested Proper Sentences:\n"
|
| 28 |
+
for i, suggestion in enumerate(response.rewritten_text, 1):
|
| 29 |
+
result += f"{i}. {suggestion}\n"
|
| 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 |
+
# Build the Gradio Interface
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=moderate_ui,
|
| 42 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
|
| 43 |
+
outputs=gr.Textbox(lines=12, label="Moderation Result"),
|
| 44 |
+
title="Multilingual Moderation API",
|
| 45 |
+
description="Enter text to detect abuse/threats and get suggested proper sentences."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Mount FastAPI onto Gradio for your external apps to use
|
|
|
|
| 49 |
demo.app.mount("/api", fastapi_app)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|