huggingface 下载模型

284 阅读2分钟

AI 模型都比较大, 如果使用浏览器自带的下载,会很慢,然后可能会自动下载到一半,自动中断; 现在我们使用官方提供的下载工具下载模型。

  1. 安装官网的工具
pip install -U huggingface_hub

现在全局环境就有了这个工具

huggingface-cli  --help
  1. 检查工具是否安装上
> huggingface-cli  --help
usage: huggingface-cli <command> [<args>]

positional arguments:
  {download,upload,repo-files,env,login,whoami,logout,auth,repo,lfs-enable-largefiles,lfs-multipart-upload,scan-cache,delete-cache,tag,version,upload-large-folder}
                        huggingface-cli command helpers
    download            Download files from the Hub
    upload              Upload a file or a folder to a repo on the Hub
    repo-files          Manage files in a repo on the Hub
    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
    auth                Other authentication related commands
    repo                {create} Commands to interact with your huggingface.co repos.
    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
    version             Print information about the huggingface-cli version.
    upload-large-folder
                        Upload a large folder to a repo on the Hub

options:
  -h, --help            show this help message and exit
  1. 在 Hugging Face 中创建 访问令牌 access token

huggingface.co/settings/to…

请提前注册好账号

image.png

创建好令牌以后, Huggingface 会给你一段字符串,一定要自己存一下, 这个秘钥 后面会用到,如果丢失,请重新创建一个

image.png

  1. 登录
huggingface-cli login

把上面创建的秘钥粘贴在这里,然后 添加token到 git 中,输入Y

已经登录成功

>  huggingface-cli  login

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

    To log in, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
Enter your token (input will not be visible): 
Add token as git credential? (Y/n) Y
Token is valid (permission: fineGrained).
The token `live-xia` has been saved to /Users/xia/.cache/huggingface/stored_tokens
Your token has been saved in your configured git credential helpers (osxkeychain).
Your token has been saved to /Users/xia/.cache/huggingface/token
Login successful.
The current active token is: `live-xia`

5. 下载模型

huggingface-cli download --resume-download --token hf_这里填写上面的秘钥字符串 --local-dir-use-symlinks False stabilityai/stable-video-diffusion-img2vid-xt-1-1
  • --resume-download 支持断点续传
  • --token 秘钥
  • --local-dir-use-symlinks 是否使用本地软连接,设置为False
  • stabilityai/stable-video-diffusion-img2vid-xt-1-1 模型名称

huggingface.co/stabilityai…

image.png

默认 模型会下载到 huggingface 的缓存路径

~/.cache/huggingface/hub

可以看到我们下载的模型, 现在把模型移到 Comfyui 的 models/checkpoints 目录下即可

> cd ~/.cache/huggingface/hub    
> hub % ls

models--stabilityai--stable-video-diffusion-img2vid-xt-1-1
version.txt
mv models--stabilityai--stable-video-diffusion-img2vid-xt-1-1  .xxxxx/comfyui/models/checkpoints

重启,则可以看到 下载的模型

image.png

models--stabilityai--stable-video-diffusion-img2vid-xt-1-1 是 图生视频的模型

现在看到,终端已经在下载模型中。

image.png