Choosing pipeline tasks

Hugging Face Transformers · Essentials · 10 min

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 as facebook/bart-large-mnli. Pass candidate_labels=[...]; set multi_label=True when several labels can apply.
  • token-classification (alias ner): finds entities. Use aggregation_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-text for vision-language models, automatic-speech-recognition with 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"])   # capital

Remember

  • 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.

Check yourself

  1. You need to tag support tickets with categories that change every week, with no training data. Which task fits?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: huggingface.co/docs/transformers/main_classes/pipelines

Builds on

Unlocks

Nothing builds on this yet.