Two services, wired by one env var (INFERENCE_URL):
- Hugging Face Spaces — Python FastAPI inference server (CPU JAX), built from the repo
Dockerfile. Free CPU tier is 2 vCPU / 16 GB RAM, which fixes the Render 512 MB OOM. Serves on port 7860. - Vercel — Next.js frontend in
web/.
We switched off Render;
render.yamlwas deleted. The build is self-contained: the step-6000 Orbax checkpoint (6000/, ~29 MB) anddata/tokenized/{vocab.json,players.json}are committed to git andCOPY'd into the image — the server loads them on boot, so they MUST be present in the Space repo.
- Create a free HF account (no card): https://huggingface.co/join
- New Space → name it
diamondai-inference→ SDK: Docker → blank template → Create. Choosing Docker scaffolds the Space'sREADME.mdwith the required front-matter (see step 4). - Add the Space as a git remote of this repo and push
mainto it:You'll authenticate with an HF access token (Settings → Access Tokens → write) as the password. This pushes thegit remote add space https://huggingface.co/spaces/<user>/diamondai-inference git push space main
Dockerfile,src/,6000/, anddata/tokenized/*.json— everything the build needs. - README front-matter (required). HF reads
README.mdat the Space repo root; it must START with this YAML block (also saved asREADME-hf-space.md):Pushing this repo overwrites the Space's--- title: DiamondAI Inference emoji: ⚾ colorFrom: blue colorTo: red sdk: docker app_port: 7860 pinned: false ---README.mdwith our project README (no front-matter), so after the push edit the Space'sREADME.mdin the HF web editor and paste the block at the very top (or commit it on thespaceremote). Without it the Space won't build as a Docker app. - The Space builds the Docker image (a few minutes — JAX install). Wait for status Running.
URL:
https://<user>-diamondai-inference.hf.space. Sanity check:https://<user>-diamondai-inference.hf.space/health→{"ok": true, "step": 6000}.
The checkpoint is ~29 MB, under HF's 10 GB-per-file limit, so plain git should work. If a large-file push is rejected, track the checkpoint with Git LFS before pushing:
git lfs install
git lfs track "6000/**"
git add .gitattributes && git commit -m "Track checkpoint with LFS"
git push space main- Vercel dashboard → Add New → Project, import this GitHub repo.
- Set Root Directory =
web(critical — the Next.js app lives in the subdirectory). Framework auto-detects as Next.js; leave build/output defaults. - Add an environment variable:
INFERENCE_URL= the Space URL from step 1.5 (e.g.https://<user>-diamondai-inference.hf.space). It is read server-side inweb/lib/mlbConfig.tsand consumed by the/api/model-statusand/api/game/[gamePk]route handlers. NoNEXT_PUBLIC_prefix — never exposed to the client.
- Deploy.
Calls are server-to-server, so CORS is belt-and-suspenders. To restrict it: add a CORS_ALLOW_ORIGINS
variable to the Space (Settings → Variables) set to your Vercel domain, and restart the Space.
The free Space sleeps after inactivity and cold-starts on the next request (~30–60s while it wakes
and JIT-compiles). During that window the game route's inference fetch times out and the frontend shows
the sim.ts fallback + "model offline" badge — the site never looks broken; real predictions appear
once the Space is warm. The 16 GB tier means it should not OOM like Render's 512 MB did.
# Inference server on the Spaces port (7860), same as the Docker CMD:
JAX_PLATFORMS=cpu .venv/bin/uvicorn src.serve.app:app --host 0.0.0.0 --port 7860
curl localhost:7860/health # -> {"ok": true, "step": 6000}
# Or build the image exactly as HF does (needs Docker):
docker build -t diamondai-inference .
docker run -p 7860:7860 diamondai-inference
# Frontend (from web/):
cd web && npm run build && npm run dev # INFERENCE_URL defaults to localhost:8000 for devLocal data-collection / test deps (not needed to serve): pip install -r requirements-dev.txt.