New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@xenova/transformers

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xenova/transformers

State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!

2.17.2
latest
Source
npm
Version published
Weekly downloads
74K
1.49%
Maintainers
1
Weekly downloads
 
Created
Source


transformers.js javascript library logo

NPM NPM Downloads jsDelivr Hits License Documentation

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.

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 '@xenova/transformers';

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

let 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
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">
    import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2';
</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

import { env } from '@xenova/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 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?
Fill-Maskfill-maskMasking some of the words in a sentence and predicting which words should replace those masks.(docs)
(models)
Question Answeringquestion-answeringRetrieve the answer to a question from a given text.(docs)
(models)
Sentence Similaritysentence-similarityDetermining how similar two texts are.(docs)
(models)
SummarizationsummarizationProducing a shorter version of a document while preserving its important information.(docs)
(models)
Table Question Answeringtable-question-answeringAnswering a question about information from a given table.
Text Classificationtext-classification or sentiment-analysisAssigning a label or class to a given text.(docs)
(models)
Text Generationtext-generationProducing new text by predicting the next word in a sequence.(docs)
(models)
Text-to-text Generationtext2text-generationConverting one text sequence into another text sequence.(docs)
(models)
Token Classificationtoken-classification or nerAssigning a label to each token in a text.(docs)
(models)
TranslationtranslationConverting text from one language to another.(docs)
(models)
Zero-Shot Classificationzero-shot-classificationClassifying text into classes that are unseen during training.(docs)
(models)
Feature Extractionfeature-extractionTransforming raw data into numerical features that can be processed while preserving the information in the original dataset.(docs)
(models)
Vision
TaskIDDescriptionSupported?
Depth Estimationdepth-estimationPredicting the depth of objects present in an image.(docs)
(models)
Image Classificationimage-classificationAssigning a label or class to an entire image.(docs)
(models)
Image Segmentationimage-segmentationDivides an image into segments where each pixel is mapped to an object. This task has multiple variants such as instance segmentation, panoptic segmentation and semantic segmentation.(docs)
(models)
Image-to-Imageimage-to-imageTransforming a source image to match the characteristics of a target image or a target image domain.(docs)
(models)
Mask Generationmask-generationGenerate masks for the objects in an image.
Object Detectionobject-detectionIdentify objects of certain defined classes within an image.(docs)
(models)
Video Classificationn/aAssigning a label or class to an entire video.
Unconditional Image Generationn/aGenerating images with no condition in any context (like a prompt text or another image).
Image Feature Extractionimage-feature-extractionTransforming raw data into numerical features that can be processed while preserving the information in the original image.(docs)
(models)
Audio
TaskIDDescriptionSupported?
Audio Classificationaudio-classificationAssigning a label or class to a given audio.(docs)
(models)
Audio-to-Audion/aGenerating audio from an input audio source.
Automatic Speech Recognitionautomatic-speech-recognitionTranscribing a given audio into text.(docs)
(models)
Text-to-Speechtext-to-speech or text-to-audioGenerating natural-sounding speech given text input.(docs)
(models)
Tabular
TaskIDDescriptionSupported?
Tabular Classificationn/aClassifying a target category (a group) based on set of attributes.
Tabular Regressionn/aPredicting a numerical value given a set of attributes.
Multimodal
TaskIDDescriptionSupported?
Document Question Answeringdocument-question-answeringAnswering questions on document images.(docs)
(models)
Image-to-Textimage-to-textOutput text from a given image.(docs)
(models)
Text-to-Imagetext-to-imageGenerates images from input text.
Visual Question Answeringvisual-question-answeringAnswering open-ended questions based on an image.
Zero-Shot Audio Classificationzero-shot-audio-classificationClassifying audios into classes that are unseen during training.(docs)
(models)
Zero-Shot Image Classificationzero-shot-image-classificationClassifying images into classes that are unseen during training.(docs)
(models)
Zero-Shot Object Detectionzero-shot-object-detectionIdentify objects of classes that are unseen during training.(docs)
(models)
Reinforcement Learning
TaskIDDescriptionSupported?
Reinforcement Learningn/aLearning from actions by interacting with an environment through trial and error and receiving rewards (negative or positive) as feedback.

Models

Keywords

transformers

FAQs

Package last updated on 29 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts