How to predownload a transformers model How to predownload a transformers model flask flask

How to predownload a transformers model


Approach 1:

Download the model from this link:

pytorch-model: https://s3.amazonaws.com/models.huggingface.co/bert/openai-gpt-pytorch_model.bin

tensorflow-model: https://s3.amazonaws.com/models.huggingface.co/bert/openai-gpt-tf_model.h5

The config file: https://s3.amazonaws.com/models.huggingface.co/bert/openai-gpt-config.json

Source: https://huggingface.co/transformers/_modules/transformers/configuration_openai.html#OpenAIGPTConfig

You can manually download the model (in your case TensorFlow model .h5 and the config.json file), put it in a folder (let's say model) in the repository. (you can try compressing the model, and then decompressing once it's in the ec2 instance if needed)

Then, you can directly load the model in your web server from the path instead of downloading (model folder which contains the .h5 and config.json):

model = TFOpenAIGPTLMHeadModel.from_pretrained("model") # model folder contains .h5 and config.jsontokenizer = OpenAIGPTTokenizer.from_pretrained("openai-gpt") # this is a light download

Approach 2:

Instead of using links to download, you can download the model in your local machine using the conventional method.

from transformers.tokenization_openai import OpenAIGPTTokenizerfrom transformers.modeling_tf_openai import TFOpenAIGPTLMHeadModelmodel = TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt")tokenizer = OpenAIGPTTokenizer.from_pretrained("openai-gpt")

This downloads the model. Now you can save the weights in a folder using save_pretrained function.

model.save_pretrained('/content/') # saving inside content folder

Now, the content folder should contain a .h5 file and a config.json.

Just upload them to the repository and load from that.


Open https://huggingface.co/models and search the model you want. Click on the model name and finnaly click on "List all files in model". You will get a list of the files you can download.


I tried to download models from the huggingface official website, but the downloaded models were git-lfs code, not the actual model files.

Fortunately, I found Tsinghua has mirrored huggingface models, so you can download actual models from Tsinghua Mirror: https://mirrors.tuna.tsinghua.edu.cn/hugging-face-models/