You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HuggingFace Transformers.js provider for LocalMode — run ML models locally in the browser.
Features
Browser-Native - Run ML models directly in the browser with WebGPU/WASM
Privacy-First - All processing happens locally, no data leaves the device
Model Caching - Model files are cached in the browser Cache API (transformers-cache) for instant subsequent loads, with a resilient write path that never fails a model load (opt out: createTransformers({ resilientCache: false }))
Optimized - Uses quantized models for smaller size and faster inference
eSpeak-NG WASM for Kokoro TTS text-to-phoneme conversion
Overview
@localmode/transformers provides model implementations for the interfaces defined in @localmode/core. It wraps HuggingFace Transformers.js to enable local ML inference in the browser.
Provider API
All models are created via the transformers provider object. Each factory method returns a model implementing a @localmode/core interface.
Embed both text and images into the same vector space for cross-modal search.
import{embed,embedImage,cosineSimilarity}from'@localmode/core';import{transformers}from'@localmode/transformers';constmodel=transformers.multimodalEmbedding('Xenova/clip-vit-base-patch32');// Text embeddingconst{embedding: textVec}=awaitembed({ model,value: 'a photo of a cat'});// Image embedding (same vector space)const{embedding: imgVec}=awaitembedImage({ model,image: catImageBlob});// Cross-modal similarityconstsimilarity=cosineSimilarity(textVec,imgVec);
import{rerank}from'@localmode/core';import{transformers}from'@localmode/transformers';constrerankerModel=transformers.reranker('Xenova/ms-marco-MiniLM-L-6-v2');const{ results }=awaitrerank({model: rerankerModel,query: 'What is machine learning?',documents: ['ML is a subset of AI...','Python is a language...'],topK: 5,});
import{classify,extractEntities}from'@localmode/core';import{transformers}from'@localmode/transformers';constsentiment=awaitclassify({model: transformers.classifier('Xenova/distilbert-base-uncased-finetuned-sst-2-english'),text: 'I love this product!',});constentities=awaitextractEntities({model: transformers.ner('Xenova/bert-base-NER'),text: 'John works at Microsoft in Seattle',});
Run ONNX-format language models in the browser with WebGPU acceleration:
import{generateText,streamText}from'@localmode/core';import{transformers}from'@localmode/transformers';constmodel=transformers.languageModel('onnx-community/Qwen3.5-0.8B-ONNX');// Single-shot generationconst{ text }=awaitgenerateText({ model,prompt: 'What is 2+2?'});// Streaming generationconstresult=awaitstreamText({ model,prompt: 'Write a haiku'});forawait(constchunkofresult.stream){process.stdout.write(chunk.text);}
Method
Interface
Description
transformers.languageModel(modelId)
LanguageModel
Text generation (ONNX, WebGPU/WASM)
Recommended ONNX LLMs (16 curated models):
Model
Size
Context
Vision
onnx-community/granite-4.0-350m-ONNX-web
~120MB
4K
No
onnx-community/Qwen3-0.6B-ONNX
~570MB
4K
No
onnx-community/Qwen3.5-0.8B-ONNX
~500MB
32K
Yes
onnx-community/granite-4.0-1b-ONNX-web
~350MB
4K
No
onnx-community/Llama-3.2-1B-Instruct-ONNX
~380MB
8K
No
onnx-community/TinyLlama-1.1B-Chat-v1.0-ONNX
~350MB
2K
No
onnx-community/Qwen2.5-Coder-1.5B-Instruct
~450MB
4K
No
onnx-community/DeepSeek-R1-Distill-Qwen-1.5B-ONNX
~500MB
4K
No
onnx-community/Llama-3.2-3B-Instruct-ONNX
~900MB
8K
No
onnx-community/Qwen3-4B-ONNX
~1.2GB
4K
No
microsoft/Phi-3-mini-4k-instruct-onnx-web
~1.2GB
4K
No
onnx-community/Qwen3.5-2B-ONNX
~1.5GB
32K
Yes
onnx-community/gemma-4-E2B-it-ONNX
~1.5GB
128K
Yes
onnx-community/Phi-4-mini-instruct-web-q4f16
~2.3GB
4K
No
onnx-community/Qwen3.5-4B-ONNX
~2.5GB
32K
Yes
onnx-community/gemma-4-E4B-it-ONNX
~3GB
128K
Yes
Vision support: Qwen3.5, Qwen2.5-VL, Qwen3-VL, and Gemma 4 models support image input via their built-in vision encoder. Check model.supportsVision for feature detection. See Vision docs for usage.
Model files are cached in the browser Cache API (cache name transformers-cache, honoring a user-set env.cacheKey) — not IndexedDB. The provider installs a resilient wrapper by default so a failing cache write (e.g. the intermittent NetworkError some browsers throw mid-write, or QuotaExceededError) never fails a model load: the model still loads from the network response, and one warning per URL per session is logged. Environments without caches (Node/SSR) skip installation and keep Transformers.js's own file cache.
import{createTransformers}from'@localmode/transformers';// Opt out of the resilient cache (global — the transformers.js env is a singleton)constprovider=createTransformers({resilientCache: false});// Manual control (advanced)import{createResilientModelCache,// build a Cache-API-backed cache implementing match/put/deleteinstallResilientModelCache,// idempotently configure env.useCustomCache + env.customCachesetResilientModelCacheEnabled,// enable/disable installation globally}from'@localmode/transformers';
Document-level OCR with table/formula recognition (~652MB)
onnx-community/LightOnOCR-2-1B-ONNX
Fast document OCR, 11 languages (~700MB)
Document QA
Model
Description
onnx-community/Florence-2-base-ft
Document QA (~223MB)
Xenova/donut-base-finetuned-docvqa
Donut (~218MB)
Model Constants
All recommended models are exported as constants for easy reference:
import{MODELS,// All models organized by taskEMBEDDING_MODELS,CLASSIFICATION_MODELS,ZERO_SHOT_MODELS,NER_MODELS,RERANKER_MODELS,SPEECH_TO_TEXT_MODELS,TEXT_TO_SPEECH_MODELS,IMAGE_CLASSIFICATION_MODELS,ZERO_SHOT_IMAGE_MODELS,IMAGE_CAPTION_MODELS,TRANSLATION_MODELS,SUMMARIZATION_MODELS,FILL_MASK_MODELS,QUESTION_ANSWERING_MODELS,OBJECT_DETECTION_MODELS,SEGMENTATION_MODELS,OCR_MODELS,DOCUMENT_QA_MODELS,IMAGE_TO_IMAGE_MODELS,IMAGE_FEATURE_MODELS,VAD_MODELS,TRANSFORMERS_LLM_MODELS,MULTIMODAL_EMBEDDING_MODELS,KOKORO_LANG_MAP,}from'@localmode/transformers';// Use with providerconstmodel=transformers.embedding(EMBEDDING_MODELS.BGE_SMALL_EN);
Kokoro Voice Catalog
The KOKORO_VOICES export provides a catalog of 29 English voices with metadata for UI display:
import{KOKORO_VOICES,KOKORO_DEFAULT_VOICE}from'@localmode/transformers';importtype{KokoroVoice}from'@localmode/transformers';// Each voice has: id, name, language, languageLabel, genderconstenglish=KOKORO_VOICES.filter((v)=>v.language==='en-US');constfemales=KOKORO_VOICES.filter((v)=>v.gender==='female');console.log(KOKORO_DEFAULT_VOICE);// 'af_heart'
Languages: American English, British English.
Advanced Usage
Custom Model Options
constmodel=transformers.embedding('Xenova/bge-small-en-v1.5',{quantized: true,// Use quantized model (smaller, faster)device: 'webgpu',// Use WebGPU for acceleration (falls back to WASM)});
Language Model Options
Language models accept additional settings via LanguageModelSettings:
constmodel=transformers.languageModel('onnx-community/Qwen3.5-0.8B-ONNX',{contextLength: 32768,maxTokens: 1024,temperature: 0.7,device: 'webgpu',// dtype accepts a string or a per-component config objectdtype: 'q4f16',// For multimodal models, use per-component dtype:// dtype: { embed_tokens: 'q4', vision_encoder: 'q4', decoder_model_merged: 'q4' },});
Provider Options
Pass provider-specific options to core functions:
const{ embedding }=awaitembed({model: transformers.embedding('Xenova/bge-small-en-v1.5'),value: 'Hello world',providerOptions: {transformers: {// Any Transformers.js specific options},},});
Preloading Models
For better UX, preload models before use:
import{preloadModel,isModelCached}from'@localmode/transformers';import{embed}from'@localmode/core';if(!(awaitisModelCached('Xenova/bge-small-en-v1.5'))){awaitpreloadModel('Xenova/bge-small-en-v1.5',{onProgress: (p)=>console.log(`Loading: ${p.progress}%`),});}// Subsequent calls are instant (loaded from cache)constembeddingModel=transformers.embedding('Xenova/bge-small-en-v1.5');const{ embedding }=awaitembed({model: embeddingModel,value: 'Hello'});
Exported Implementation Classes
For advanced use cases, implementation classes are available: