Spaces:
Sleeping
Sleeping
Commit
·
11f51c7
1
Parent(s):
aa77c66
Auto-start FastAPI and MCP server for Hugging Face Spaces deployment
Browse files
app.py
CHANGED
|
@@ -2728,11 +2728,68 @@ with gr.Blocks(
|
|
| 2728 |
|
| 2729 |
if __name__ == "__main__":
|
| 2730 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2731 |
# For Hugging Face Spaces, bind to 0.0.0.0; for local dev, use 127.0.0.1
|
| 2732 |
# HF Spaces sets SPACE_ID environment variable
|
| 2733 |
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 2734 |
server_name = "0.0.0.0" if is_hf_space else "127.0.0.1"
|
| 2735 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2736 |
demo.launch(
|
| 2737 |
server_name=server_name,
|
| 2738 |
server_port=7860,
|
|
|
|
| 2728 |
|
| 2729 |
if __name__ == "__main__":
|
| 2730 |
import os
|
| 2731 |
+
import threading
|
| 2732 |
+
import time
|
| 2733 |
+
import requests
|
| 2734 |
+
|
| 2735 |
# For Hugging Face Spaces, bind to 0.0.0.0; for local dev, use 127.0.0.1
|
| 2736 |
# HF Spaces sets SPACE_ID environment variable
|
| 2737 |
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 2738 |
server_name = "0.0.0.0" if is_hf_space else "127.0.0.1"
|
| 2739 |
|
| 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 thread."""
|
| 2744 |
+
try:
|
| 2745 |
+
import uvicorn
|
| 2746 |
+
from backend.mcp_server import server
|
| 2747 |
+
uvicorn.run(
|
| 2748 |
+
server.app,
|
| 2749 |
+
host="0.0.0.0",
|
| 2750 |
+
port=int(os.getenv("MCP_PORT", "8900")),
|
| 2751 |
+
log_level="info"
|
| 2752 |
+
)
|
| 2753 |
+
except Exception as e:
|
| 2754 |
+
print(f"Warning: Could not start MCP server: {e}")
|
| 2755 |
+
|
| 2756 |
+
def start_fastapi_server():
|
| 2757 |
+
"""Start FastAPI server in background thread."""
|
| 2758 |
+
try:
|
| 2759 |
+
import uvicorn
|
| 2760 |
+
uvicorn.run(
|
| 2761 |
+
"backend.api.main:app",
|
| 2762 |
+
host="0.0.0.0",
|
| 2763 |
+
port=int(os.getenv("API_PORT", "8000")),
|
| 2764 |
+
log_level="info"
|
| 2765 |
+
)
|
| 2766 |
+
except Exception as e:
|
| 2767 |
+
print(f"Warning: Could not start FastAPI server: {e}")
|
| 2768 |
+
|
| 2769 |
+
# Start services in background threads
|
| 2770 |
+
print("Starting backend services...")
|
| 2771 |
+
mcp_thread = threading.Thread(target=start_mcp_server, daemon=True)
|
| 2772 |
+
api_thread = threading.Thread(target=start_fastapi_server, daemon=True)
|
| 2773 |
+
|
| 2774 |
+
mcp_thread.start()
|
| 2775 |
+
time.sleep(2) # Give MCP server time to start
|
| 2776 |
+
api_thread.start()
|
| 2777 |
+
time.sleep(3) # Give FastAPI time to start
|
| 2778 |
+
|
| 2779 |
+
# Wait for services to be ready
|
| 2780 |
+
max_attempts = 30
|
| 2781 |
+
for attempt in range(max_attempts):
|
| 2782 |
+
try:
|
| 2783 |
+
response = requests.get("http://localhost:8000/health", timeout=2)
|
| 2784 |
+
if response.status_code == 200:
|
| 2785 |
+
print("✅ FastAPI backend is ready!")
|
| 2786 |
+
break
|
| 2787 |
+
except:
|
| 2788 |
+
if attempt < max_attempts - 1:
|
| 2789 |
+
time.sleep(1)
|
| 2790 |
+
else:
|
| 2791 |
+
print("⚠️ FastAPI backend may not be ready, continuing anyway...")
|
| 2792 |
+
|
| 2793 |
demo.launch(
|
| 2794 |
server_name=server_name,
|
| 2795 |
server_port=7860,
|