Hugging Face Pipelines
Hugging Face pipelines are the simplest way to run a model with transformers. A pipeline wraps three steps, tokenizing the input, running the model and turning raw numbers into readable results, into one function call.
Choosing a Task and a Model
Pass a task name, and optionally a model ID. Always pinning a model is a good habit: the default for a task can change between versions, and you want results that do not shift under you.
from transformers import pipeline
sentiment = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
print(sentiment(["The delivery was fast.", "The box arrived crushed."]))[{'label': 'POSITIVE', 'score': 0.9991}, {'label': 'NEGATIVE', 'score': 0.9993}]Passing a list processes several inputs at once and returns one result per item.
Common Pipeline Tasks
- text-classification: sentiment, spam, topic labels
- token-classification (ner): find names, places and organizations
- question-answering: extract an answer span from a context paragraph
- summarization and translation: sequence-to-sequence text tasks
- zero-shot-classification: classify into labels you choose at run time
- text-generation: continue text or chat with an LLM
- image-classification, automatic-speech-recognition and many more
Zero-Shot Classification
Zero-shot models let you invent labels without training anything. This is handy for quick prototypes such as routing support tickets:
from transformers import pipeline
zs = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
result = zs(
"My invoice shows the wrong amount for March.",
candidate_labels=["billing", "technical issue", "account access"],
)
print(result["labels"][0], round(result["scores"][0], 3))billing 0.942 (sample output)
Named Entity Recognition
from transformers import pipeline
ner = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
for ent in ner("Naeem moved from Lahore to Dubai to work at Emirates."):
print(ent["entity_group"], ent["word"])PER Naeem LOC Lahore LOC Dubai ORG Emirates
aggregation_strategy="simple" merges word pieces back into whole words, so you get "Lahore" instead of separate sub-word tokens.
Running a Pipeline on a GPU
Use the device argument: "cuda" for NVIDIA, "mps" for Apple Silicon, or "cpu". For large models, device_map="auto" (which needs accelerate) spreads the weights over all available devices.
pipe = pipeline("summarization", model="facebook/bart-large-cnn", device="cuda")
text = "Hugging Face hosts models, datasets and demo apps. " * 10
print(pipe(text, max_length=40, min_length=10)[0]["summary_text"])Hugging Face hosts models, datasets and demo apps . ... (sample output)
Frequently asked questions
What is a pipeline in Hugging Face?
A high-level helper in transformers that bundles a tokenizer, a model and post-processing for a specific task, so you can get predictions with a single call.
How do I make a Hugging Face pipeline use the GPU?
Pass device="cuda" (or device=0) when creating the pipeline, or device_map="auto" for large models with accelerate installed.
Which model does a pipeline use by default?
Each task has a default checkpoint, but it can change between versions and a warning is printed. Pass model= explicitly for reproducible results.