Beginner

The Pipeline API

The fastest way to use pre-trained models. The pipeline() function handles tokenization, inference, and post-processing in a single call.

What is a Pipeline?

A pipeline wraps a pre-trained model and its associated preprocessing into a single, easy-to-use object. You specify the task, and the pipeline handles everything else - downloading the model, tokenizing input, running inference, and formatting the output.

Python
from transformers import pipeline

# Create a pipeline by specifying the task
classifier = pipeline("sentiment-analysis")

# Use it on any text
result = classifier("This movie was absolutely fantastic!")
print(result)
# [{'label': 'POSITIVE', 'score': 0.9998}]

Text Pipelines

Text Classification

Python
# Sentiment analysis
classifier = pipeline("sentiment-analysis")
results = classifier([
    "I love this product!",
    "This is terrible.",
    "It's okay, nothing special."
])
for r in results:
    print(f"{r['label']}: {r['score']:.4f}")

Text Generation

Python
generator = pipeline("text-generation", model="gpt2")
output = generator(
    "The key to learning machine learning is",
    max_length=100,
    num_return_sequences=2,
    temperature=0.7
)
for seq in output:
    print(seq['generated_text'])

Summarization & Translation

Python
# Summarization
summarizer = pipeline("summarization")
summary = summarizer(long_article, max_length=130, min_length=30)

# Translation
translator = pipeline("translation_en_to_fr")
result = translator("Hello, how are you?")
# [{'translation_text': 'Bonjour, comment allez-vous?'}]

Zero-Shot Classification

Classify text into categories the model has never been explicitly trained on:

Python
classifier = pipeline("zero-shot-classification")
result = classifier(
    "The stock market crashed today after the Fed announcement",
    candidate_labels=["politics", "finance", "sports", "technology"]
)
# {'labels': ['finance', 'politics', ...], 'scores': [0.92, 0.05, ...]}

Image & Audio Pipelines

Python
# Image classification
image_classifier = pipeline("image-classification")
result = image_classifier("photo.jpg")

# Object detection
detector = pipeline("object-detection")
objects = detector("street_scene.jpg")

# Speech recognition (Whisper)
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
text = transcriber("audio.mp3")

Specifying Models

You can use any compatible model from the Hub:

Python
# Use a specific model
classifier = pipeline(
    "sentiment-analysis",
    model="nlptown/bert-base-multilingual-uncased-sentiment"
)

# Use on GPU
classifier = pipeline("sentiment-analysis", device=0)  # GPU 0
classifier = pipeline("sentiment-analysis", device="cuda")
💡
Tip: When you do not specify a model, the pipeline uses a default model for each task. You can find the default models in the official documentation.

What's Next?

The Pipeline API is great for quick experimentation, but for more control you will want to work with models and tokenizers directly. The next lesson covers AutoModel, AutoTokenizer, and model architectures.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.