Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Your First Pipeline

Use pipeline() to run a pretrained model in two lines, learn what happens inside, choose models and devices, and build a runnable stand-in of the same flow.

The pipeline() function is the fastest way to use a pretrained model. You name a task, optionally a model, and receive a callable object that accepts raw input (a string, an image path, an audio file) and returns readable results. It hides the tokenizer, the model call and the post-processing.

Running a pipeline

Real library code, read-only here:

Python
from transformers import pipeline

classifier = pipeline("sentiment-analysis")
print(classifier("The setup took five minutes. Brilliant!"))
print(classifier(["Terrible battery.", "Works as described."]))

The first call downloads a default model and prints a warning if you did not pin one. For anything beyond a demo, name the model so your results stay reproducible:

Python
classifier = pipeline(
    "text-classification",
    model="distilbert-base-uncased-finetuned-sst-2-english",
    device=-1,          # -1 = CPU, 0 = first GPU
)

You can also pass a revision (a branch, tag or commit hash) to pin an exact version of the weights.

The three steps inside

Every text pipeline performs the same three steps. Knowing them makes errors easy to place.

flowchart LR
  A["Raw input"] --> B["preprocess: tokenizer"]
  B --> C["forward: model"]
  C --> D["postprocess: softmax, labels"]
  D --> E["Readable result"]

Preprocess turns text into tensors, forward runs the network, postprocess turns raw scores into labels.

Here is the same flow written by hand with the real classes (read-only). You will study each part in the next lessons.

Python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name)

inputs = tokenizer("The setup took five minutes. Brilliant!", return_tensors="pt")
with torch.no_grad():
    logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)
label_id = int(probs.argmax())
print(model.config.id2label[label_id], float(probs[0, label_id]))

A runnable stand-in of the whole flow

The class below is a stand-in: a tiny pipeline built from the same three methods (preprocess, forward, postprocess), but its "model" is a hand-set weight per word. It lets you experiment with batching and the output format.

Python Stand-in: a three-step pipeline class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A sentence with no known words has logits [0, 0], so both probabilities are 0.5 and the label is just the first maximum. Real classifiers are the same: a score of 0.51 means "almost undecided", not "certain".

Choosing the task string

The task string selects a default model and the output format. Common values:

Task Meaning
text-classification (sentiment-analysis) one label per text
token-classification (ner) a label per token
question-answering answer span from a context
summarization, translation_en_to_fr text to text
text-generation continue a prompt
fill-mask predict a hidden word
zero-shot-classification label with classes you choose at run time
image-classification, automatic-speech-recognition vision and audio

The next chapters cover them in depth, starting with pipelines for text tasks.

Good habits from the start

  • Pin the model (and revision) in anything you ship.
  • Pass a list instead of looping, so the library can batch (see batching and speed).
  • Look at scores, not only labels, and decide what to do with low-confidence cases.
  • Load once, call many times. Creating a pipeline inside a loop reloads weights every time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three steps inside a pipeline?

    Input is tokenized, the model runs, and raw scores are turned into readable results.
  2. 2. Why should you pass a model name to pipeline() in production code?

    Defaults are a convenience. Pinning the model (and revision) keeps results stable.
  3. 3. What does device=0 mean?

    device=-1 is the CPU, device=0 the first CUDA GPU.
  4. 4. A pipeline is created inside a for loop over 1000 texts. What is wrong?

    Load the pipeline once and call it with the list of texts.

Summary

  • pipeline(task, model=...) returns a callable that wraps tokenizer, model and post-processing.
  • Single strings and lists are both accepted; lists give lists.
  • The manual version uses AutoTokenizer, AutoModelFor... and a softmax.
  • Pin models, create the pipeline once, and inspect scores.

Next: Model Families and Tasks