Favicon of Transformers.js

Transformers.js

An open-source JavaScript library that runs AI models in your browser using ONNX Runtime, with CPU execution and optional WebGPU acceleration.

Screenshot of Transformers.js website

Transformers.js is a JavaScript library for developers building web apps that run AI models on the user's device. Inference happens in the browser, so an app doesn't need a separate model server to process its inputs. The library is open source under Apache 2.0.

Its scope extends beyond text generation. It supports summarization, translation, sentiment analysis and questions answered from supplied text, along with embeddings for comparing content. Image tasks include object detection, background removal, segmentation and depth estimation. Audio support covers speech transcription, audio classification and text-to-speech. It can also extract text from images and answer questions about document images.

The API follows Hugging Face's Python Transformers library closely, which makes it a practical choice for developers bringing existing model-based work to JavaScript. Its pipeline interface combines a pretrained model with input preparation and output processing. Compatible architectures include BERT, DistilBERT, BART, CLIP and CodeLlama, with compatible models available through the Hugging Face Hub.

ONNX Runtime handles model execution. The browser uses the CPU through WebAssembly by default, with WebGPU available for GPU acceleration. Quantized models reduce download size and resource demands in browser environments. Developers can also bring their own pretrained PyTorch, TensorFlow or JAX models by converting them to ONNX with Hugging Face Optimum.

Similar to Transformers.js