moderation-api / app.py
AsyncCoder-10's picture
Update app.py
5a3a2b2 verified
Raw History Blame
1.76 kB
import gradio as gr
import spaces
# Import your FastAPI app and logic
from app.main import app as fastapi_app, moderate_text, TextRequest
# 1. Dummy function to pass ZeroGPU check
@spaces.GPU
def _satisfy_zerogpu_startup_check():
return "GPU available"
# 2. The UI function
async def moderate_ui(text):
if not text.strip():
return "Please enter some text."
try:
request = TextRequest(text=text)
response = await moderate_text(request)
result = f"🚨 Flagged: {response.is_flagged}\n\n"
result += f"📝 Original: {response.original_text}\n\n"
if response.is_flagged and response.rewritten_text:
result += "✅ Suggested Proper Sentences:\n"
for i, suggestion in enumerate(response.rewritten_text, 1):
result += f"{i}. {suggestion}\n"
elif response.is_flagged:
result += "✅ (Model could not generate a rewrite)"
else:
result += "✅ This text is clean. No proper sentence needed."
return result
except Exception as e:
return f"Error: {str(e)}"
# 3. Create Gradio UI
demo = gr.Interface(
fn=moderate_ui,
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
outputs=gr.Textbox(lines=12, label="Moderation Result"),
title="Multilingual Moderation API",
)
# 4. ✅ THE FIX: Create a Server and mount your apps
server = gr.Server()
# Mount your FastAPI app (this brings in your /moderate route)
server.mount("/api", fastapi_app)
# Mount the Gradio UI at a different path
server.mount("/ui", demo)
# 5. Launch
if __name__ == "__main__":
server.launch(server_name="0.0.0.0", server_port=7860)