The shortest thing that works
from transformers import pipeline
clf = pipeline("sentiment-analysis")
print(clf("The delivery was late but the food was excellent."))
# [{'label': 'POSITIVE', 'score': 0.9993}]Two lines, no GPU, no configuration. This works on a laptop and on the free tier of Colab, and it is the right place to start because it lets you check that your environment is sane before you take on anything harder.
It also prints a warning that almost everybody scrolls past:
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english
That sentence is doing a lot of work. You did not choose a model; the library chose one for you. It is a 67-million-parameter English-only classifier fine-tuned on movie reviews from 2013. It is fast and it is decent on product feedback. It will be wrong in ways you cannot predict on Hinglish, on sarcasm, on medical notes, and on anything longer than 512 tokens, which it silently truncates.
The first real skill is naming the model yourself:
clf = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
)What the one line is actually doing
pipeline assembles five steps and hides them:
- Resolve: read the repository’s
config.jsonto find the architecture, and pick the right classes. - Tokenize: turn your string into integers, add the special tokens the model expects, build an attention mask.
- Forward pass: run the tensors through the network and get raw scores out.
- Post-process: softmax the scores, map the winning index through
id2label, round. - Format: return a list of dictionaries.
Every one of those steps is a place a later lesson will take apart. For now the point is that score: 0.9993 is not a probability of being right. It is a softmax over two numbers. A model can be catastrophically wrong at 0.9993 and there is nothing in the pipeline output to tell you so.
The task names, and why they matter
The first argument is a task from a fixed list, and it must match the model’s pipeline_tag:
text-classification(aliasedsentiment-analysis)token-classification(aliasedner)question-answering: extractive, pulls a span out of a passage you supplysummarization,translation,text2text-generationtext-generation: the chat and completion modelsfeature-extraction: embeddingsautomatic-speech-recognition,audio-classificationimage-classification,object-detection,image-segmentationzero-shot-classification: you pass candidate labels at call time
zero-shot-classification deserves a look early, because it solves a real problem with no training:
z = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
z("My parcel has not arrived and nobody replies.",
candidate_labels=["delivery", "refund", "product quality", "account"])It is slower than a fine-tuned classifier and usually a few points worse, and it needs no labelled data at all, which is often the trade you want in week one.
Making it run on what you have
import torch
pipe = pipeline(
"text-generation",
model="Qwen/Qwen2.5-0.5B-Instruct",
device_map="auto", # GPU if present, CPU if not
dtype=torch.bfloat16, # half the memory of float32
)device=-1 forces CPU and device=0 forces the first GPU; device_map="auto" is the modern form and handles multiple devices. The dtype argument was called torch_dtype until 2025 and you will see both spellings in tutorials.
On a CPU, bfloat16 is often slower than float32, because many CPUs have no native half-precision arithmetic and the runtime converts back and forth. This is the opposite of the GPU advice, and it surprises people who copied a Colab notebook onto a laptop.
Where the pipeline stops being the right tool
Use it for exploration, demos, one-off scripts and Spaces. Move past it when you need:
- Batching that you control, because the pipeline’s batching heuristics are conservative.
- The raw scores, because you want a threshold rather than an argmax.
- A shared model across several calls, since re-creating a pipeline reloads weights.
- Anything unusual: a custom head, an intermediate layer, a loss.
The next lesson opens the box.
Do this now
Run the two-line sentiment example, then run it again on a sentence in a language other than English, and on a sentence with sarcasm in it. Print the score both times. The default model does not know it is out of its depth, and neither does its confidence number.
Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.
Check your understanding
A team ships pipeline("sentiment-analysis") with no model argument, and it performs well in testing on English product reviews but poorly on their real Hinglish support messages. What is the mechanism?
Pick the one you would defend. Your answer stays private.
Stuck on something?Ask Arjun, your AI tutor
