I am using the SentenceTransformers library (here: https://pypi.org/project/sentence-transformers/#pretrained-models) for creating embeddings of sentences using the pretrained model bert-base-nli-mean-tokens
. I have an application that will be deployed to a device that does not have internet access. How can I save this model locally so that when I call it, it loads the model locally, rather than attempting to download from the internet? As the library maintainers make clear, the method SentenceTransformer
downloads the model from the internet (see here: https://pypi.org/project/sentence-transformers/#pretrained-models) and I cannot find a method for saving the model locally.
Hugging face usage
You can download the models locally by using the Hugging Face transformer library method.
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/bert-base-nli-mean-tokens")
model = AutoModel.from_pretrained("sentence-transformers/bert-base-nli-mean-tokens")
tokenizer.save_pretrained('./local_directory/')
model.save_pretrained('./local_directory/')
After instantiating the SentenceTransformer via download, you can then save it to any path of your choosing with the 'save()' method.
model = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
model.save('/my/local/directory/for/models/')
The accepted answer doesn't work, as it doesn't have the encapsulating folder and config.json that SentenceTransformer is looking for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With