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.
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:
Type huggingface-cli login in your terminal and enter your token.
If in a python notebook, you can use notebook_login.
from huggingface_hub import notebook_login
notebook_login()
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).