Transformers.js: Building Next-Generation WebAI Applications
By Chrome for Developers
Key Concepts
- Hugging Face: A platform for AI community collaboration on models, datasets, and applications (Spaces).
- Transformers.js: A JavaScript library for running AI models 100% locally in the browser.
- In-Browser Inference: Running AI models directly on the user's device without sending data to a server.
- Web AI Applications: AI-powered applications designed to run in a web browser.
- Quantization: Reducing model size and computational cost by using lower precision data types.
- WebGPU: A web API for high-performance graphics and general-purpose computation on the GPU.
- WebAssembly (Wasm): A binary instruction format for a stack-based virtual machine, enabling high-performance execution of code compiled from various languages in web browsers.
- ONNX (Open Neural Network Exchange): A standard format for representing machine learning models, facilitating interoperability between different frameworks.
- Pipelines: A high-level API in Transformers.js that simplifies the use of pre-trained models for specific tasks.
Hugging Face Ecosystem and Transformers.js Introduction
Hugging Face is presented as a central hub for the machine learning community, hosting over 2.1 million AI models, half a million datasets, and over 1 million AI applications (Spaces). The platform emphasizes collaboration and provides tools for searching, filtering, and interacting with these resources. Hugging Face also maintains several open-source libraries, including the well-known transformers library.
The focus of the talk is Transformers.js, a JavaScript library designed to enable the execution of AI models entirely within the user's web browser. This approach offers significant advantages:
- 100% Local Execution: All computations happen on the user's device.
- Data Security and Privacy: Sensitive data remains on the user's machine.
- Low Latency: Eliminates network round trips to servers.
- Effortless Scalability: Distributes computational load to users' devices.
- Leverages Browser APIs: Utilizes technologies like WebAssembly, WebGPU, and WebNN for optimized performance.
Benefits of In-Browser Inference
The presentation highlights several key benefits of performing AI inference directly in the browser:
- Security and Privacy: Crucial for applications handling sensitive user data (e.g., video, microphone input, confidential documents) as data is not transmitted to external servers.
- Real-Time Applications: The absence of server dependencies allows for immediate responses, which is particularly beneficial in areas with poor internet connectivity. It also avoids the need to transfer large files over the network.
- Developer and User Advantages:
- Developers: Can showcase models without needing dedicated GPU hosting, distributing compute to users.
- Users: Experience no API key exchanges, no per-token costs, and pay for compute only through their device usage.
- Simplified Distribution: Deploying an AI application becomes as simple as sharing a link. Developers avoid complex dependency management (e.g., PyTorch, Python) and cross-platform compatibility issues (Mac, Linux, Windows).
Optimizing for In-Browser Inference
Several strategies are recommended for optimizing AI models for browser execution:
- Quantization: Reducing model size and computational requirements by using lower-precision data types (e.g., 8-bit integers, 16-bit floating points). This can lead to model size reductions of up to 8x with minimal quality degradation, though the impact is model-specific. Transformers.js offers various quantization options and sensible defaults.
- Leveraging Browser APIs: Utilizing WebGPU and WebNN to harness the native hardware capabilities of users' devices in an efficient and optimized manner.
- Model Export Optimization: When migrating models from Python-based ecosystems to the web, careful consideration of export formats is necessary. This can involve techniques like fused kernels and custom operations to achieve significant performance boosts (e.g., a 4x performance improvement for a simple burst embedding model).
JavaScript Versatility and Ecosystem Integration
Transformers.js benefits from the broad versatility of JavaScript:
- JavaScript Runtimes: The library can run not only in browsers but also in other JavaScript runtimes like Node.js, Bun, and Deno, with growing WebGPU support in these environments.
- Framework Compatibility: It integrates seamlessly with popular web frameworks such as React, Svelte, Angular, and Vue.
- Deployment Environments: Applications built with Transformers.js can be deployed in various forms:
- Websites (potentially using web workers).
- Browser extensions.
- Serverless functions (e.g., Superbase edge functions).
- Desktop applications (e.g., Electron).
- Build Tool Integration: Works well with build tools like Vite and Webpack for efficient application bundling.
- Mobile Support: Development is underway for mobile support via React Native.
Browser Support and Performance
- Chromium-based Browsers (e.g., Chrome): Offer excellent WebGPU support, enabling efficient utilization of hardware capabilities. Demos are primarily recorded in these browsers.
- Firefox: Transformers.js powers Firefox's AI runtime, supporting tasks like image classification and translation. WebGPU and WebNN support are experimental but expected soon.
- Safari: WebGPU support was recently introduced in Safari 16.4, enabling web AI applications on macOS, iOS, iPadOS, and VisionOS.
Usage Growth and Community Impact
The growth of Transformers.js is evident in its adoption metrics:
- NPM Downloads: Reached approximately 1.68 million in the last month, a 7% increase from the previous month.
- Unique Monthly Users: Around 1.7 million, up 12% from the previous month.
- CDN Requests: Nearly 11 million requests, up 13% from the previous month, indicating adoption by users who prefer direct CDN access.
The evolution of Transformers.js versions shows a significant growth trajectory:
- Version 1 (March 2023): Started as a small side project with very low usage.
- Version 2: Reached around 5,000 unique monthly users.
- Version 3 (released around a year ago): Achieved approximately 750,000 unique monthly users.
- Current (over 1.7 million unique monthly users): Represents more than a twofold increase in users over the past year, attributed to the active community building web AI applications.
The speaker expresses gratitude to the community for their contributions.
Getting Started with Transformers.js: A Simple Example
The core functionality of Transformers.js can be accessed with just three lines of code:
- Import the
pipelinefunction:import { pipeline } from '@xenova/transformers'; - Create a pipeline instance for a specific task (e.g., sentiment analysis):
const classifier = await pipeline('sentiment-analysis'); - Run the input through the pipeline:
const result = await classifier('I love transformers.js!'); console.log(result); // Output: [{ label: 'POSITIVE', score: 0.99... }]
The library also supports using custom models. For example, a background removal task can be implemented by specifying a community-created model:
const removeBackground = await pipeline('background-removal', {
model: 'path/to/custom/background-removal-model'
});
const outputImage = await removeBackground('path/to/input/image.jpg');
Loading and Runtime Parameters
Users can configure model loading and runtime behavior:
- Loading Parameters: When creating a pipeline, specify the
device(e.g., 'gpu', 'webgpu', 'webnn', 'cpu', 'wasm') anddtype(data type, e.g., for quantization like 'q4_0' for 4-bit quantization with 0-bit quantization parameters). - Runtime Parameters: At runtime, parameters like
max_new_tokens,do_sample, andtemperaturecan be adjusted for tasks like text generation.
Advanced Usage and Model Conversion
For more intricate integrations, Transformers.js offers lower-level access, similar to the Python transformers library. This is demonstrated with an example of image segmentation using the "Segment Anything" model.
The underlying mechanism involves:
- Pre-converted Models: Hugging Face hosts around 2,500 pre-converted models.
- Custom Model Conversion: For custom models, scripts and libraries are provided to convert PyTorch, Jax, or TensorFlow models to the ONNX (Open Neural Network Exchange) format.
- ONNX Runtime Web: This runtime then enables the execution of ONNX models on WebAssembly, WebGPU, and WebNN, allowing selection of the target device (CPU, GPU, NPU).
Building Web AI Applications: A Step-by-Step Approach
The process of building web AI applications with Transformers.js involves several considerations:
- Idea Generation: Identify the problem to solve or the experience to create.
- Justification for In-Browser Execution: Determine why running the model locally is advantageous (e.g., low latency, distribution, privacy).
- Task Identification: Find an existing task (e.g., sentiment analysis, embedding computation, depth estimation) that aligns with the problem.
- Model Selection: Choose a model that best suits the use case, considering factors like accuracy, size (e.g., 10-20MB for background removal vs. hundreds of MB), and real-time performance requirements.
- Development with Transformers.js: If the above criteria are met, proceed with building the application.
- Learning from the Community: Explore example applications and resources provided by Hugging Face to understand possibilities and integrate them into specific workflows.
Factors to Consider When Building Web AI Applications
- Bandwidth: Users need to download models, which are then cached. Model size is a critical factor.
- Accuracy vs. Speed: A trade-off often exists between achieving high accuracy and running models in real-time.
- Device Features: Consider the capabilities of the user's device, such as access to browser APIs (WebGPU, microphone).
- Target Devices: Decide whether the application should run on mobile, desktop, or both, as this influences model and feature choices.
Developer Showcase: Real-World Applications
The presentation features several impressive examples of web AI applications built with Transformers.js:
- Traditional Chatbot Experience: Demonstrates a 1.7 billion parameter model running on an M4 Mac at over 160 tokens per second, highlighting its suitability for real-time interactions.
- Reasoning Models: Features DeepSeek R1's distilled 1.5 billion parameter model, which outperformed GPT-4o and Claude 3.5 Sonnet on math benchmarks, running in the browser.
- Vision Language Model (VLM): Capable of live captioning video streams or camera input, describing frames in real-time, recognizing text, and performing object detection.
- Bedtime Story Generator (Gemma 3B): A small, on-device model for a specific task, generating stories based on user inputs and speaking them out with low latency.
- Tool Calling Model: Demonstrates a language model that can call JavaScript functions for tasks like math evaluation or random number generation, and can integrate with browser APIs like location and time.
- Semantic Galaxy (Embedding Gemma): Visualizes document embeddings in 3D, allowing for real-time, interactive, and semantic search of documents.
- Coco (Text-to-Speech): A groundbreaking 82 million parameter model producing high-quality, realistic text-to-speech.
- Whisper Web (Speech Recognition): Enables real-time speech recognition in the browser using OpenAI's Whisper models with WebGPU.
- Doodle Dash: A game based on Google's Quick Draw, using image classification for real-time drawing detection, playable on mobile.
- Vision Transformer Educational Tool: Visualizes how vision transformers process images, showing the progression of understanding through different network layers, using a tiger image as an example.
- Dino V3 (Meta): Enables video tracking in the browser and provides visualization tools to understand feature highlighting. It demonstrates generalization capabilities, even for tasks it wasn't explicitly trained for.
- Real-time Conversational Agent: A sophisticated demo integrating multiple models: voice activity detection, speech recognition, a language model backbone, and text-to-speech. The team is working on unifying these into a single model for improved latency and performance.
Latest News, Current Plans, and Next Steps
The evolution of Transformers.js is marked by significant milestones:
- Early 2023: Initial idea for a simple spam detection version.
- Version 1: Released with a few architectures supported.
- Version 2: A complete rewrite using ES modules, supporting 19 architectures.
- Version 3: Introduced WebGPU and WebNN support, expanding to 119 architectures.
- Current: Supports approximately 170 architectures.
Transformers.js Version 4 (Developer Preview):
- Announced: Currently in developer preview.
- Goals: Faster execution and support for an even wider range of models.
- Release Candidate: Expected on npm in a couple of weeks.
The speaker concludes by encouraging developers to explore Transformers.js for their web AI applications and expresses excitement for what the community will build with it.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "Transformers.js: Building Next-Generation WebAI Applications". What would you like to know?