Hugging Face Transformers Tutorial: Complete Guide to Using Pretrained Models
โก Quick Answer
Hugging Face Transformers tutorial โ load, fine-tune, and deploy pretrained models for text classification, generation, summarization, and translation with practical Python examples.
Get more content like this on Telegram!
Daily AI tips, notes & resources โ free
Advertisement
Hugging Face Transformers Tutorial: Complete Guide to Using Pretrained Models
Hugging Face Transformers is an open-source Python library that gives one consistent API to hundreds of thousands of pretrained models โ BERT, GPT, LLaMA, T5, and more. It replaces what used to mean implementing a model architecture from scratch and debugging tensor shapes by hand.
Sentiment analysis on a custom dataset is now 50 lines of Python. Fine-tuning BERT for text classification takes an afternoon, not a research sprint. This guide covers the patterns that show up in 90% of real projects.
Installation
Four packages cover almost every Transformers workflow: the core library, dataset handling, evaluation metrics, and multi-GPU training support.
pip install transformers datasets evaluate accelerate
pip install torch torchvision # Or tensorflow
pip install peft # For efficient fine-tuning
# Optional: GPU acceleration
pip install bitsandbytes # 4-bit/8-bit quantizationThe Pipeline API: One-Line Inference
pipeline() is the highest-level API in Transformers โ one function call runs inference for a given task with no manual tokenization or model configuration. It's the fastest way to check whether a pretrained model solves your problem before writing any custom code.
from transformers import pipeline
# Sentiment analysis
sentiment = pipeline("sentiment-analysis")
result = sentiment("I absolutely loved this product!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
# Batch processing
texts = [
"This is great!",
"Not what I expected.",
"Completely useless.",
]
results = sentiment(texts)
# Named entity recognition
ner = pipeline("ner", grouped_entities=True)
entities = ner("Apple CEO Tim Cook announced new products in San Francisco.")
for e in entities:
print(f"{e['entity_group']}: {e['word']} ({e['score']:.2f})")
# Question answering
qa = pipeline("question-answering")
context = "LangChain is a framework for building LLM applications. It was created in 2022."
answer = qa(question="When was LangChain created?", context=context)
print(f"Answer: {answer['answer']} (score: {answer['score']:.3f})")
# Text generation
generator = pipeline("text-generation", model="gpt2", max_length=100)
output = generator("The future of artificial intelligence is")
print(output[0]["generated_text"])
# Summarization
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
long_text = """[Your long article text here...]"""
summary = summarizer(long_text, max_length=130, min_length=30)
print(summary[0]["summary_text"])
# Translation
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
translated = translator("Hello, how are you doing today?")
print(translated[0]["translation_text"]) # "Bonjour, comment allez-vous aujourd'hui?"
# Zero-shot classification (no training needed)
classifier = pipeline("zero-shot-classification")
result = classifier(
"I need to cancel my subscription immediately.",
candidate_labels=["account management", "billing", "technical support", "complaint"]
)
print(result["labels"][0]) # Most likely labelAutoModel and AutoTokenizer
AutoModel and AutoTokenizer are the lower-level API โ you handle tokenization, the forward pass, and output processing yourself. Reach for these when pipeline() doesn't cover your task, or you need custom batching and post-processing for production.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Tokenization
text = "This movie was absolutely fantastic!"
inputs = tokenizer(
text,
return_tensors="pt", # PyTorch tensors
truncation=True, # Truncate to max_length
padding=True, # Pad to same length in batch
max_length=512
)
print(f"Input IDs shape: {inputs['input_ids'].shape}")
print(f"Token IDs: {inputs['input_ids'][0][:10].tolist()}")
# Forward pass
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probabilities = torch.softmax(logits, dim=1)
predicted_class = torch.argmax(logits, dim=1).item()
labels = model.config.id2label
print(f"Prediction: {labels[predicted_class]} ({probabilities[0][predicted_class]:.3f})")
# Batch processing efficiently
texts = ["I love this!", "I hate this.", "It's okay.", "Absolutely amazing!"]
inputs = tokenizer(
texts,
return_tensors="pt",
truncation=True,
padding=True,
max_length=128
)
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.softmax(outputs.logits, dim=1)
for text, pred in zip(texts, predictions):
label = labels[torch.argmax(pred).item()]
confidence = torch.max(pred).item()
print(f"[{label} {confidence:.2f}] {text}")Text Generation with LLaMA
AutoModelForCausalLM loads a generative model like LLaMA for next-token text generation, the same underlying mechanism behind chat assistants. The chat template call below formats your messages into whatever prompt structure that specific model was trained on.
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map="auto", # Distribute across available GPUs
)
# Format as instruction following (LLaMA 3.1 chat format)
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Write a Python function to find prime numbers up to n."}
]
# Apply chat template (handles special tokens automatically)
input_text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.3, # Lower = more deterministic for code
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
repetition_penalty=1.1 # Avoid repetitive output
)
# Decode only the generated tokens (not the prompt)
new_tokens = outputs[0][inputs['input_ids'].shape[-1]:]
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
print(response)Fine-Tuning for Classification
Trainer is Transformers' built-in training loop โ it handles batching, gradient updates, evaluation, and checkpointing so you don't hand-write a training loop. This example fine-tunes DistilBERT into a sentiment classifier from five labeled examples up to a saved, reusable model.
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification,
TrainingArguments, Trainer, DataCollatorWithPadding
)
from datasets import Dataset
import evaluate
import numpy as np
# Prepare your data
train_data = {
"text": ["Great product!", "Terrible experience", "Works as expected", "Love it!", "Waste of money"],
"label": [1, 0, 1, 1, 0] # 1 = positive, 0 = negative
}
eval_data = {
"text": ["Really enjoyed it", "Not satisfied"],
"label": [1, 0]
}
train_dataset = Dataset.from_dict(train_data)
eval_dataset = Dataset.from_dict(eval_data)
# Load model and tokenizer
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=2,
id2label={0: "NEGATIVE", 1: "POSITIVE"},
label2id={"NEGATIVE": 0, "POSITIVE": 1}
)
# Tokenize dataset
def tokenize_function(examples):
return tokenizer(
examples["text"],
truncation=True,
max_length=128
)
train_tokenized = train_dataset.map(tokenize_function, batched=True)
eval_tokenized = eval_dataset.map(tokenize_function, batched=True)
# Data collator (handles padding)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
# Metrics
accuracy = evaluate.load("accuracy")
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return accuracy.compute(predictions=predictions, references=labels)
# Training arguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
warmup_steps=100,
weight_decay=0.01,
learning_rate=2e-5,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
logging_dir="./logs",
)
# Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_tokenized,
eval_dataset=eval_tokenized,
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train()
trainer.save_model("./my-sentiment-model")
# Use fine-tuned model
fine_tuned = pipeline("sentiment-analysis", model="./my-sentiment-model")
print(fine_tuned("This is wonderful!"))Efficient Fine-Tuning with PEFT/LoRA
PEFT (Parameter-Efficient Fine-Tuning) trains a small adapter on top of a frozen base model instead of updating every weight. The example below trains just 0.3% of FLAN-T5-Large's parameters โ a model that would otherwise be too large to fine-tune on a single consumer GPU.
from peft import LoraConfig, get_peft_model, TaskType
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, TrainingArguments, Trainer
# Load a larger model that would normally be too big to fine-tune
model_name = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# Configure LoRA
lora_config = LoraConfig(
task_type=TaskType.SEQ_2_SEQ_LM,
r=16, # LoRA rank (lower = fewer parameters)
lora_alpha=32, # Scaling factor
lora_dropout=0.1,
target_modules=["q", "v"] # Which weight matrices to add LoRA to
)
# Wrap model with LoRA
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# Output: trainable params: 2,359,296 || all params: 783,150,080 (0.30%)
# Only 0.3% of parameters are trained!
# Now train as normal โ much smaller memory footprint
# ... (rest of training is identical to standard Trainer usage)
# Save LoRA adapter only (much smaller than full model)
model.save_pretrained("./lora-adapter")
# Later: load base model + LoRA adapter togetherPushing Models to Hugging Face Hub
The Hugging Face Hub is the model repository where trained models are versioned, shared, and pulled by name โ pushing your fine-tuned model there makes it loadable from any machine with one line of code.
from huggingface_hub import HfApi
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# Login (get token from huggingface.co/settings/tokens)
from huggingface_hub import notebook_login
notebook_login() # Or: huggingface-cli login in terminal
# Push model to Hub
model.push_to_hub("your-username/my-sentiment-model")
tokenizer.push_to_hub("your-username/my-sentiment-model")
# Pull your model anywhere
model = AutoModelForSequenceClassification.from_pretrained("your-username/my-sentiment-model")The Bottom Line
Transformers, Datasets, PEFT, Evaluate, and the Hub together form a complete ML platform, each piece handling one part of the workflow: pipeline() for trivial inference, AutoModel for full control, Trainer for the fine-tuning boilerplate, PEFT for fitting large models on consumer hardware.
The pattern that works in practice: pull a pretrained model from the Hub, fine-tune with LoRA on your domain data, evaluate with the Evaluate library, and deploy through the pipeline API or a TGI server.
For using Hugging Face models in RAG pipelines, see our RAG system tutorial. For understanding the transformer architecture these models are built on, see our transformer architecture guide.
Further Reading
- RAG System Tutorial: Build a Production Retrieval-Augmented Generation System
- Deploy AI Model to Production: FastAPI, Docker, and Cloud Deployment Guide
- Semantic Search Tutorial: Build Search That Understands Meaning, Not Just Keywords
- Streamlit Tutorial: Build and Deploy AI Apps with Python in Minutes
- Build a Personal AI Assistant: Complete Python Project with Memory and Tools
- Neural Networks Explained: From Perceptron to Deep Learning
- Multimodal AI Explained: How Models Process Text, Images, Audio, and Video
- AI Hallucination Explained: Why LLMs Make Things Up (and How to Fix It)
Advertisement
๐ฌ DiscussionPowered by GitHub Discussions
Frequently Asked Questions

Software Testing Expert & Prompt Engineering
Ensures every release is bug-free through rigorous testing, and crafts high-precision prompts that power our AI-driven workflows. Abdullah Al Arman Emon leads QA and prompt engineering across AiTechWorlds.
Not sure yet? Ask AI about this article
Get an instant, unbiased AI summary of โHugging Face Transformers Tutorial: Complete Guide to Using Pretrained Modelsโ.
Advertisement
Related Articles
AI API Cost Management: How to Cut LLM Costs by 80% Without Losing Quality
AI API cost management โ practical strategies to reduce OpenAI, Claude, and Gemini API costs by 80% using model selection, caching, RAG, prompt optimization, and batch processing.
Build an AI Chatbot with Python: Complete Guide from Scratch to Deployment
Build an AI chatbot with Python โ complete tutorial from OpenAI API integration to conversation memory, streaming responses, and deploying a production-ready chatbot application.
Build a Personal AI Assistant: Complete Python Project with Memory and Tools
Build a personal AI assistant in Python with persistent memory, web search, file access, and calendar integration โ a complete project from architecture to working prototype.
CrewAI Tutorial: Build Multi-Agent AI Systems That Work Together
CrewAI tutorial โ build multi-agent AI systems where specialized agents collaborate to complete complex tasks, with practical Python examples for research, coding, and content workflows.