Working with Hugging Face
What is Hugging Face?
Hugging Face is an open-source platform and community that provides state-of-the-art tools and models for Natural Language Processing (NLP) and other machine learning domains like computer vision, audio, and multimodal AI.
It’s best known for:
Why Use Hugging Face?
Key Libraries in Hugging Face
| Library | Purpose |
| transformers | Pretrained models for NLP, vision, audio |
| datasets | Access and prepare thousands of ML datasets |
| huggingface_hub | Upload/download models, datasets, and spaces |
| gradio | Build and share interactive demos (Spaces) |
| evaluate | Standardized ML evaluation metrics |
Getting Started with Hugging Face Transformers
Installation
| pip install transformers
pip install datasets |
Load a Pretrained Model
| from transformers import pipeline
# Create a text classification pipeline classifier = pipeline(“sentiment-analysis”)
# Use the model result = classifier(“I love learning with Hugging Face!”) print(result) |
Output
| [{‘label’: ‘POSITIVE’, ‘score’: 0.9998}] |
Load a Model & Tokenizer Manually
| from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = “distilbert-base-uncased-finetuned-sst-2-english”
tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) |
Explore Datasets
| from datasets import load_dataset
dataset = load_dataset(“imdb”) print(dataset[“train”][0]) |
Using Hugging Face Hub
Search for Models & Datasets: Visit https://huggingface.co/models
You can search by:
Upload Your Own Model
| huggingface-cli login |
Then:
| from huggingface_hub import HfApi
api = HfApi() api.create_repo(name=”my-model”) |
Common Use Cases
| Use Case | Model/Pipeline |
| Sentiment Analysis | pipeline(“sentiment-analysis”) |
| Text Summarization | pipeline(“summarization”) |
| Question Answering | pipeline(“question-answering”) |
| Translation | pipeline(“translation”) |
| Text Generation | pipeline(“text-generation”) |
Bonus: Hugging Face Spaces
Spaces lets you deploy and share ML-powered web apps using Gradio or Streamlit.
Try it: https://huggingface.co/spaces
Learning Outcomes
By the end of this module, you will:
Not a member yet? Register now
Are you a member? Login now