All skills
Skillintermediate

Allocate a pipeline for sentiment-analysis

<p align="center"> <br/> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-dark.svg" width="500" style="max-width: 100%;"> <source media="(prefers-color-scheme: light)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-light.svg" width="500" style="max-width: 1

Claude Code Knowledge Pack7/10/2026

Overview

<p align="center"> <br/> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-dark.svg" width="500" style="max-width: 100%;"> <source media="(prefers-color-scheme: light)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-light.svg" width="500" style="max-width: 100%;"> <img alt="transformers.js javascript library logo" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-light.svg" width="500" style="max-width: 100%;"> </picture> <br/> </p> <p align="center"> <a href="https://www.npmjs.com/package/@xenova/transformers"> <img alt="NPM" src="https://img.shields.io/npm/v/@xenova/transformers"> </a> <a href="https://www.npmjs.com/package/@xenova/transformers"> <img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@xenova/transformers"> </a> <a href="https://www.jsdelivr.com/package/npm/@xenova/transformers"> <img alt="jsDelivr Hits" src="https://img.shields.io/jsdelivr/npm/hw/@xenova/transformers"> </a> <a href="https://github.com/xenova/transformers.js/blob/main/LICENSE"> <img alt="License" src="https://img.shields.io/github/license/xenova/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> </p>

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, and segmentation.
  • šŸ—£ļø Audio: automatic speech recognition and audio classification.
  • šŸ™ Multimodal: zero-shot image classification.

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.

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.

<table> <tr> <th width="440px" align="center"><b>Python (original)</b></th> <th width="440px" align="center"><b>Javascript (ours)</b></th> </tr> <tr> <td>
from transformers import pipeline

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

out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}]
</td> <td>

// Allocate a pipeline for sentiment-analysis
let pipe = await pipeline("sentiment-analysis");

let out = await pipe("I love transformers!");
// [{'label': 'POSITIVE', 'score': 0.999817686}]
</td> </tr> </table>

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
let pipe = await pipeline(
  "sentiment-analysis",
  "Xenova/bert-base-multilingual-uncased-sentiment",
);

Installation

To install via NPM, run:

npm i @xenova/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">

</script>

Examples

Want to jump straight in? Get started with one of our sample applications/templates:

NameDescriptionLinks
Whisper WebSpeech recognition w/ Whispercode, demo
Doodle DashReal-time sketch-recognition gameblog, code, demo
Code PlaygroundIn-browser code completion websitecode, demo
Semantic Image Search (client-side)Search for images with textcode, demo
Semantic Image Search (server-side)Search for images with text (Supabase)code, demo
Vanilla JavaScriptIn-browser object detectionvideo, code, demo
ReactMultilingual translation websitecode, demo
Text to speech (client-side)In-browser speech synthesiscode, demo
Browser extensionText classification extensioncode
ElectronText classification applicationcode
Next.js (client-side)Sentiment analysis (in-browser inference)code, demo
Next.js (server-side)Sentiment analysis (Node.js inference)code, demo
Node.jsSentiment analysis APIcode
Demo siteA collection of demoscode, demo

Check out the Transformers.js template on Hugging Face to get started in one click!

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


// 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 our conversion script to convert your PyTorch, TensorFlow, or JAX models to ONNX in a single command. Behind the scenes, it uses šŸ¤— Optimum to perform conversion and quantization of your model.

python -m scripts.convert --quantize --model_id <model_name_or_path>

For example, convert and quantize bert-base-uncased using:

python -m scripts.convert --quantize --model_id bert-base-uncased

This will save the following files to ./models/:

bert-base-uncased/
ā”œā”€ā”€ config.json
ā”œā”€ā”€ tokenizer.json
ā”œā”€ā”€ tokenizer_config.json
└── onnx/
    ā”œā”€ā”€ model.onnx
    └── model_quantized.onnx

For the full list of supported architectures, see 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

TaskIDDescriptionSupported?
ConversationalconversationalGenerating conversational text that is relevant, coherent and knowledgable given a prompt.āŒ
Fill-Maskfill-maskMasking some of the words in a sentence and predicting which words should replace those masks.āœ… (docs)<br>(models)
Question Answeringquestion-answeringRetrieve the answer to a question from a given text.āœ… (docs)<br>(models)
Sentence Similaritysentence-similarityDetermining how similar two texts are.āœ… (docs)<br>(models)
SummarizationsummarizationProducing a shorter version of a document while preserving its important information.āœ… (docs)<br>(models)
Table Question Answeringtable-question-answeringAnswering a question about information from a given table.āŒ