把huggingface.co替换成hf-mirror.com

本文介绍如何通过替换 huggingface.co 为 hf-mirror.com 来加速 Hugging Face 模型和数据集下载。提供三种方法:设置 HF_ENDPOINT 环境变量、使用基于 Git 和 aria2 的 hfd 脚本(支持多线程和镜像端点)、以及安装 hf_transfer 模块(Rust 实现,速度可达 500MB/s)。包含详细的命令行示例和登录令牌配置。

作者:zhuge···预计阅读 9 分钟·2,163 阅读·0 评论
把huggingface.co替换成hf-mirror.com

核心:

#全自动 ./hdf.sh xxx/xxxx

hfd.zip

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

或者

find /home/aistudio/work/ComfyUI/custom_nodes/ -type f -exec sed -i 's/huggingface.co/hf-mirror.com/g' {} +


# 下载模型
huggingface-cli download --resume-download --local-dir-use-symlinks False bigscience/bloom-560m --local-dir bloom-560m
# 下载数据集
huggingface-cli download --resume-download --repo-type dataset lavita/medical-qa-shared-task-v1-toy

下载需要登录的模型(Gated Model) 请添加 --token hf_*** 参数,其中 hf_*** 是 access token,请在 Hugging Face官网这里 获取。示例:

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

hfd 是基于 Git 和 aria2 实现的专用于huggingface 下载的命令行脚本: hfd.sh(Gitst链接)。hfd 相比 huggingface-cli ,鲁棒性更好,很少会有奇奇怪怪的报错,此外多线程控制力度也更细,可以设置线程数量。 其原理是 Step1:Git clone 项目仓库中lfs文件之外的所有文件,并自动获取 lfs 文件的 url;Step2:利用 aria2 多线程下载文件。

该工具同样支持设置镜像端点的环境变量:

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

基本命令:

./hdf.sh bigscience/bloom-560m --tool aria2c -x 4

如果没有安装 aria2,则可以默认用 wget:

./hdf.sh bigscience/bloom-560m

完整命令格式:

$ ./hfd.sh -h
Usage:
hfd <model_id> [--include include_pattern] [--exclude exclude_pattern] [--hf_username username] [--hf_token token] [--tool wget|aria2c] [-x threads] [--dataset]

Description: 使用提供的模型ID从Hugging Face下载模型或数据集。

Parameters:
model_id Hugging Face模型ID,格式为'repo/model_name'。
--include (可选)标志,用于指定要包括在下载中的文件的字符串模式。
--exclude (可选)标志,用于指定要从下载中排除的文件的字符串模式。
exclude_pattern 匹配文件名以排除的模式。
--hf_username (可选)Hugging Face用户名,用于身份验证。
--hf_token (可选)Hugging Face令牌,用于身份验证。
--tool (可选)使用的下载工具。可以是wget(默认)或aria2c。
-x (可选)aria2c的下载线程数。
--dataset (可选)标志,表示下载数据集。

示例:

hfd bigscience/bloom-560m --exclude safetensors
hfd meta-llama/Llama-2-7b --hf_username myuser --hf_token mytoken --tool aria2c -x 8
hfd lavita/medical-qa-shared-task-v1-toy --dataset

hf_transfer 依附并兼容 huggingface-cli,是 hugging face 官方专门为提高下载速度基于 Rust 开发的一个模块,开启后在带宽充足的机器上可以跑到 500MB/s。 开启方法 (1)安装依赖

pip install -U hf-transfer

(2)设置 HF_HUB_ENABLE_HF_TRANSFER 环境变量为 1。 Linux

export HF_HUB_ENABLE_HF_TRANSFER=1

Windows Powershell

$env:HF_HUB_ENABLE_HF_TRANSFER = 1

开启后使用方法同 huggingface-cli:

huggingface-cli download --resume-download bigscience/bloom-560m --local-dir bloom-560m

https://zhuanlan.zhihu.com/p/676222159 https://zhuanlan.zhihu.com/p/663712983

相关文章

评论

加载中...