Working with Models from Hugging Face Hub

Working with Hugging Face

Jacob H. Marquez

Lead Data Engineer

Introduction to the Transformers Library

 

  • Simplifies working with pre-trained models

Transformers GitHub repo

1 https://github.com/huggingface/transformers
Working with Hugging Face

How Pre-Trained Models Are Used

  • Analyze text, images, and audio
  • Summarize documents and recognize speech
  • Enable autonomous driving by detecting objects

$$ Sentiment.jpg

Text, images, and audio

autonomous driving.jpg

Working with Hugging Face

Navigating the Hub

 

Hugging Face Hub top navigation

1 https://huggingface.co/
Working with Hugging Face

Searching for models

Hugging Face Model Hub section

1 https://huggingface.co/models
Working with Hugging Face

Searching for models

Hugging Face Model Hub highlighting the filter

1 https://huggingface.co/models
Working with Hugging Face

Searching for models

Hugging Face Model Hub highlighting the language filters

1 https://huggingface.co/models
Working with Hugging Face

Searching for models

Hugging Face Model Hub highlighting the libraries filter section

1 https://huggingface.co/models
Working with Hugging Face

Model cards

Model card for the Open AI Whisper model

1 https://huggingface.co/openai/whisper-large-v3
Working with Hugging Face

Running a Basic Pipeline

from transformers import pipeline


my_pipeline = pipeline( "text-classification", model="distilbert-base-uncased-finetuned-sst-2-english" )
print(my_pipeline("DataCamp is awesome!"))
[{'label': 'POSITIVE', 'score': 0.99}]
Working with Hugging Face

Adjusting Pipeline Parameters

from transformers import pipeline

my_pipeline = pipeline("text-generation", model="gpt2")

results = my_pipeline("What if AI", max_length=10, num_return_sequences=2)
for result in results: print(result['generated_text'])
What if AI had never existed?  
What if AI could be really smarter than us?
Working with Hugging Face

When and why to save models

$$

  • Save models locally for:
    • Offline access
    • Custom modifications
    • Large-scale deployments

$$

  • 💾 Check model size on the Hub before saving!

d.jpg

1 https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english/tree/main
Working with Hugging Face

Saving a model locally

my_pipeline = pipeline("text-classification", 
                        model="distilbert-base-uncased-finetuned-sst-2-english")

my_pipeline.save_pretrained("/path/to/saved_model_directory")
reloaded_pipeline = pipeline("text-classification", model="/path/to/saved_model_directory") print(reloaded_pipeline("Hugging Face is great!"))
[{'label': 'POSITIVE', 'score': 0.9998798370361328}]
Working with Hugging Face

Let's practice!

Working with Hugging Face

Preparing Video For Download...