Upload 2 files
Browse filesFiles changed (2)
- DockerfileAdded+41
- README.md+65-5
1234567891011121314151617181920212223242526272829303132333435363738394041# Inference backend for the hosted Laya API (platform/api-platform), as a# Hugging Face Docker Space on CPU Upgrade hardware (8 vCPU, 32 GB).# Runs upstream `laya-serve` (Jev-compatible POST /v1/systemone and# /v1/systemone/batch). The same image runs on T4 hardware: set the Space# variable LAYA_DEVICE=cuda after switching.FROM python:3.11-slim
RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ HF_HOME=/home/user/.cache/huggingface \ USE_TF=0 \ PIP_NO_CACHE_DIR=1WORKDIR /home/user/app
# CPU-only PyTorch first, so laya does not pull the large CUDA build.# For T4 hardware, drop the --index-url so the CUDA build is installed.# 0.3.22+ is needed for /v1/systemone/batch; 0.3.24 fixes its usage totals.RUN pip install --user --index-url https://download.pytorch.org/whl/cpu torch \ && pip install --user "laya[serve]==0.3.24"
# Bake all three checkpoints (~2.3 GB) into the image, so a restart serves# immediately instead of downloading first. The API exposes all three.RUN python -c "import laya; \laya.load('convaiinnovations/laya'); \laya.load('convaiinnovations/laya', subfolder='multilingual'); \laya.load('convaiinnovations/laya', subfolder='typed-decisions')"
# LAYA_THREADS: torch threads per request. Start at 4 (the physical cores# behind 8 vCPUs) and compare with 8 using platform/api-platform/scripts/# bench-upstream.mjs; override it as a Space variable without rebuilding.ENV LAYA_HOST=0.0.0.0 \ LAYA_PORT=7860 \ LAYA_DEVICE=cpu \ LAYA_PRELOAD=1 \ LAYA_MODELS=english,multilingual,typed-decisions \ LAYA_THREADS=4
EXPOSE 7860CMD ["laya-serve"]1234523456789101111121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071---title: Laya Apiemoji: ⚡colorFrom: bluecolorTo: yellowtitle: Laya API backendemoji: 🧭colorFrom: redcolorTo: graysdk: dockerapp_port: 7860pinned: falselicense: apache-2.0---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# 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 threecheckpoints preloaded. Only the hosted API gateway (`platform/api-platform`) callsit; 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
```bashSPACE=https://<user>-<space-name>.hf.spacecurl -s $SPACE/healthcurl -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
```bashcd platform/api-platformUPSTREAM_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 keepthe 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`) toabout 80% of the measured requests per second × 60.
## Point the API gateway at it
```bashcd 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 above3 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 ofPyTorch is installed, and set the Space variable `LAYA_DEVICE=cuda`. The URL staysthe 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'sown response shape.DockerfileAdded+41
README.md+65-5