文章目录
- 所需环境
- huggingface-cli 用法
- 登录
- token 获取
 
- huggingface 镜像
- huggingface 缓存
- hf-transfer 拉满下载带宽
- 如果开了的话,记得关掉科学上网!!!
所需环境
- python
huggingface-cli 用法
huggingface-cli的更多用法点击这里跳转官方
安装 huggingface_hub
PS C:\Windows\system32> pip install -U huggingface_hub
查看 huggingface-cli的用法
PS C:\Windows\system32> huggingface-cli --helpusage: 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 helpersenv                 Print information about the environment.login               Log in using a token from huggingface.co/settings/tokenswhoami              Find out which huggingface.co account you are logged in as.logout              Log outrepo                {create} Commands to interact with your huggingface.co repos.upload              Upload a file or a folder to a repo on the Hubdownload            Download files from the Hublfs-enable-largefilesConfigure 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 huboptions:-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
官方文档