Pipeline API & Quick Start
From the course Hugging Face & Transformers
The Hugging Face Ecosystem
Hugging Face is the GitHub of machine learning — hosting 500K+ models, 100K+ datasets, and providing tools to use them.
Pipeline API
The simplest way to use any model:
from transformers import pipeline
# Text generation
generator = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct")
result = generator("The future of AI is", max_new_tokens=100)
# Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this product!")
# [{"label": "POSITIVE", "score": 0.9998}]
# Summarization
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
result = summarizer(long_text, max_length=130, min_length=30)
Available Pipelines
| Pipeline | Use Case |
|---|---|
| text-generation | Chat, completion, creative writing |
| text-classification | Sentiment, topic, intent |
| summarization | Document summarization |
| translation | Language translation |
| question-answering | Extract answers from context |
| image-classification | Label images |
| object-detection | Find objects in images |
| automatic-speech-recognition | Transcribe audio |
Installation
pip install transformers torch # PyTorch backend
pip install transformers[tf] # TensorFlow backend
Common Pitfall: Downloading Models at Import Time on Cold Start
When you import a pipeline without specifying a local cache path, Hugging Face downloads the model to ~/.cache/huggingface/hub on the first call. In containerized deployments, this directory is ephemeral — the model re-downloads on every cold start, adding 30–120 seconds of latency. Bake model weights into the Docker image:
RUN python -c "from transformers import pipeline; pipeline('sentiment-analysis')"
Or set HF_HOME to a Docker volume that persists across restarts.
Best Practice: Specify a Revision for Reproducibility
Model weights on the Hub can be updated without changing the model name. Pin to a specific Git revision to ensure reproducible behavior:
classifier = pipeline(
"sentiment-analysis",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0f", # Pin to exact commit hash
)
Named Product Reference: Hugging Face Inference Endpoints
Hugging Face's managed Inference Endpoints service lets you deploy any Hub model to AWS, GCP, or Azure with one click. Endpoints auto-scale to zero when idle (great for development) or maintain warm instances for production. For teams that want the quality of Hugging Face models without managing GPUs, Inference Endpoints is the fastest path to production.
Key Takeaways
- •The Pipeline API provides one-line access to any task and model
- •Hugging Face hosts 500K+ models across all AI domains
- •Pipelines handle tokenization, inference, and post-processing automatically
- •Supports PyTorch and TensorFlow backends
Progress is kept in this browser only.