Hugging Face CLI
Hugging Face (https://huggingface.co) is the leading platform for sharing and collaborating on AI models, datasets, and spaces. This skill enables interaction with the Hub through the official hf CLI.
Installation
Check if hf is available by running hf version. If not installed:
CODEBLOCK0
If the options above do not work, follow the official installation guide.
After installation, run hf version to verify. If the command is not found, run source ~/.bashrc (or source ~/.zshrc for zsh) to reload the PATH, then try again.
Authentication
A Hugging Face User Access Token is required. The token is provided via the HF_TOKEN environment variable.
If authentication fails or the token is missing, instruct the user to:
- 1. Go to https://huggingface.co/settings/tokens
- Create a new token — there are two permission levels:
-
Read (safer): sufficient for searching, downloading models/datasets, listing repos, browsing papers, and most read-only operations. Choose this if you only need to explore and download.
-
Write (less safe, broader access): required for creating/deleting repos, uploading files, managing discussions, deploying endpoints, and running jobs. Example 3 (create a repo and upload weights) requires a write token.
- 3. Set it as an environment variable:
export HF_TOKEN="hf_..." (add to shell profile for persistence)
Important: Do NOT run hf auth login interactively — it requires terminal input. Instead, use the environment variable directly. The hf CLI automatically picks up HF_TOKEN from the environment for all commands. To verify authentication, run:
CODEBLOCK1
Key Commands
| Task | Command |
|---|
| Check current user | INLINECODE11 |
| Download files |
hf download <repo_id> [files...] [--local-dir <path>] |
| Download specific revision |
hf download <repo_id> --revision <branch\|tag\|commit> |
| Download with filters |
hf download <repo_id> --include "*.safetensors" --exclude "*.bin" |
| Upload files |
hf upload <repo_id> <local_path> [path_in_repo] |
| Upload as PR |
hf upload <repo_id> <local_path> [path_in_repo] --create-pr |
| Upload (private repo) |
hf upload <repo_id> <local_path> [path_in_repo] --private |
| Upload large folder |
hf upload-large-folder <repo_id> <local_path> |
| Create a repo |
hf repos create <name> [--repo-type model\|dataset\|space] [--private] |
| Delete a repo |
hf repos delete <repo_id> |
| Delete files from repo |
hf repos delete-files <repo_id> <path>... |
| Duplicate a repo |
hf repos duplicate <repo_id> [--type model\|dataset\|space] |
| Repo settings |
hf repos settings <repo_id> [--private\|--public] |
| Manage branches |
hf repos branch create\|delete <repo_id> <branch> |
| Manage tags |
hf repos tag create\|delete <repo_id> <tag> |
| List models |
hf models ls [--search <query>] [--sort downloads] [--limit N] |
| Model info |
hf models info <repo_id> |
| List datasets |
hf datasets ls [--search <query>] |
| Dataset info |
hf datasets info <repo_id> |
| Run SQL on data |
hf datasets sql "<SQL>" |
| List spaces |
hf spaces ls [--search <query>] |
| Space info |
hf spaces info <repo_id> |
| Space dev mode |
hf spaces dev-mode <repo_id> |
| List papers |
hf papers ls [--limit N] |
| List collections |
hf collections ls [--owner <user>] [--sort trending] |
| Create collection |
hf collections create "<title>" |
| Collection info |
hf collections info <collection_slug> |
| Add to collection |
hf collections add-item <collection_slug> <repo_id> <type> |
| Delete collection |
hf collections delete <collection_slug> |
| Run a cloud job |
hf jobs run <docker_image> <command> |
| List jobs |
hf jobs ps |
| Job logs |
hf jobs logs <job_id> |
| Cancel a job |
hf jobs cancel <job_id> |
| Job hardware |
hf jobs hardware |
| Deploy endpoint |
hf endpoints deploy <name> --repo <repo_id> --framework <fw> --accelerator <hw> ... |
| List endpoints |
hf endpoints ls |
| Endpoint info |
hf endpoints describe <name> |
| Pause/resume endpoint |
hf endpoints pause\|resume <name> |
| Delete endpoint |
hf endpoints delete <name> |
| List discussions |
hf discussions ls <repo_id> |
| Create discussion |
hf discussions create <repo_id> --title "<title>" |
| Comment on discussion |
hf discussions comment <repo_id> <num> --body "<text>" |
| Close discussion |
hf discussions close <repo_id> <num> |
| Merge PR |
hf discussions merge <repo_id> <num> |
| Manage cache |
hf cache ls,
hf cache rm <id>,
hf cache prune |
| Delete bucket / files |
hf buckets delete <user>/<bucket>,
hf buckets rm <user>/<bucket>/<path> |
| Sync to bucket |
hf sync <local_path> hf://buckets/<user>/<bucket> |
| Print environment |
hf env |
End-to-End Examples
Example 1: Explore trending models, pick one, and preview a download
CODEBLOCK2
Example 2: Browse today's papers and find related datasets
CODEBLOCK3
Example 3: Create a private model repo and upload weights
CODEBLOCK4
Further Reference
Reference version: hf CLI v1.x
For the full list of commands and options, use built-in help:
CODEBLOCK5
- - Full documentation: https://huggingface.co/docs/huggingfacehub/guides/cli
- CLI reference: https://huggingface.co/docs/huggingfacehub/package_reference/cli
Safety Rules
- - Destructive commands require explicit user confirmation. Before running any of the following, describe what will happen and ask the user to confirm:
-
hf repos delete — permanently deletes a repository
-
hf repos delete-files — deletes files from a repository
-
hf buckets delete /
hf buckets rm — deletes buckets or bucket files
-
hf discussions close /
hf discussions merge — closes or merges PRs/discussions
-
hf collections delete — permanently deletes a collection
-
hf endpoints delete — permanently deletes an Inference Endpoint
-
hf jobs cancel — cancels a running compute job
- Any command with
--delete flag (e.g., sync with deletion)
-
hf cache rm /
hf cache prune — removes cached data from disk (re-downloadable, but may waste bandwidth)
- - Never expose or log the
HF_TOKEN value. Do not include it in command output or commit it to files. - When uploading, warn the user if the target repo is public and the upload may contain sensitive data.
Hugging Face CLI
Hugging Face (https://huggingface.co) 是共享和协作AI模型、数据集和空间领域的领先平台。本技能支持通过官方 hf CLI 与 Hub 进行交互。
安装
通过运行 hf version 检查 hf 是否可用。如果未安装:
bash
pip install -U huggingface_hub[cli]
或
brew install hf
如果上述方法无效,请遵循官方安装指南。
安装完成后,运行 hf version 进行验证。如果找不到命令,请运行 source ~/.bashrc(或 zsh 用户运行 source ~/.zshrc)重新加载 PATH,然后重试。
身份验证
需要 Hugging Face 用户访问令牌。令牌通过 HF_TOKEN 环境变量提供。
如果身份验证失败或缺少令牌,请指导用户:
- 1. 访问 https://huggingface.co/settings/tokens
- 创建新令牌——有两种权限级别:
-
读取(更安全):足以用于搜索、下载模型/数据集、列出仓库、浏览论文以及大多数只读操作。如果只需要探索和下载,请选择此项。
-
写入(安全性较低,访问范围更广):用于创建/删除仓库、上传文件、管理讨论、部署端点和运行任务。示例3(创建仓库并上传权重)需要写入令牌。
- 3. 将其设置为环境变量:export HFTOKEN=hf...(添加到 shell 配置文件以持久化)
重要提示: 不要交互式运行 hf auth login——它需要终端输入。请直接使用环境变量。hf CLI 会自动从环境中获取 HF_TOKEN 用于所有命令。要验证身份验证,请运行:
bash
hf auth whoami
关键命令
| 任务 | 命令 |
|---|
| 检查当前用户 | hf auth whoami |
| 下载文件 |
hf download
[files...] [--local-dir ] |
| 下载特定版本 | hf download --revision |
| 带筛选条件下载 | hf download --include .safetensors --exclude .bin |
| 上传文件 | hf upload id> path> [pathinrepo] |
| 以PR形式上传 | hf upload id> path> [pathinrepo] --create-pr |
| 上传(私有仓库) | hf upload id> path> [pathinrepo] --private |
| 上传大文件夹 | hf upload-large-folder id> path> |
| 创建仓库 | hf repos create [--repo-type model\|dataset\|space] [--private] |
| 删除仓库 | hf repos delete |
| 从仓库删除文件 | hf repos delete-files ... |
| 复制仓库 | hf repos duplicate [--type model\|dataset\|space] |
| 仓库设置 | hf repos settings [--private\|--public] |
| 管理分支 | hf repos branch create\|delete |
| 管理标签 | hf repos tag create\|delete |
| 列出模型 | hf models ls [--search ] [--sort downloads] [--limit N] |
| 模型信息 | hf models info |
| 列出数据集 | hf datasets ls [--search ] |
| 数据集信息 | hf datasets info |
| 对数据运行SQL | hf datasets sql |
| 列出空间 | hf spaces ls [--search ] |
| 空间信息 | hf spaces info |
| 空间开发模式 | hf spaces dev-mode |
| 列出论文 | hf papers ls [--limit N] |
| 列出收藏集 | hf collections ls [--owner ] [--sort trending] |
| 创建收藏集 | hf collections create |
| 收藏集信息 | hf collections info |
| 添加到收藏集 | hf collections add-item slug> id> |
| 删除收藏集 | hf collections delete |
| 运行云端任务 | hf jobs run |
| 列出任务 | hf jobs ps |
| 任务日志 | hf jobs logs |
| 取消任务 | hf jobs cancel |
| 任务硬件 | hf jobs hardware |
| 部署端点 | hf endpoints deploy --repo --framework --accelerator ... |
| 列出端点 | hf endpoints ls |
| 端点信息 | hf endpoints describe |
| 暂停/恢复端点 | hf endpoints pause\|resume |
| 删除端点 | hf endpoints delete |
| 列出讨论 | hf discussions ls |
| 创建讨论 | hf discussions create --title |
| 评论讨论 | hf discussions comment --body |
| 关闭讨论 | hf discussions close |
| 合并PR | hf discussions merge |
| 管理缓存 | hf cache ls, hf cache rm , hf cache prune |
| 删除存储桶/文件 | hf buckets delete /, hf buckets rm // |
| 同步到存储桶 | hf sync hf://buckets// |
| 打印环境 | hf env |
端到端示例
示例1:探索热门模型,选择一个,并预览下载
bash
hf models ls --sort trending_score --limit 5
hf models info openai-community/gpt2
hf download --dry-run openai-community/gpt2 config.json tokenizer.json
hf download openai-community/gpt2 config.json tokenizer.json --local-dir ./gpt2
示例2:浏览今日论文并查找相关数据集
bash
hf papers ls --limit 5
hf datasets ls --search code --sort downloads --limit 5
hf datasets info bigcode/the-stack
示例3:创建私有模型仓库并上传权重
bash
hf repos create my-fine-tuned-model --private
create 返回 /my-fine-tuned-model — 在下面使用该完整ID
hf upload /my-fine-tuned-model ./output --commit-message Add fine-tuned weights
hf repos tag create /my-fine-tuned-model v1.0 -m Initial release
进一步参考
参考版本:hf CLI v1.x
如需完整命令和选项列表,请使用内置帮助:
bash
hf --help
hf --help
- - 完整文档: https://huggingface.co/docs/huggingfacehub/guides/cli
- CLI参考: https://huggingface.co/docs/huggingfacehub/package_reference/cli
安全规则
- - 破坏性命令需要用户明确确认。 在运行以下任何命令之前,请描述将要发生的情况并要求用户确认:
- hf repos delete — 永久删除仓库
- hf repos delete-files — 从仓库删除文件
- hf buckets delete / hf buckets rm — 删除存储桶或存储桶文件
- hf discussions close / hf discussions merge — 关闭或合并PR/讨论
- hf collections delete — 永久删除收藏集
- hf endpoints delete — 永久删除推理端点
- hf jobs cancel — 取消正在运行的计算任务
- 任何带有 --delete 标志的命令(例如带删除的同步)
- hf cache rm / hf cache prune — 从磁盘删除缓存数据(可重新下载,但可能浪费带宽)
- - 切勿暴露或记录 HF_TOKEN 值。不要将其包含在命令输出中或提交到文件中。
- 上传时,如果目标仓库是公开的且上传内容可能包含敏感数据,请警告用户。