AsyncCoder-10 commited on
Commit
85aee19
·
verified ·
1 Parent(s): a4d12b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -2,12 +2,12 @@ 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!"
@@ -16,28 +16,29 @@ def dummy_gpu_check():
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()
 
2
  import gradio as gr
3
  from fastapi import FastAPI
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ import uvicorn
6
 
7
  # Import your moderation router
8
  from app.api.v1.endpoints.moderation import router as moderation_router
9
 
10
+ # 1. Dummy GPU function for ZeroGPU validation
 
11
  @spaces.GPU
12
  def dummy_gpu_check():
13
  return "ZeroGPU Hardware Connected!"
 
16
  with gr.Blocks() as demo:
17
  gr.Markdown("# 🛡️ Moderation API")
18
  gr.Markdown("The API is live at `/api/v1/moderate`.")
 
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
+ # 3. Create the main FastAPI application FIRST
25
+ app = FastAPI(title="Moderation API")
26
 
27
+ # 4. Add CORS middleware
28
  app.add_middleware(
29
  CORSMiddleware,
30
+ allow_origins=["*"],
31
  allow_credentials=True,
32
  allow_methods=["*"],
33
  allow_headers=["*"],
34
  )
35
 
36
+ # 5. Include your moderation routes BEFORE mounting Gradio
37
  app.include_router(moderation_router, prefix="/api/v1")
38
 
39
+ # 6. Mount Gradio at the root path with ssr_mode=False to prevent interception
40
+ app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=False)
41
+
42
+ # 7. Run the FastAPI app (not demo.launch())
43
  if __name__ == "__main__":
44
+ uvicorn.run(app, host="0.0.0.0", port=7860)