Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,9 @@ import uvicorn
|
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
@spaces.GPU
|
| 13 |
-
def _satisfy_zerogpu_startup_check():
|
| 14 |
-
return "GPU available"
|
| 15 |
|
| 16 |
|
| 17 |
async def moderate_ui(text):
|
|
@@ -35,28 +30,30 @@ async def moderate_ui(text):
|
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
root.add_middleware(
|
| 50 |
CORSMiddleware,
|
| 51 |
-
allow_origins=["*"], #
|
| 52 |
allow_methods=["*"],
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
| 56 |
-
#
|
| 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__":
|
|
|
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
|
| 7 |
+
# main.py next to app.py:
|
| 8 |
+
from main import app as moderation_api, moderate_text, TextRequest, generate_suggestions
|
| 9 |
+
# If main.py is inside an app/ folder, use: from app.main import ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
async def moderate_ui(text):
|
|
|
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
|
| 33 |
+
with gr.Blocks(title="Multilingual Moderation API") as demo:
|
| 34 |
+
gr.Markdown("# Multilingual Moderation API")
|
| 35 |
+
inp = gr.Textbox(lines=3, placeholder="Type a toxic sentence here...", label="Text")
|
| 36 |
+
btn = gr.Button("Moderate")
|
| 37 |
+
out = gr.Textbox(lines=12, label="Moderation Result")
|
| 38 |
+
btn.click(moderate_ui, inp, out, api_name="moderate_ui")
|
| 39 |
|
| 40 |
+
# Hidden event: registers the @spaces.GPU function so ZeroGPU detects it
|
| 41 |
+
with gr.Row(visible=False):
|
| 42 |
+
h_in = gr.Textbox()
|
| 43 |
+
h_out = gr.JSON()
|
| 44 |
+
h_btn = gr.Button()
|
| 45 |
+
h_btn.click(generate_suggestions, h_in, h_out, api_name="rewrite")
|
| 46 |
|
| 47 |
+
|
| 48 |
+
root = FastAPI()
|
| 49 |
root.add_middleware(
|
| 50 |
CORSMiddleware,
|
| 51 |
+
allow_origins=["*"], # restrict to your Angular domain in production
|
| 52 |
allow_methods=["*"],
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
| 56 |
+
root.mount("/api", moderation_api) # -> /api/moderate, /api/health
|
|
|
|
|
|
|
|
|
|
| 57 |
app = gr.mount_gradio_app(root, demo, path="/")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|