Transformers.js brings Hugging Face’s transformer models directly into the browser via WebAssembly and WebGPU — running actual AI inference client-side, no server round-trip or API key required.
import { pipeline } from '@xenova/transformers';
// Runs entirely in the browser - model downloads once, cached after
const classifier = await pipeline('sentiment-analysis');
const result = await classifier('This library is genuinely impressive.');
console.log(result); // [{ label: 'POSITIVE', score: 0.99 }]
// Text generation, translation, and embeddings work the same way
const embedder = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
const embedding = await embedder('Some text to embed', { pooling: 'mean', normalize: true });
Why Run AI in the Browser at All
- No API costs or rate limits — inference runs on the user’s own device
- Genuine privacy — text never leaves the browser
- Works offline once the model is cached
The trade-off is model size and speed — browser-based inference is realistically limited to smaller models than a dedicated GPU server would run, but for tasks like sentiment analysis, embeddings, or small classification tasks, it’s a genuinely practical option today.