Course contentsShow
← AI Engineering
Lesson 50 of 1,8862. Working with Pre-trained ModelsFree lesson

Pipeline API for Quick Inference

Use the high-level pipeline() function for common tasks like text generation, sentiment analysis, and QA.

Pipeline API for Quick Inference

You'll learn: How to use Hugging Face's pipeline() function to run AI models with just a few lines of code.

The One-Line Model Interface

The pipeline() function is like a universal remote control for pre-trained models. Instead of writing dozens of lines to load models, tokenize text, run inference, and decode outputs, you call one function that handles everything automatically.

Think of it as ordering a complete meal versus cooking from scratch — you still get the same result, but pipeline() does all the preparation work for you.

How It Works

The pipeline function takes a task name (like "text-generation" or "sentiment-analysis") and automatically:

  • Downloads an appropriate model
  • Sets up the tokenizer
  • Handles input preprocessing
  • Runs the model
  • Returns human-readable results

Common Pipeline Tasks

Based on what you learned about task-specific models, here are the most popular pipelines:

from transformers import pipeline

# Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this tutorial!")
# Returns: [{'label': 'POSITIVE', 'score': 0.9998}]

# Text generation
generator = pipeline("text-generation")
output = generator("AI engineering is")

# Question answering
qa = pipeline("question-answering")
answer = qa(question="What is a pipeline?", 
            context="A pipeline simplifies model inference.")

Each pipeline returns results in a consistent, easy-to-use format — no need to understand tensors or model architectures yet.

Key Takeaway: The pipeline() API is your fastest path from zero to working AI inference, handling all complexity behind a simple function call that's perfect for prototyping and common tasks.