ArturoNereu commited on
Commit
3f63d4f
·
1 Parent(s): b21d7e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -14,13 +14,16 @@ from backend.main import app as fastapi_app
14
  # Get base URLs from environment
15
  # Auto-detect HF Spaces environment
16
  FASTAPI_INTERNAL = "http://localhost:8000" # Always use localhost for internal calls
17
- if os.getenv("SPACE_ID"):
18
- # Running on HF Spaces - use the public Space URL for iframe/external access
 
 
 
 
19
  space_host = os.getenv("SPACE_HOST", "")
20
- FASTAPI_URL = f"https://{space_host}" if space_host else FASTAPI_INTERNAL
21
- SPACE_URL = FASTAPI_URL
22
  print(f"🌐 HF Spaces detected: SPACE_ID={os.getenv('SPACE_ID')}, SPACE_HOST={space_host}")
23
- print(f" FASTAPI_URL (external)={FASTAPI_URL}")
24
  print(f" FASTAPI_INTERNAL={FASTAPI_INTERNAL}")
25
  else:
26
  # Local development - FastAPI on 8000, Gradio on 7860
@@ -85,18 +88,20 @@ def wait_for_fastapi(max_retries=30, retry_interval=1):
85
  return False
86
 
87
 
88
- # Start FastAPI/MCP server in background
89
- def start_fastapi():
90
- print("\n" + "="*60)
91
- print("Starting FastAPI/MCP server on port 8000...")
92
- print("="*60 + "\n")
93
- uvicorn.run(fastapi_app, host="0.0.0.0", port=8000, log_level="info")
 
 
94
 
95
- fastapi_thread = threading.Thread(target=start_fastapi, daemon=True)
96
- fastapi_thread.start()
97
 
98
- # Wait for FastAPI to be ready
99
- wait_for_fastapi()
100
 
101
 
102
  def create_default_scene():
@@ -504,5 +509,14 @@ with gr.Blocks(
504
  if __name__ == "__main__":
505
  # Enable queue for handling multiple concurrent users (important for HF Spaces)
506
  demo.queue()
507
- # Gradio 6: theme and css moved from gr.Blocks() to launch()
508
- demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft(), css=APP_CSS)
 
 
 
 
 
 
 
 
 
 
14
  # Get base URLs from environment
15
  # Auto-detect HF Spaces environment
16
  FASTAPI_INTERNAL = "http://localhost:8000" # Always use localhost for internal calls
17
+ IS_HF_SPACES = bool(os.getenv("SPACE_ID"))
18
+ if IS_HF_SPACES:
19
+ # Running on HF Spaces
20
+ # Use relative URLs for iframe (browser will resolve against current origin)
21
+ # This avoids CORS/auth issues with cross-origin requests
22
+ FASTAPI_URL = "" # Empty = relative URLs like "/view/scene/welcome"
23
  space_host = os.getenv("SPACE_HOST", "")
24
+ SPACE_URL = f"https://{space_host}" if space_host else ""
 
25
  print(f"🌐 HF Spaces detected: SPACE_ID={os.getenv('SPACE_ID')}, SPACE_HOST={space_host}")
26
+ print(f" Using relative URLs for iframe")
27
  print(f" FASTAPI_INTERNAL={FASTAPI_INTERNAL}")
28
  else:
29
  # Local development - FastAPI on 8000, Gradio on 7860
 
88
  return False
89
 
90
 
91
+ # Start FastAPI/MCP server in background (only for local dev)
92
+ # On HF Spaces, FastAPI will be mounted on Gradio's port
93
+ if not IS_HF_SPACES:
94
+ def start_fastapi():
95
+ print("\n" + "="*60)
96
+ print("Starting FastAPI/MCP server on port 8000...")
97
+ print("="*60 + "\n")
98
+ uvicorn.run(fastapi_app, host="0.0.0.0", port=8000, log_level="info")
99
 
100
+ fastapi_thread = threading.Thread(target=start_fastapi, daemon=True)
101
+ fastapi_thread.start()
102
 
103
+ # Wait for FastAPI to be ready
104
+ wait_for_fastapi()
105
 
106
 
107
  def create_default_scene():
 
509
  if __name__ == "__main__":
510
  # Enable queue for handling multiple concurrent users (important for HF Spaces)
511
  demo.queue()
512
+
513
+ if IS_HF_SPACES:
514
+ # On HF Spaces: Mount Gradio onto FastAPI so everything runs on port 7860
515
+ # This is required because HF Spaces only exposes one port
516
+ fastapi_app.mount("/", gr.mount_gradio_app(fastapi_app, demo, path="/", app_kwargs={"theme": gr.themes.Soft(), "css": APP_CSS}))
517
+ print("🚀 Starting combined FastAPI + Gradio on port 7860")
518
+ uvicorn.run(fastapi_app, host="0.0.0.0", port=7860, log_level="info")
519
+ else:
520
+ # Local dev: FastAPI already running on 8000, just launch Gradio on 7860
521
+ # Gradio 6: theme and css moved from gr.Blocks() to launch()
522
+ demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft(), css=APP_CSS)