huggingface加速下载模型

所需环境

  • python

huggingface-cli 用法

huggingface-cli的更多用法点击这里跳转官方

安装 huggingface_hub

PS C:\Windows\system32> pip install -U huggingface_hub

查看 huggingface-cli的用法

PS C:\Windows\system32> huggingface-cli --help

usage: huggingface-cli <command> [<args>]

positional arguments:
  {env,login,whoami,logout,repo,upload,download,lfs-enable-largefiles,lfs-multipart-upload,scan-cache,delete-cache,tag}
                        huggingface-cli command helpers
    env                 Print information about the environment.
    login               Log in using a token from huggingface.co/settings/tokens
    whoami              Find out which huggingface.co account you are logged in as.
    logout              Log out
    repo                {create} Commands to interact with your huggingface.co repos.
    upload              Upload a file or a folder to a repo on the Hub
    download            Download files from the Hub
    lfs-enable-largefiles
                        Configure your repository to enable upload of files > 5GB.
    scan-cache          Scan cache directory.
    delete-cache        Delete revisions from the cache directory.
    tag                 (create, list, delete) tags for a repo in the hub

options:
  -h, --help            show this help message and exit

登录

方式一
使用huggingface-cli login命令

PS C:\Windows\system32> huggingface-cli login

_|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
_|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
_|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
_|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
_|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .

# 这里输入token,去huggingface个人中心复制创建好的token,然后粘贴到这里(鼠标右键或者 Shift + Insert)
Token:

# 这里输入 Y
Add token as git credential? (Y/n)
Token is valid (permission: write).
Your token has been saved in your configured git credential helpers (store).
Your token has been saved to /home/wauplin/.cache/huggingface/token
Login successful

方式二
直接在命令行中写入token:
huggingface-cli login --token 你的token --add-to-git-credential

验证是否登录成功:如果可以正确输出自己的huggingface用户名就代表登录成功了!

PS C:\Windows\system32> huggingface-cli whoami
xiaobaiyuan
Authenticated through private endpoint: https://hf-mirror.com

token 获取

huggingface官网注册并登录,点击个人头像-Access Token-New Token-Copy

在这里插入图片描述

huggingface 镜像

以上登录之后便可以开始下载模型了,下载之前可以设置一个镜像,让下载速度快速提升!

https://hf-mirror.com/ 是huggingface的一个镜像站,站点可以直接搜索模型点击下载。当然,这里介绍的是通过huggingface-cli工具进行模型的下载。

设置镜像:
管理员身份运行pc上的powershell终端,然后设置环境变量

PS C:\Windows\system32> $env:HF_ENDPOINT = "https://hf-mirror.com"

之后便可以使用huggingface-cli下载模型了,这里下载sd3的一个模型作为示例:

--resume-download代表断点续传,就是下载链接断开后会自动重试下载
下载多个文件之间用空格隔开即可

PS C:\Windows\system32> huggingface-cli download --resume-download  stabilityai/stable-diffusion-3-medium sd3_medium.safetensors

在这里插入图片描述

huggingface 缓存

模型文件默认会下载到缓存中,位置为C:\Users\用户名\.cache\huggingface,如果想重新设置缓存的位置也可以实现:
在系统变量中新建一个变量名HUGGINGFACE_HUB_CACHE,变量值就是你要设置的缓存的绝对路径,路径最好不要带中文以及特殊符号

在这里插入图片描述

hf-transfer 拉满下载带宽

在这里插入图片描述

huggingface官方表示hf-transfer可以大幅度加快下载速度,我试了下确实很牛,带宽几乎可以拉满,缺点就是不稳定,开启方式:

pip install -U hf-transfer

管理员身份运行powershell,并设置环境变量

PS C:\Windows\system32> $env:HF_HUB_ENABLE_HF_TRANSFER = 1

官方文档

如果开了的话,记得关掉科学上网!!!

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那个小白猿

讨杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值