Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Pipelines and Inference

Pipelines for Text Tasks

Use text pipelines for classification, zero-shot labels, NER, question answering, fill-mask, summarization and translation, and learn to read their outputs.

Pipelines cover a large share of everyday NLP needs. This lesson tours the text tasks, shows the real call and the shape of each result, and gives runnable stand-ins for the post-processing logic that is easy to get wrong: turning raw model output into entities, answer spans and masked-word candidates.

All real calls below are read-only (they download models); output structure is described in words.

Text classification and zero-shot

Python
from transformers import pipeline

clf = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
clf("Delivery was late but support fixed it quickly.")           # [{'label': ..., 'score': ...}]
clf("Great phone", top_k=None)                                    # scores for every label

top_k=None returns all labels with scores, which is useful when you set your own thresholds.

Zero-shot classification lets you choose labels at run time without training. Under the hood an entailment model asks, for each label, whether the text "implies" a hypothesis such as "This text is about billing."

Python
zs = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
zs("My invoice shows a double charge.",
   candidate_labels=["billing", "shipping", "login problem"])
# a dict with 'sequence', 'labels' (sorted by score) and 'scores'

It is slow (one forward pass per label) but great for prototypes and labelling data.

Token classification (NER)

Python
ner = pipeline("token-classification", aggregation_strategy="simple")
ner("Ada Lovelace worked with Charles Babbage in London.")
# a list of dicts: entity_group, score, word, start, end

The model labels every token with a tag in the BIO scheme: B-PER begins a person, I-PER continues it, O means outside any entity. aggregation_strategy="simple" merges tokens into whole entities. Without it you get one result per subword piece, which is rarely what you want.

Python Stand-in: decode BIO tags into entities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is a stand-in for what aggregation_strategy does. The real pipeline also averages scores and reports character offsets.

Question answering

Python
qa = pipeline("question-answering")
qa(question="Where does Ana work?", context="Ana works at a robotics lab in Lisbon.")
# {'score': ..., 'start': ..., 'end': ..., 'answer': ...}

Extractive QA does not write an answer; it picks a span of the context. The model produces a start score and an end score for every token, and the pipeline chooses the best valid pair (start before end, limited length).

Python Stand-in: choose the best answer span

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If the true answer is not in the context, an extractive model still returns something. Check the score and compare it with a threshold, or use a model trained with unanswerable questions.

Fill-mask

Python
fm = pipeline("fill-mask", model="distilbert-base-uncased")
fm("Paris is the capital of [MASK].", top_k=3)

Each candidate is a dict with token_str, score and the completed sequence. Use the exact mask token of your model (tokenizer.mask_token): [MASK] for BERT, <mask> for RoBERTa.

Summarization and translation

Python
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
summarizer(long_text, max_length=80, min_length=30, do_sample=False)

translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
translator("The meeting starts at nine.")

Both are sequence-to-sequence tasks: the decoder generates text token by token, so the generation settings in generation and decoding apply. Inputs longer than the model's limit must be split into chunks and summarised piece by piece.

Text generation and feature extraction

Python
gen = pipeline("text-generation", model="gpt2")
gen("Once upon a time", max_new_tokens=30, do_sample=True, temperature=0.8)

extractor = pipeline("feature-extraction", model="sentence-transformers/all-MiniLM-L6-v2")
vectors = extractor(["a cat", "a kitten"])

Feature extraction returns hidden states. To compare two texts, average the token vectors and take the cosine similarity; sentence-embedding models are trained to make that comparison meaningful.

Python Stand-in: cosine similarity of embeddings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The vectors are invented for the demo (a stand-in); real embeddings have hundreds of dimensions.

Choosing the right pipeline

flowchart TD
  A["What do you need?"] --> B["One label per text"]
  A --> C["Find names or dates"]
  A --> D["Answer from a passage"]
  A --> E["New text"]
  B --> B1["text-classification or zero-shot"]
  C --> C1["token-classification"]
  D --> D1["question-answering"]
  E --> E1["summarization, translation, text-generation"]

Match the output you want to the pipeline task.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does aggregation_strategy='simple' do in a NER pipeline?

    Without it you receive one prediction per subword piece.
  2. 2. What does the B- in B-PER mean?

    BIO tagging marks Beginning, Inside and Outside.
  3. 3. Extractive question answering returns:

    The model scores start and end positions in the context.
  4. 4. When is zero-shot classification a good choice?

    It needs no training but is slower; with lots of labelled data a fine-tuned classifier is better.

Summary

  • Text pipelines cover classification, zero-shot, NER, QA, fill-mask, summarization, translation, generation and embeddings.
  • NER output comes from BIO tags merged into entities; QA output is a span chosen from start and end scores.
  • Check scores and use thresholds; pin models; mind the model's token limit.

Next: Pipelines for Vision and Audio