Spaces:
Running on Zero
Running on Zero
Download app.py from AsyncCoder-10/moderation-api: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/AsyncCoder-10/moderation-api/resolve/1baa2286cd3b8976c4e472f528c752fc4fc9b9fc/app.py
- Command line
-
hf download hf://spaces/AsyncCoder-10/moderation-api@1baa2286cd3b8976c4e472f528c752fc4fc9b9fc/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AsyncCoder-10/moderation-api/resolve/1baa2286cd3b8976c4e472f528c752fc4fc9b9fc/app.py
1.62 kB
| import uvicorn | |
| import gradio as gr | |
| import spaces | |
| # Import your FastAPI app and logic | |
| from app.main import app as fastapi_app, moderate_text, TextRequest | |
| def _satisfy_zerogpu_startup_check(): | |
| return "GPU available" | |
| # 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)}" | |
| # 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", | |
| ) | |
| # 🔥 THE MAGIC FIX: Mount Gradio INSIDE FastAPI, at the "/ui" path | |
| app = gr.mount_gradio_app(fastapi_app, demo, path="/ui") | |
| if __name__ == "__main__": | |
| # Run FastAPI (which now contains Gradio) | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |