Huggingface镜像站使用及常见报错

时间:2025-05-08 07:53:49

方法一:使用 huggingface-cli 下载模型

(1) 安装依赖

pip install -U huggingface_hub hf_transfer

可使用清华源:

pip install -U huggingface_hub hf_transfer
 -i /simple

(2) 命令
设置镜像站

export HF_ENDPOINT=

开启hf_transfer加速下载

export HF_HUB_ENABLE_HF_TRANSFER=1

下载命令

huggingface-cli download --resume-download 模型名称 --local-dir 本地目录

以"openai-community/gpt2-medium"为例:

huggingface-cli download --resume-download openai-community/gpt2-medium

如果不设置 --local-dir,模型会默认保存到.cache/huggingface/hub/models--openai-community--gpt2-medium/
(3) 下载需要登录的模型(Gated Model)
添加–token hf_***参数,其中hf_***是 access token,在huggingface官网获取

huggingface-cli download --token hf_*** --resume-download 模型名称 --local-dir 本地目录

常见报错

  1. huggingface-cli: error: invalid choice: ‘download’ (choose from ‘env’, ‘login’, ‘whoami’, ‘logout’, ‘repo’, ‘lfs-enable-largefiles’, ‘lfs-multipart-upload’, ‘scan-cache’, ‘delete-cache’)
    • 可能是由于python版本<3.8,下载的huggingface-cli版本过低;在python>=3.8环境下运行pip install -U huggingface_hub命令后解决
  2. ModuleNotFoundError: No module named ‘chardet’
    • 安装对应模块即可:pip install chardet

方法二:使用url直接下载

将 直接替换为 ,使用浏览器或者 wget -c、curl -L、aria2c 等命令行方式。下载需登录的模型需命令行添加 --header hf_*** 参数。

参考文章:

  • Huggingface 镜像站