Monday , November 30 2026
Use Hugging Face transformers: pipelines, tokenizers, AutoModel, running open LLMs locally and datasets.

Hugging Face Transformers Tutorial for Beginners

Updated: 28 November 2026Level: IntermediateReading time: about 10 minutestransformers 4.x

Hugging Face is the main hub for open AI models and datasets. Its transformers library lets you download and run thousands of pre-trained models – for sentiment, translation, summarisation, embeddings and text generation – in a few lines of Python, on your own computer.

What You Will Learn

  • What the Hugging Face Hub offers
  • How to use pipelines for common NLP tasks
  • How tokenizers and models work together
  • How to run an open LLM locally
  • How to load datasets

Prerequisites

LLM Fundamentals.

Install

Install
pip install transformers torch datasets

1. Pipelines: the Easiest Way

Example
from transformers import pipeline

clf = pipeline("sentiment-analysis")
print(clf(["I loved this course!", "The videos were too long."]))
Output
[{'label': 'POSITIVE', 'score': 0.9998}, {'label': 'NEGATIVE', 'score': 0.9982}]

Explanation: pipeline downloads a default model the first time and caches it. Scores are the model’s confidence; exact values can differ slightly between versions.

Task name Does
"sentiment-analysis" Positive/negative
"summarization" Shortens text
"translation_en_to_fr" Translates
"ner" Finds names, places, organisations
"question-answering" Answers from a given context
"text-generation" Continues text / chat

2. Tokenizer + Model

Example
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

name = "distilbert-base-uncased-finetuned-sst-2-english"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name)

inputs = tok("This tutorial is very clear", return_tensors="pt")
print(inputs["input_ids"])
with torch.no_grad():
    logits = model(**inputs).logits
print(model.config.id2label[logits.argmax().item()])

Explanation: The tokenizer turns text into token IDs; the model turns IDs into scores (logits); the label with the highest score is the prediction. Pipelines do these steps for you.

3. Run an Open LLM Locally

Example
from transformers import pipeline

chat = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct")
messages = [{"role": "user", "content": "Give 3 tips for learning SQL."}]
print(chat(messages, max_new_tokens=120)[0]["generated_text"][-1]["content"])

Explanation: Small instruct models (0.5–3 billion parameters) run on a laptop CPU, slowly, or quickly on a GPU. Bigger models need more memory; see Fine-Tuning LLMs for quantization.

4. Datasets

Example
from datasets import load_dataset
ds = load_dataset("imdb", split="train[:1%]")
print(ds)
print(ds[0]["text"][:80], ds[0]["label"])

Choosing a Model on the Hub

  • Filter by task (e.g. Text Classification) and language (e.g. Hindi).
  • Check downloads, likes and the model card (training data, licence, limits).
  • Check the licence before commercial use.

Common Errors

Problem Cause and fix
Very slow first run The model is downloading; later runs use the cache.
CUDA out of memory Use a smaller model, torch_dtype="auto", or CPU.
Gated model error Accept the licence on the model page and log in with huggingface-cli login.

Series

LLM series: LLM Fundamentals · Transformer Architecture · OpenAI API · Hugging Face · LangChain · LlamaIndex · Vector Databases · LLM Evaluation

Interview Questions

Q: What does AutoModel do?
Loads the right model class automatically from the model’s configuration.

Q: Pipeline vs manual model?
Pipelines are quick for standard tasks; manual tokenizer + model gives full control.

Practice Exercises

  1. Summarise a 300-word article with the summarization pipeline.
  2. Run NER on a news sentence and list all organisations.
  3. Compare two sentiment models on 10 of your own sentences.

Summary

Hugging Face gives free access to thousands of open models. Pipelines solve common tasks in one line, while tokenizers and AutoModels give full control, including running open LLMs locally.

Next: LangChain · Fine-Tuning LLMs

About Machine Learning

Check Also

AI Agent Frameworks Compared: LangGraph vs CrewAI vs OpenAI Agents SDK

Compare AI agent frameworks in 2026: LangGraph, CrewAI, OpenAI Agents SDK, Claude Agent SDK, Google ADK and Microsoft Agent Framework, with code and how to choose.