nothingworry commited on
Commit
e87ab5b
·
1 Parent(s): 4fd1dc7

fix(startup): simplify backend readiness logic for HF Spaces

Browse files
Files changed (1) hide show
  1. app.py +25 -34
app.py CHANGED
@@ -2740,34 +2740,38 @@ if __name__ == "__main__":
2740
  # Start backend services if running in HF Spaces
2741
  if is_hf_space:
2742
  def start_mcp_server():
2743
- """Start MCP server in background process."""
2744
  try:
2745
  import sys
2746
  import subprocess
2747
- # Use subprocess.Popen to run in background
2748
- subprocess.Popen([
2749
- sys.executable, "-m", "uvicorn",
2750
- "backend.mcp_server.server:app",
2751
- "--host", "0.0.0.0",
2752
- "--port", os.getenv("MCP_PORT", "8900"),
2753
- "--log-level", "info"
2754
- ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
2755
  except Exception as e:
2756
  print(f"Warning: Could not start MCP server: {e}")
2757
 
2758
  def start_fastapi_server():
2759
- """Start FastAPI server in background process."""
2760
  try:
2761
  import sys
2762
  import subprocess
2763
- # Use subprocess.Popen to run in background
2764
- subprocess.Popen([
2765
- sys.executable, "-m", "uvicorn",
2766
- "backend.api.main:app",
2767
- "--host", "0.0.0.0",
2768
- "--port", os.getenv("API_PORT", "8000"),
2769
- "--log-level", "info"
2770
- ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
2771
  except Exception as e:
2772
  print(f"Warning: Could not start FastAPI server: {e}")
2773
 
@@ -2777,23 +2781,10 @@ if __name__ == "__main__":
2777
  api_thread = threading.Thread(target=start_fastapi_server, daemon=True)
2778
 
2779
  mcp_thread.start()
2780
- time.sleep(2) # Give MCP server time to start
2781
  api_thread.start()
2782
- time.sleep(3) # Give FastAPI time to start
2783
-
2784
- # Wait for services to be ready
2785
- max_attempts = 30
2786
- for attempt in range(max_attempts):
2787
- try:
2788
- response = requests.get("http://localhost:8000/health", timeout=2)
2789
- if response.status_code == 200:
2790
- print("✅ FastAPI backend is ready!")
2791
- break
2792
- except:
2793
- if attempt < max_attempts - 1:
2794
- time.sleep(1)
2795
- else:
2796
- print("⚠️ FastAPI backend may not be ready, continuing anyway...")
2797
 
2798
  demo.launch(
2799
  server_name=server_name,
 
2740
  # Start backend services if running in HF Spaces
2741
  if is_hf_space:
2742
  def start_mcp_server():
2743
+ """Start MCP server in a background process."""
2744
  try:
2745
  import sys
2746
  import subprocess
2747
+ # Use subprocess.Popen to run in background and surface logs in HF Spaces
2748
+ subprocess.Popen(
2749
+ [
2750
+ sys.executable, "-m", "uvicorn",
2751
+ "backend.mcp_server.server:app",
2752
+ "--host", "0.0.0.0",
2753
+ "--port", os.getenv("MCP_PORT", "8900"),
2754
+ "--log-level", "info",
2755
+ ]
2756
+ )
2757
  except Exception as e:
2758
  print(f"Warning: Could not start MCP server: {e}")
2759
 
2760
  def start_fastapi_server():
2761
+ """Start FastAPI server in a background process."""
2762
  try:
2763
  import sys
2764
  import subprocess
2765
+ # Use subprocess.Popen to run in background and surface logs in HF Spaces
2766
+ subprocess.Popen(
2767
+ [
2768
+ sys.executable, "-m", "uvicorn",
2769
+ "backend.api.main:app",
2770
+ "--host", "0.0.0.0",
2771
+ "--port", os.getenv("API_PORT", "8000"),
2772
+ "--log-level", "info",
2773
+ ]
2774
+ )
2775
  except Exception as e:
2776
  print(f"Warning: Could not start FastAPI server: {e}")
2777
 
 
2781
  api_thread = threading.Thread(target=start_fastapi_server, daemon=True)
2782
 
2783
  mcp_thread.start()
2784
+ time.sleep(4) # Give MCP server time to start
2785
  api_thread.start()
2786
+ # Give FastAPI extra time to start on first cold boot (model downloads etc.)
2787
+ time.sleep(10)
 
 
 
 
 
 
 
 
 
 
 
 
 
2788
 
2789
  demo.launch(
2790
  server_name=server_name,