Downloading transformers models to use offline

Viewed 8811

I have a trained transformers NER model that I want to use on a machine not connected to the internet. When loading such a model, currently it downloads cache files to the .cache folder.

To load and run the model offline, you need to copy the files in the .cache folder to the offline machine. However, these files have long, non-descriptive names, which makes it really hard to identify the correct files if you have multiple models you want to use. Any thoughts on this?

Example of model files

3 Answers

One relatively easy way to deal with this issue is to simply "rename" the pretrained models, as is detailed in this thread.

Essentially, all you have to do is something like this for whatever model you're trying to work with:

from transformers import BertModel

model = BertModel.from_pretrained("bert-base-uncased")
model.save_pretrained("./my_named_bert")

The thread also details how the local model folders are named, see LysandreJik's post:

Hi, they are named as such because that's a clean way to make sure the model on the S3 is the same as the model in the cache. The name is created from the etag of the file hosted on the S3. [...]

For the first time save the model using model.save_pretrained("./your_file_name") and load the model from your file through BertModel.from_pretrained("./your_file_name") Do the same for tokenizer if you are using it.

model.save_pretrained("./your_file_name")
BertModel.from_pretrained("./your_file_name")

You can find the steps here. According to this guide, "You can download the corresponding model here and unzip it. Then, simply use the following to create your embedding model:

from sentence_transformers import SentenceTransformer
embedding_model = SentenceTransformer('path/to/unzipped/model')

Then, pass it to BERTopic:

from bertopic import BERTopic
topic_model = BERTopic(embedding_model=embedding_model)
Related