Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from app.main import app as moderation_app # your existing FastAPI app with /moderate
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@spaces.GPU
|
| 9 |
def _satisfy_zerogpu_startup_check():
|
| 10 |
return "GPU available"
|
| 11 |
|
| 12 |
-
|
| 13 |
async def moderate_ui(text):
|
| 14 |
if not text.strip():
|
| 15 |
return "Please enter some text."
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
request = TextRequest(text=text)
|
| 19 |
-
response = await moderate_text(request)
|
| 20 |
|
| 21 |
result = f"π¨ Flagged: {response.is_flagged}\n\n"
|
| 22 |
result += f"π Original: {response.original_text}\n\n"
|
| 23 |
if response.is_flagged and response.rewritten_text:
|
| 24 |
result += "β
Suggested Proper Sentences:\n"
|
| 25 |
-
for i,
|
| 26 |
-
result += f"{i}. {
|
| 27 |
elif response.is_flagged:
|
| 28 |
result += "β
(Model could not generate a rewrite)"
|
| 29 |
else:
|
|
@@ -32,7 +34,7 @@ async def moderate_ui(text):
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error: {str(e)}"
|
| 34 |
|
| 35 |
-
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=moderate_ui,
|
| 38 |
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
|
|
@@ -40,25 +42,22 @@ demo = gr.Interface(
|
|
| 40 |
title="Multilingual Moderation API",
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
CORSMiddleware,
|
| 49 |
-
allow_origins=["*"],
|
| 50 |
-
allow_credentials=True,
|
| 51 |
allow_methods=["*"],
|
| 52 |
allow_headers=["*"],
|
| 53 |
)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
app.
|
| 60 |
|
| 61 |
-
# If you run this file directly:
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
import uvicorn
|
| 64 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import spaces # must be first
|
| 2 |
import gradio as gr
|
| 3 |
+
import uvicorn
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
|
| 7 |
+
from app.main import app as moderation_api, moderate_text, TextRequest
|
| 8 |
+
# If main.py sits next to app.py (no app/ folder), use:
|
| 9 |
+
# from main import app as moderation_api, moderate_text, TextRequest
|
| 10 |
+
|
| 11 |
+
|
| 12 |
@spaces.GPU
|
| 13 |
def _satisfy_zerogpu_startup_check():
|
| 14 |
return "GPU available"
|
| 15 |
|
| 16 |
+
|
| 17 |
async def moderate_ui(text):
|
| 18 |
if not text.strip():
|
| 19 |
return "Please enter some text."
|
| 20 |
try:
|
| 21 |
+
response = await moderate_text(TextRequest(text=text))
|
|
|
|
|
|
|
| 22 |
|
| 23 |
result = f"π¨ Flagged: {response.is_flagged}\n\n"
|
| 24 |
result += f"π Original: {response.original_text}\n\n"
|
| 25 |
if response.is_flagged and response.rewritten_text:
|
| 26 |
result += "β
Suggested Proper Sentences:\n"
|
| 27 |
+
for i, s in enumerate(response.rewritten_text, 1):
|
| 28 |
+
result += f"{i}. {s}\n"
|
| 29 |
elif response.is_flagged:
|
| 30 |
result += "β
(Model could not generate a rewrite)"
|
| 31 |
else:
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
+
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=moderate_ui,
|
| 40 |
inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
|
|
|
|
| 42 |
title="Multilingual Moderation API",
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Root FastAPI app
|
| 46 |
+
root = FastAPI()
|
| 47 |
|
| 48 |
+
# CORS so your Angular app can call it from the browser
|
| 49 |
+
root.add_middleware(
|
| 50 |
CORSMiddleware,
|
| 51 |
+
allow_origins=["*"], # replace with your Angular domain(s) in production
|
|
|
|
| 52 |
allow_methods=["*"],
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# API first -> /api/moderate and /api/health
|
| 57 |
+
root.mount("/api", moderation_api)
|
| 58 |
|
| 59 |
+
# Gradio UI at "/"
|
| 60 |
+
app = gr.mount_gradio_app(root, demo, path="/")
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 63 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|