国内加速下载 Hugging Face 的模型的办法

0 / 94

方法一:网页下载

hf-mirror.com搜索,并在模型主页的 Files and Version 中下载文件。

方法二:huggingface-cli

huggingface-cli 是 Hugging Face 官方提供的命令行工具,自带完善的下载功能。

1. 安装依赖

pip install -U huggingface_hub

2. 设置环境变量
Linux

export HF_ENDPOINT=https://hf-mirror.com

Windows Powershell

$env:HF_ENDPOINT = "https://hf-mirror.com"

建议将上面这一行写入 ~/.bashrc

3.1 下载模型

huggingface-cli download --resume-download gpt2 --local-dir gpt2

3.2 下载数据集

huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext

可以添加 --local-dir-use-symlinks False 参数禁用文件软链接,这样下载路径下所见即所得。

方法三:使用 hfd

hfd 是hf-mirror.com开发的 huggingface 专用下载工具,基于成熟工具 git+aria2,可以做到稳定下载不断线。

1. 下载 hfd

wget https://hf-mirror.com/hfd/hfd.sh
chmod a+x hfd.sh

2. 设置环境变量
Linux

export HF_ENDPOINT=https://hf-mirror.com

Windows Powershell

$env:HF_ENDPOINT = "https://hf-mirror.com"

3.1 下载模型

./hfd.sh gpt2 --tool aria2c -x 4

3.2 下载数据集

./hfd.sh wikitext --dataset --tool aria2c -x 4

方法四:使用环境变量(非侵入式)

非侵入式,能解决大部分情况。huggingface 工具链会获取 HF_ENDPOINT 环境变量来确定下载文件所用的网址,所以可以使用通过设置变量来解决。

HF_ENDPOINT=https://hf-mirror.com python your_script.py

不过有些数据集有内置的下载脚本,那就需要手动改一下脚本内的地址来实现了。

常见问题

Q: 有些项目需要登录,如何下载?

A :部分 Gated Repo 需登录申请许可。为保障账号安全,本站不支持登录,需先前往 Hugging Face 官网登录、申请许可,在官网这里获取 Access Token 后回镜像站用命令行下载。
部分工具下载 Gated Repo 的方法:

huggingface-cli: 添加 --token 参数

huggingface-cli download --token hf_*** --resume-download meta-llama/Llama-2-7b-hf --local-dir Llama-2-7b-hf

hfd: 添加 --hf_username``--hf_token 参数

hfd meta-llama/Llama-2-7b --hf_username YOUR_HF_USERNAME --hf_token hf_***