LogicGoInfotechSpaces commited on
Commit
b3b1893
·
1 Parent(s): 72d441c

fix: mount FastAPI router into Gradio app for API endpoints on port 7860

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -250,23 +250,18 @@ with gr.Blocks(title="Video Face Swap", theme=gr.themes.Soft()) as demo:
250
  demo.queue()
251
 
252
  if __name__ == "__main__":
253
- import threading
254
- import uvicorn
255
- import os
256
-
257
- # Start FastAPI server in background thread
258
- def run_api():
259
- try:
260
- from api.main import app as api_app
261
- uvicorn.run(api_app, host="0.0.0.0", port=8000, log_level="info")
262
- except Exception as e:
263
- print(f"API startup error: {e}")
264
-
265
- # Only start API if not in Gradio's main thread
266
- if os.environ.get("GRADIO_SERVER_NAME") is None:
267
- api_thread = threading.Thread(target=run_api, daemon=True)
268
- api_thread.start()
269
- print("FastAPI server starting on port 8000...")
270
 
271
- # Launch Gradio app
272
  demo.launch(server_port=7860, share=False)
 
250
  demo.queue()
251
 
252
  if __name__ == "__main__":
253
+ # Mount FastAPI routes into Gradio's FastAPI app
254
+ try:
255
+ from api.main import app as api_app
256
+
257
+ # Mount API routes directly to Gradio's FastAPI app
258
+ # demo.app is the FastAPI instance used by Gradio
259
+ demo.app.include_router(api_app.router)
260
+ print("✅ FastAPI routes mounted to Gradio app")
261
+ except Exception as e:
262
+ print(f"API integration error: {e}")
263
+ import traceback
264
+ traceback.print_exc()
 
 
 
 
 
265
 
266
+ # Launch Gradio app (FastAPI routes are now accessible on same port 7860)
267
  demo.launch(server_port=7860, share=False)