Picking the right task and options often solves a problem without any training.
Beyond plain classification, pipelines cover many everyday tasks. A few with options worth knowing:
zero-shot-classification: classify text into labels you name at call time, using an NLI model such asfacebook/bart-large-mnli. Passcandidate_labels=[...]; setmulti_label=Truewhen several labels can apply.token-classification(aliasner): finds entities. Useaggregation_strategy="simple"to merge subword pieces into whole entities like"New York"instead of"New","##York".fill-mask: predicts a masked token; useful for probing what a model learned.feature-extraction: returns hidden states, a quick route to embeddings.text-generation: LLM completion or chat (covered in the generation module).- Vision and audio:
image-classification,object-detection,image-text-to-textfor vision-language models,automatic-speech-recognitionwith Whisper.
Each task returns its own result structure (lists of dicts with label/score, entity_group/start/end, sequence/token_str...). Read the task section in the pipelines reference to know what to expect.
Pick a model that was fine-tuned for the task: the Hub's task filters list compatible checkpoints. A general base model under a task pipeline will run but give meaningless results.
Example
from transformers import pipeline
zs = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
res = zs("The battery dies after two hours.",
candidate_labels=["battery", "screen", "price"])
print(res["labels"][0]) # battery
ner = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
for ent in ner("Ada Lovelace was born in London."):
print(ent["entity_group"], ent["word"])
# PER Ada Lovelace
# LOC London
fill = pipeline("fill-mask", model="google-bert/bert-base-uncased")
print(fill("Paris is the [MASK] of France.")[0]["token_str"]) # capitalRemember
- Zero-shot classification handles labels chosen at run time
- Use
aggregation_strategy="simple"to get whole entities from NER - Choose a checkpoint fine-tuned for the task you run
Watch out for
- Running NER without an aggregation strategy and then trying to stitch '##' subword pieces back together by hand.