Complete Hugging Face Transformers Tutorial: Modern NLP with Python

# Tutorial Lengkap Hugging Face Transformers: Pretrained Models untuk NLP dan Vision Hugging Face Transformers adalah library paling populer untuk state-of-the-art pretrained models. Dengan ribuan mo...

By Ruby Abdullah · · tutorial
Hugging FaceTransformersNLPBERTPythonDeep Learning

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

Use Cases:
  • 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'])

Working with Models and Tokenizers

1. Loading Models

from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification

Auto classes (recommended)

tokenizer = AutoTokenizer.frompretrained("bert-base-uncased")

model = AutoModel.frompretrained("bert-base-uncased")

Task-specific model

model = AutoModelForSequenceClassification.frompretrained(

"bert-base-uncased",

numlabels=2

)

From local path

model = AutoModel.frompretrained("./mymodel/")

With specific revision

model = AutoModel.frompretrained(

"bert-base-uncased",

revision="v1.0"

)

2. Tokenization

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.frompretrained("bert-base-uncased")

Basic tokenization

text = "Hello, how are you?"

tokens = tokenizer.tokenize(text)

print(tokens) # ['hello', ',', 'how', 'are', 'you', '?']

Encode to IDs

inputids = tokenizer.encode(text)

print(inputids) # [101, 7592, 1010, 2129, 2024, 2017, 1029, 102]

Full encoding

encoding = tokenizer(

text,

padding=True,

truncation=True,

maxlength=512,

returntensors="pt" # PyTorch tensors

)

print(encoding.keys()) # dictkeys(['inputids', 'attentionmask'])

Batch encoding

texts = ["Hello!", "How are you?", "I'm fine."]

encodings = tokenizer(

texts,

padding=True,

truncation=True,

returntensors="pt"

)

3. Inference

import torch

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.frompretrained("bert-base-uncased")

model = AutoModelForSequenceClassification.frompretrained(

"bert-base-uncased",

numlabels=2

)

Prepare input

text = "This movie is great!"

inputs = tokenizer(text, returntensors="pt")

Forward pass

with torch.nograd():

outputs = model(inputs)

logits = outputs.logits

predictions = torch.softmax(logits, dim=-1)

print(f"Positive: {predictions[0][1]:.4f}")

print(f"Negative: {predictions[0][0]:.4f}")

Fine-tuning Models

1. Using Trainer API

from transformers import (

AutoTokenizer,

AutoModelForSequenceClassification,

TrainingArguments,

Trainer

)

from datasets import loaddataset

import numpy as np

from sklearn.metrics import accuracyscore

Load data

dataset = loaddataset("imdb")

Load model and tokenizer

modelname = "bert-base-uncased"

tokenizer = AutoTokenizer.frompretrained(modelname)

model = AutoModelForSequenceClassification.frompretrained(

modelname,

numlabels=2

)

Tokenize data

def tokenizefunction(examples):

return tokenizer(

examples["text"],

padding="maxlength",

truncation=True,

maxlength=512

)

tokenizeddatasets = dataset.map(tokenizefunction, batched=True)

Training arguments

trainingargs = TrainingArguments(

outputdir="./results",

numtrainepochs=3,

perdevicetrainbatchsize=16,

perdeviceevalbatchsize=64,

warmupsteps=500,

weightdecay=0.01,

loggingdir="./logs",

loggingsteps=100,

evaluationstrategy="epoch",

savestrategy="epoch",

loadbestmodelatend=True,

)

Metrics

def computemetrics(evalpred):

logits, labels = evalpred

predictions = np.argmax(logits, axis=-1)

return {"accuracy": accuracyscore(labels, predictions)}

Trainer

trainer = Trainer(

model=model,

args=trainingargs,

traindataset=tokenizeddatasets["train"],

evaldataset=tokenizeddatasets["test"],

computemetrics=computemetrics,

)

Train

trainer.train()

Save

trainer.savemodel("./finetunedmodel")

2. Custom Training Loop

import torch

from torch.utils.data import DataLoader

from transformers import AutoTokenizer, AutoModelForSequenceClassification

from torch.optim import AdamW

from tqdm import tqdm

Setup

device = torch.device("cuda" if torch.cuda.isavailable() else "cpu")

model = AutoModelForSequenceClassification.frompretrained("bert-base-uncased", numlabels=2)

model.to(device)

optimizer = AdamW(model.parameters(), lr=2e-5)

Training loop

model.train()

for epoch in range(3):

for batch in tqdm(traindataloader):

batch = {k: v.to(device) for k, v in batch.items()}

outputs = model(batch)

loss = outputs.loss

loss.backward()

optimizer.step()

optimizer.zerograd()

print(f"Epoch {epoch + 1} completed")

Save

model.savepretrained("./mymodel")

tokenizer.savepretrained("./mymodel")

Vision Models

1. Image Classification

from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification

from PIL import Image

Using pipeline

classifier = pipeline("image-classification", model="google/vit-base-patch16-224")

image = Image.open("cat.jpg")

result = classifier(image)

print(result)

Manual inference

processor = AutoImageProcessor.frompretrained("google/vit-base-patch16-224")

model = AutoModelForImageClassification.frompretrained("google/vit-base-patch16-224")

inputs = processor(images=image, returntensors="pt")

outputs = model(inputs)

logits = outputs.logits

predictedclass = logits.argmax(-1).item()

print(model.config.id2label[predictedclass])

2. Object Detection

from transformers import pipeline

from PIL import Image

detector = pipeline("object-detection", model="facebook/detr-resnet-50")

image = Image.open("street.jpg")

results = detector(image)

for result in results:

print(f"{result['label']}: {result['score']:.2f}")

print(f" Box: {result['box']}")

3. Image Segmentation

from transformers import pipeline

from PIL import Image

segmenter = pipeline("image-segmentation", model="facebook/mask2former-swin-base-coco-panoptic")

image = Image.open("image.jpg")

results = segmenter(image)

for segment in results:

print(f"{segment['label']}: {segment['score']:.2f}")

Saving and Sharing Models

1. Save Locally

# Save model and tokenizer

model.savepretrained("./mymodel")

tokenizer.savepretrained("./mymodel")

Load later

model = AutoModel.frompretrained("./mymodel")

tokenizer = AutoTokenizer.frompretrained("./mymodel")

2. Push to Hub

from huggingfacehub import login

Login

login() # or use token

Push model

model.pushtohub("my-username/my-model")

tokenizer.pushtohub("my-username/my-model")

Or with trainer

trainer.pushtohub("my-username/my-model")

Optimization

1. Mixed Precision Training

from transformers import TrainingArguments

trainingargs = TrainingArguments(

outputdir="./results",

fp16=True, # Enable mixed precision

# bf16=True, # For newer GPUs

)

2. Gradient Checkpointing

model.gradientcheckpointingenable()

3. Model Quantization

from transformers import AutoModelForCausalLM

8-bit quantization

model = AutoModelForCausalLM.frompretrained(

"meta-llama/Llama-2-7b-hf",

loadin8bit=True,

devicemap="auto"

)

4-bit quantization

model = AutoModelForCausalLM.frompretrained(

"meta-llama/Llama-2-7b-hf",

loadin4bit=True,

devicemap="auto"

)

Best Practices

1. Use Auto Classes

# Good

from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.frompretrained("bert-base-uncased")

model = AutoModel.frompretrained("bert-base-uncased")

Avoid specific classes unless necessary

from transformers import BertTokenizer, BertModel

2. Handle Long Texts

# Truncation

tokenizer(text, truncation=True, maxlength=512)

Sliding window

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.frompretrained("bert-base-uncased")

def processlongtext(text, maxlength=512, stride=256):

tokens = tokenizer(

text,

maxlength=maxlength,

truncation=True,

stride=stride,

returnoverflowingtokens=True,

return_tensors="pt"

)

return tokens

Conclusion

Hugging Face Transformers is essential for modern NLP and CV with:

  • Easy pipelines: Quick inference with one line
  • Thousands of models: Access to state-of-the-art
  • Fine-tuning: Trainer API for custom tasks
  • Multi-modal: Text, vision, audio models
  • Production ready**: Optimization and deployment
  • Key takeaways:

    • Start with pipelines for quick prototyping
    • Use Auto classes for flexibility
    • Fine-tune with Trainer API
    • Optimize with quantization for deployment
    • Share models on Hugging Face Hub

    Related Articles

    PyTorch Geometric Tutorial: Graph Neural Networks in PyTorch

    Memulai dengan PyTorch Geometric: Graph Neural Network di PyTorch Sebagian besar machine learning bekerja pada grid (gam...

    BERTopic Tutorial: Modern Topic Modeling with Embeddings

    BERTopic: Pemodelan Topik Modern dengan Embedding BERTopic adalah library pemodelan topik yang menggabungkan embedding t...

    Sentence Transformers Tutorial: Embeddings, Similarity, and Rerankers

    Sentence Transformers: Embedding, Kemiripan Semantik, dan Reranker Sentence Transformers (sering disebut SBERT) adalah p...

    spaCy Tutorial: Industrial-Strength NLP in Python

    spaCy: NLP Kelas Industri di Python spaCy adalah pustaka open-source untuk pemrosesan bahasa alami (NLP) yang dirancang ...