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.
            Asked
            
        
        
            Active
            
        
            Viewed 1.3k times
        
    9
            
            
        
        Josh
        
- 1,155
 - 4
 - 12
 - 21
 
- 
                    I'm trying out the same thing. Were you able to identify it? – Raghul Raj Sep 06 '20 at 13:27
 
2 Answers
6
            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/')
        Shivan
        
- 76
 - 1
 
- 
                    4Hey, The generated config.json doesn't have a version. So I'm getting a `KeyError: '__version__'` – Raghul Raj Sep 06 '20 at 13:42
 - 
                    1
 
4
            
            
        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
        Wes Smith
        
- 41
 - 1