How to download model from huggingface?

14,638

Solution 1

The models are automatically cached locally when you first use it. So, to download a model, all you have to do is run the code that is provided in the model card (I chose the corresponding model card for bert-base-uncased).

At the top right of the page you can find a button called "Use in Transformers", which even gives you the sample code, showing you how to use it in Python. Again, for bert-base-uncased, this gives you the following code snippet:

from transformers import AutoTokenizer, AutoModelForMaskedLM
  
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForMaskedLM.from_pretrained("bert-base-uncased")

When you run this code for the first time, you will see a download bar appear on screen. See this post (disclaimer: I gave one of the answers) if you want to find the actual folder where Huggingface stores their models.

Solution 2

Accepted answer is good, but writing code to download model is not always convenient. It seems git works fine with getting models from huggingface. Here is an example:

git lfs clone https://huggingface.co/sberbank-ai/ruT5-base

where 'lfs' stays for 'large file storage'. Technically this command is deprecated and simple 'git clone' should work, but then you need to setup filters to not skip large files (How do I clone a repository that includes Git LFS files?)

Share:
14,638
marlon
Author by

marlon

Updated on June 12, 2022

Comments

  • marlon
    marlon about 2 years
    https://huggingface.co/models
    

    For example, I want to download 'bert-base-uncased', but cann't find a 'Download' link. Please help. Or is it not downloadable?

  • Shaida Muhammad
    Shaida Muhammad over 2 years
    How to download the models to local disk?
  • Shaida Muhammad
    Shaida Muhammad over 2 years
    What if I want to download the model to a specific directory?
  • dennlinger
    dennlinger over 2 years
    I think that merits its own question; in fact, I believe a possible answer is given here: stackoverflow.com/questions/63312859/…
  • rkechols
    rkechols over 2 years
    This should be a comment on Jahjajaka's answer
  • S.Fan
    S.Fan over 2 years
    i have no permission to make a comment under jahjajaka's answer, it requires 50 reputation, at least.