Transformers.js
Use @huggingface/transformers for JavaScript or TypeScript inference in Bun, Node-compatible runtimes, and browsers.
Setup
bun add @huggingface/transformers
import { pipeline } from "@huggingface/transformers";
const classifier = await pipeline("text-classification");
try {
const result = await classifier("Cybara runs this model locally.");
console.log(result);
} finally {
await classifier.dispose();
}
Workflow
- Choose a model whose card lists Transformers.js or ONNX compatibility for the required task.
- Pin the model ID and revision when reproducibility matters.
- Start with CPU/WASM compatibility, then enable WebGPU only after feature detection.
- Select dtype or quantization from measured memory, latency, and quality requirements.
- Configure the cache explicitly for desktop or server runtimes.
- Warm the model before benchmarking and separate download time from inference latency.
- Dispose pipelines and tensors when the workflow ends.
- Test unavailable WebGPU, interrupted downloads, offline cache reuse, and malformed inputs.
Do not load untrusted custom model code. For browser applications, keep model downloads visible to the user and avoid blocking the main thread during initialization or long inference.