The pipeline API
Last updated
Last updated
Just like the , Transformers.js provides users with a simple way to leverage the power of transformers. The pipeline()
function is the easiest and fastest way to use a pretrained model for inference.
For the full list of available tasks/pipelines, check out .
Start by creating an instance of pipeline()
and specifying a task you want to use it for. For example, to create a sentiment analysis pipeline, you can do:
Copied
When running for the first time, the pipeline
will download and cache the default pretrained model associated with the task. This can take a while, but subsequent calls will be much faster.
By default, models will be downloaded from the and stored in , but there are ways to specify custom models and cache locations. For more information see .
You can now use the classifier on your target text by calling it as a function:
Copied
If you have multiple inputs, you can pass them as an array:
Copied
You can also specify a different model to use for the pipeline by passing it as the second argument to the pipeline()
function. For example, to use a different model for sentiment analysis (like one trained to predict sentiment of a review as a number of stars between 1 and 5), you can do:
Copied
The pipeline()
function is a great way to quickly use a pretrained model for inference, as it takes care of all the preprocessing and postprocessing for you. For example, if you want to perform Automatic Speech Recognition (ASR) using OpenAI’s Whisper model, you can do:
Copied
We offer a variety of options to control how models are loaded from the BOINC AI Hub (or locally). By default, the quantized version of the model is used, which is smaller and faster, but usually less accurate. To override this behaviour (i.e., use the unquantized model), you can use a custom PretrainedOptions
object as the third parameter to the pipeline
function:
Copied
You can also specify which revision of the model to use, by passing a revision
parameter. Since the BOINC AI Hub uses a git-based versioning system, you can use any valid git revision specifier (e.g., branch name or commit hash)
Copied
Many pipelines have additional options that you can specify. For example, when using a model that does multilingual translation, you can specify the source and target languages like this:
Copied
For example, to generate a poem using LaMini-Flan-T5-783M
, you can do:
Copied
which outputs:
Copied
Natural Language Processing
conversational
Generating conversational text that is relevant, coherent and knowledgable given a prompt.
❌
fill-mask
Masking some of the words in a sentence and predicting which words should replace those masks.
question-answering
Retrieve the answer to a question from a given text.
sentence-similarity
Determining how similar two texts are.
summarization
Producing a shorter version of a document while preserving its important information.
table-question-answering
Answering a question about information from a given table.
❌
text-classification
or sentiment-analysis
Assigning a label or class to a given text.
text-generation
Producing new text by predicting the next word in a sequence.
text2text-generation
Converting one text sequence into another text sequence.
token-classification
or ner
Assigning a label to each token in a text.
translation
Converting text from one language to another.
zero-shot-classification
Classifying text into classes that are unseen during training.
Vision
depth-estimation
Predicting the depth of objects present in an image.
❌
image-classification
Assigning a label or class to an entire image.
image-segmentation
Divides 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.
image-to-image
Transforming a source image to match the characteristics of a target image or a target image domain.
❌
mask-generation
Generate masks for the objects in an image.
❌
object-detection
Identify objects of certain defined classes within an image.
n/a
Assigning a label or class to an entire video.
❌
n/a
Generating images with no condition in any context (like a prompt text or another image).
❌
Audio
audio-classification
Assigning a label or class to a given audio.
n/a
Generating audio from an input audio source.
❌
automatic-speech-recognition
Transcribing a given audio into text.
text-to-speech
or text-to-audio
Generating natural-sounding speech given text input.
Tabular
n/a
Classifying a target category (a group) based on set of attributes.
❌
n/a
Predicting a numerical value given a set of attributes.
❌
Multimodal
document-question-answering
Answering questions on document images.
feature-extraction
Transforming raw data into numerical features that can be processed while preserving the information in the original dataset.
image-to-text
Output text from a given image.
text-to-image
Generates images from input text.
❌
visual-question-answering
Answering open-ended questions based on an image.
❌
zero-shot-image-classification
Classifying images into classes that are unseen during training.
Reinforcement Learning
n/a
Learning from actions by interacting with an environment through trial and error and receiving rewards (negative or positive) as feedback.
❌
Transformers.js supports loading any model hosted on the BOINC AI Hub, provided it has ONNX weights (located in a subfolder called onnx
). For more information on how to convert your PyTorch, TensorFlow, or JAX model to ONNX, see the .
For the full list of options, check out the documentation.
When using models that support auto-regressive generation, you can specify generation parameters like the number of new tokens, sampling methods, temperature, repetition penalty, and much more. For a full list of available parameters, see to the class.
For more information on the available options for each pipeline, refer to the . If you would like more control over the inference process, you can use the , , or classes instead.
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅