Transformers.js

Run 🤗 Transformers directly in your browser, with no need for a server!

2024-06-14

Transformers.js is designed to be functionally equivalent to Hugging Face's Transformers Python library, enabling users to run the same pretrained models using a very similar API. This library supports a wide range of tasks across different modalities, including:

  • Natural Language Processing (NLP): Text classification, named entity recognition, question answering, summarization, translation, and text generation.
  • Computer Vision: Image classification, object detection, segmentation, and depth estimation.
  • Audio Processing: Automatic speech recognition, audio classification, and text-to-speech.
  • Multimodal Tasks: Embeddings, zero-shot classification for audio and images, and zero-shot object detection.

Transformers.js leverages ONNX Runtime to execute models in the browser. One of its key strengths is the ability to easily convert pretrained PyTorch, TensorFlow, or JAX models to ONNX using Hugging Face's Optimum library. This makes it highly versatile for web-based AI applications.

Key Features:

  1. Browser-Based Execution: No server required; models run directly in the browser using WASM or WebGPU.
  2. Easy Conversion: Convert models from PyTorch/TensorFlow/JAX to ONNX with a single command.
  3. Quantization Support: Optimize performance with quantized models (e.g., 4-bit quantization).
  4. Pipeline API: Simplified model usage with preprocessing and postprocessing built-in.
  5. Cross-Platform Compatibility: Works with vanilla JS, React, Next.js, Electron, and more.

Installation:

bash npm i @huggingface/transformers

Example Usage:

```javascript import { pipeline } from '@huggingface/transformers';

const pipe = await pipeline('sentiment-analysis'); const out = await pipe('I love transformers!'); console.log(out); // [{'label': 'POSITIVE', 'score': 0.999817686}] ```

Transformers.js is ideal for developers looking to integrate AI capabilities into web applications without backend dependencies. It supports a growing list of models and tasks, making it a powerful tool for modern web development.

Artificial Intelligence Natural Language Processing Computer Vision Audio Processing Multimodal Models