--- 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 ` 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://-.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://-.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.