Huggingface: Download and Save Model to Custom Path

how-to-download-save-and-load-huggingface-model-using-python

In this post, I will show you how you can save huggingface model locally inside your working directory or custom folder path. If you are looking to save or load TF-Hub model in custom folder path, I have written another tutorial, you can check that.

Nowadays deep learning models are getting popular day by day. For all kinds of tasks, we are looking for a deep learning model-based solution.

But training a deep learning model needs high computing machines, which most of us don’t have. Thanks to Huggingface and TF hub. They provide lots of pre-trained models for various tasks starting from computer vision to NLP.

You can easily download any pretrained models from those model hubs by executing two lines of Python script. But the problem is those models will be saved inside your C:\ drive.

Now if you have a system like mine which has limited memory in C:\ drive. For that, you need to find a solution to save those pre trained models (from huggingface or TF Hub) locally inside D:\ drive or to your working directory or any custom folder path. In this post we will explore those methods.

Also Read: How to Save/Load TF Hub model in custom folder path

Download Huggingface model

To download Huggingface model using Python script, we need to install a library nammed “transformers“. It is the python library provided by Huggingface to access their models from Python.

To install transformers you need to have Python version 3.7.0 or higher (Python >=3.7.0). Use below command to install it.

pip install transformers

After successful installation, we can download any Huggingface model through Python. For this tutorial, I am going to download BERT question-answering model from huggingface. You can check the official model by going to this hugging face link.

Also Read:  CondaError: An error occurred when loading cached repodata

Also Read: Build Question Answering System with BERT model

Below Python code is to download and save huggingface model and tokenizer.

# Download and save bert model to C:\ drive
from transformers import BertForQuestionAnswering
from transformers import BertTokenizer

# huggingface save model and tokenizer
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
jupyter-notebook-output-to-download-and-save-huggingface-model-and-tokenizer-using-python

If you execute above Python code, BERT Huggingface model and tokenizer will be saved locally inside your C:\ drive.

For me, the saved model location was C:\Users\Anindya.cache\huggingface. This is the default cache path for hugging face model.

Course for You: Build AI Apps-OpenAI, LLAMA2 & HuggingFace

Save Huggingface model locally

Now if you want to store or save this downloaded model to any other disk like D:\ drive or to your working directory locally, there are mainly two ways.

Method1: Save Huggingface model to custom folder path

In this way, you can copy the downloaded model to any custom folder path. Below is the Python code to save huggingface model to any disk or directory.

# Save huggingface bert model files to working directorty
model.save_pretrained('D:/Question_Answering/model_files')
tokenizer.save_pretrained('D:/Question_Answering/model_files')
huggingface-save-model-files-to-disk-or-d-drive-or-working-directory-or-custom-folder-path

This code is to copy the model files from this location (C:\Users\Anindya.cache\huggingface) to this location (D:/Question_Answering/model_files)

Method2: Change cache location

In this way, you can directly change the default cache location of Huggingface. So that model files will directly be downloaded to that custom folder path.

# Download model to custom cache directory
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad', cache_dir="D:/Question_Answering/huggingface_cache/")
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad', cache_dir="D:/Question_Answering/huggingface_cache/")
download-and-save-and-load-huggingface-models-in-custom-cache-location-change-default-cache-path-of-hugging-face-hub-in-python

In the above Python code, we are defining a custom cache path (D:/Question_Answering/huggingface_cache/) of hugging face model. So that model can be downloaded and saved in that custom folder path.

Also Read:  How to download NLTK corpus manually

Note: Previously huggingface default cache location was: C:\Users\Anindya.cache\huggingface

Just to Say

This is not a tutorial. I just wanted to share with you the difficulty I faced while downloading huggingface model and how I solved that.

By default, Huggingface models were getting saved inside C:\ drive. But my system had only 5GB of free space. So I had to find out a solution to save those models inside D:\ drive.

Before thinking to increase C:\ drive space, I did a little bit of research and found some techniques, which I shared in this post.

This is it for this pain point post. If you have any questions or suggestions regarding this post, please let me know in the comment section below.

2 thoughts on “Huggingface: Download and Save Model to Custom Path”

    • That was also my first question as I found this article. But using the code above with the ‘cache_dir’ argument is all you need to do.

      Reply

Leave a comment