Complete Hugging Face Transformers Tutorial: Pretrained Models for NLP and Vision
Hugging Face Transformers is the most popular library for state-of-the-art pretrained models. With thousands of models available, you can easily use BERT, GPT, T5, and more for text classification, generation, translation, and computer vision tasks.
Why Hugging Face Transformers?
Transformers Advantages:- Thousands of models: Access to 200,000+ pretrained models
- Multiple frameworks: PyTorch, TensorFlow, JAX support
- Easy to use: Simple pipelines API
- Production ready: Optimized inference
- Active community: Regular updates and new models
- Text classification and NER
- Question answering
- Text generation (GPT, LLaMA)
- Translation
- Image classification and object detection
Installation
pip install transformers
With PyTorch
pip install transformers torch
With TensorFlow
pip install transformers tensorflow
With all extras
pip install transformers[torch,sentencepiece,tokenizers]
Verify
python -c "import transformers; print(transformers.version)"
Quick Start with Pipelines
1. Text Classification
from transformers import pipeline
Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this product! It's amazing!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
Multiple texts
results = classifier([
"This is great!",
"This is terrible!",
"It's okay, nothing special."
])
Custom model
classifier = pipeline(
"sentiment-analysis",
model="nlptown/bert-base-multilingual-uncased-sentiment"
)
2. Named Entity Recognition
from transformers import pipeline
ner = pipeline("ner", groupedentities=True)
result = ner("My name is John and I work at Google in New York.")
print(result)
[{'entitygroup': 'PER', 'word': 'John', ...},
{'entitygroup': 'ORG', 'word': 'Google', ...},
{'entitygroup': 'LOC', 'word': 'New York', ...}]
3. Question Answering
from transformers import pipeline
qa = pipeline("question-answering")
context = """
Hugging Face is a company that develops tools for machine learning.
They are best known for the Transformers library and the Hugging Face Hub.
The company was founded in 2016.
"""
result = qa(
question="When was Hugging Face founded?",
context=context
)
print(result) # {'answer': '2016', 'score': 0.98, ...}
4. Text Generation
from transformers import pipeline
GPT-2
generator = pipeline("text-generation", model="gpt2")
result = generator(
"The future of AI is",
maxlength=50,
numreturnsequences=3
)
With better model
generator = pipeline("text-generation", model="meta-llama/Llama-2-7b-hf")
result = generator(
"Explain machine learning:",
maxnewtokens=100,
temperature=0.7
)
5. Translation
from transformers import pipeline
English to French
translator = pipeline("translationentofr")
result = translator("Hello, how are you?")
print(result) # [{'translationtext': 'Bonjour, comment allez-vous?'}]
Multi-language
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-id")
result = translator("Machine learning is fascinating.")
6. Summarization
from transformers import pipeline
summarizer = pipeline("summarization")
article = """
Long article text here...
"""
summary = summarizer(
article,
maxlength=100,
minlength=30,
dosample=False
)
print(summary[0]['summarytext'])