Pipelines are the fastest way to try a model or ship a simple inference feature.
A pipeline bundles three steps: a preprocessor (tokenizer, image processor or feature extractor) that turns raw input into tensors, the model forward pass, and postprocessing that turns logits into readable results such as labels and scores.
clf = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")The first argument is a task name. Always pass model= explicitly: without it the pipeline picks a default checkpoint, which can change between releases and logs a warning.
Useful arguments:
device=0(first GPU),device="cpu", ordevice_map="auto"to let Accelerate place large models.dtype="auto"(the v5 default) loads weights in the precision they were saved in; passtorch.bfloat16to force half precision.- Pass a list of inputs and
batch_size=to process many items efficiently.
Pipelines accept a string, a list of strings, or for vision/audio tasks a file path, URL or PIL image. The model weights are downloaded once to the local cache (~/.cache/huggingface/hub) and reused afterwards.
Pipelines trade control for convenience. When you need custom batching, access to hidden states, or training, drop down to the tokenizer and model classes covered next. Note that in v5 several legacy tasks such as summarization and translation were removed from pipelines; use a chat model through text-generation for those.
Example
from transformers import pipeline
clf = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
print(clf("I love this library!"))
# [{'label': 'POSITIVE', 'score': 0.999...}]
reviews = ["Terrible support.", "Works great, fast shipping."]
for r in clf(reviews, batch_size=2):
print(r["label"], round(r["score"], 3))
# NEGATIVE 0.999...
# POSITIVE 0.999...
asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
# asr("meeting.wav") -> {'text': ' Hello everyone, ...'}Remember
pipeline(task, model=...)= preprocess + forward + postprocess- Always pin
model=instead of relying on the task default - Pass lists with
batch_sizefor throughput; usedevice/device_mapfor GPUs
Watch out for
- Calling a pipeline inside a loop one string at a time on GPU, which wastes most of its throughput.