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
Overview
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.
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:
| Name | Description | Links |
|---|---|---|
| Whisper Web | Speech recognition w/ Whisper | code, demo |
| Doodle Dash | Real-time sketch-recognition game | blog, code, demo |
| Code Playground | In-browser code completion website | code, demo |
| Semantic Image Search (client-side) | Search for images with text | code, demo |
| Semantic Image Search (server-side) | Search for images with text (Supabase) | code, demo |
| Vanilla JavaScript | In-browser object detection | video, code, demo |
| React | Multilingual translation website | code, demo |
| Text to speech (client-side) | In-browser speech synthesis | code, demo |
| Browser extension | Text classification extension | code |
| Electron | Text classification application | code |
| Next.js (client-side) | Sentiment analysis (in-browser inference) | code, demo |
| Next.js (server-side) | Sentiment analysis (Node.js inference) | code, demo |
| Node.js | Sentiment analysis API | code |
| Demo site | A collection of demos | code, 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
| Task | ID | Description | Supported? |
|---|---|---|---|
| Conversational | conversational | Generating conversational text that is relevant, coherent and knowledgable given a prompt. | ā |
| Fill-Mask | fill-mask | Masking some of the words in a sentence and predicting which words should replace those masks. | ā (docs)<br>(models) |
| Question Answering | question-answering | Retrieve the answer to a question from a given text. | ā (docs)<br>(models) |
| Sentence Similarity | sentence-similarity | Determining how similar two texts are. | ā (docs)<br>(models) |
| Summarization | summarization | Producing a shorter version of a document while preserving its important information. | ā (docs)<br>(models) |
| Table Question Answering | table-question-answering | Answering a question about information from a given table. | ā |