RotNot / deploy /huggingface /DEPLOY.md
deploy
Deploy RotNot
9580dce
|
Raw History Blame Contribute Delete
4.07 kB
# Deploying RotNot to a Hugging Face Docker Space
The app ships as a Docker Space (single long-running container β€” SQLite and the
in-memory problem store work as-is). Files involved:
- `Dockerfile` (repo root) β€” builds and runs the Next.js app on port 7860.
- `scripts/hf-start.sh` β€” migrates + seeds the DB on boot, then starts the app.
- `deploy/huggingface/README.md` β€” the Space's root `README.md` (YAML front
matter sets `sdk: docker` and `app_port: 7860`).
## 1. Create the Space (one time)
Either in the UI β€” <https://huggingface.co/new-space> β†’ **Docker** β†’ *Blank* β€”
or with the CLI:
```bash
pip install -U "huggingface_hub[cli]"
huggingface-cli login # paste a WRITE token
huggingface-cli repo create rotnot --type space --space_sdk docker
```
## 2. Push the app
From a clean checkout of this branch:
```bash
export HF_TOKEN=hf_xxx # a WRITE token
scripts/deploy-hf.sh NAP1320/RotNot # <user>/<space>
```
`deploy-hf.sh` snapshots the current commit, swaps in the front-matter
`README.md`, and force-pushes it to the Space's `main` branch. HF then builds
the Dockerfile and boots the app.
## 3. Set secrets
In the Space: **Settings β†’ Variables and secrets**
- `HF_TOKEN` β€” **secret.** A Hugging Face token (free; create at
<https://huggingface.co/settings/tokens>). Powers all AI features via HF's
OpenAI-compatible router β€” no Anthropic key needed.
- `HF_MODEL` β€” **variable** (optional). Open instruct model to serve; defaults
to `meta-llama/Llama-3.1-8B-Instruct`. Use a larger model
(e.g. `meta-llama/Llama-3.3-70B-Instruct`) for more reliable JSON output.
- `LICHESS_TOKEN`, `CHESSCOM_USERNAME` β€” optional (chess).
> Prefer Anthropic instead? Set `ANTHROPIC_API_KEY` and leave `HF_TOKEN` unset.
The Space rebuilds and starts on `https://huggingface.co/spaces/NAP1320/RotNot`.
## 4. (Optional) Let each visitor use their OWN Hugging Face token
The Space-wide `HF_TOKEN` secret means every visitor's AI usage is billed to the
owner's token. To let anyone bring their own free token instead (and only fall
back to the Space secret when they haven't), implement a per-request key path.
This is **not built yet** β€” it's the plan for a follow-up:
1. **Request-scoped client.** Add `getClaude({ hfToken?, hfModel? })` (in
`src/lib/claude.ts` or a new `src/lib/llm.ts`): use the passed token, else
`process.env.HF_TOKEN`, else Anthropic. Keep the existing `claude` export for
back-compat/tests. Have the AI API routes build the client from request
headers `x-hf-token` / `x-hf-model` (falling back to env). Unit-test the
selection logic (inject `fetch`, as `llm-hf.test.ts` already does).
2. **Client headers.** In `src/lib/api-client.ts` `apiFetch`, attach
`x-hf-token` / `x-hf-model` from `localStorage` (`rotnot:hfToken`,
`rotnot:hfModel`) when present; no-op on the server.
3. **Settings UI.** In `src/app/settings/page.tsx`, add a "Hugging Face" section:
a password-type token input, an optional model input, Save (to localStorage),
and a "Test connection" button hitting a new `GET /api/llm/health` route that
does a tiny completion with the request's token and returns `{ ok, model }`
or an error.
4. **Security.** Never log or server-persist a user's token β€” it lives only in
their browser and is sent per request over HTTPS. Say so in the UI. Without
any token, AI features degrade gracefully (Coding's written grading already
falls back to an offline heuristic).
Acceptance: a visitor pastes their own token in Settings, clicks **Test** β†’ ok,
then the AI features run on their token.
## Notes
- Storage is ephemeral: XP/progress resets on rebuild/restart. Attach HF
**persistent storage** (Settings) and point `DATABASE_URL` at the mounted
path (e.g. `file:/data/dev.db`) to keep it.
- AI provider: `HF_TOKEN` (Hugging Face router) takes priority; set
`ANTHROPIC_API_KEY` and leave `HF_TOKEN` unset to use Anthropic instead.
- The build needs no secrets; the token is only read at runtime.