1. Introduction

In this tutorial we demonstrate the methods to use to download an AI model from the Hugging Face platform. The Hugging Face platform is a useful resource for deep-learning researchers, programmers looking to leverage deep-learning models and AI enthusiasts looking to dip their toes in the deep-learning ocean.

There are many Large Language Models hosted there. This tutorial demonstrates how to access the models hosted by Hugging Face and describes multiple ways to download the available models.

2. What Can Be Done?

We cover five methods to download Hugging Face models. The methods range from programmatic access, to command line approaches and manual downloads. The general methods covered are listed:

Each method is slightly different and cover various needs. However, each method easily downloads the desired model files.

In the remaining sections, we cover each of these approaches in more depth and provide examples.

3. Using the Transformers Library

If working with Hugging Face Transformersdownload models easily using the from_pretrained() method:

from transformers import AutoModel, AutoTokenizer
model_name = "bert-base-uncased"
# Download the model
model = AutoModel.from_pretrained(model_name)
# Download the tokenizer (optional but recommended)
tokenizer = AutoTokenizer.from_pretrained(model_name)

In this example we will download a BERT transformer model. By default, models downloaded this way are stored in ~/.cache/huggingface/transformers/ . This is usually in the home directory. However, it is customizable.

The cache directory is specified programmatically via the cache_dir argument on all methods. Here we update the code to show where we use the cache_dir argument to specify the directory to be used by Hugging Face for caching as /usr/user_caches/hugging_face_example_cache:

from transformers import AutoModel, AutoTokenizer model_name = "bert-base-uncased" 
# Download the model 
model = AutoModel.from_pretrained(model_name,cache_dir="/usr/user_caches/hugging_face_example_cache")
# Download the tokenizer (optional but recommended)
tokenizer = AutoTokenizer.from_pretrained(model_name,cache_dir="/usr/user_caches/hugging_face_example_cache")

This revised code shows how easy it is to set the cache directory used when downloading models.

The directory is also able to set the directory at a global level. Simply define either HF_HOME or HF_HUB_CACHE environment variable to point to the desired location. After that models will download to the chosen directory.

4. Using the Hugging Face Library

If the raw model files are desired, without loading them in Python, that is also achievable:

from huggingface_hub import snapshot_download

# Download and save all files of a model
snapshot_download(repo_id="bert-base-uncased", cache_dir="./models")

This will store the model inside ./models/bert-base-uncased/.

5. Using the Hugging Face CLI

Hugging Face CLI provides a further set of tools to download and interact with the Hugging Face Model Hub via the command line.

This requires the Hugging Face CLI tool to be installed. Install it via:

pip install huggingface_hub

After that, launch the huggingface-cli tool which requires a login and API key:

huggingface-cli login

Once logged in downloading a model is easy and similar to how we have interacted with the Hugging Face platform already:

huggingface-cli download bert-base-uncased --cache-dir bert-base-uncased

The –cache-dir argument specifies where the model will download to. If it is unspecified the model will download to HF_HOME if it is defined as an environment variable, otherwise it will default to ~/. cache/huggingface .

6. Manual Download from the Hugging Face Website

The Hugging Face Model Hub provides listings for all the models they have available for download. If looking for something specific the interface can be browsed through a web browser. Models can be downloaded directly from there. All there is to do is follow the steps outlined below.

Steps involved:

  1. Go to Hugging Face Model Hub.
  2. Search for a model (e.g., bert-base-uncased).
  3. Click on the “Files and versions” tab.
  4. Download the .bin model weights and config.json manually.

Once downloaded the model can be loaded following the steps from the previous example:

from transformers import AutoModel

model = AutoModel.from_pretrained("/path/to/downloaded/files")

Loading the model is as easy as specifying where it is stored.

7. Using Git

Hugging face models are also accessible directly from their git repositories.

Sticking with the bert-base-uncased model as an example. The model repository is cloned like so:

git clone https://huggingface.co/bert-base-uncased

The model will be downloaded to the directory the command is run from. Following that the model can be loaded from the local copy by pointing the load function at the directory. For example (if running from the same directory):

model = AutoModel.from_pretrained("./bert-base-uncased")

This is similar to the approach used for manual downloads.

8. Loading Your Downloaded Model

Once a model is cloned or downloaded the model it can be used! This can also be done easily. The approach is similar to how we used the transformers library to download the model.

The transformers library approach downloads the model and loads it into memory. The key method is the from_pretrained method:

from transformers import AutoModel, AutoTokenizer

# Load model from the cloned directory
model_path = "./bert-base-uncased"  # Adjust the path if needed

model = AutoModel.from_pretrained(pretrained_model_name_or_path=model_path)
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_path)

Here we see the same code as in the transformer library section. We use the from_pretrained method and pass in the model_path specifying the parameter name to make it clear. The log output shows where the model config is found and how the model is configured. Finally we also are shown the tensors are loaded and where they were loaded from:

loading configuration file ./bert-base-uncased/config.json Model config BertConfig { "architectures": [ "BertForMaskedLM" ], "attention_probs_dropout_prob": 0.1, "classifier_dropout": null, "gradient_checkpointing": false, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 768, "initializer_range": 0.02, "intermediate_size": 3072, "layer_norm_eps": 1e-12, "max_position_embeddings": 512, "model_type": "bert", "num_attention_heads": 12, "num_hidden_layers": 12, "pad_token_id": 0, "position_embedding_type": "absolute", "transformers_version": "4.50.3", "type_vocab_size": 2, "use_cache": true, "vocab_size": 30522 } loading weights file ./bert-base-uncased/model.safetensors Some weights of the model checkpoint at ./bert-base-uncased were not used when initializing BertModel: ['cls.predictions.bias', 'cls.predictions.transform.LayerNorm.beta', 'cls.predictions.transform.LayerNorm.gamma', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight'] - This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). All the weights of BertModel were initialized from the model checkpoint at ./bert-base-uncased. If your task is similar to the task the model of the checkpoint was trained on, you can already use BertModel for predictions without further training.

We see that the from_pretrained function can be passed the model name or the appropriate path when loading. This makes it easy to swap in models that have already been downloaded into a pipeline.

9. Conclusion

In this article we demonstrated the many ways to interact with the Hugging Face Model Hub to download models**. Models can be downloaded** programmatically inside Python code, It can be done via Git or it can be achieved via manual download.

We also showed how to specify the download directory to change from the default model cache. Now that we know how to download the models why not try a project and use one for sentiment analysis?


原始标题:Download a Hugging Face Model

« 上一篇: Gale-Shapley Algorithm