上传、下载huggingface仓库文件(模型、数据等)

下载

例如,想要从huggingface hub下载llama-13b模型文件到本地:
在这里插入图片描述

可以用如下命令,local_dir就是你想要下载到的本地文件夹:

from huggingface_hub import snapshot_download
snapshot_download(repo_id="decapoda-research/llama-13b-hf",cache_dir="./cache", local_dir="./model_weights/llama-13b-hf")

上述命令等价于git clone,更多参数, 例如过滤、指定文件,建立符号链接等,详见官网教程:

上传

想要上传文件,例如模型权重的话,首先得找到自己huggingface hub的access code
在这里插入图片描述

然后将自己的access code设置到自己的机器上:

pip install huggingface_hub  ## 如果没有安装huggingface_hub库
python -c "from huggingface_hub.hf_api import HfFolder; HfFolder.save_token('MY_HUGGINGFACE_TOKEN_HERE')"

最后用如下命令,把相关文件(模型、tokenizer)上传就行(等价于git push)。

pt_model.push_to_hub("my-awesome-model")  # 这里的my-awesome-model就是你仓库的名字(用户名+仓库名),例如“Reza8848/MUFFIN-T5-3B”
tokenizer.push_to_hub("my-awesome-model")

注意,如果遇到类似于TypeError: create_repo() got an unexpected keyword argument 'organization'的报错,需要把huggingface hub的版本更新

pip install transformers==4.24.0
pip install --upgrade huggingface_hub  # 确保更新huggingface hub到最新版本
python -c "from huggingface_hub.hf_api import HfFolder; HfFolder.save_token('MY_HUGGINGFACE_TOKEN_HERE')" # 记得重新再设置一次access token

之后再上传模型。


参考:

  • https://stackoverflow.com/questions/75385142/tokenizer-push-to-hubrepo-name-is-not-working
  • https://discuss.huggingface.co/t/how-to-login-to-huggingface-hub-with-access-token/22498