Mike0021/MiniCPM5-Pi-Web-Agent
0
1---2title: MiniCPM5 Pi Web Agent3sdk: static4app_file: dist/index.html5fullWidth: true6models:7 - Mike0021/MiniCPM5-1B-ONNX-Web8custom_headers:9 cross-origin-embedder-policy: credentialless10 cross-origin-opener-policy: same-origin11 cross-origin-resource-policy: cross-origin12---13 14# MiniCPM5-1B Pi Web Agent15 16This workspace converts `openbmb/MiniCPM5-1B` into a browser-loadable Transformers.js model and ships a browser-only pi agent app.17 18Published artifact: https://huggingface.co/Mike0021/MiniCPM5-1B-ONNX-Web19 20The required runtime layout is:21 22- `config.json`, `generation_config.json`, tokenizer files, and `chat_template.jinja` at the repo root23- q4 ONNX weights at `onnx/model_q4.onnx`24- `config.json` includes `transformers.js_config.dtype = "q4"` so the default loader selects the web-sized artifact25 26The conversion uses an ONNX export with KV cache (`text-generation-with-past`) and then applies ONNX Runtime 4-bit MatMul quantization. A generic ONNX export without KV cache is not enough for normal Transformers.js autoregressive generation.27 28## Run the Web App29 30```bash31npm install32npm run dev33```34 35Open http://localhost:5173/.36 37The app uses:38 39- `@earendil-works/pi-agent-core` for the agent loop, transcript state, and tool execution.40- `@huggingface/transformers` with `Mike0021/MiniCPM5-1B-ONNX-Web` for the local browser model.41- `@webcontainer/api` for the client-only sandbox with a virtual filesystem and browser-contained Node.js processes.42 43Vite serves the app with COOP/COEP headers and boots WebContainers with `coep: "credentialless"`. The deterministic test model is available at `http://localhost:5173/?mode=mock&device=wasm` for fast harness and sandbox smoke tests without downloading the full ONNX model.44 45The Static Space uses the same isolation policy through `custom_headers` in this README frontmatter. The app is built with `npm run build` and the generated `dist/` directory is uploaded to the Space.46 47## Test the Agent App48 49Start the dev server, then run:50 51```bash52npm run smoke:web53```54 55The smoke test opens Chromium, confirms `crossOriginIsolated`, boots the WebContainer sandbox, runs the pi agent in deterministic mode, writes `hello.js`, spawns `node hello.js`, and checks for `pi sandbox result: 42` in the transcript.56 57For the heavier end-to-end check with the real MiniCPM5 ONNX model in browser WASM mode:58 59```bash60npm run smoke:local-model61```62 63This downloads/loads the q4 ONNX artifact in Chromium, runs the same pi/WebContainer task, and checks that the model reaches `Model ready` before the sandbox result is accepted.64 65## Verify the Published Artifact66 67```bash68npm install69node scripts/verify_tjs_model.mjs Mike0021/MiniCPM5-1B-ONNX-Web70```71 72The verifier asks Transformers.js for the `text-generation` file plan, checks for `onnx/model_q4.onnx`, then loads the model and generates a short completion.73 74## Convert and Upload75 76The published repo was produced locally with a CPU fp16 export followed by q4 ONNX quantization:77 78```bash79uv run --python 3.12 \80 --with "numpy<2" \81 --with "transformers==4.57.6" \82 --with "optimum[onnx]" \83 --with "onnxruntime==1.20.1" \84 --with onnxslim \85 --with "huggingface_hub>=0.33" \86 --with accelerate \87 --with sentencepiece \88 --with protobuf \89 scripts/convert_minicpm5_tjs.py \90 --source-model openbmb/MiniCPM5-1B \91 --target-repo Mike0021/MiniCPM5-1B-ONNX-Web \92 --output-dir output/MiniCPM5-1B-ONNX-Web \93 --work-dir output/minicpm5-work \94 --device cpu \95 --export-dtype fp1696```97 98For a clean remote conversion, the same script can be run on Hugging Face Jobs with a configured Hub token:99 100```bash101hf repos create Mike0021/MiniCPM5-1B-ONNX-Web --repo-type model --exist-ok102hf jobs uv run scripts/convert_minicpm5_tjs.py \103 --flavor l4x1 \104 --timeout 6h \105 --secrets HF_TOKEN \106 --with "numpy<2" \107 --with "transformers==4.57.6" \108 --with "optimum[onnx]" \109 --with "onnxruntime==1.20.1" \110 --with onnxslim \111 --with "huggingface_hub>=0.33" \112 --with accelerate \113 --with sentencepiece \114 --with protobuf \115 --python 3.12 \116 -- \117 --target-repo Mike0021/MiniCPM5-1B-ONNX-Web \118 --export-dtype fp16119```120 