bapi143 commited on
Commit
36c06b8
·
1 Parent(s): 201bef3

Include FastAPI backend router and reorder routes ahead of catchall

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -60,12 +60,14 @@ with gr.Blocks(title="🌾 CropAI Agronomic Cloud") as demo:
60
  * **Drone VRA:** Autonomous Spot-Spraying Lawnmower Grid Generator
61
  """)
62
 
63
- # Attach all FastAPI backend routes to the FRONT of Gradio's router
64
- # This ensures REST API endpoints (/api/v1/..., /health, /docs) take precedence
65
- # over Gradio's catch-all SPA route, while the root '/' serves the Gradio UI.
66
- backend_routes = [r for r in backend_app.routes]
67
- gradio_routes = [r for r in demo.app.router.routes if getattr(r, 'path', '') not in {getattr(br, 'path', '') for br in backend_routes}]
68
- demo.app.router.routes = backend_routes + gradio_routes
 
 
69
 
70
  if __name__ == "__main__":
71
  demo.launch(ssr_mode=False)
 
60
  * **Drone VRA:** Autonomous Spot-Spraying Lawnmower Grid Generator
61
  """)
62
 
63
+ # Attach all FastAPI backend routes onto Gradio's internal FastAPI app
64
+ demo.app.include_router(backend_app.router)
65
+
66
+ # Move Gradio's catch-all SPA routes to the very end so that REST API endpoints
67
+ # (/api/v1/..., /health, /docs) take full precedence without interference.
68
+ non_catchall = [r for r in demo.app.router.routes if '{path:path}' not in getattr(r, 'path', '')]
69
+ catchall = [r for r in demo.app.router.routes if '{path:path}' in getattr(r, 'path', '')]
70
+ demo.app.router.routes = non_catchall + catchall
71
 
72
  if __name__ == "__main__":
73
  demo.launch(ssr_mode=False)