{"course":{"slug":"hugging-face","title":"Hugging Face, End to End","url":"https://addaly.com/learn/hugging-face","level":"intermediate","track":"create"},"lesson":{"slug":"your-first-pipeline","title":"One line that hides five steps","url":"https://addaly.com/learn/hugging-face/your-first-pipeline","position":10,"of":75,"minutes":8,"answer":"pipeline resolves, tokenizes, runs, post-processes and formats in one call.","keyIdea":"`pipeline` resolves, tokenizes, runs, post-processes and formats in one call, and when you do not name a model it silently picks a small English-only default whose confidence score is a softmax over two numbers rather than a probability of being right."},"body":{"format":"markdown","text":"## The shortest thing that works\n\n```python\nfrom transformers import pipeline\n\nclf = pipeline(\"sentiment-analysis\")\nprint(clf(\"The delivery was late but the food was excellent.\"))\n# [{'label': 'POSITIVE', 'score': 0.9993}]\n```\n\nTwo 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.\n\nIt also prints a warning that almost everybody scrolls past:\n\n> No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english\n\nThat 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.\n\nThe first real skill is naming the model yourself:\n\n```python\nclf = pipeline(\n    \"sentiment-analysis\",\n    model=\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\n)\n```\n\n## What the one line is actually doing\n\n`pipeline` assembles five steps and hides them:\n\n1. **Resolve**: read the repository's `config.json` to find the architecture, and pick the right classes.\n2. **Tokenize**: turn your string into integers, add the special tokens the model expects, build an attention mask.\n3. **Forward pass**: run the tensors through the network and get raw scores out.\n4. **Post-process**: softmax the scores, map the winning index through `id2label`, round.\n5. **Format**: return a list of dictionaries.\n\nEvery 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.\n\n## The task names, and why they matter\n\nThe first argument is a task from a fixed list, and it must match the model's `pipeline_tag`:\n\n- `text-classification` (aliased `sentiment-analysis`)\n- `token-classification` (aliased `ner`)\n- `question-answering`: extractive, pulls a span out of a passage you supply\n- `summarization`, `translation`, `text2text-generation`\n- `text-generation`: the chat and completion models\n- `feature-extraction`: embeddings\n- `automatic-speech-recognition`, `audio-classification`\n- `image-classification`, `object-detection`, `image-segmentation`\n- `zero-shot-classification`: you pass candidate labels at call time\n\n`zero-shot-classification` deserves a look early, because it solves a real problem with no training:\n\n```python\nz = pipeline(\"zero-shot-classification\", model=\"facebook/bart-large-mnli\")\nz(\"My parcel has not arrived and nobody replies.\",\n  candidate_labels=[\"delivery\", \"refund\", \"product quality\", \"account\"])\n```\n\nIt 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.\n\n## Making it run on what you have\n\n```python\nimport torch\n\npipe = pipeline(\n    \"text-generation\",\n    model=\"Qwen/Qwen2.5-0.5B-Instruct\",\n    device_map=\"auto\",          # GPU if present, CPU if not\n    dtype=torch.bfloat16,       # half the memory of float32\n)\n```\n\n`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.\n\nOn 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.\n\n## Where the pipeline stops being the right tool\n\nUse it for exploration, demos, one-off scripts and Spaces. Move past it when you need:\n\n- **Batching that you control**, because the pipeline's batching heuristics are conservative.\n- **The raw scores**, because you want a threshold rather than an argmax.\n- **A shared model across several calls**, since re-creating a pipeline reloads weights.\n- **Anything unusual**: a custom head, an intermediate layer, a loss.\n\nThe next lesson opens the box.\n\n## Do this now\n\nRun 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."},"check":{"question":"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?","options":[{"text":"The default model is a small English-only classifier, and nothing in the API signals that the input is outside what it was trained on.","correct":true},{"text":"The pipeline detects the input language and routes it to a multilingual model, which is weaker on short informal text than the English default would be.","correct":false,"tempting":"Invents language routing; the pipeline applies one fixed model to whatever it is given."},{"text":"Mixed-script input breaks the attention mask, so the tokenizer emits padding tokens that the classifier reads as neutral content and scores near 0.5.","correct":false,"tempting":"Reaches for a plausible-sounding low-level failure when the cause is simply a model applied outside its training distribution."},{"text":"Confidence scores are uncalibrated for any input, so the model is equally unreliable on English and the testing result was chance.","correct":false,"tempting":"Correctly notes that scores are not calibrated probabilities, but wrongly concludes the English result carried no information."}]},"prev":{"slug":"counters-and-trending","title":"What a download count is counting","url":"https://addaly.com/learn/hugging-face/counters-and-trending"},"next":{"slug":"the-auto-classes","title":"AutoModel gives you numbers, not answers","url":"https://addaly.com/learn/hugging-face/the-auto-classes"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Exam keys checked blind by a second model: 3,665 of 3,666 agreed. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'One line that hides five steps', in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/your-first-pipeline (Accessed: 13 October 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"One line that hides five steps","courseTitle":"Hugging Face, End to End","module":{"index":2,"slug":"transformers-from-text-to-text","title":"Transformers, from characters to output"},"answer":null,"keyIdea":"`pipeline` resolves, tokenizes, runs, post-processes and formats in one call, and when you do not name a model it silently picks a small English-only default whose confidence score is a softmax over two numbers rather than a probability of being right.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"the-shortest-thing-that-works","runs":[{"text":"The shortest thing that works"}]},{"t":"code","lang":"python","label":"Python","text":"from transformers import pipeline\n\nclf = pipeline(\"sentiment-analysis\")\nprint(clf(\"The delivery was late but the food was excellent.\"))\n# [{'label': 'POSITIVE', 'score': 0.9993}]"},{"t":"p","runs":[{"text":"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."}]},{"t":"p","runs":[{"text":"It also prints a warning that almost everybody scrolls past:"}]},{"t":"quote","runs":[{"text":"No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english"}]},{"t":"p","runs":[{"text":"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."}]},{"t":"p","runs":[{"text":"The first real skill is naming the model yourself:"}]},{"t":"code","lang":"python","label":"Python","text":"clf = pipeline(\n    \"sentiment-analysis\",\n    model=\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\n)"},{"t":"h2","id":"what-the-one-line-is-actually-doing","runs":[{"text":"What the one line is actually doing"}]},{"t":"p","runs":[{"text":"pipeline","code":true},{"text":" assembles five steps and hides them:"}]},{"t":"ol","start":1,"items":[[{"text":"Resolve","b":true},{"text":": read the repository’s "},{"text":"config.json","code":true},{"text":" to find the architecture, and pick the right classes."}],[{"text":"Tokenize","b":true},{"text":": turn your string into integers, add the special tokens the model expects, build an attention mask."}],[{"text":"Forward pass","b":true},{"text":": run the tensors through the network and get raw scores out."}],[{"text":"Post-process","b":true},{"text":": softmax the scores, map the winning index through "},{"text":"id2label","code":true},{"text":", round."}],[{"text":"Format","b":true},{"text":": return a list of dictionaries."}]]},{"t":"p","runs":[{"text":"Every one of those steps is a place a later lesson will take apart. For now the point is that "},{"text":"score: 0.9993","code":true},{"text":" 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."}]},{"t":"h2","id":"the-task-names-and-why-they-matter","runs":[{"text":"The task names, and why they matter"}]},{"t":"p","runs":[{"text":"The first argument is a task from a fixed list, and it must match the model’s "},{"text":"pipeline_tag","code":true},{"text":":"}]},{"t":"ul","items":[[{"text":"text-classification","code":true},{"text":" (aliased "},{"text":"sentiment-analysis","code":true},{"text":")"}],[{"text":"token-classification","code":true},{"text":" (aliased "},{"text":"ner","code":true},{"text":")"}],[{"text":"question-answering","code":true},{"text":": extractive, pulls a span out of a passage you supply"}],[{"text":"summarization","code":true},{"text":", "},{"text":"translation","code":true},{"text":", "},{"text":"text2text-generation","code":true}],[{"text":"text-generation","code":true},{"text":": the chat and completion models"}],[{"text":"feature-extraction","code":true},{"text":": embeddings"}],[{"text":"automatic-speech-recognition","code":true},{"text":", "},{"text":"audio-classification","code":true}],[{"text":"image-classification","code":true},{"text":", "},{"text":"object-detection","code":true},{"text":", "},{"text":"image-segmentation","code":true}],[{"text":"zero-shot-classification","code":true},{"text":": you pass candidate labels at call time"}]]},{"t":"p","runs":[{"text":"zero-shot-classification","code":true},{"text":" deserves a look early, because it solves a real problem with no training:"}]},{"t":"code","lang":"python","label":"Python","text":"z = pipeline(\"zero-shot-classification\", model=\"facebook/bart-large-mnli\")\nz(\"My parcel has not arrived and nobody replies.\",\n  candidate_labels=[\"delivery\", \"refund\", \"product quality\", \"account\"])"},{"t":"p","runs":[{"text":"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."}]},{"t":"h2","id":"making-it-run-on-what-you-have","runs":[{"text":"Making it run on what you have"}]},{"t":"code","lang":"python","label":"Python","text":"import torch\n\npipe = pipeline(\n    \"text-generation\",\n    model=\"Qwen/Qwen2.5-0.5B-Instruct\",\n    device_map=\"auto\",          # GPU if present, CPU if not\n    dtype=torch.bfloat16,       # half the memory of float32\n)"},{"t":"p","runs":[{"text":"device=-1","code":true},{"text":" forces CPU and "},{"text":"device=0","code":true},{"text":" forces the first GPU; "},{"text":"device_map=\"auto\"","code":true},{"text":" is the modern form and handles multiple devices. The "},{"text":"dtype","code":true},{"text":" argument was called "},{"text":"torch_dtype","code":true},{"text":" until 2025 and you will see both spellings in tutorials."}]},{"t":"p","runs":[{"text":"On a CPU, bfloat16 is often "},{"text":"slower","i":true},{"text":" 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."}]},{"t":"h2","id":"where-the-pipeline-stops-being-the-right-tool","runs":[{"text":"Where the pipeline stops being the right tool"}]},{"t":"p","runs":[{"text":"Use it for exploration, demos, one-off scripts and Spaces. Move past it when you need:"}]},{"t":"ul","items":[[{"text":"Batching that you control","b":true},{"text":", because the pipeline’s batching heuristics are conservative."}],[{"text":"The raw scores","b":true},{"text":", because you want a threshold rather than an argmax."}],[{"text":"A shared model across several calls","b":true},{"text":", since re-creating a pipeline reloads weights."}],[{"text":"Anything unusual","b":true},{"text":": a custom head, an intermediate layer, a loss."}]]},{"t":"p","runs":[{"text":"The next lesson opens the box."}]},{"t":"h2","id":"do-this-now","runs":[{"text":"Do this now"}],"role":"try"},{"t":"p","runs":[{"text":"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."}]}],"check":{"question":[{"text":"A team ships "},{"text":"pipeline(\"sentiment-analysis\")","code":true},{"text":" 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?"}],"options":[{"text":[{"text":"The default model is a small English-only classifier, and nothing in the API signals that the input is outside what it was trained on."}],"correct":true,"tempting":null},{"text":[{"text":"The pipeline detects the input language and routes it to a multilingual model, which is weaker on short informal text than the English default would be."}],"correct":false,"tempting":[{"text":"Invents language routing; the pipeline applies one fixed model to whatever it is given."}]},{"text":[{"text":"Mixed-script input breaks the attention mask, so the tokenizer emits padding tokens that the classifier reads as neutral content and scores near 0.5."}],"correct":false,"tempting":[{"text":"Reaches for a plausible-sounding low-level failure when the cause is simply a model applied outside its training distribution."}]},{"text":[{"text":"Confidence scores are uncalibrated for any input, so the model is equally unreliable on English and the testing result was chance."}],"correct":false,"tempting":[{"text":"Correctly notes that scores are not calibrated probabilities, but wrongly concludes the English result carried no information."}]}],"order":[3,2,1,0],"why":null},"prev":{"slug":"counters-and-trending","title":"What a download count is counting","minutes":8},"next":{"slug":"the-auto-classes","title":"AutoModel gives you numbers, not answers","minutes":9}}}