Spaces:
Running on CPU Upgrade

File size: 2,671 Bytes
e6c3b81
d526a86
 
 
 
e6c3b81
d526a86
e6c3b81
 
 
 
d526a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Laya API backend
emoji: 🧭
colorFrom: red
colorTo: gray
sdk: docker
app_port: 7860
pinned: false
license: apache-2.0
---

# Laya API backend (for api.laya-ai.com)

Runs the upstream [`laya-serve`](https://github.com/NandhaKishorM/laya) HTTP server
(Jev-compatible `POST /v1/systemone` and `/v1/systemone/batch`) with all three
checkpoints preloaded. Only the hosted API gateway (`platform/api-platform`) calls
it; the laya-ai.com playground keeps using the upstream demo Space.

## One-time setup

1. Create a new Space: **Docker** SDK, **CPU Upgrade** hardware (8 vCPU, 32 GB,
   about $22/month). Visibility can stay public: every request needs the key below.
2. Upload `Dockerfile` and this `README.md` to the Space repository.
3. In **Settings → Variables and secrets**, add a secret `LAYA_API_KEY` with a long
   random value (`openssl rand -base64 32`). `laya-serve` then requires
   `Authorization: Bearer <key>` on every request.
4. Wait for the build. The first one downloads PyTorch and about 2.3 GB of weights;
   startup then preloads the checkpoints (about 10 seconds).

## Check it

```bash
SPACE=https://<user>-<space-name>.hf.space
curl -s $SPACE/health
curl -s $SPACE/v1/systemone \
  -H "authorization: Bearer $LAYA_API_KEY" -H 'content-type: application/json' \
  -d '{"state":"I was charged twice","questions":{"refund":{"type":"noul","instructions":"Refund requested?"}}}'
```

The response must contain `usage.input_tokens`: the gateway bills on it.

## Measure it

```bash
cd platform/api-platform
UPSTREAM_URL=$SPACE UPSTREAM_TOKEN=$LAYA_API_KEY node scripts/bench-upstream.mjs
```

Run it with `LAYA_THREADS` at 4 and at 8 (Space variable, then restart) and keep
the faster one. Then set the gateway's upstream limit (`limits.rate.upstream` in
`src/server/config/apps/laya.ts` and `UPSTREAM_LIMITER` in `wrangler.jsonc`) to
about 80% of the measured requests per second × 60.

## Point the API gateway at it

```bash
cd platform/api-platform
# wrangler.jsonc: "UPSTREAM_LAYA_CPU_URL": "https://<user>-<space-name>.hf.space"
pnpm exec wrangler secret put UPSTREAM_LAYA_CPU_TOKEN   # same value as LAYA_API_KEY
```

## Move to a GPU

When traffic outgrows the CPU (sustained over ~1 question per second, p95 above
3 seconds, or usage revenue above ~$300/month): switch the Space hardware to
**T4 small**, remove `--index-url ...cpu` from the Dockerfile so the CUDA build of
PyTorch is installed, and set the Space variable `LAYA_DEVICE=cuda`. The URL stays
the same, so the gateway needs no change except a higher upstream limit.

Paid Spaces do not sleep. Do not set `LAYA_JEV_STRICT`: the gateway reads Laya's
own response shape.