使用 huggingface-cli 下载模型

王 茂南 2025年5月20日07:22:43
评论
2305字阅读7分41秒
摘要本文介绍了如何使用 huggingface-cli 命令行工具下载 Hugging Face 上的模型,并结合 nohup 实现后台下载及查看下载进度。

简介

在进行深度学习项目时,下载大规模的预训练模型是常见的需求。huggingface-cli 是 Hugging Face 提供的命令行工具,用于下载和管理模型。目前最新版本使用 hf 即可。

如果模型较大,或者需要在后台运行下载任务时,可以结合 nohup 命令来确保下载过程不会被中断。本文将介绍如何使用这些工具进行模型下载,并如何查看下载进程。

最新完整教程可以参考链接,Huggingface-Command Line Interface (CLI)

 

hf 使用(新版本)

首先使用下面的命令进行安装:

  1. curl -LsSf https://hf.co/cli/install.sh | bash

安装完毕后使用 hf --help 检查是否安装成功:

  1. >>> hf --help
  2. Usage: hf [OPTIONS] COMMAND [ARGS]...
  3.   Hugging Face Hub CLI
  4. Options:
  5.   --install-completion  Install completion for the current shell.
  6.   --show-completion     Show completion for the current shell, to copy it or
  7.                         customize the installation.
  8.   --help                Show this message and exit.
  9. Commands:
  10.   auth                 Manage authentication (login, logout, etc.).
  11.   cache                Manage local cache directory.
  12.   download             Download files from the Hub.
  13.   endpoints            Manage Hugging Face Inference Endpoints.
  14.   env                  Print information about the environment.
  15.   jobs                 Run and manage Jobs on the Hub.
  16.   repo                 Manage repos on the Hub.
  17.   repo-files           Manage files in a repo on the Hub.
  18.   upload               Upload a file or a folder to the Hub.
  19.   upload-large-folder  Upload a large folder to the Hub.
  20.   version              Print information about the hf version.

所有检查完毕之后,可以使用下面的命令进行下载。下面是一个下载 whisper 模型的例子:

  1. hf download openai/whisper-large-v3 --local-dir="/home/whisper-large-v3“

 

hf-cli 使用(旧版本)

huggingface-cli 的安装

使用下面的命令进行安装:

  1. pip install -U "huggingface_hub[cli]"

安装完毕之后,可以使用下面的命令查看是否安装成功:

  1. huggingface-cli --help

 

使用 huggingface-cli 下载模型

安装完毕之后,我们可以通过简单的命令下载 Hugging Face 上的模型。例如,我们可以使用以下命令下载 sd-controlnet-seg 模型:

  1. nohup huggingface-cli download lllyasviel/sd-controlnet-seg --local-dir="/home/sd-controlnet-seg" > download.log 2>&1 &

上面的命令的含义为:

    • nohup:使得下载命令在后台运行,并且即使终端关闭,下载进程依然会继续执行。
    • huggingface-cli download lllyasviel/sd-controlnet-seg:指定下载模型的名称(此处是 `sd-controlnet-seg`)。
    • --local-dir="/home/sd-controlnet-seg":指定模型存储的本地目录。
    • > download.log 2>&1 &:将下载过程中的标准输出和错误信息重定向到 download.log 文件,并将进程放到后台执行。

同样地,你可以使用类似的命令来下载其他模型。例如:

 

使用 ps 和 pgrep 查看下载进程

为了查看下载进程是否正在运行,可以使用 ps 命令查看系统中所有的进程,并筛选出涉及 huggingface-cli 的进程:

  1. ps aux | grep huggingface-cli

如果希望使用更简洁的方式,也可以使用 pgrep 命令来查看是否存在 huggingface-cli 相关的进程:

  1. pgrep -f huggingface-cli

这两个命令会返回包含 huggingface-cli 关键字的进程信息。如果下载进程正在运行,你将能够看到对应的进程 ID 和其他相关信息。

  • 微信公众号
  • 关注微信公众号
  • weinxin
  • QQ群
  • 我们的QQ群号
  • weinxin
王 茂南
  • 本文由 发表于 2025年5月20日07:22:43
  • 转载请务必保留本文链接:https://mathpretty.com/19668.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: