bapi143 commited on
Commit
0bc80e1
·
1 Parent(s): 2f4b890

Mount FastAPI backend routes at router head for direct REST API access

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -60,11 +60,12 @@ 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 onto Gradio's internal FastAPI app
64
- existing_paths = {getattr(r, 'path', '') for r in demo.app.routes}
65
- for route in backend_app.routes:
66
- if getattr(route, 'path', '') not in existing_paths:
67
- demo.app.routes.append(route)
 
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
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()