fastai Integration with Hugging Face Hub

Hugging Face has integrated the fastai library into the Hugging Face Hub, enabling users to share, upload, and download fastai models with a single line of Python. This integration democratizes access to pre-trained models by allowing fastai practitioners to leverage the Hub's central platform for model hosting, version control, and community discovery.

Direct Hub Integration for fastai Learners

Fastai practitioners can now interact with the Hugging Face Hub using the huggingface_hub Python library. A Learner in fastai—which bundles a model, data loaders, and a loss function—can be pushed to or pulled from the Hub seamlessly.

Installation and Setup

To use these features, users require a Hugging Face account and the huggingface_hub library. The installation can be performed by specifying the "fastai" extra to ensure all necessary dependencies (including fastai>=2.4, fastcore>=1.3.27, and toml) are installed:

pip install huggingface_hub["fastai"]

Uploading Models to the Hub

Users can share a Learner object to the Hub using the push_to_hub_fastai function. This requires authentication via a write token, which can be handled through the huggingface-cli login command, the notebook_login() function in Python notebooks, or by passing the token directly to the function.

from huggingface_hub import push_to_hub_fastai

# repo_id follows the format "namespace/repo_name"
repo_id = "username/model-name"
push_to_hub_fastai(learner=learn, repo_id=repo_id)

Once uploaded, the Hub automatically creates a model card, which users are encouraged to edit for better reproducibility and discoverability.

Loading Models from the Hub

Loading a pre-trained Learner from the Hub is achieved using the from_pretrained_fastai function, allowing users to immediately perform predictions or use the model as a basis for transfer learning.

from huggingface_hub import from_pretrained_fastai

repo_id = "username/model-name"
learner = from_pretrained_fastai(repo_id)

Combining fastai and Transformers with Blurr

Beyond standard fastai models, the integration extends to Blurr, a library designed for fastai developers to train and deploy Hugging Face transformers.

By using the high-level Blurr API, developers can load a transformer model (such as distilbert-base-uncased) from the Hub, train it as a Blearner within the fastai ecosystem, and then use the same push_to_hub_fastai and from_pretrained_fastai functions to share and reload the model from the Hub.

Hub Features for fastai Models

Sharing fastai models on the Hub provides several technical advantages over local storage:

  • Version Control: Models are stored as Git repositories, providing full version control via git-lfs for large files, including commits and branches.
  • Discoverability: Users can filter the Hugging Face Model Hub specifically for the fastai library to find and explore existing models.
  • Reproducibility: The use of model cards allows practitioners to document the training process and model capabilities, ensuring others can reproduce the results.

Sources