Transformers.js
Transformers.js is Hugging Face's JavaScript library for running pretrained ML models in the browser.
- The docs say: "Run 🤗 Transformers directly in your browser, with no need for a server!" It aims to match the Python
transformersAPI. - Models run through ONNX Runtime. In the browser the default is WASM on the CPU;
device: 'webgpu'uses the GPU (the docs note WebGPU is still experimental in many browsers). dtypepicks precision:fp32(default for WebGPU),fp16,q8(default for WASM) orq4.- Install:
npm i @huggingface/transformers. Thepipeline()function bundles a model with pre- and post-processing. - Embedding example from the Xenova/all-MiniLM-L6-v2 card:
pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'), then call it with{ pooling: 'mean', normalize: true }to get 384-dimension vectors. See all-MiniLM-L6-v2. - Used by FetchMoji for in-browser semantic search. Background: Embeddings and semantic search.