Hugging Face CLI 技能
使用 Hugging Face Hub CLI (hf) 执行各种操作。
环境变量
- - HF_TOKEN:Hugging Face API 令牌(从 https://huggingface.co/settings/tokens 获取)
核心功能
1. 身份验证管理 (hf auth)
bash
检查登录状态
hf auth whoami
列出所有令牌
hf auth list
登录
hf auth login
登出
hf auth logout
切换令牌
hf auth switch
2. 模型管理 (hf models)
bash
列出模型(支持排序和筛选)
hf models ls --sort downloads --limit 10
hf models ls --search llama
获取模型信息
hf models info meta-llama/Llama-3.2-1B-Instruct
3. 数据集管理 (hf datasets)
bash
列出数据集
hf datasets ls --limit 10
hf datasets ls --search imagenet
获取数据集信息
hf datasets info HuggingFaceFW/fineweb
4. Spaces 管理 (hf spaces)
bash
列出 Spaces
hf spaces ls --limit 10
获取 Space 信息
hf spaces info username/repo-name
热重载(实验性功能,适用于 Gradio 6.1+)
hf spaces hot-reload username/repo-name app.py
hf spaces hot-reload username/repo-name -f ./local/app.py
5. 仓库管理 (hf repos)
bash
创建新仓库
hf repos create my-model --type model
hf repos create my-dataset --type dataset
hf repos create my-space --type space
删除仓库
hf repos delete username/repo-name
设置为私有
hf repos settings username/repo-name --private
管理分支
hf repos branch create username/repo-name feature-branch
hf repos branch delete username/repo-name feature-branch
管理标签
hf repos tag create username/repo-name v1.0
hf repos tag delete username/repo-name v1.0
移动仓库到其他命名空间
hf repos move old-namespace/my-model new-namespace/my-model
6. 下载文件 (hf download)
bash
下载整个模型
hf download meta-llama/Llama-3.2-1B-Instruct
下载特定文件
hf download meta-llama/Llama-3.2-1B-Instruct config.json tokenizer.json
使用通配符模式下载
hf download meta-llama/Llama-3.2-1B-Instruct --include *.safetensors
hf download meta-llama/Llama-3.2-1B-Instruct --include
.json --exclude .bin
下载到本地目录
hf download meta-llama/Llama-3.2-1B-Instruct --local-dir ./models/llama
下载数据集
hf download HuggingFaceM4/FineVision --repo-type dataset
7. 上传文件 (hf upload)
bash
上传整个目录
hf upload my-cool-model . .
上传单个文件
hf upload username/my-model ./models/model.safetensors
上传到数据集
hf upload username/my-dataset ./data /train --repo-type dataset
附带提交信息
hf upload username/my-model ./models . --commit-message=Epoch 34/50 --commit-description=Val accuracy: 68%
创建拉取请求
hf upload bigcode/the-stack . . --repo-type dataset --create-pr
创建私有仓库
hf upload username/my-private-model . . --private
8. 收藏集管理 (hf collections)
bash
创建收藏集
hf collections create My Models
向收藏集添加项目
hf collections add-item username/my-collection moonshotai/kimi-k2 model
列出收藏集
hf collections ls
获取收藏集信息
hf collections info username/my-collection
更新收藏集
hf collections update username/my-collection --title New Title
更新收藏集项目
hf collections update-item username/my-collection ITEM
OBJECTID --note Updated note
删除项目
hf collections delete-item username/my-collection ITEM
OBJECTID
删除收藏集
hf collections delete username/my-collection
使用示例
示例 1:下载和上传模型
bash
下载模型
hf download meta-llama/Llama-3.2-1B-Instruct --local-dir ./llama-model
上传到你的仓库
hf upload username/my-llama ./llama-model .
示例 2:管理 Space
bash
创建 Space
hf repos create my-app --type space
上传代码
hf upload username/my-app ./app.py
开发时热重载
hf spaces hot-reload username/my-app app.py
示例 3:批量操作
bash
下载所有 safetensors 文件
hf download meta-llama/Llama-3.2-1B-Instruct --include *.safetensors
上传并创建 PR
hf upload username/model . . --create-pr --commit-message=Update model
注意事项
- 1. 令牌管理:确保设置了 HF_TOKEN 环境变量,或使用 --token 参数
- 大文件上传:对于大文件夹,考虑使用 hf upload-large-folder
- Space 热重载:仅适用于 Gradio 6.1+,实验性功能
- 免费 Space 限制:
- 免费固定 vCPU:2
- 内存:16GB
- 无持久存储(使用外部存储或 HF 数据集)
资源