from __future__ import annotations import os # Disable Gradio SSR mode to prevent port 7860 collisions on HF Spaces os.environ["GRADIO_SSR_MODE"] = "0" # --------------------------------------------------------------------------- # ZeroGPU compatibility (must run BEFORE torch is imported anywhere). # # Free HF accounts can no longer create CPU Gradio Spaces, and an existing # ZeroGPU Space cannot be downgraded to CPU without PRO. ZeroGPU refuses to # start ("No @spaces.GPU function detected during startup") unless: # 1. at least one @spaces.GPU function is registered, and # 2. `spaces.zero.startup()` reports it - normally triggered by # `gr.Blocks.launch()`, which we don't use because we serve FastAPI # directly with uvicorn. So we call it ourselves. # The judge runs entirely on CPU; the GPU placeholder is never called. # On CPU hardware / local dev `spaces` is absent and this block is skipped. # --------------------------------------------------------------------------- try: import spaces # type: ignore @spaces.GPU def _zerogpu_placeholder() -> str: return "ok" _zero = getattr(spaces, "zero", None) _startup = getattr(_zero, "startup", None) if _zero is not None else None if callable(_startup): _startup() print("[codestar] ZeroGPU startup report sent", flush=True) except ImportError: pass except Exception as exc: # never block the API from starting print(f"[codestar] ZeroGPU init warning: {exc!r}", flush=True) import os import sys from pathlib import Path import uvicorn # Ensure repository root is on sys.path ROOT = Path(__file__).resolve().parent if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from codestar.server.app import app as fastapi_app # NOTE: We intentionally do NOT mount a Gradio UI. On HF Spaces Gradio enables # SSR mode by default, whose startup hook spawns a Node server that grabs port # 7860 before uvicorn can bind it ("address already in use"). The frontend # lives on Netlify; the Space only needs to serve the JSON API. app = fastapi_app # Hugging Face Gradio Spaces start the app with `python app.py`, so we must # launch the server ourselves on port 7860. if __name__ == "__main__": port = int(os.environ.get("PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)