File size: 4,069 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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.