AsyncCoder-10 commited on
Commit
4e2ae91
·
verified ·
1 Parent(s): ca36815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -1,25 +1,43 @@
1
- # 1. MUST be the very first import
2
  import spaces
3
  import gradio as gr
4
  from fastapi import FastAPI
5
- from app.main import app as fastapi_app
6
 
7
- # 2. Dummy GPU function
 
 
 
8
  @spaces.GPU
9
  def dummy_gpu_check():
10
  return "ZeroGPU Hardware Connected!"
11
 
12
- # 3. Create the Gradio UI with a button that calls the GPU function
13
  with gr.Blocks() as demo:
14
  gr.Markdown("# 🛡️ Moderation API")
15
  gr.Markdown("This is a live API. Send POST requests to `/api/v1/moderate`.")
16
  gr.Markdown("You can view the interactive API docs at [here](/docs) once it starts.")
17
 
18
- # This button satisfies the ZeroGPU AST parser
19
  with gr.Row():
20
  btn = gr.Button("Test ZeroGPU Connection")
21
  output = gr.Textbox()
 
22
  btn.click(fn=dummy_gpu_check, inputs=[], outputs=output)
23
 
24
- # 4. Mount FastAPI
25
- app = gr.mount_gradio_app(fastapi_app, demo, path="/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1. CRITICAL: Import spaces before anything else that might touch CUDA
2
  import spaces
3
  import gradio as gr
4
  from fastapi import FastAPI
5
+ from fastapi.middleware.cors import CORSMiddleware
6
 
7
+ # Import your moderation router
8
+ from app.api.v1.endpoints.moderation import router as moderation_router
9
+
10
+ # 2. Dummy GPU function that MUST be wired to a Gradio event
11
  @spaces.GPU
12
  def dummy_gpu_check():
13
  return "ZeroGPU Hardware Connected!"
14
 
15
+ # 3. Build the Gradio UI and wire the GPU function to a button click
16
  with gr.Blocks() as demo:
17
  gr.Markdown("# 🛡️ Moderation API")
18
  gr.Markdown("This is a live API. Send POST requests to `/api/v1/moderate`.")
19
  gr.Markdown("You can view the interactive API docs at [here](/docs) once it starts.")
20
 
 
21
  with gr.Row():
22
  btn = gr.Button("Test ZeroGPU Connection")
23
  output = gr.Textbox()
24
+ # The function passed here is the one decorated with @spaces.GPU
25
  btn.click(fn=dummy_gpu_check, inputs=[], outputs=output)
26
 
27
+ # 4. Get Gradio's internal FastAPI app
28
+ app = demo.app
29
+
30
+ # 5. Attach your FastAPI routes and middleware to Gradio's app
31
+ app.include_router(moderation_router, prefix="/api/v1")
32
+
33
+ app.add_middleware(
34
+ CORSMiddleware,
35
+ allow_origins=["*"],
36
+ allow_credentials=True,
37
+ allow_methods=["*"],
38
+ allow_headers=["*"],
39
+ )
40
+
41
+ # 6. Launch Gradio (this initializes @spaces.GPU properly)
42
+ if __name__ == "__main__":
43
+ demo.launch()