Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 8 min read How-to mediumAdept

How to Use Transformers.js in a Chrome Extension

Running local AI in a Chrome Manifest V3 extension works best when the background service worker hosts all Transformers.js models and agent state, while the side panel UI and content scripts act as thin clients over a typed messaging contract. The reference build (a Gemma 4 E2B browser assistant) uses Gemma 4 E2B (q4f16, WebGPU) for tool-calling reasoning and MiniLM embeddings for semantic search over pages and history. Inference in the background gives one model instance, one cache under the extension origin, and a responsive UI. The guide also covers parsing Gemma tool-call tokens into deterministic tool executions, splitting the internal model transcript from the UI transcript, and placing state by lifecycle across memory, chrome.storage and IndexedDB. Service workers can be suspended, so model runtime state should be recoverable, and Vite should produce one artifact per manifest entry point.

Related posts

  • Transformers.js v4: Now Available on NPM!

    Hugging Face mediumAdept

    Transformers.js v4 ships on NPM with a new C++ WebGPU runtime built with the ONNX Runtime team, enabling WebGPU-accelerated models in browsers as well as Node, Bun and Deno. Models are re-exported operation by operation using ONNX Runtime contrib operators such as GroupQueryAttention, MatMulNBits and MultiHeadAttention, giving about a 4x speedup for BERT embedding models. The release also adds Mamba, MLA and MoE architectures such as GPT-OSS 20B, which runs at about 60 tokens/s on an M4 Pro Max. Other changes include a move from Webpack to esbuild (10x faster builds, 53% smaller default bundle), a pnpm monorepo, a ModelRegistry API for inspecting and caching pipeline assets, and a standalone 8.8kB tokenizers library.

  • Experimenting with the proposed Cross-Origin Storage API in Transformers.js

    Hugging Face mediumAdept

    Browser cache partitioning (in Chrome, keying by Network Isolation Key plus URL) forces every origin running Transformers.js to re-download identical model weights and the ONNX Runtime Wasm file. In the toy example that duplication costs 177 MB for Whisper alone. The proposed Cross-Origin Storage API (navigator.crossOriginStorage) fixes this by addressing files by SHA-256 hash instead of URL, with an OPFS-like getFileHandle interface. It also verifies hashes on write and lets developers scope visibility via an origins list that can be widened but never narrowed. Privacy is handled through availability gating for rarely seen resources, so apps must treat any error as a cache miss. Transformers.js already ships an experimental opt-in COS backend via env.experimental_useCrossOriginStorage, testable today with a polyfill extension.

  • Gemma 3n fully available in the open-source ecosystem!

    Hugging Face mediumAdept

    Gemma 3n, Google's natively multimodal on-device model accepting text, image, audio and video, now runs in transformers, timm, MLX, llama.cpp (text only), Transformers.js/ONNX and Ollama. The E2B and E4B variants have 5B and 8B real parameters but need only about 2GB and 3GB of GPU memory. Per-Layer Embeddings offload embeddings to the CPU, and a nested MatFormer design trains E2B as a sub-model of E4B so layers can be mixed and matched to a memory budget. Vision uses a new MobileNet-V5-300 encoder, audio uses a USM-based encoder over 160ms chunks, and code snippets plus free Colab notebooks cover inference and fine-tuning.

  • My Journey to a serverless transformers pipeline on Google Cloud

    Hugging Face mediumAdept

    A Hugging Face transformers sentiment-analysis pipeline (DistilBERT fine-tuned on SST-2) was deployed as a low-traffic microservice on Google Cloud Run after AI Platform Prediction (beta instability, checkpoint not a pure TensorFlow model) and App Engine (TensorFlow install failure, PyTorch handling only 2 requests per instance) fell short. The final setup is a Flask app served by gunicorn with one worker and one thread in a Docker image, with Cloud Run memory raised to 4 GB because the model needs most of it. Requests take under five seconds including model loading, plus roughly 10 seconds on cold start; loading the model at startup instead of per request would cut both latency and memory. The image is about 1 GB (PyTorch ~700 MB, model ~250 MB), so storage costs roughly €0.10 per GB per month on top of request costs.

  • Total noob’s intro to Hugging Face Transformers

    Hugging Face easyNovice

    A beginner-oriented walkthrough of running Microsoft's Phi-2 language model with the Hugging Face Transformers library inside a JupyterLab notebook hosted on a Hugging Face Space with an NVIDIA A10G GPU. It explains foundational concepts along the way (libraries, the Hub, Spaces, classes, methods, tokenizers, instruction-tuned vs base models) and shows the minimal code: install torch and transformers, load the model and tokenizer with from_pretrained, tokenize a prompt, call generate with max_new_tokens, and decode the output. Because Phi-2 is a base model, its output reads like autocomplete rather than a chat reply.

The quest for software engineering knowledge.

Look:
Layout: