Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Dummy function to pass ZeroGPU check
|
| 6 |
@spaces.GPU
|
|
@@ -12,9 +14,10 @@ async def moderate_ui(text):
|
|
| 12 |
if not text.strip():
|
| 13 |
return "Please enter some text."
|
| 14 |
try:
|
|
|
|
| 15 |
request = TextRequest(text=text)
|
| 16 |
response = await moderate_text(request)
|
| 17 |
-
|
| 18 |
result = f"🚨 Flagged: {response.is_flagged}\n\n"
|
| 19 |
result += f"📝 Original: {response.original_text}\n\n"
|
| 20 |
if response.is_flagged and response.rewritten_text:
|
|
@@ -37,10 +40,25 @@ demo = gr.Interface(
|
|
| 37 |
title="Multilingual Moderation API",
|
| 38 |
)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 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 |
# Dummy function to pass ZeroGPU check
|
| 8 |
@spaces.GPU
|
|
|
|
| 14 |
if not text.strip():
|
| 15 |
return "Please enter some text."
|
| 16 |
try:
|
| 17 |
+
from app.main import moderate_text, TextRequest
|
| 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:
|
|
|
|
| 40 |
title="Multilingual Moderation API",
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# Create the main FastAPI app
|
| 44 |
+
app = FastAPI(title="Multilingual Moderation API")
|
| 45 |
+
|
| 46 |
+
# Add CORS middleware – allow all origins (or restrict as needed)
|
| 47 |
+
app.add_middleware(
|
| 48 |
+
CORSMiddleware,
|
| 49 |
+
allow_origins=["*"], # replace "*" with your Angular app's origin for production
|
| 50 |
+
allow_credentials=True,
|
| 51 |
+
allow_methods=["*"],
|
| 52 |
+
allow_headers=["*"],
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Mount the Gradio UI at the root path (or "/ui")
|
| 56 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 57 |
+
|
| 58 |
+
# Mount the moderation API at "/api" – keeps your /api/moderate endpoint
|
| 59 |
+
app.mount("/api", moderation_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)
|