This skill enables private, low-latency browser inference with ONNX Runtime Web for image, object, or NLP tasks.
npx playbooks add skill thongnt0208/browser-onnx-skills --skill browser-onnx
This skill provides a comprehensive workflow for executing ONNX models locally in the browser using ONNX Runtime Web (ORT-Web). Local inference offers significant advantages in data privacy, reduced server costs, and unlimited scalability as each user brings their own compute power.
Install the required library via npm:
npm install onnxruntime-web
Note: For experimental features like WebGPU or WebNN, use the nightly version onnxruntime-web@dev.
Set global ort.env flags before creating a session to optimize the runtime environment.
ort.env.wasm.numThreads (default is half of hardware concurrency) and use a Proxy Worker (ort.env.wasm.proxy = true) to keep the UI responsive.ort.env.wasm.wasmPaths to point to local assets or a CDN.ort.env.webgpu.profiling = { mode: 'default' } for performance diagnosis during development.Initialize the session by choosing the appropriate Execution Provider (EP):
import * as ort from 'onnxruntime-web';
const session = await ort.InferenceSession.create('./model.onnx', {
executionProviders: ['webgpu', 'wasm'], // Prioritize GPU, fallback to CPU
graphOptimizationLevel: 'all' // Enable all graph-level optimizations
});
Input data must match the model's training format (e.g., NCHW for vision models).
new ort.Tensor('float32', float32Data,) to prepare the input feeds.enableGraphCapture: true to reduce CPU overhead by replaying kernel executions.ort.Tensor.fromGpuBuffer() and setting preferredOutputLocation: 'gpu-buffer' to avoid expensive memory copies.ArrayBuffer to ~2GB. Models exceeding this must be exported with external data. const session = await ort.InferenceSession.create(modelUrl, {
externalData: [{ path: './model.data', data: dataUrl }]
});
tensor.dispose() for GPU tensors to prevent memory leaks.Offload heavy translation tasks to a separate Web Worker using a singleton pattern to ensure the model (e.g., NLLB-200) loads only once.
Implement Non-Max Suppression (NMS). If the browser lacks support for specific NMS ops, run a separate NMS ONNX model to filter overlapping boxes locally.
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.