Get startedGet started for free

Working with Models from Hugging Face Hub

1. Working with Models from Hugging Face Hub

Welcome back! In this video, we’ll learn how to find, use, and save machine learning models from the Hugging Face Hub.

2. Introduction to the Transformers Library

Hugging Face developed the Transformers library to simplify working with pre-trained models. Let’s explore real-world examples of how these models can be used.

3. How Pre-Trained Models Are Used

Pre-trained models are powerful tools used for tasks involving text, images, and audio. For example, they can classify text to identify if a review is positive or negative, or detect objects in images. These models are also used for summarizing long documents, recognizing speech, and even enabling autonomous driving by detecting objects on the road.

4. Navigating the Hub

In the Hugging Face Hub, we can browse open-source models and datasets using the navigation bar. This allows us to quickly explore resources for choosing and calling the right models for our tasks.

5. Searching for models

The model tab lists all available models on Hugging Face. Keep in mind that some models may require special access tokens and payment. Luckily, most are open-source and free to use.

6. Searching for models

We can search for models using task filters, for example, a text classification model,

7. Searching for models

different languages

8. Searching for models

libraries, and more.

9. Model cards

Each model in the hub has a model card that includes key metadata such as a description, the model developer, the license it is available under, the supported languages and tasks, and more.

10. Running a Basic Pipeline

Pipelines in Hugging Face make it simple to perform tasks like text classification with minimal setup. Here’s how it works: First, we import the pipeline function from transformers. Then, we specify the task and model. In our example, the task is text-classification, and we’ve chosen a pre-trained DistilBERT model. Calling our pipeline on "DataCamp is awesome!" returns the label POSITIVE and a score of 0.99, meaning the model is highly confident that this comment is positive.

11. Adjusting Pipeline Parameters

Just like in text classification, pipelines can be customized to better suit our needs. In this example, we create a text generation pipeline. When calling the pipeline, we can set the max_length parameter to limit the output to 10 tokens. Tokens are smaller units of text, like words or characters, that language models process to generate results. The num_return_sequences parameter generates two variations of the result. The results are displayed separately, and keep in mind that running the code again will produce different outputs each time.

12. When and why to save models

When using pipelines, models are loaded temporarily through Hugging Face’s caching system, which is efficient for most tasks. Saving models locally is necessary in specific scenarios: when we need offline access; if we plan to customize or fine-tune the model; and for large-scale deployments requiring better storage control. Before saving, check the model size on the Hugging Face Hub in the Files and Versions tab. For example, the DistilBERT model from earlier examples is over 1GB, but larger models can weigh significantly more.

13. Saving a model locally

To save a model locally, use the save_pretrained() function to download it into a folder. We can later reload the model to use it again without redownloading. We covered navigating the Hugging Face Hub, using pipelines, and saving models locally.

14. Let's practice!

Now it’s your turn—time to practice!