Pipeline

Pipeline is the official high-level inference API: you pass a task name and a model ID; it wires Preprocessor + model + post-processing. Use it to explore Hub checkpoints. Drop down when you need generate() knobs or a training loop.


Common tasks

taskInputTypical ungated model
text-classificationstringdistilbert/distilbert-base-uncased-finetuned-sst-2-english
token-classificationstring (NER)dslim/bert-base-NER (read the card first)
text-generationpromptdistilbert/distilgpt2, Qwen/Qwen2.5-0.5B-Instruct
summarizationlong texta small T5 / DistilBART; check the license
automatic-speech-recognitionaudio path / arrayopenai/whisper-tiny
image-classificationimage path / URLgoogle/vit-base-patch16-224
zero-shot-classificationtext + candidate labelsa small MNLI model

See the full list in the Pipeline docs. In production, always set model=—do not rely on the implicit default.


Text: classification

from transformers import pipeline

clf = pipeline(
    "text-classification",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
print(clf(["A thoughtful and funny script.", "Waste of time."]))

A list is batched. Each item has label and score.


Text: generation

from transformers import pipeline

gen = pipeline(
    "text-generation",
    model="distilbert/distilgpt2",
    max_new_tokens=32,
    do_sample=True,
    temperature=0.8,
)
print(gen("The secret of a good tutorial is")[0]["generated_text"])

max_new_tokens caps new tokens so you neither stop too early nor ramble. Greedy vs sampling is in Text Generation.

For Chinese instructions use Qwen/Qwen2.5-0.5B-Instruct with device_map="auto" and dtype="auto". DeepSeek Instruct models on the Hub are similarly CN-friendly—confirm they are ungated before downloading.


Vision: image classification

from transformers import pipeline

vision = pipeline(
    "image-classification",
    model="google/vit-base-patch16-224",
)
print(vision("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"))

An ImageProcessor (a Preprocessor) normalizes pixels into model tensors.


Audio: Whisper-tiny

from transformers import pipeline

asr = pipeline(
    "automatic-speech-recognition",
    model="openai/whisper-tiny",
)
# asr("path/to/short.wav")

whisper-tiny is small and ungated—good enough to prove a WAV / mic path. For long or multilingual audio, step up to whisper-small+ and watch VRAM.


What Pipeline does

raw input  →  Preprocessor.encode / preprocess
           →  PreTrainedModel.forward or generate()
           →  post-process (id→token, softmax, timestamps)

That is the same as hand-wiring AutoTokenizer + AutoModel*. Pipeline saves boilerplate; custom padding, beam search, or multi-turn chat templates are cleaner with AutoClasses.

ArgumentRole
modelHub ID or local folder
devicecpu / cuda:0 / …
device_map / dtypesharding and precision (common for generation)
batch_sizebatching for list inputs
return_all_scoresall class scores for classification

When to switch tools

  • Local chat only: Ollama is simpler.
  • Paper metrics, new heads, your own DataLoader: stay on Transformers.
  • Tensor shapes and .to(device): PyTorch tutorial.

Next steps

评论