MCPcopy
hub / github.com/huggingface/transformers.js

github.com/huggingface/transformers.js @4.2.0 sqlite

repository ↗ · DeepWiki ↗ · release 4.2.0 ↗
2,884 symbols 6,091 edges 638 files 653 documented · 23%
README
    <img alt="transformers.js javascript library logo" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/transformersjs-light.svg" width="500" style="max-width: 100%;">













<a href="https://www.npmjs.com/package/@huggingface/transformers"><img alt="NPM" src="https://img.shields.io/npm/v/@huggingface/transformers"></a>
<a href="https://www.npmjs.com/package/@huggingface/transformers"><img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@huggingface/transformers"></a>
<a href="https://www.jsdelivr.com/package/npm/@huggingface/transformers"><img alt="jsDelivr Hits" src="https://img.shields.io/jsdelivr/npm/hw/@huggingface/transformers"></a>
<a href="https://github.com/huggingface/transformers.js/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/huggingface/transformers.js?color=blue"></a>
<a href="https://huggingface.co/docs/transformers.js/index"><img alt="Documentation" src="https://img.shields.io/website/http/huggingface.co/docs/transformers.js/index.svg?down_color=red&down_message=offline&up_message=online"></a>

State-of-the-art Machine Learning for the Web

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

Transformers.js is designed to be functionally equivalent to Hugging Face's transformers python library, meaning you can run the same pretrained models using a very similar API. These models support common tasks in different modalities, such as: - 📝 Natural Language Processing: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation. - 🖼️ Computer Vision: image classification, object detection, segmentation, and depth estimation. - 🗣️ Audio: automatic speech recognition, audio classification, and text-to-speech. - 🐙 Multimodal: embeddings, zero-shot audio classification, zero-shot image classification, and zero-shot object detection.

Transformers.js uses ONNX Runtime to run models in the browser. The best part about it, is that you can easily convert your pretrained PyTorch, TensorFlow, or JAX models to ONNX using 🤗 Optimum.

For more information, check out the full documentation.

Installation

To install via NPM, run:

npm i @huggingface/transformers

Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using ES Modules, you can import the library with:

<script type="module">
    import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.2.0';
</script>

Quick tour

It's super simple to translate from existing code! Just like the python library, we support the pipeline API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library.

Python (original) Javascript (ours)
from transformers import pipeline

# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')

out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}]
import { pipeline } from '@huggingface/transformers';

// Allocate a pipeline for sentiment-analysis
const pipe = await pipeline('sentiment-analysis');

const out = await pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}]

You can also use a different model by specifying the model id or path as the second argument to the pipeline function. For example:

// Use a different model for sentiment-analysis
const pipe = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');

By default, when running in the browser, the model will be run on your CPU (via WASM). If you would like to run the model on your GPU (via WebGPU), you can do this by setting device: 'webgpu', for example:

// Run the model on WebGPU
const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
  device: 'webgpu',
});

For more information, check out the WebGPU guide.

[!WARNING] The WebGPU API is still experimental in many browsers, so if you run into any issues, please file a bug report.

In resource-constrained environments, such as web browsers, it is advisable to use a quantized version of the model to lower bandwidth and optimize performance. This can be achieved by adjusting the dtype option, which allows you to select the appropriate data type for your model. While the available options may vary depending on the specific model, typical choices include "fp32" (default for WebGPU), "fp16", "q8" (default for WASM), and "q4". For more information, check out the quantization guide.

// Run the model at 4-bit quantization
const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
  dtype: 'q4',
});

Ready to dive in? Explore our wide variety of demo applications and templates here. You can also launch your own project instantly using the official Transformers.js template on Hugging Face!

Custom usage

By default, Transformers.js uses hosted pretrained models and precompiled WASM binaries, which should work out-of-the-box. You can customize this as follows:

Settings

import { env } from '@huggingface/transformers';

// Specify a custom location for models (defaults to '/models/').
env.localModelPath = '/path/to/models/';

// Disable the loading of remote models from the Hugging Face Hub:
env.allowRemoteModels = false;

// Set location of .wasm files. Defaults to use a CDN.
env.backends.onnx.wasm.wasmPaths = '/path/to/files/';

For a full list of available settings, check out the API Reference.

Convert your models to ONNX

We recommend using Optimum to convert your PyTorch models to ONNX in a single command. For the full list of supported architectures, check out the Optimum documentation.

Supported tasks/models

Here is the list of all tasks and architectures currently supported by Transformers.js. If you don't see your task/model listed here or it is not yet supported, feel free to open up a feature request here.

To find compatible models on the Hub, select the "transformers.js" library tag in the filter menu (or visit this link). You can refine your search by selecting the task you're interested in (e.g., text-classification).

Tasks

Natural Language Processing

Task ID Description Supported?
Fill-Mask fill-mask Masking some of the words in a sentence and predicting which words should replace those masks. (docs)

(models) | | Question Answering | question-answering | Retrieve the answer to a question from a given text. | ✅ (docs)

(models) | | Sentence Similarity | sentence-similarity | Determining how similar two texts are. | ✅ (docs)

(models) | | Summarization | summarization | Producing a shorter version of a document while preserving its important information. | ✅ (docs)

(models) | | Table Question Answering | table-question-answering | Answering a question about information from a given table. | ❌ | | Text Classification | text-classification or sentiment-analysis | Assigning a label or class to a given text. | ✅ (docs)

(models) | | Text Generation | text-generation | Producing new text by predicting the next word in a sequence. | ✅ (docs)

(models) | | Text-to-text Generation | text2text-generation | Converting one text sequence into another text sequence. | ✅ (docs)

(models) | | Token Classification | token-classification or ner | Assigning a label to each token in a text. | ✅ (docs)

(models) | | Translation | translation | Converting text from one language to another. | ✅ (docs)

(models) | | Zero-Shot Classification | zero-shot-classification | Classifying text into classes that are unseen during training. | ✅ (docs)

(models) | | Feature Extraction | feature-extraction | Transforming raw data into numerical features that can be processed while preserving the information in the original dataset. | ✅ (docs)

(models) |

Vision

Task ID Description Supported?
Background Removal background-removal Isolating the main subject of an image by removing or making the background transparent. (docs)

(models) | | Depth Estimation | depth-estimation | Predicting the depth of objects present in an image. | ✅ (docs)

[(models

Extension points exported contracts — how you extend this code

CrossOriginStorageRequestFileHandleHash (Interface)
* Represents the dictionary for hash algorithms and values.
packages/transformers/src/utils/cache/cross-origin-storage.d.ts
CrossOriginStorageRequestFileHandleOptions (Interface)
* Represents the options for requesting file handles.
packages/transformers/src/utils/cache/cross-origin-storage.d.ts
CrossOriginStorageManager (Interface)
* The CrossOriginStorageManager interface. * [SecureContext]
packages/transformers/src/utils/cache/cross-origin-storage.d.ts
Navigator (Interface)
* Augment the standard Navigator interface.
packages/transformers/src/utils/cache/cross-origin-storage.d.ts

Core symbols most depended-on inside this repo

from_pretrained
called by 260
packages/transformers/src/configs.js
push
called by 229
packages/transformers/src/generation/logits_process.js
tolist
called by 200
packages/transformers/src/utils/tensor.js
map
called by 191
packages/transformers/src/utils/tensor.js
slice
called by 140
packages/transformers/src/utils/tensor.js
mean
called by 139
packages/transformers/src/utils/tensor.js
item
called by 138
packages/transformers/src/utils/tensor.js
dispose
called by 137
packages/transformers/src/utils/tensor.js

Shape

Class 1,866
Method 732
Function 282
Interface 4

Languages

TypeScript100%
Python1%

Modules by API surface

packages/transformers/src/utils/tensor.js89 symbols
packages/transformers/src/generation/logits_process.js70 symbols
packages/transformers/src/models/auto/modeling_auto.js66 symbols
packages/transformers/src/models/modeling_utils.js46 symbols
packages/transformers/src/utils/maths.js40 symbols
packages/transformers/src/models/modeling_outputs.js29 symbols
packages/transformers/src/utils/image.js26 symbols
packages/transformers/src/tokenization_utils.js26 symbols
packages/transformers/src/generation/stopping_criteria.js24 symbols
packages/transformers/src/utils/audio.js23 symbols
packages/transformers/src/image_processors_utils.js23 symbols
packages/transformers/src/models/wavlm/modeling_wavlm.js19 symbols

Dependencies from manifests, versioned

@huggingface/jinja0.5.6 · 1×
@huggingface/tokenizers0.1.3 · 1×
@types/jest30.0.0 · 1×
@types/node24.1.0 · 1×
@webgpu/types0.1.69 · 1×
esbuild0.27.2 · 1×
jest30.2.0 · 1×
jest-environment-node30.2.0 · 1×
jsdoc-to-markdown9.1.3 · 1×
onnxruntime-node1.24.3 · 1×
onnxruntime-web1.26.0-dev.20260416- · 1×
prettier3.8.1 · 1×

For agents

$ claude mcp add transformers.js \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact