Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,43 @@
|
|
| 1 |
-
# 1. Import spaces FIRST, before any other CUDA-related packages
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
import uvicorn
|
| 7 |
|
| 8 |
# Import your moderation router
|
| 9 |
from app.api.v1.endpoints.moderation import router as moderation_router
|
| 10 |
|
| 11 |
-
#
|
|
|
|
| 12 |
@spaces.GPU
|
| 13 |
def dummy_gpu_check():
|
| 14 |
return "ZeroGPU Hardware Connected!"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
gr.Markdown("# 🛡️ Moderation API")
|
| 19 |
gr.Markdown("The API is live at `/api/v1/moderate`.")
|
|
|
|
| 20 |
with gr.Row():
|
| 21 |
btn = gr.Button("Test ZeroGPU Connection")
|
| 22 |
output = gr.Textbox()
|
|
|
|
| 23 |
btn.click(fn=dummy_gpu_check, inputs=[], outputs=output)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
app =
|
| 27 |
|
| 28 |
-
#
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
| 31 |
-
allow_origins=["*"], #
|
| 32 |
allow_credentials=True,
|
| 33 |
allow_methods=["*"],
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
app.include_router(moderation_router, prefix="/api/v1")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
app = gr.mount_gradio_app(app, demo, path="/ui")
|
| 42 |
-
|
| 43 |
-
# 8. Run the FastAPI app (not demo.launch())
|
| 44 |
if __name__ == "__main__":
|
| 45 |
-
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 5 |
|
| 6 |
# Import your moderation router
|
| 7 |
from app.api.v1.endpoints.moderation import router as moderation_router
|
| 8 |
|
| 9 |
+
# 1. Create a dummy GPU function.
|
| 10 |
+
# CRITICAL: This EXACT function must be passed to the button click below.
|
| 11 |
@spaces.GPU
|
| 12 |
def dummy_gpu_check():
|
| 13 |
return "ZeroGPU Hardware Connected!"
|
| 14 |
|
| 15 |
+
# 2. Build the Gradio UI
|
| 16 |
with gr.Blocks() as demo:
|
| 17 |
gr.Markdown("# 🛡️ Moderation API")
|
| 18 |
gr.Markdown("The API is live at `/api/v1/moderate`.")
|
| 19 |
+
|
| 20 |
with gr.Row():
|
| 21 |
btn = gr.Button("Test ZeroGPU Connection")
|
| 22 |
output = gr.Textbox()
|
| 23 |
+
# Bind the decorated function directly to the event
|
| 24 |
btn.click(fn=dummy_gpu_check, inputs=[], outputs=output)
|
| 25 |
|
| 26 |
+
# 3. Access Gradio's internal FastAPI app and add your routes
|
| 27 |
+
app = demo.app
|
| 28 |
|
| 29 |
+
# 4. Add CORS middleware for Angular
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
| 32 |
+
allow_origins=["*"], # Your Angular app URL can go here in production
|
| 33 |
allow_credentials=True,
|
| 34 |
allow_methods=["*"],
|
| 35 |
allow_headers=["*"],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# 5. Attach your moderation API to Gradio's app
|
| 39 |
app.include_router(moderation_router, prefix="/api/v1")
|
| 40 |
|
| 41 |
+
# 6. Launch Gradio (This passes the ZeroGPU startup scanner)
|
|
|
|
|
|
|
|
|
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|