What is Hugging Face?
Hugging Face is the leading platform for sharing and using AI models. Think of it as GitHub for machine learning - with over 500,000+ models, datasets, and tools.
Key Components:
- 🤗 Hub: Repository for models, datasets, and demos
- 🔧 Transformers: Library for using pre-trained models
- 🚀 Accelerate: Easy multi-GPU training
- 🎨 Spaces: Host ML demos with Gradio/Streamlit
- 📊 Datasets: Access 100,000+ datasets
🚀 Quick Start with Transformers
Installation
pip install transformers torch accelerate
Using Pre-trained Models
from transformers import pipeline
# Text generation
generator = pipeline("text-generation", model="gpt2")
output = generator("Once upon a time", max_length=50)
print(output[0]["generated_text"])
# Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this product!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
# Translation
translator = pipeline("translation_en_to_fr", model="t5-base")
result = translator("Hello, how are you?")
print(result) # [{'translation_text': 'Bonjour, comment allez-vous?'}]
# Question answering
qa = pipeline("question-answering")
context = "Hugging Face is creating tools for democratizing AI."
question = "What is Hugging Face doing?"
result = qa(question=question, context=context)
print(result["answer"]) # "democratizing AI"
🔍 Finding & Using Models
Browse the Hub
from huggingface_hub import list_models
# Search for models
models = list_models(filter="text-generation", sort="downloads", limit=5)
for model in models:
print(f"{model.modelId} - {model.downloads} downloads")
# Popular models:
# - meta-llama/Llama-2-7b-chat-hf (Chat model)
# - mistralai/Mistral-7B-Instruct-v0.2 (Instruction following)
# - stabilityai/stable-diffusion-xl-base-1.0 (Image generation)
# - openai/whisper-large-v3 (Speech recognition)
Load Any Model
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load model and tokenizer
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto" # Automatically uses GPU if available
)
# Generate text
inputs = tokenizer("What is AI?", return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_length=100)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
💾 Working with Datasets
from datasets import load_dataset
# Load popular datasets
dataset = load_dataset("squad") # Question answering
# dataset = load_dataset("imdb") # Sentiment analysis
# dataset = load_dataset("wikitext", "wikitext-103-v1") # Language modeling
print(dataset)
# DatasetDict({
# train: Dataset({features: ['id', 'title', 'context', 'question', 'answers']})
# validation: Dataset(...)
# })
# Access data
example = dataset["train"][0]
print(f"Question: {example['question']}")
print(f"Answer: {example['answers']['text'][0]}")
# Stream large datasets
dataset = load_dataset("c4", "en", streaming=True)
for example in dataset["train"].take(5):
print(example["text"][:100])
# Create your own dataset
from datasets import Dataset
data = {
"text": ["Example 1", "Example 2"],
"label": [0, 1]
}
dataset = Dataset.from_dict(data)
dataset.push_to_hub("username/my-dataset") # Share on Hub!
🎨 Creating Interactive Demos with Spaces
Gradio Interface
import gradio as gr
from transformers import pipeline
# Create a text generation demo
generator = pipeline("text-generation", model="gpt2")
def generate_text(prompt, max_length):
result = generator(prompt, max_length=max_length, num_return_sequences=1)
return result[0]["generated_text"]
# Build interface
demo = gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(label="Prompt", lines=3),
gr.Slider(minimum=50, maximum=200, value=100, label="Max Length")
],
outputs=gr.Textbox(label="Generated Text", lines=5),
title="GPT-2 Text Generator",
description="Generate text using GPT-2"
)
# Launch locally
demo.launch()
# Deploy to Hugging Face Spaces:
# 1. Create Space at huggingface.co/spaces
# 2. Push code with requirements.txt
# 3. Auto-deploys!
Streamlit Alternative
import streamlit as st
from transformers import pipeline
st.title("Sentiment Analysis")
text = st.text_area("Enter text to analyze")
if st.button("Analyze"):
classifier = pipeline("sentiment-analysis")
result = classifier(text)[0]
st.write(f"**Sentiment:** {result['label']}")
st.write(f"**Confidence:** {result['score']:.2%}")
🏋️ Training Models
Using Trainer API
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
from datasets import load_dataset
# Load data
dataset = load_dataset("imdb")
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
# Load model
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased",
num_labels=2
)
# Training arguments
training_args = TrainingArguments(
output_dir="./results",
evaluation_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
num_train_epochs=3,
weight_decay=0.01,
)
# Train
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"].shuffle(seed=42).select(range(1000)),
eval_dataset=tokenized_datasets["test"].shuffle(seed=42).select(range(500))
)
trainer.train()
# Save and share
model.save_pretrained("./my-sentiment-model")
model.push_to_hub("username/my-sentiment-model") # Share with world!
⚡ Advanced Features
Efficient Inference with 🤗 Accelerate
from transformers import AutoModelForCausalLM, AutoTokenizer
from accelerate import Accelerator
accelerator = Accelerator()
model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
# Automatically uses all available GPUs
model, tokenizer = accelerator.prepare(model, tokenizer)
# Generate on multiple GPUs
inputs = tokenizer("Hello", return_tensors="pt")
outputs = accelerator.unwrap_model(model).generate(**inputs)
8-bit Quantization
from transformers import AutoModelForCausalLM
# Load model in 8-bit (uses 50% less memory!)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
load_in_8bit=True,
device_map="auto"
)
# Works with minimal quality loss
# Great for running large models on consumer GPUs
Model Versioning
# Load specific version
model = AutoModelForCausalLM.from_pretrained(
"username/my-model",
revision="v2.0" # Git tag or commit hash
)
# Upload new version
model.push_to_hub(
"username/my-model",
commit_message="Improved performance",
create_pr=True # Create pull request for review
)
🔑 Authentication & Private Models
# Login to Hugging Face
from huggingface_hub import login
login(token="hf_...") # Get token from huggingface.co/settings/tokens
# Or use environment variable
import os
os.environ["HUGGING_FACE_HUB_TOKEN"] = "hf_..."
# Access private/gated models
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf", # Gated model
use_auth_token=True
)
# Upload private model
model.push_to_hub("username/private-model", private=True)
📊 Popular Models by Task
💬 Text Generation
- meta-llama/Llama-2-7b-chat-hf
- mistralai/Mistral-7B-Instruct-v0.2
- google/flan-t5-xxl
🖼️ Image Generation
- stabilityai/stable-diffusion-xl-base-1.0
- runwayml/stable-diffusion-v1-5
- CompVis/stable-diffusion-v1-4
🎤 Speech
- openai/whisper-large-v3
- facebook/wav2vec2-base-960h
- suno/bark (text-to-speech)
🔍 Embeddings
- sentence-transformers/all-MiniLM-L6-v2
- BAAI/bge-large-en-v1.5
- intfloat/e5-large-v2
🎯 Key Takeaways
- Hugging Face Hub is the central place for AI models and datasets
- Transformers library makes using any model easy with pipelines
- Spaces let you deploy demos with just a few clicks
- Free hosting for models, datasets, and apps
- Active community with 500k+ models to explore