Download Files From Hugging Face using Python

Download Files From Hugging Face using Python

Hugging Face is a platform for sharing machine learning models, model weights, datasets, etc. The models have been trained on large amounts of data and fine-tuned to solve various problems. By downloading these pre-trained models, developers can save a significant amount of time and resources that would otherwise be required to train models from scratch. This tutorial explains how to download files from Hugging Face using Python.

Prepare environment

  • Install the following package using pip:
pip install huggingface

Code

In the following code, we use the hf_hub_download function to download a specific file from a Hugging Face repository and save it in the local directory.

from huggingface_hub import hf_hub_download

hf_hub_download(
    repo_id='google/mobilenet_v2_1.0_224',
    filename='pytorch_model.bin',
    local_dir='models/mobilenet_v2_1.0_224',
    local_dir_use_symlinks=False
)

Here is an explanation of the parameters used in the function:

ArgumentDescription
repo_idSpecifies the repository ID in the Hugging Face from which we want to download the file (e.g. google/mobilenet_v2_1.0_224).
filenameSpecifies the name of the file we want to download (e.g. pytorch_model.bin).
local_dirSpecifies the local directory where we want to save the downloaded file.
local_dir_use_symlinksSpecifies how the file must be saved in your local directory. If set to False, the file will be downloaded directly to the specified directory without using symbolic links to cache directory.

Leave a Comment

Cancel reply

Your email address will not be published.