Hugging Face Hub

Integration with the Hugging Face Hub to share and load models

Why share to the Hugging Face Hub

The Hub is a central platform where anyone can share and explore models, datasets, and ML demos. It aims to build the most extensive collection of Open Source models, datasets, and demos.

Sharing to the Hub could amplify the impact of a fastai Learner by making it available for others to download and explore.

Anyone can access all the fastai models in the Hub by filtering the huggingface.co/models webpage by the fastai library, as in the image below.

hf_hub_fastai

The Hub has built-in version control based on git (git-lfs, for large files), discussions, pull requests, and model cards for discoverability and reproducibility. For more information on navigating the Hub, see this introduction.

Installation

Install huggingface_hub. Additionally, the integration functions require the following packages:

  • toml,
  • fastai>=2.4,
  • fastcore>=1.3.27

You can install these packages manually or specify ["fastai"] when installing huggingface_hub, and your environment will be ready:

pip install huggingface_hub["fastai"]

To share models in the Hub, you will need to have a user. Create it on the Hugging Face website.

Sharing a Learner to the Hub

First, log in to the Hugging Face Hub. You will need to create a write token in your Account Settings. Then there are three options to log in:

  1. Type huggingface-cli login in your terminal and enter your token.

  2. If in a python notebook, you can use notebook_login.

from huggingface_hub import notebook_login

notebook_login()
  1. Use the token argument of the push_to_hub_fastai function.

Input push_to_hub_fastai with the Learner you want to upload and the repository id for the Hub in the format of “namespace/repo_name”. The namespace can be an individual account or an organization you have write access to (for example, ‘fastai/stanza-de’). For more details, refer to the Hub Client documentation.

from huggingface_hub import push_to_hub_fastai

# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
repo_id = "espejelomar/identify-my-cat"

push_to_hub_fastai(learner=learn, repo_id=repo_id)

The Learner is now in the Hub in the repo named espejelomar/identify-my-cat. An automatic model card is created with some links and next steps. When uploading a fastai Learner (or any other model) to the Hub, it is helpful to edit its model card (image below) so that others better understand your work (refer to the Hugging Face documentation).

hf_model_card

push_to_hub_fastai has additional arguments that could be of interest; refer to the Hub Client Documentation. The model is a Git repository with all the advantages that this entails: version control, commits, branches, discussions and pull requests.

Loading a Learner from Hub

Load the Learner we just shared in the Hub.

from huggingface_hub import from_pretrained_fastai

# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
repo_id = "espejelomar/identify-my-cat"

learner = from_pretrained_fastai(repo_id)

The Hub Client documentation includes addtional details on from_pretrained_fastai.