Hugging Face went from "a chatbot startup" to "the GitHub of AI" in five years. In 2026 it's where you find pretrained models, fine-tune with three lines of Trainer config, ship a demo with one Gradio script, and host it publicly with a git push to a Space. This is the practical tour — every major library at a depth where you can read other people's code and write your own. (Full version on the original — link at the end.)
git-versioned. Free for public; paid for private + serverless inference.transformers`` AutoModel + AutoTokenizer + pipeline() are the three you'll use most.datasets`` tokenizers``use_fast=True. ~20x faster. Trainer``peft`` gradio
Models, datasets, and Spaces are each a Git repository with extra metadata — you clone, push, version, and tag them like GitHub repos. Authenticate once and forget it:
pip install -U huggingface_hub
huggingface-cli login # paste a token; it persists in ~/.cache/huggingface/token
Every subsequent from_pretrained(...), load_dataset(...), and push_to_hub(...) uses it automatically. One rule before you train: read the model card. Licenses range from Apache 2.0 ("do whatever") to research-only. Get it wrong and you can't ship.
Three classes cover the 80% case: AutoTokenizer, AutoModelFor... (CausalLM, SequenceClassification, QuestionAnswering...), and pipeline() for one-line inference.
from transformers import pipeline
gen = pipeline("text-generation", model="Qwen/Qwen2.5-1.5B-Instruct", device_map="auto")
sent = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
zs = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
zs("The new iPhone has a better camera", candidate_labels=["technology", "sports", "politics"])
The longer form gives manual control, including chat templates:
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
tok = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
messages = [{"role": "user", "content": "Explain backpropagation in 50 words."}]
inputs = tok.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(model.device)
out = model.generate(inputs, max_new_tokens=120, do_sample=True, temperature=0.7, top_p=0.9)
print(tok.decode(out[0][inputs.shape[1]:], skip_special_tokens=True))
device_map="auto" splits the model across GPUs and offloads to CPU when VRAM is tight. (Check model.hf_device_map — it doesn't always do what you want.)
use_fast=True (the default) is ~20x faster and unlocks offset_mapping, which is essential for NER tagging and QA span extraction:
out = tok("Hello, world!", return_offsets_mapping=True)
Memory-mapped via Apache Arrow, so a 200 GB dataset fits in "memory" without living in RAM. Load from the Hub or local files, stream what won't fit on disk, and transform with .map():
from datasets import load_dataset
ds = load_dataset("openai/gsm8k", "main") # from the Hub
ds = load_dataset("json", data_files="train.jsonl", split="train") # local
ds = load_dataset("HuggingFaceFW/fineweb-edu", split="train", streaming=True) # TB-scale
def tokenize(batch): return tok(batch["question"], truncation=True, max_length=512)
ds_tok = ds["train"].map(tokenize, batched=True, remove_columns=["question", "answer"])
batched=True is the most important flag — often 10-50x faster. ds.push_to_hub(...) versions and shares it.
For 90% of fine-tuning, Trainer is faster and less buggy than a hand-rolled loop. ~30 lines gets you gradient accumulation, mixed precision, periodic eval + saving, multi-GPU, logging, and resumable training:
from transformers import (AutoModelForSequenceClassification, TrainingArguments,
Trainer, DataCollatorWithPadding)
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=2)
args = TrainingArguments(
output_dir="sst2-distilbert", num_train_epochs=3,
per_device_train_batch_size=32, learning_rate=2e-5, warmup_ratio=0.06,
weight_decay=0.01, eval_strategy="epoch", save_strategy="epoch",
load_best_model_at_end=True, metric_for_best_model="accuracy", bf16=True)
trainer = Trainer(model=model, args=args,
train_dataset=ds_tok["train"], eval_dataset=ds_tok["validation"],
tokenizer=tok, data_collator=DataCollatorWithPadding(tok),
compute_metrics=compute_metrics)
trainer.train()
trainer.push_to_hub("your-username/sst2-distilbert") # auto-generated model card
In 2026, tutorials without LoRA are outdated. Train ~1% of parameters; the adapter is ~50 MB for a 7B base:
from peft import LoraConfig, get_peft_model, TaskType
lora = LoraConfig(r=16, lora_alpha=32,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
lora_dropout=0.05, bias="none", task_type=TaskType.CAUSAL_LM)
model = get_peft_model(model, lora)
model.print_trainable_parameters() # trainable%: ~1.2%
from transformers import BitsAndBytesConfig
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype="bfloat16", bnb_4bit_use_double_quant=True)
Merge for deployment with model.merge_and_unload(). For preference tuning, trl gives you SFTTrainer (the modern default for instruction fine-tuning — handles chat templates and prompt masking) and DPOTrainer. For multi-GPU, accelerate (which Trainer uses under the hood) scales a loop with accelerate launch train.py.
import gradio as gr
from transformers import pipeline
clf = pipeline("sentiment-analysis")
def classify(text):
r = clf(text)[0]
return f"{r['label']} ({r['score']:.2%})"
gr.Interface(fn=classify, inputs=gr.Textbox(lines=3), outputs=gr.Textbox()).launch()
gr.Blocks gives richer layouts, and gr.ChatInterface + a generator function is a full streaming chat app in ~30 lines.
A Space is a Git repo with app.py, requirements.txt, and a README.md whose YAML frontmatter configures the SDK. Push it; HF builds a container and exposes it at https://huggingface.co/spaces/<you>/<name>. Free tier is 16 GB RAM / 2 vCPU; paid GPU is per-minute; ZeroGPU gives free serverless A100 for bursty demos:
import spaces
@spaces.GPU
def generate(prompt):
... # runs on a serverless A100
For production, use Inference Endpoints (create_inference_endpoint(...) — dedicated serving you don't operate); for prototyping, the free, rate-limited Serverless Inference API via InferenceClient.
The full article ships one example: fine-tune DistilBERT on Amazon Polarity with Trainer (push_to_hub=True), then a ~20-line Gradio app.py that loads your pushed model and deploys with:
huggingface-cli upload --repo-type=space your-username/amazon-sentiment-demo .
Public URL in 2 minutes. Total: ~45 min of work, ~25 min of waiting.
AutoTokenizer + AutoModel.
Hugging Face turned "set up Anaconda, find the model on GitHub, fight with CUDA, deploy on AWS, build a Flask UI" into "from_pretrained + Trainer + gradio + git push." Three habits make you fast: always start from a pretrained model (scratch is research; 2026 production ML is fine-tuning); use the highest abstraction that meets your need (pipeline() → Trainer → accelerate → raw PyTorch); and ship the demo on Spaces, even for internal tools — a URL stakeholders can click beats a thousand "send me the notebook" emails.
This is a condensed version. The full guide — with every library covered in depth, the complete end-to-end fine-tune+push+Space example, and the 2026 toolkit table — is on PrepStack: