AsyncCoder-10 commited on
Commit
a4d12b8
·
verified ·
1 Parent(s): 5c0cac9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
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
- # 2. Dummy GPU function that MUST be wired to a Gradio event for ZeroGPU
 
12
  @spaces.GPU
13
  def dummy_gpu_check():
14
  return "ZeroGPU Hardware Connected!"
15
 
16
- # 3. Build a minimal Gradio UI and bind the GPU function
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
- # 4. Create the main FastAPI application
26
- app = FastAPI(title="Moderation API")
27
 
28
- # 5. Add CORS middleware to allow your Angular app to connect
29
  app.add_middleware(
30
  CORSMiddleware,
31
- allow_origins=["*"], # In production, replace with your Angular app URL
32
  allow_credentials=True,
33
  allow_methods=["*"],
34
  allow_headers=["*"],
35
  )
36
 
37
- # 6. Include your moderation routes FIRST
38
  app.include_router(moderation_router, prefix="/api/v1")
39
 
40
- # 7. Mount the Gradio UI as a sub-application at the end
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
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
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()