December 22, 2024

How to Correctly and Quickly Download Files, Folders, and Complete Repositories from Hugging Face Using huggingface_hub library

In this tutorial, we explain how to correctly and quickly download files, folders, and complete repositories from the Hugging Face website to folders on your (local) computer. In this tutorial, we will use the huggingface_hub library to download the files. The YouTube tutorial accompanying this webpage tutorial is given below.

The standard procedure is:

  1. Find the model/repository ID on Hugging Face website: https://huggingface.co/
  2. Create a folder on your computer.
  3. Create and activate a Python virtual environment in the new folder.
  4. Install huggingface_hub using pip
  5. Create and run a Python script that will download the repository from the Hugging face

Find the model/repository ID on Hugging Face website: https://huggingface.co/

Go to the webpage https://huggingface.co/ and search for models:

In this tutorial, we will use an example of the model called Florence-2-large. Search for this model, and you will see this page:

Then copy the model ID  by clicking on the copy button as shown in the image above.

The model ID is:

microsoft/Florence-2-large

2. Create a folder on your computer.

Open a terminal, and create two folders:

cd ~
mkdir codes
cd codes
mkdir testDownload
cd testDownload

3. Create and activate a Python virtual environment in the new folder

Let us create and activate Python virtual environment

python3 -m venv test1
source test1/bin/activate

4. Install huggingface_hub using pip

Install huggingface-hub library that is necessary to download the online repositories

pip install huggingface-hub

5. Create and run a Python script that will download the repository from the Hugging face

Start VS Code, by typing

code .

And write this code

from huggingface_hub import snapshot_download

snapshot_download(repo_id="microsoft/Florence-2-large",
                  local_dir="/home/aleksandar/codes/testDownloads")

Here, repo_id=”microsoft/Florence-2-large”,  is the repository ID that we copied from the Hugging Face website. Then, local_dir=”/home/aleksandar/codes/testDownloads”, is the absolute path of the folder in which we we will download the remote repository.

Save this file and execute it. This fill will download the online the Hugging Face repository.