Lesson 4 / 33

Your First Pipeline

Run a pretrained model in three lines with pipeline() and explain the three steps it hides.

By · 15 years in web development

What pipeline() does for you

pipeline() is the quickest way to get a result from a pretrained model. You give it a task name such as "sentiment-analysis" and, ideally, a model id from the Hub. Behind the scenes it performs three steps. Preprocessing: a tokenizer (or an image or audio processor) turns your raw input into tensors of numbers. Forward pass: the model turns those tensors into raw scores called logits. Postprocessing: the pipeline converts logits into something human-readable, for example a label with a probability, a list of entities, or generated text. The same pattern holds for every task, which is why one function covers text classification, question answering, summarisation, image classification and speech recognition. Pipelines are perfect for prototypes, demos and batch scripts. When you need control over padding, custom loss or speed, you drop down to the tokenizer and model classes you will meet in the next sections.

Inside a pipeline

Every pipeline is the same three-stage assembly line, whatever the task.

Three connected stages: preprocessing with a tokenizer, the model forward pass producing logits, and postprocessing into labels or text.
Figure 2.1: preprocess (tokenizer) -> model (logits) -> postprocess (readable result)

Sentiment analysis in three lines

Always pass an explicit model id so your results do not change if the task's default model changes. The first call downloads the model; scores will be close to 1.0 for clear-cut sentences.

from transformers import pipeline

classifier = pipeline(
    "sentiment-analysis",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)

results = classifier(["The canteen food was amazing today!",
                      "My train was two hours late again."])
for r in results:
    print(r["label"], round(r["score"], 3))  # e.g. POSITIVE 0.999 / NEGATIVE 0.99

A thali counter

A pipeline is like the thali counter in a mess: you hand over a token, the kitchen (model) does the real work, and the server arranges everything neatly on a plate. You never see the raw ingredients (tensors) unless you walk into the kitchen yourself.

Quick check: In a text-classification pipeline, which stage converts the model's logits into a label and a score?

  • Preprocessing
  • Postprocessing
  • Tokenization
  • The forward pass
Answer

Postprocessing — The model returns logits. Postprocessing applies softmax and maps the highest index to a label name from the model config.