Hugging Face cheatsheet
Hugging Face: Transformers
7 common Hugging Face tasks.
Install Transformers
Install the library, PyTorch and Accelerate for loading large models across devices.
pip install transformers torch accelerate
Classify text in two lines
Use a pipeline for a task with a default model.
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
classifier("I love this library!")
Generate text with a pipeline
Generate text with a specific model.
from transformers import pipeline
generator = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct")
generator("Once upon a time", max_new_tokens=50)
Load a model and tokenizer
Load a language model, placing it on the best available device.
from transformers import AutoTokenizer, AutoModelForCausalLM
name = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name, device_map="auto")
Generate a chat reply
Format messages with the model's chat template, generate, and decode only the new tokens.
messages = [{"role": "user", "content": "Give me three tips for learning Python."}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
Tokenize text
Convert text to token IDs as PyTorch tensors.
tokens = tokenizer("Hello world", return_tensors="pt")
print(tokens["input_ids"])
Share a model
Upload a fine-tuned model and tokenizer to your account. Log in first.
model.push_to_hub("my-user/my-model")
tokenizer.push_to_hub("my-user/my-model")