import os import sys import socket import time # CPU execution for onnxruntime/Faiss; disable Gradio SSR to prevent port/process collision os.environ.setdefault("DEVICE", "cpu") os.environ["GRADIO_SSR_MODE"] = "false" os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0" # Satisfy Hugging Face ZeroGPU startup scanner try: import spaces except ImportError: spaces = None if spaces is not None: @spaces.GPU def _zerogpu_probe(*args, **kwargs): """Never called. Exists only to satisfy ZeroGPU's startup detection.""" return None def _zerogpu_startup_report(): """ ZeroGPU expects gr.Blocks.launch() to POST a startup report before the server binds. Since we mount Gradio onto FastAPI and serve via uvicorn directly, we call the startup hook explicitly so ZeroGPU supervisor knows the container is ready and doesn't abort the Space. """ if spaces is None: return try: from spaces.zero import startup as zero_startup except ImportError: return try: zero_startup() print("✅ ZeroGPU startup report successfully sent to supervisor") except Exception as exc: print(f"⚠️ ZeroGPU startup report skipped/failed: {exc!r}") # Ensure current directory is on python path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import gradio as gr import uvicorn from api.app import app as fastapi_app # Minimal Gradio block to satisfy Hugging Face Spaces Gradio SDK inspection with gr.Blocks(title="Castorini IR Studio") as demo: gr.Markdown("# 🔍 Castorini IR Studio Backend Active") gr.Markdown("Full interactive React workbench is mounted at the root path `/`.") if spaces is not None: _probe_btn = gr.Button("zerogpu_probe", visible=False) _probe_btn.click(fn=_zerogpu_probe) # Mount Gradio helper interface on /gradio with explicit ssr_mode=False try: app = gr.mount_gradio_app( fastapi_app, demo, path="/gradio", ssr_mode=False, node_port=7869, ) except TypeError: try: app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio", ssr_mode=False) except TypeError: app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio") def _port_in_use(port: int, host: str = "127.0.0.1") -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe: probe.settimeout(0.3) return probe.connect_ex((host, port)) == 0 def _choose_port() -> int: on_space = bool(os.getenv("SPACE_ID")) candidates = [] if on_space: candidates.append(7860) port_env = os.getenv("PORT") if port_env and port_env.isdigit(): candidates.append(int(port_env)) else: port_env = os.getenv("PORT") if port_env and port_env.isdigit(): candidates.append(int(port_env)) candidates.append(7860) gradio_port = os.getenv("GRADIO_SERVER_PORT") if gradio_port and gradio_port.isdigit(): candidates.append(int(gradio_port)) for candidate in candidates: if not _port_in_use(candidate): return candidate return candidates[0] if __name__ == "__main__": host = os.getenv("HOST") or "0.0.0.0" port = _choose_port() print(f"[IR Studio] Serving on {host}:{port}") # Dispatch ZeroGPU startup report before binding uvicorn _zerogpu_startup_report() uvicorn.run(app, host=host, port=port, log_level="info")