""" Startup entrypoint for HuggingFace Spaces (Option A port layout). Launches two servers in parallel: - FastAPI (OpenEnv API): port 7860 ← HF Spaces primary, grader connects here - Gradio (Demo UI): port 7861 ← human-readable demo This ensures the OpenEnv grader can reach the API at the Space root URL while still serving the demo interface on port 7861. Usage (Docker CMD): python startup.py """ import os import sys import threading import subprocess # Pre-train models if .pkl files are missing def ensure_models(): """Train and persist ML models before starting servers.""" from baseline_inference import load_or_train print("[startup] Pre-training classification models...") for difficulty in ["easy", "medium", "hard"]: model = load_or_train(difficulty) print(f"[startup] {difficulty} model ready \u2713") print("[startup] All models ready!") def run_fastapi(): """Launch the OpenEnv FastAPI server on port 7860.""" import uvicorn # Import here so sys.path is already set correctly sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from server.app import app print("[startup] Starting FastAPI (OpenEnv API) on port 7860...") uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info") def run_gradio(): """Launch the Gradio demo UI on port 7861.""" print("[startup] Starting Gradio UI on port 7861...") # Patch the Gradio launch port before importing app.py # app.py calls demo.launch(server_port=7860) \u2014 we override it via env var env = os.environ.copy() env["GRADIO_SERVER_PORT"] = "7861" subprocess.Popen( [sys.executable, "app.py"], env=env, stdout=sys.stdout, stderr=sys.stderr, ) if __name__ == "__main__": # Step 1: pre-train models (blocking, needed before either server starts) ensure_models() # Step 2: start Gradio in a subprocess (it has its own event loop) run_gradio() # Step 3: start FastAPI in the main thread (blocks until stopped) run_fastapi()