Hub Python 函式庫文件
命令列介面 (CLI)
並獲得增強的文件體驗
開始使用
命令列介面 (CLI)
huggingface_hub Python 套件內建了一個稱為 hf 的 CLI 工具。此工具讓您可以直接從終端機與 Hugging Face Hub 進行互動。例如,您可以登入帳戶、建立儲存庫、上傳和下載檔案等。它還具備設定機器或管理快取的便利功能。在本指南中,我們將介紹 CLI 的主要功能及其使用方式。
本指南涵蓋
hfCLI 最重要的功能。如需所有指令與選項的完整參考資料,請參閱 CLI 參考手冊。
想在 AI 代理 (AI agents) 中使用
hfCLI 嗎?請安裝該 Skill 並查看 AI 代理的 Hugging Face CLI 指南。# for Codex, Cursor, OpenCode, Pi and other agents that load skills from `.agents/skills` hf skills add # includes the above + Claude Code hf skills add --claude
入門指南
獨立安裝程式 (推薦)
您可以使用單一指令安裝 hf CLI
在 macOS 和 Linux 上
>>> curl -LsSf https://huggingface.co/cli/install.sh | bash在 Windows 上
>>> powershell -ExecutionPolicy ByPass -c "irm https://huggingface.co/cli/install.ps1 | iex"安裝完成後,您可以檢查 CLI 是否設定正確
>>> hf --help
Usage: hf [OPTIONS] COMMAND [ARGS]...
Hugging Face Hub CLI
Options:
--install-completion Install completion for the current shell.
--show-completion Show completion for the current shell, to copy it or customize the installation.
-h, --help Show this message and exit.
Main commands:
auth Manage authentication (login, logout, etc.).
buckets Commands to interact with buckets.
cache Manage local cache directory.
collections Interact with collections on the Hub.
datasets Interact with datasets on the Hub.
download Download files from the Hub.
endpoints Manage Hugging Face Inference Endpoints.
extensions Manage hf CLI extensions.
jobs Run and manage Jobs on the Hub.
models Interact with models on the Hub.
papers Interact with papers on the Hub.
repo Manage repos on the Hub.
skills Manage skills for AI assistants.
spaces Interact with spaces on the Hub.
sync Sync files between local directory and a bucket.
upload Upload a file or a folder to the Hub.
upload-large-folder Upload a large folder to the Hub.
Help commands:
env Print information about the environment.
version Print information about the hf version.如果 CLI 安裝正確,您應該會看到 CLI 中所有可用選項的列表。如果您收到類似 command not found: hf 的錯誤訊息,請參閱 安裝 指南。
--help選項對於取得指令的詳細資訊非常方便。您可以隨時使用它來列出所有可用的選項及其說明。例如,hf upload --help會提供關於如何使用 CLI 上傳檔案的更多資訊。
使用 uv
使用 hf CLI 最簡單的方法是透過 uvx。它始終在隔離環境中執行最新版本——無需安裝!
請確保已先安裝 uv。請參閱 uv 安裝指南 以取得說明。
然後直接使用 CLI
>>> uvx hf auth login
>>> uvx hf download
>>> uvx hf ...
uvx hf會使用hfPyPI 套件。
使用 pip 安裝
該 CLI 也隨核心 huggingface_hub 套件一同提供
>>> pip install -U "huggingface_hub"使用 Homebrew
您也可以使用 Homebrew 安裝 CLI
>>> brew install hf請點擊 這裡 查看 Homebrew huggingface 頁面以了解更多細節。
更新
若要升級至最新版本,請執行
>>> hf update這會偵測 hf 的安裝方式(Homebrew、獨立安裝程式或 pip)並執行相應的更新指令。
預設情況下,當 PyPI 上有更新版本時,CLI 也會向 stderr 列印一行黃色警告。若要隱藏它(例如在離線 CI 中),請設定 HF_HUB_DISABLE_UPDATE_CHECK=1。
輸出格式化
大多數 hf 指令都支援同一組全域格式化旗標。它們記錄在每個 --help 頁面的專屬 格式化選項 (Formatting options) 章節中,並且可以新增至任何指令,無需針對每個指令個別宣告。
| 旗標 | 等效 | 說明 |
|---|---|---|
--format <value> | — | 明確選擇輸出格式。接受的值:auto(預設)、human、agent、json、quiet。 |
--json | --format json | 列印結構化 JSON。適合導向至 jq 或其他指令碼。 |
-q, --quiet | --format quiet | 僅列印 ID(每行一個)。適合將 ID 導向至其他指令。 |
--no-truncate | — | 在人類可讀的表格中顯示完整的純量值,而不是用 ... 縮減長值。列表和字典類型的欄位(例如 tags)會保持縮減;若要查看完整內容,請使用 --format json。 |
auto(預設)在互動式終端機中選擇 human,當 CLI 由 AI 代理調用時則選擇 agent。human 會新增顏色和美觀的表格;agent 產生無縮減的定位字元分隔值 (TSV);json 輸出精簡的 JSON 物件或陣列。當您需要完整的純量值時(例如長快取 ID),請在人類可讀的表格中使用 --no-truncate;對於完整的列表或字典欄位,請使用 --format json。混合使用兩個輸出模式旗標(例如同時使用 --json 和 --format table)會導致使用錯誤。
# JSON output for scripting
>>> hf models ls --search bert --limit 2 --json | jq '.[].id'
# IDs only, one per line
>>> hf collections ls --owner nvidia -q
nvidia/nemotron-supervised-fine-tuning-69eab9824c9120a3a3b1e25e
nvidia/nvidia-nemotron-v3-69388dda16167bb1607171ea少數指令保留了它們自己的本機格式化選項。例如,
hf jobs ps和hf jobs scheduled ps接受透過--format傳入的 Go 範本(例如--format '{{.id}} {{.status}}');hf buckets sync有自己的-q/--quiet來控制同步的詳細程度。在這些情況下,全域旗標會被隱式改寫,因此使用者面對的行為保持不變。
hf auth login
在許多情況下,您必須登入 Hugging Face 帳戶才能與 Hub 互動(下載私人儲存庫、上傳檔案、建立 PR 等)。為此,您需要從 設定頁面 取得 使用者存取權杖 (User Access Token)。使用者存取權杖用於向 Hub 驗證您的身分。若您想要上傳或修改內容,請確保設定一個具有寫入權限的權杖。
取得權杖後,請在終端機執行以下指令
>>> hf auth login如果您已經登入,此指令將跳過提示並顯示一則訊息。若要強制重新登入(例如更換權杖),請使用 --force
>>> hf auth login --force如果您尚未登入,該指令會提示您輸入權杖。複製貼上您的權杖並按 Enter。接著,系統會詢問是否應將權杖儲存為 git 憑證。如果您打算在本地使用 git,請再次按 Enter(預設為 yes)。最後,它將呼叫 Hub 以驗證您的權杖是否有效,並將其儲存在本地。
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
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)
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或者,如果您不想在輸入提示下登入,可以直接從命令列傳遞權杖。為了更安全,我們建議將權杖作為環境變數傳遞,以避免將其留在指令歷史記錄中。
# Or using an environment variable
>>> hf auth login --token $HF_TOKEN --add-to-git-credential
Token is valid (permission: write).
The token `token_name` has been saved to /home/wauplin/.cache/huggingface/stored_tokens
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
The current active token is: `token_name`如需更多關於身份驗證的詳細資訊,請參閱此章節。
hf auth whoami
若要確認您是否已登入,可以使用 hf auth whoami。此指令沒有任何選項,僅會列印您的使用者名稱以及您所屬的 Hub 組織。
hf auth whoami
Wauplin
orgs: huggingface,eu-test,OAuthTesters,hf-accelerate,HFSmolCluster如果您尚未登入,將會列印錯誤訊息。
hf auth logout
此指令會登出您的帳戶。實際上,它將刪除儲存在您電腦上的所有權杖。如果您想移除特定權杖,可以將權杖名稱作為參數指定。
如果您是使用 HF_TOKEN 環境變數登入,則此指令將不會登出您(請參閱 參考手冊)。若為此情況,您必須在機器設定中取消設定該環境變數。
hf download
使用 hf download 指令直接從 Hub 下載檔案。在內部,它使用了 下載 指南中描述的相同 hf_hub_download() 和 snapshot_download() 輔助函式,並將返回的路徑列印至終端機。在下方的範例中,我們將逐步介紹最常見的使用案例。若要查看所有可用選項的完整列表,您可以執行
hf download --help下載單一檔案
要從儲存庫下載單一檔案,只需提供 repo_id 和檔案名稱,如下所示
>>> hf download gpt2 config.json
downloading https://huggingface.co/gpt2/resolve/main/config.json to /home/wauplin/.cache/huggingface/hub/tmpwrq8dm5o
(…)ingface.co/gpt2/resolve/main/config.json: 100%|██████████████████████████████████| 665/665 [00:00<00:00, 2.49MB/s]
/home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10/config.json該指令總會在最後一行列印您本地機器上該檔案的路徑。
若要下載位於儲存庫子目錄中的檔案,您應該以 posix 格式提供檔案在儲存庫中的路徑,如下所示
>>> hf download HiDream-ai/HiDream-I1-Full text_encoder/model.safetensors下載整個儲存庫
在某些情況下,您只想從儲存庫下載所有檔案。這可以透過僅指定儲存庫 ID 來完成
>>> hf download HuggingFaceH4/zephyr-7b-beta
Fetching 23 files: 0%| | 0/23 [00:00<?, ?it/s]
...
...
/home/wauplin/.cache/huggingface/hub/models--HuggingFaceH4--zephyr-7b-beta/snapshots/3bac358730f8806e5c3dc7c7e19eb36e045bf720下載多個檔案
您也可以使用單一指令從儲存庫下載檔案的子集。這可以透過兩種方式完成。如果您已經有要下載檔案的精確清單,只需依序提供它們即可
>>> hf download gpt2 config.json model.safetensors
Fetching 2 files: 0%| | 0/2 [00:00<?, ?it/s]
downloading https://huggingface.co/gpt2/resolve/11c5a3d5811f50298f278a704980280950aedb10/model.safetensors to /home/wauplin/.cache/huggingface/hub/tmpdachpl3o
(…)8f278a7049802950aedb10/model.safetensors: 100%|██████████████████████████████| 8.09k/8.09k [00:00<00:00, 40.5MB/s]
Fetching 2 files: 100%|████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 3.76it/s]
/home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10另一種方法是使用 --include 和 --exclude 提供模式來篩選您要下載的檔案。例如,如果您想從 stabilityai/stable-diffusion-xl-base-1.0 下載所有 safetensors 檔案,但不包含 FP16 精度的檔案
>>> hf download stabilityai/stable-diffusion-xl-base-1.0 --include "*.safetensors" --exclude "*.fp16.*"*
Fetching 8 files: 0%| | 0/8 [00:00<?, ?it/s]
...
...
Fetching 8 files: 100%|█████████████████████████████████████████████████████████████████████████| 8/8 (...)
/home/wauplin/.cache/huggingface/hub/models--stabilityai--stable-diffusion-xl-base-1.0/snapshots/462165984030d82259a11f4367a4eed129e94a7b下載資料集或 Space
上面的範例顯示了如何從模型儲存庫下載。若要下載資料集或 Space,請使用 --repo-type 選項
# https://huggingface.co/datasets/HuggingFaceH4/ultrachat_200k
>>> hf download HuggingFaceH4/ultrachat_200k --repo-type dataset
# https://huggingface.co/spaces/HuggingFaceH4/zephyr-chat
>>> hf download HuggingFaceH4/zephyr-chat --repo-type space
...下載特定修訂版本
上面的範例顯示了如何從主分支的最新提交下載。若要從特定修訂版本(提交雜湊值、分支名稱或標籤)下載,請使用 --revision 選項
>>> hf download bigcode/the-stack --repo-type dataset --revision v1.1
...下載至本地資料夾
從 Hub 下載檔案的建議(也是預設)方式是使用快取系統。然而在某些情況下,您希望下載檔案並將其移動到特定資料夾。這對於讓工作流程更接近 git 指令提供的功能非常有用。您可以使用 --local-dir 選項來完成此操作。
一個 .cache/huggingface/ 資料夾會在您的本地目錄根目錄中建立,其中包含有關已下載檔案的中繼資料。這可以防止在檔案已是最新狀態時重新下載。如果中繼資料已變更,則會下載新的檔案版本。這使得 local-dir 最適合僅拉取最新的變更。
關於下載至本地檔案如何運作的更多詳細資訊,請查看 下載 指南。
>>> hf download adept/fuyu-8b model-00001-of-00002.safetensors --local-dir fuyu
...
fuyu/model-00001-of-00002.safetensors預演模式 (Dry-run)
在某些情況下,您可能希望在實際下載之前先檢查會下載哪些檔案。您可以使用 --dry-run 參數來檢查。它會列出儲存庫中要下載的所有檔案,並檢查它們是否已經下載。這讓您了解有多少檔案需要下載以及它們的大小。
>>> hf download openai-community/gpt2 --dry-run
[dry-run] Fetching 26 files: 100%|█████████████| 26/26 [00:04<00:00, 6.26it/s]
[dry-run] Will download 11 files (out of 26) totalling 5.6G.
File Bytes to download
--------------------------------- -----------------
.gitattributes -
64-8bits.tflite 125.2M
64-fp16.tflite 248.3M
64.tflite 495.8M
README.md -
config.json -
flax_model.msgpack 497.8M
generation_config.json -
merges.txt -
model.safetensors 548.1M
onnx/config.json -
onnx/decoder_model.onnx 653.7M
onnx/decoder_model_merged.onnx 655.2M
onnx/decoder_with_past_model.onnx 653.7M
onnx/generation_config.json -
onnx/merges.txt -
onnx/special_tokens_map.json -
onnx/tokenizer.json -
onnx/tokenizer_config.json -
onnx/vocab.json -
pytorch_model.bin 548.1M
rust_model.ot 702.5M
tf_model.h5 497.9M
tokenizer.json -
tokenizer_config.json -
vocab.json -欲了解更多詳細資訊,請查看 下載指南。
指定快取目錄
如果不使用 --local-dir,所有檔案預設都會下載到由 HF_HOME 環境變數 定義的快取目錄中。您可以使用 --cache-dir 指定自訂快取
>>> hf download adept/fuyu-8b --cache-dir ./path/to/cache
...
./path/to/cache/models--adept--fuyu-8b/snapshots/ddcacbcf5fdf9cc59ff01f6be6d6662624d9c745指定權杖
若要存取私人或受限制的儲存庫,您必須使用權杖。預設情況下,將使用儲存在本地的權杖(透過 hf auth login 儲存)。如果您想明確指定身分驗證,請使用 --token 選項
>>> hf download gpt2 config.json --token=hf_****
/home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10/config.json安靜模式 (Quiet mode)
預設情況下,hf download 指令會很詳細。它會列印警告訊息、有關下載檔案的資訊以及進度條等詳細資訊。如果您想隱藏所有這些內容,請使用 --quiet 選項。只有最後一行(即下載檔案的路徑)會被列印出來。如果您想將輸出內容傳遞給指令碼中的另一個指令,這將非常有用。
>>> hf download gpt2 --quiet
/home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10下載逾時
在連線速度較慢的機器上,您可能會遇到如下的逾時問題
`httpx.TimeoutException: (TimeoutException("HTTPSConnectionPool(host='cdn-lfs-us-1.huggingface.co', port=443): Read timed out. (read timeout=10)"), '(Request ID: a33d910c-84c6-4514-8362-c705e2039d38)')`若要減輕此問題,您可以將 HF_HUB_DOWNLOAD_TIMEOUT 環境變數設定為較大的值(預設為 10)
export HF_HUB_DOWNLOAD_TIMEOUT=30欲了解更多詳細資訊,請查看 環境變數參考手冊。然後重新執行您的下載指令。
hf upload
使用 hf upload 指令直接將檔案上傳到 Hub。在內部,它使用了 上傳 指南中描述的相同 upload_file() 和 upload_folder() 輔助函式。在下方的範例中,我們將逐步介紹最常見的使用案例。若要查看所有可用選項的完整列表,您可以執行
>>> hf upload --help上傳整個資料夾
此指令的預設用法是
# Usage: hf upload [repo_id] [local_path] [path_in_repo]若要將當前目錄上傳到儲存庫的根目錄,請使用
>>> hf upload my-cool-model . .
https://huggingface.co/Wauplin/my-cool-model/tree/main/如果儲存庫尚未存在,它會自動建立。
您也可以上傳特定資料夾
>>> hf upload my-cool-model ./models .
https://huggingface.co/Wauplin/my-cool-model/tree/main/最後,您可以將資料夾上傳到儲存庫中的特定目的地
>>> hf upload my-cool-model ./path/to/curated/data /data/train
https://huggingface.co/Wauplin/my-cool-model/tree/main/data/train上傳單一檔案
您也可以透過將 local_path 指向您機器上的檔案來上傳單一檔案。在此情況下,path_in_repo 是選用的,預設為您本地檔案的名稱
>>> hf upload Wauplin/my-cool-model ./models/model.safetensors
https://huggingface.co/Wauplin/my-cool-model/blob/main/model.safetensors如果您想將單一檔案上傳到特定目錄,請相應地設定 path_in_repo
>>> hf upload Wauplin/my-cool-model ./models/model.safetensors /vae/model.safetensors
https://huggingface.co/Wauplin/my-cool-model/blob/main/vae/model.safetensors上傳多個檔案
若要一次上傳資料夾中的多個檔案而不上傳整個資料夾,請使用 --include 和 --exclude 模式。它也可以與 --delete 選項結合使用,以便在上傳新檔案時刪除儲存庫中的舊檔案。在下方的範例中,我們同步本地 Space,方式為刪除遠端檔案並上傳除了 /logs 中的所有檔案
# Sync local Space with Hub (upload new files except from logs/, delete removed files)
>>> hf upload Wauplin/space-example --repo-type=space --exclude="/logs/*" --delete="*" --commit-message="Sync local Space with Hub"
...上傳至資料集或 Space
若要上傳至資料集或 Space,請使用 --repo-type 選項
>>> hf upload Wauplin/my-cool-dataset ./data /train --repo-type=dataset
...上傳至組織
若要將內容上傳至組織擁有的儲存庫,而不是個人儲存庫,您必須在 repo_id 中明確指定它
>>> hf upload MyCoolOrganization/my-cool-model . .
https://huggingface.co/MyCoolOrganization/my-cool-model/tree/main/上傳至特定修訂版本
預設情況下,檔案會上傳至 main 分支。如果您想將檔案上傳至其他分支或參考,請使用 --revision 選項
# Upload files to a PR
>>> hf upload bigcode/the-stack . . --repo-type dataset --revision refs/pr/104
...注意:如果 revision 不存在且未設定 --create-pr,則會自動從 main 分支建立一個分支。
上傳並建立 PR
如果您沒有推送到儲存庫的權限,您必須開啟一個 PR,讓作者知道您想要進行的變更。這可以透過設定 --create-pr 選項來完成
# Create a PR and upload the files to it
>>> hf upload bigcode/the-stack . . --repo-type dataset --revision refs/pr/104
https://huggingface.co/datasets/bigcode/the-stack/blob/refs%2Fpr%2F104/定期上傳
在某些情況下,您可能希望定期將更新推送到儲存庫。例如,如果您正在訓練模型,並且想要每 10 分鐘上傳一次 logs 資料夾,這將非常有用。您可以使用 --every 選項來完成此操作
# Upload new logs every 10 minutes
hf upload training-model logs/ --every=10指定提交訊息
使用 --commit-message 和 --commit-description 來設定自訂的提交訊息與描述,而非預設訊息
>>> hf upload Wauplin/my-cool-model ./models . --commit-message="Epoch 34/50" --commit-description="Val accuracy: 68%. Check tensorboard for more details."
...
https://huggingface.co/Wauplin/my-cool-model/tree/main指定權杖
若要上傳檔案,您必須使用權杖。預設情況下,將使用儲存在本地的權杖(透過 hf auth login 儲存)。如果您想明確指定身分驗證,請使用 --token 選項
>>> hf upload Wauplin/my-cool-model ./models . --token=hf_****
...
https://huggingface.co/Wauplin/my-cool-model/tree/main安靜模式 (Quiet mode)
預設情況下,hf upload 指令會很詳細。它會列印警告訊息、有關已上傳檔案的資訊以及進度條等詳細資訊。如果您想隱藏所有這些內容,請使用 --quiet 選項。只有最後一行(即已上傳檔案的 URL)會被列印出來。如果您想將輸出內容傳遞給指令碼中的另一個指令,這將非常有用。
>>> hf upload Wauplin/my-cool-model ./models . --quiet
https://huggingface.co/Wauplin/my-cool-model/tree/mainhf upload-large-folder
使用 hf upload-large-folder 將非常大的資料夾(數百 GB 甚至 TB)上傳到 Hub。此指令針對可續傳上傳進行了最佳化,並能妥善處理失敗情況。
# Upload a large folder to a model repository
>>> hf upload-large-folder Wauplin/my-cool-model ./large_model_dir
# Upload to a specific revision
>>> hf upload-large-folder Wauplin/my-cool-model ./large_model_dir --revision v1.0
# Upload a dataset
>>> hf upload-large-folder Wauplin/my-cool-dataset ./large_data_dir --repo-type dataset該指令會自動
- 將大型檔案分割成區塊,以實現可靠的上傳
- 從中斷處恢復上傳
- 妥善處理網路故障
當您有非常大的檔案或可能需要很長時間才能上傳的資料夾時,請使用
hf upload-large-folder。對於較小的上傳,建議使用hf upload。
hf buckets
使用 hf buckets 管理 Hugging Face Hub 上的儲存貯體 (buckets)。Buckets 在 Hugging Face 上提供類似 S3 的物件儲存,由 Xet 儲存後端驅動。與基於 git 並追蹤檔案歷史記錄的儲存庫不同,buckets 是專為大型檔案設計的遠端物件儲存容器,具有內容定址去重功能。它們適用於需要簡單、快速、可變儲存的使用案例,例如儲存訓練檢查點、記錄、中間產物或任何不需要版本控制的大量檔案集合。在下方的範例中,我們將逐步介紹最常見的使用案例。如需完整指南,請參閱 Buckets 指南。
建立 bucket
若要建立新的 bucket,請使用 hf buckets create。預設情況下,bucket 將會在您的命名空間下建立
>>> hf buckets create my-bucket您也可以使用 --private 旗標建立私人 bucket
>>> hf buckets create my-bucket --private列出並查看 buckets
若要列出您所有的 buckets,請使用 hf buckets list(或其簡寫 hf buckets ls)。您也可以列出特定組織中的 buckets
>>> hf buckets list
ID PRIVATE SIZE TOTAL_FILES CREATED_AT
-------------------- ------- ---------- ----------- ----------
username/my-bucket 32 5 2026-02-16
username/checkpoints 117609095 700 2026-02-13
username/logs 321757477 2000 2026-02-13
# Human-readable sizes
>>> hf buckets list -h
ID PRIVATE SIZE TOTAL_FILES CREATED_AT
-------------------- ------- -------- ----------- ----------
username/my-bucket 32 B 5 2026-02-16
username/checkpoints 117.6 MB 700 2026-02-13
username/logs 321.8 MB 2000 2026-02-13
# List buckets in a specific namespace
>>> hf buckets ls my-org
# Filter buckets by name
>>> hf buckets list --search "checkpoint"若要取得特定 bucket 的詳細資訊(以 JSON 返回),請使用 hf buckets info
>>> hf buckets info username/my-bucket
{
"id": "username/my-bucket",
"private": false,
"created_at": "2026-02-16T15:28:32+00:00",
"size": 32,
"total_files": 5
}刪除 bucket
若要刪除 bucket,請使用 hf buckets delete。系統會提示您確認,除非您加上 --yes
>>> hf buckets delete username/my-bucket --yes移除檔案
使用 hf buckets remove(或其簡寫 hf buckets rm)從 bucket 中移除檔案。
若要移除單一檔案,請指定其路徑
>>> hf buckets rm username/my-bucket/old-model.bin若要移除特定前綴下的所有檔案,請使用 --recursive
>>> hf buckets rm username/my-bucket/logs/ --recursive您也可以針對不帶前綴的 bucket 中的所有檔案進行操作
>>> hf buckets rm username/my-bucket --recursive --include "*.tmp"使用 --dry-run 預覽將會被刪除的內容,而不實際進行任何刪除
>>> hf buckets rm username/my-bucket/checkpoints/ --recursive --dry-run瀏覽檔案
使用帶有 bucket ID 的 hf buckets list 來列出 bucket 中的檔案
>>> hf buckets list username/my-bucket
2048 2026-01-15 10:30:00 big.bin
5 2026-01-15 10:30:00 file.txt
2026-01-15 10:30:00 sub/新增 -R 進行遞迴列表,並新增 -h 以顯示人類可讀的檔案大小和簡短日期。您也可以使用 --tree 顯示 ASCII 樹狀檢視,或使用 --tree --quiet 獲得沒有中繼資料的乾淨樹狀圖
# Recursive with human-readable sizes
>>> hf buckets list username/my-bucket -R -h
2.0 KB Jan 15 10:30 big.bin
5 B Jan 15 10:30 file.txt
14 B Jan 15 10:30 sub/nested.txt
4 B Jan 15 10:30 sub/deep/file.txt
# Tree with human-readable sizes
>>> hf buckets list username/my-bucket --tree -h -R
2.0 KB Jan 15 10:30 ├── big.bin
5 B Jan 15 10:30 ├── file.txt
└── sub/
├── deep/
4 B Jan 15 10:30 │ └── file.txt
14 B Jan 15 10:30 └── nested.txt
# Clean tree without metadata
>>> hf buckets list username/my-bucket --tree --quiet -R
├── big.bin
├── file.txt
└── sub/
├── deep/
│ └── file.txt
└── nested.txt若要依前綴篩選,請將前綴附加到 bucket 路徑
>>> hf buckets list username/my-bucket/sub -R複製檔案
使用 hf buckets cp 將本地檔案複製到 bucket,或將託管在 Hub 上的任何檔案複製到 bucket。
若要上傳檔案
>>> hf buckets cp ./config.json hf://buckets/username/my-bucket您可以上傳到特定的子目錄
>>> hf buckets cp ./data.csv hf://buckets/username/my-bucket/logs/若要下載檔案
>>> hf buckets cp hf://buckets/username/my-bucket/config.json ./config.json您也可以使用 - 串流至標準輸出 (stdout) 或從標準輸入 (stdin) 串流
# Download to stdout
>>> hf buckets cp hf://buckets/username/my-bucket/config.json - | jq .
# Upload from stdin
>>> echo "hello" | hf buckets cp - hf://buckets/username/my-bucket/hello.txt若要從 Hub 上的儲存庫或 bucket 進行複製
# Bucket to bucket
>>> hf buckets cp hf://buckets/username/source-bucket/logs/ hf://buckets/username/archive-bucket/logs/
# Repo to bucket
>>> hf buckets cp hf://datasets/username/my-dataset/data/train/ hf://buckets/username/my-bucket/datasets/train/複製資料夾時,來源路徑末尾的 / 會控制該資料夾本身是否被巢狀處理,還是僅複製其內容(類似 rsync 風格)
# Without trailing slash: "logs" dir is nested => archive/logs/...
>>> hf buckets cp hf://buckets/username/my-bucket/logs hf://buckets/username/archive-bucket/
# With trailing slash: only contents of "logs" are copied => archive/...
>>> hf buckets cp hf://buckets/username/my-bucket/logs/ hf://buckets/username/archive-bucket/備註:
- 目前尚未支援 Bucket 到儲存庫的複製。
同步目錄
使用 hf buckets sync 同步本地機器和 bucket 之間的目錄。它會比較來源和目的地,並僅傳輸變更過的檔案。
若要將本地目錄上傳到 bucket
>>> hf buckets sync ./data hf://buckets/username/my-bucket若要從 bucket 下載到本地目錄
>>> hf buckets sync hf://buckets/username/my-bucket ./data使用 --delete 移除目的地中存在但來源中不存在的檔案
>>> hf buckets sync ./data hf://buckets/username/my-bucket --delete您可以使用 --include 和 --exclude 模式來篩選要同步的檔案
>>> hf buckets sync ./data hf://buckets/username/my-bucket --include "*.safetensors" --exclude "*.tmp"若要僅更新現有檔案(跳過新檔案),請使用 --existing。若要僅建立新檔案(跳過現有檔案),請使用 --ignore-existing
>>> hf buckets sync ./data hf://buckets/username/my-bucket --existing
>>> hf buckets sync ./data hf://buckets/username/my-bucket --ignore-existing為了增加安全性,您可以在執行之前產生同步計畫以供審閱,然後再套用它
# Generate a plan
>>> hf buckets sync ./data hf://buckets/username/my-bucket --plan sync-plan.jsonl
# Review and apply the plan
>>> hf buckets sync --apply sync-plan.jsonl使用 --dry-run 將同步計畫以 JSONL 格式列印至標準輸出 (stdout),而不執行任何操作。這適合導向至 jq 或其他工具
>>> hf buckets sync ./data hf://buckets/username/my-bucket --dry-run | jq .
hf sync是hf buckets sync的便利頂層別名。請參閱 Buckets 指南 以取得所有同步選項的完整詳情。
hf models
使用 hf models 列出 Hub 上的模型並取得特定模型的詳細資訊。
列出模型
# List trending models
>>> hf models ls
# Search for models
>>> hf models ls --search "lora"
# Filter by author
>>> hf models ls --author Qwen
# Filter by parameter count
>>> hf models ls --num-parameters min:6B,max:128B
# Sort by downloads
>>> hf models ls --sort downloads --limit 10當使用模型 ID 呼叫時,hf models ls 會列出該模型儲存庫中的檔案
# List files in a model repo
>>> hf models ls meta-llama/Llama-3.2-1B-Instruct
# List files recursively
>>> hf models ls meta-llama/Llama-3.2-1B-Instruct -R
# Tree view with human-readable sizes
>>> hf models ls meta-llama/Llama-3.2-1B-Instruct --tree -h
# List files at a specific revision
>>> hf models ls meta-llama/Llama-3.2-1B-Instruct --revision main取得模型資訊
>>> hf models info Lightricks/LTX-2使用 --expand 取得額外屬性,如 downloads、likes、tags 等。
取得模型卡片
使用 hf models card 取得模型的模型卡片 (README)。預設情況下,將完整的卡片內容列印至標準輸出 (stdout)。
# Full card (metadata + text)
>>> hf models card google/gemma-4-31B-it
# Just the metadata (from the YAML frontmatter)
>>> hf models card google/gemma-4-31B-it --metadata
# Metadata as JSON (useful for scripting and agents)
>>> hf models card google/gemma-4-31B-it --metadata --format json
# Just the text body (no YAML frontmatter)
>>> hf models card google/gemma-4-31B-it --texthf datasets
使用 hf datasets 列出 Hub 上的資料集並取得特定資料集的詳細資訊。
列出資料集
# List trending datasets
>>> hf datasets ls
# Search for datasets
>>> hf datasets ls --search "code"
# List official benchmark datasets
>>> hf datasets ls --filter benchmark:official
# Sort by downloads
>>> hf datasets ls --sort downloads --limit 10當使用資料集 ID 呼叫時,hf datasets ls 會列出該資料集儲存庫中的檔案
# List files in a dataset repo
>>> hf datasets ls HuggingFaceFW/fineweb
# List files recursively with human-readable sizes
>>> hf datasets ls HuggingFaceFW/fineweb -R -h
# Tree view
>>> hf datasets ls HuggingFaceFW/fineweb --tree列出資料集排行榜
使用 hf datasets leaderboard 顯示提交至基準測試資料集的模型分數,以便您可以找到特定任務的最佳模型,或透過基準測試分數來比較模型。
>>> hf datasets leaderboard SWE-bench/SWE-bench_Verified
>>> hf datasets leaderboard SWE-bench/SWE-bench_Verified --limit 5 --format json取得資料集資訊
>>> hf datasets info HuggingFaceFW/fineweb取得資料集卡片
使用 hf datasets card 取得資料集的資料集卡片 (README)。預設情況下,將完整的卡片內容列印至標準輸出 (stdout)。
# Full card (metadata + text)
>>> hf datasets card HuggingFaceFW/fineweb
# Just the metadata (from the YAML frontmatter)
>>> hf datasets card HuggingFaceFW/fineweb --metadata
# Metadata as JSON (useful for scripting and agents)
>>> hf datasets card HuggingFaceFW/fineweb --metadata --format json
# Just the text body (no YAML frontmatter)
>>> hf datasets card HuggingFaceFW/fineweb --text列出 parquet URL
在編寫 SQL 查詢之前,使用 hf datasets parquet 來搜尋資料集的 parquet 檔案 URL。Hub 上的資料集會由 Dataset Viewer 服務在後端自動轉換為 Parquet(適用於符合資格的資料集)。請參閱 Parquet 轉換指南 以了解詳細資訊。
>>> hf datasets parquet cfahlgren1/hub-stats
>>> hf datasets parquet cfahlgren1/hub-stats --subset models
>>> hf datasets parquet cfahlgren1/hub-stats --split train
>>> hf datasets parquet cfahlgren1/hub-stats --format json預設的表格輸出包括來自 Hub API 的子集、分割區和 parquet 檔案 URL。
對資料集 parquet 執行 SQL
使用 hf datasets sql 對資料集 parquet URL 執行 DuckDB 的原始 SQL 查詢。先用 hf datasets parquet 搜尋 URL,然後直接使用 read_parquet(...) 查詢它們。
>>> hf datasets sql "SELECT COUNT(*) AS rows FROM read_parquet('https://huggingface.co/api/datasets/cfahlgren1/hub-stats/parquet/models/train/0.parquet')"
>>> hf datasets sql "SELECT * FROM read_parquet('https://huggingface.co/api/datasets/cfahlgren1/hub-stats/parquet/models/train/0.parquet') LIMIT 5" --format json如有需要,請先安裝 DuckDB
# Python package
>>> pip install duckdb
# or standalone DuckDB CLI (Homebrew: macOS/Linux)
>>> brew install duckdbhf spaces
使用 hf spaces 列出 Hub 上的 Spaces 並取得特定 Space 的詳細資訊。
列出 Spaces
# List trending Spaces
>>> hf spaces ls
# Search for Spaces
>>> hf spaces ls --search "3d"
# Sort by likes
>>> hf spaces ls --sort likes --limit 10當呼叫時提供 Space ID,hf spaces ls 會列出該 Space 儲存庫中的檔案
# List files in a Space repo
>>> hf spaces ls victor/deepsite
# List files recursively with tree view
>>> hf spaces ls victor/deepsite --tree -R -h取得 Space 資訊
>>> hf spaces info victor/deepsite取得 Space 卡片
使用 hf spaces card 取得 Space 的卡片(README)。預設會將完整的卡片內容輸出至標準輸出 (stdout)。
# Full card (metadata + text)
>>> hf spaces card mteb/leaderboard
# Just the card metadata (from the YAML frontmatter)
>>> hf spaces card mteb/leaderboard --metadata
# Card metadata as JSON
>>> hf spaces card mteb/leaderboard --metadata --format json
# Just the text body (no YAML frontmatter)
>>> hf spaces card mteb/leaderboard --text暫停或重新啟動 Space 會移除其容器,因此寫入暫時性檔案系統的任何內容都會遺失。若要在重新啟動後保留資料,請使用
hf spaces volumes set掛載儲存卷或儲存桶(執行hf spaces volumes --help以取得詳細資訊)。
暫停 Space
當您不使用 Space 時,請使用 hf spaces pause 暫停它(暫停期間不計費)。稍後再使用 hf spaces restart 重新啟動。
>>> hf spaces pause username/my-space重新啟動 Space
使用 hf spaces restart 來重新啟動 Space。傳入 --factory-reboot 可在不使用建置快取的情況下,從頭開始重建 Space。
>>> hf spaces restart username/my-space
>>> hf spaces restart username/my-space --factory-reboot列出可用硬體
使用 hf spaces hardware 列出 Spaces 所有可用的硬體選項,包含定價。
>>> hf spaces hardware更新 Space 設定
使用 hf spaces settings 更新 Space 的設定。
>>> hf spaces settings username/my-space --sleep-time 3600
>>> hf spaces settings username/my-space --hardware t4-medium--sleep-time:Space 進入休眠前的閒置時間(秒)。使用-1可永不休眠。僅適用於升級後的硬體(請參閱 Spaces 休眠時間文件)。--hardware:硬體規格(例如cpu-basic、t4-medium、l4x4)。執行hf spaces hardware查看所有選項。
管理 Space 密鑰
使用 hf spaces secrets ls 列出 Space 上的密鑰,使用 hf spaces secrets add 新增或更新一或多個密鑰,並使用 hf spaces secrets delete 移除密鑰。傳入 --secrets-file PATH 可從 .env 格式的檔案載入密鑰。現有的鍵值會被覆寫。
>>> hf spaces secrets ls username/my-space
>>> hf spaces secrets add username/my-space -s OPENAI_API_KEY=sk-...
>>> hf spaces secrets add username/my-space --secrets-file .env.secrets
>>> hf spaces secrets delete username/my-space OPENAI_API_KEY --yes密鑰值是唯寫的,因此
hf spaces secrets ls僅顯示鍵、描述和更新時間戳,永遠不會顯示密鑰值本身。
管理 Space 環境變數
使用 hf spaces variables 管理 Space 上非密鑰的環境變數。與密鑰不同,變數是可以讀取的,因此 ls 會同時顯示鍵和值。在 add 時傳入 --env-file PATH 可從 .env 格式的檔案載入。
>>> hf spaces variables ls username/my-space
>>> hf spaces variables add username/my-space -e MODEL_ID=gpt2 -e MAX_TOKENS=512
>>> hf spaces variables add username/my-space --env-file .env
>>> hf spaces variables delete username/my-space MAX_TOKENS --yeshf papers
使用 hf papers 來列出、搜尋、取得結構化資訊,以及閱讀 Hugging Face Hub 上論文的 Markdown 內容。
列出論文
# List most recent daily papers
>>> hf papers ls
# List trending papers
>>> hf papers ls --sort=trending
# List papers from a specific date
>>> hf papers ls --date=2025-01-23
# List today's papers
>>> hf papers ls --date=today
# List papers from a specific week
>>> hf papers ls --week=2025-W09
# List papers from a specific month
>>> hf papers ls --month=2025-02
# List papers submitted by a specific user
>>> hf papers ls --submitter=akhaliq
# Limit results
>>> hf papers ls --sort=trending --limit=5搜尋論文
# Search papers by keyword
>>> hf papers search "vision language"
# Limit search results
>>> hf papers search "diffusion models" --limit=10
# Output as JSON
>>> hf papers search "attention" --format=json取得論文資訊
# Get structured metadata for a paper (returns JSON)
>>> hf papers info 2601.15621以 Markdown 格式閱讀論文
# Read the full paper content as markdown
>>> hf papers read 2601.15621hf discussions
使用 hf discussions 直接從您的終端管理 Hub 儲存庫上的討論與合併請求 (PR)。您可以列出、檢視、建立、評論、關閉、重新開啟及合併討論與 PR。關於 Hub 社群功能的完整指南,請參閱 討論與合併請求指南。
列出討論
若要列出儲存庫中的開放討論與 PR,請將儲存庫 ID 傳給 hf discussions list(或簡寫 hf discussions ls)。
>>> hf discussions list username/my-model您可以根據類型(discussion 或 pull_request)、狀態(open、closed、merged 或 all)或作者來縮小搜尋範圍。
>>> hf discussions list username/my-model --kind pull_request --status merged
>>> hf discussions list username/my-model --author alice若要進行腳本編寫,請使用 --format json 取得結構化輸出,或使用 --quiet 僅列印討論編號(每行一個)。
>>> hf discussions list username/my-model --format json
>>> hf discussions ls username/my-model --quiet取得討論或 PR 的資訊
若要檢查特定的討論或 PR,請傳入儲存庫 ID 與討論編號。
>>> hf discussions info username/my-model 5預設僅顯示討論的中繼資料(標題、狀態、作者等)。加入 --comments 以包含完整的對話串,或加入 --diff 顯示 PR 的差異內容。
>>> hf discussions info username/my-model 5 --comments
>>> hf discussions info username/my-model 5 --diff使用 --format json 進行機器可讀的輸出,並使用 --no-color 在管線傳輸至其他工具時移除 ANSI 色碼。
建立討論或 PR
若要開啟新討論,請使用 --title 提供標題。您可以選擇使用 --body 直接內嵌描述,或使用 --body-file 從檔案載入。
>>> hf discussions create username/my-model --title "Bug report"
>>> hf discussions create username/my-model --title "Feature request" --body "Please add X"
>>> hf discussions create username/my-model --title "Report" --body-file report.md若要建立合併請求而非一般討論,請加上 --pull-request 旗標。
>>> hf discussions create username/my-model --title "Fix typo" --pull-request評論討論或 PR
指定討論或 PR 編號即可對其進行評論。評論內容可以透過 --body 內嵌傳入,透過 --body-file 從檔案讀取,或使用 --body-file - 從標準輸入 (stdin) 導管傳入。
>>> hf discussions comment username/my-model 5 --body "Thanks for reporting!"
>>> hf discussions comment username/my-model 5 --body-file review.md
>>> echo "LGTM" | hf discussions comment username/my-model 5 --body-file -關閉、重新開啟與合併
您可以使用 hf discussions close 關閉討論或 PR。預設會提示您進行確認。傳入 --yes 可跳過提示,並使用 --comment 留下關閉訊息。
>>> hf discussions close username/my-model 5
>>> hf discussions close username/my-model 5 --yes --comment "Resolved"若要重新開啟已關閉的討論,請使用 hf discussions reopen。
>>> hf discussions reopen username/my-model 5 --yes若要合併合併請求,請使用 hf discussions merge。
>>> hf discussions merge username/my-model 5 --yes重新命名與差異對照
您可以透過提供新標題來重新命名討論。
>>> hf discussions rename username/my-model 5 "Updated title"若要直接在您的終端機查看合併請求的差異 (diff),請使用 hf discussions diff。
>>> hf discussions diff username/my-model 5hf repos
hf repos 讓您能在 Hugging Face Hub 上建立、刪除、移動儲存庫、更新其設定以及刪除檔案。它也包含管理分支與標籤的子指令。
建立儲存庫
>>> hf repos create Wauplin/my-cool-model
Successfully created Wauplin/my-cool-model on the Hub.
Your repo is now available at https://huggingface.co/Wauplin/my-cool-model建立私人資料集或 Space
>>> hf repos create my-cool-dataset --repo-type dataset --private
>>> hf repos create my-gradio-space --repo-type space --space-sdk gradio若儲存庫可能已存在,請使用 --exist-ok;若要針對企業資源群組,請使用 --resource-group-id。
在特定區域建立儲存庫
>>> hf repos create my-model --region us刪除儲存庫
>>> hf repos delete Wauplin/my-cool-model資料集與 Spaces
>>> hf repos delete my-cool-dataset --repo-type dataset
>>> hf repos delete my-gradio-space --repo-type space移動儲存庫
>>> hf repos move old-namespace/my-model new-namespace/my-model更新儲存庫設定
>>> hf repos settings Wauplin/my-cool-model --gated auto
>>> hf repos settings Wauplin/my-cool-model --private true
>>> hf repos settings Wauplin/my-cool-model --private false--gated:選項為auto、manual或false--private true|false:設定儲存庫隱私權
從儲存庫刪除檔案
hf repos delete-files <repo_id> 子指令允許您從儲存庫刪除檔案。以下是一些使用範例。
刪除資料夾
>>> hf repos delete-files Wauplin/my-cool-model folder/
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...刪除多個檔案
>>> hf repos delete-files Wauplin/my-cool-model file.txt folder/pytorch_model.bin
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...使用萬用字元模式刪除多個檔案。模式遵循 此處 記載的標準萬用字元(globbing 模式)。模式比對基於 fnmatch。
請注意,與傳統的 Unix shell globbing 不同,
fnmatch會跨越路徑邊界比對*。例如,"data/*.json"會比對data/file.json以及data/subdir/file.json。若僅要比對直接位於目錄下的檔案,您需要明確列出它們或使用更精確的模式。
>>> hf repos delete-files Wauplin/my-cool-model "*.txt" "folder/*.bin"
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...若要從儲存庫刪除檔案,您必須經過驗證並獲得授權。預設會使用本機儲存的 token(使用 hf auth login)。若要明確進行驗證,請使用 --token 選項。
>>> hf repos delete-files --token=hf_**** Wauplin/my-cool-model file.txthf repos branch
使用 hf repos branch 來建立和刪除 Hub 上儲存庫的分支。
# Create a branch
>>> hf repos branch create Wauplin/my-cool-model dev
# Create a branch from a specific revision
>>> hf repos branch create Wauplin/my-cool-model release-1 --revision refs/pr/104
# Delete a branch
>>> hf repos branch delete Wauplin/my-cool-model dev所有指令都接受
--repo-type(選項為model、dataset、space)及--token(如果您需要明確驗證)。對任何指令使用--help可查看所有選項。
hf cache
使用 hf cache 管理您的本機 Hugging Face 快取目錄。快取會儲存從 Hub 下載的模型、資料集及其他檔案。
# List cached repositories
>>> hf cache ls
# List cached revisions
>>> hf cache ls --revisions
# Remove specific items from cache
>>> hf cache rm model/gpt2
# Remove unreferenced revisions
>>> hf cache prune
# Verify cached file checksums
>>> hf cache verify gpt2hf cache ls
使用 hf cache ls 檢查本機 Hugging Face 快取儲存了什麼。預設會以儲存庫為單位進行聚合。
>>> hf cache ls
ID SIZE LAST_ACCESSED LAST_MODIFIED REFS
--------------------------- -------- ------------- ------------- -----------
dataset/nyu-mll/glue 157.4M 2 days ago 2 days ago main script
model/LiquidAI/LFM2-VL-1.6B 3.2G 4 days ago 4 days ago main
model/microsoft/UserLM-8b 32.1G 4 days ago 4 days ago main
Found 3 repo(s) for a total of 5 revision(s) and 35.5G on disk.加上 --revisions 可深入檢視特定的快照,並可鏈接篩選器以聚焦於重要的內容。
>>> hf cache ls --filter "size>30g" --revisions
ID REVISION SIZE LAST_MODIFIED REFS
------------------------- ---------------------------------------- -------- ------------- ----
model/microsoft/UserLM-8b be8f2069189bdf443e554c24e488ff3ff6952691 32.1G 4 days ago main
Found 1 repo(s) for a total of 1 revision(s) and 32.1G on disk.該指令支援多種用於腳本編寫的輸出格式:--format json 列印結構化物件,--format csv 寫入逗號分隔的列,而 --quiet 僅列印 ID。使用 --sort 可依 accessed(存取時間)、modified(修改時間)、name(名稱)或 size(大小)排序項目(附加 :asc 或 :desc 以控制順序),並使用 --limit 將結果限制在前 N 個項目。將這些與 --cache-dir 結合以鎖定替代的快取位置。請參閱 管理您的快取 指南以取得進階工作流程。
透過管線將 ID 傳入 hf cache rm,以刪除透過 hf cache ls --q 篩選出的快取項目。
>>> hf cache rm $(hf cache ls --filter "accessed>1y" -q) -y
About to delete 2 repo(s) totalling 5.31G.
- model/meta-llama/Llama-3.2-1B-Instruct (entire repo)
- model/hexgrad/Kokoro-82M (entire repo)
Delete repo: ~/.cache/huggingface/hub/models--meta-llama--Llama-3.2-1B-Instruct
Delete repo: ~/.cache/huggingface/hub/models--hexgrad--Kokoro-82M
Cache deletion done. Saved 5.31G.
Deleted 2 repo(s) and 2 revision(s); freed 5.31G.hf cache rm
hf cache rm 會移除已快取的儲存庫或個別修訂版本 (revisions)。傳入一或多個儲存庫 ID(例如 model/bert-base-uncased)、儲存庫層級的 hf:// URI 或修訂雜湊碼。
>>> hf cache rm model/LiquidAI/LFM2-VL-1.6B
About to delete 1 repo(s) totalling 3.2G.
- model/LiquidAI/LFM2-VL-1.6B (entire repo)
Proceed with deletion? [y/N]: y
Delete repo: ~/.cache/huggingface/hub/models--LiquidAI--LFM2-VL-1.6B
Cache deletion done. Saved 3.2G.
Deleted 1 repo(s) and 2 revision(s); freed 3.2G.也支援儲存庫層級的 hf:// URI。
>>> hf cache rm hf://models/openai-community/gpt2 --dry-run
About to delete 1 repo(s) totalling 1.1G.
- model/openai-community/gpt2 (entire repo)
Dry run: no files were deleted.在同一次呼叫中混合使用儲存庫和特定修訂版本。使用 --dry-run 預覽影響,或使用 --yes 跳過確認提示(在自動化腳本中非常實用)。
>>> hf cache rm model/t5-small 8f3ad1c --dry-run
About to delete 1 repo(s) and 1 revision(s) totalling 1.1G.
- model/t5-small:
8f3ad1c [main] 1.1G
Dry run: no files were deleted.當在預設快取位置之外工作時,請搭配使用 --cache-dir PATH。
hf cache prune
hf cache prune 是一個便利的快捷指令,可刪除快取中所有已分離(未參照)的修訂版本。這只會保留仍可透過分支或標籤存取的修訂版本。
>>> hf cache prune
About to delete 3 unreferenced revision(s) (2.4G total).
- model/t5-small:
1c610f6b [refs/pr/1] 820.1M
d4ec9b72 [(detached)] 640.5M
- dataset/google/fleurs:
2b91c8dd [(detached)] 937.6M
Proceed? [y/N]: y
Deleted 3 unreferenced revision(s); freed 2.4G.與其他快取指令一樣,支援 --dry-run、--yes 與 --cache-dir。請參考 管理您的快取 指南以取得更多範例。
hf cache verify
使用 hf cache verify 來驗證本機檔案與 Hub 上的總和檢查碼 (checksums) 是否相符。您可以驗證快取快照或一般本機目錄。
範例
# Verify main revision of a model in cache
>>> hf cache verify deepseek-ai/DeepSeek-OCR
# Verify a specific revision
>>> hf cache verify deepseek-ai/DeepSeek-OCR --revision refs/pr/5
>>> hf cache verify deepseek-ai/DeepSeek-OCR --revision ef93bf4a377c5d5ed9dca78e0bc4ea50b26fe6a4
# Verify a private repo
>>> hf cache verify me/private-model --token hf_***
# Verify a dataset
>>> hf cache verify karpathy/fineweb-edu-100b-shuffle --repo-type dataset
# Verify files in a local directory
>>> hf cache verify deepseek-ai/DeepSeek-OCR --local-dir /path/to/repo預設情況下,該指令會對缺失或多餘的檔案發出警告。使用旗標可將這些警告轉變為錯誤。
>>> hf cache verify deepseek-ai/DeepSeek-OCR --fail-on-missing-files --fail-on-extra-files成功後,您將看到摘要。
✅ Verified 13 file(s) for 'deepseek-ai/DeepSeek-OCR' (model) in ~/.cache/huggingface/hub/models--meta-llama--Llama-3.2-1B-Instruct/snapshots/9213176726f574b556790deb65791e0c5aa438b6 All checksums match.
若偵測到不符,指令會列印詳細清單並以非零狀態碼結束。
hf repos tag
使用 hf repos tag 來建立、列出和刪除 Hub 上儲存庫的標籤。
# Create a tag
>>> hf repos tag create my-model v1.0
# List tags
>>> hf repos tag list my-model
# Delete a tag
>>> hf repos tag delete my-model v1.0標記模型
若要標記儲存庫,您需要提供 repo_id 與 tag 名稱。
>>> hf repos tag create Wauplin/my-cool-model v1.0
You are about to create tag v1.0 on model Wauplin/my-cool-model
Tag v1.0 created on Wauplin/my-cool-model標記特定修訂版本的模型
若要標記特定修訂版本,您可以使用 --revision 選項。預設會在 main 分支上建立標籤。
>>> hf repos tag create Wauplin/my-cool-model v1.0 --revision refs/pr/104
You are about to create tag v1.0 on model Wauplin/my-cool-model
Tag v1.0 created on Wauplin/my-cool-model標記資料集或 Space
若要標記資料集或 Space,必須指定 --repo-type 選項。
>>> hf repos tag create bigcode/the-stack v1.0 --repo-type dataset
You are about to create tag v1.0 on dataset bigcode/the-stack
Tag v1.0 created on bigcode/the-stack列出標籤
若要列出儲存庫的所有標籤,請使用 list(或 ls)子指令。
>>> hf repos tag list Wauplin/gradio-space-ci --repo-type space
Tags for space Wauplin/gradio-space-ci:
0.2.2
0.2.1
0.2.0
0.1.2
0.0.2
0.0.1刪除標籤
若要刪除標籤,請使用 delete 子指令。
>>> hf repos tag delete Wauplin/my-cool-model v1.0
You are about to delete tag v1.0 on model Wauplin/my-cool-model
Proceed? [Y/n] y
Tag v1.0 deleted on Wauplin/my-cool-model您也可以傳入 -y 以跳過確認步驟。
hf env
hf env 指令會列印關於您機器設定的詳細資訊。當您在 GitHub 開立 issue 時,這有助於維護者調查您的問題。
>>> hf env
Copy-and-paste the text below in your GitHub issue.
- huggingface_hub version: 1.0.0.rc6
- Platform: Linux-6.8.0-85-generic-x86_64-with-glibc2.35
- Python version: 3.11.14
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Running in Google Colab Enterprise ?: No
- Token path ?: /home/wauplin/.cache/huggingface/token
- Has saved token ?: True
- Who am I ?: Wauplin
- Configured git credential helpers: store
- Installation method: unknown
- Torch: N/A
- httpx: 0.28.1
- hf_xet: 1.1.10
- gradio: 5.41.1
- tensorboard: N/A
- pydantic: 2.11.7
- ENDPOINT: https://huggingface.co
- HF_HUB_CACHE: /home/wauplin/.cache/huggingface/hub
- HF_ASSETS_CACHE: /home/wauplin/.cache/huggingface/assets
- HF_TOKEN_PATH: /home/wauplin/.cache/huggingface/token
- HF_STORED_TOKENS_PATH: /home/wauplin/.cache/huggingface/stored_tokens
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_DISABLE_XET: False
- HF_HUB_ETAG_TIMEOUT: 10
- HF_HUB_DOWNLOAD_TIMEOUT: 10hf jobs
以類似 Docker 的熟悉介面,在 Hugging Face 基礎設施上執行運算任務。
hf jobs 是一個命令列工具,讓您能使用簡單的指令在 Hugging Face 的基礎設施(包含 GPU 和 TPU!)上執行任何程式。想像一下 docker run,但用於在 A100 上執行程式碼。
關於 Jobs 與定價的總覽,請參閱 Hub Jobs 文件。 若要查看配合 CLI 使用的 Python API,請參閱 執行與管理 Jobs 指南。
# Directly run Python code
>>> hf jobs run python:3.12 python -c 'print("Hello from the cloud!")'
# Use GPUs without any setup
>>> hf jobs run --flavor a10g-small pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel \
... python -c "import torch; print(torch.cuda.get_device_name())"
# Run in an organization account
>>> hf jobs run --namespace my-org-name python:3.12 python -c 'print("Running in an org account")'
# Run from Hugging Face Spaces
>>> hf jobs run hf.co/spaces/lhoestq/duckdb duckdb -c 'select "hello world"'
# Run a Python script with `uv` (experimental)
>>> hf jobs uv run my_script.pyHugging Face Jobs 提供給任何擁有 預付點數 的使用者或組織使用。
快速入門
1. 執行您的第一個 Job
# Run a simple Python script
>>> hf jobs run python:3.12 python -c 'print("Hello from HF compute!")'此指令會執行 Job 並顯示日誌。您可以傳入 --detach 以在背景執行 Job 並僅列印 Job ID。
2. 檢查 Job 狀態
# List your running jobs
>>> hf jobs ps
# List all jobs
>>> hf jobs ps -a
# Inspect the status of a job
>>> hf jobs inspect <job_id>
# View logs from a job
>>> hf jobs logs <job_id>
# View resources usage stats and metrics of running jobs
>>> hf jobs stats
# View resources usage stats and metrics of some jobs
>>> hf jobs stats [job_ids]...
# Cancel a job
>>> hf jobs cancel <job_id>3. 在 GPU 上執行
您也可以使用 --flavor 選項在 GPU 或 TPU 上執行 Job。例如,在 A10G GPU 上執行 PyTorch Job:
# Use an A10G GPU to check PyTorch CUDA
>>> hf jobs run --flavor a10g-small pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel \
... python -c 'import torch; print(f"This code ran with the following GPU: {torch.cuda.get_device_name()}")'執行此指令將顯示以下輸出!
This code ran with the following GPU: NVIDIA A10G
可以使用 -- 將指令與 Job 選項分開,以利清晰,例如 hf jobs run --flavor a10g-small -- python -c '...'
就這樣!您現在正在 Hugging Face 的基礎設施上執行程式碼。
常見使用案例
- 模型訓練:在 GPU (T4, A10G, A100) 上微調或訓練模型,無需管理基礎設施
- 合成資料生成:使用 LLM 在強大硬體上產生大規模資料集
- 資料處理:使用高 CPU 配置處理海量資料集,以進行平行工作負載
- 批次推論:使用最佳化的 GPU 設定對數千個樣本執行離線推論
- 實驗與基準測試:在一致的硬體上執行機器學習實驗,以取得可重現的結果
- 開發與偵錯:無需在本機設定 CUDA 即可測試 GPU 程式碼
傳遞環境變數與密鑰
您可以使用以下方式傳遞環境變數給您的 Job:
# Pass environment variables
>>> hf jobs run -e FOO=foo -e BAR=bar python:3.12 python -c 'import os; print(os.environ["FOO"], os.environ["BAR"])'# Pass an environment from a local .env file
>>> hf jobs run --env-file .env python:3.12 python -c 'import os; print(os.environ["FOO"], os.environ["BAR"])'# Pass secrets - they will be encrypted server side
>>> hf jobs run -s MY_SECRET=psswrd python:3.12 python -c 'import os; print(os.environ["MY_SECRET"])'# Pass secrets from a local .env.secrets file - they will be encrypted server side
>>> hf jobs run --secrets-file .env.secrets python:3.12 python -c 'import os; print(os.environ["MY_SECRET"])'使用
--secrets HF_TOKEN隱含傳遞您的本機 Hugging Face token。使用此語法,密鑰會從環境變數中擷取。若環境變數未設定,對於HF_TOKEN,它可能會讀取位於 Hugging Face 家目錄中的 token 檔案。
內建環境變數
在 Job 容器內,以下環境變數會自動可用:
| 可變 | 說明 |
|---|---|
JOB_ID | 目前 Job 的唯一識別碼。使用此識別碼以程式方式參照 Job。 |
ACCELERATOR | 可用的加速器類型(例如 t4-medium、a10g-small、a100x4)。若無加速器則為空。 |
CPU_CORES | 分配給 Job 的 CPU 核心數(例如 2、4、8)。 |
MEMORY | 分配給 Job 的記憶體量(例如 16Gi、32Gi)。 |
# Access job environment information
>>> hf jobs run python:3.12 python -c "import os; print(f'Job: {os.environ.get(\"JOB_ID\")}, CPU: {os.environ.get(\"CPU_CORES\")}, Mem: {os.environ.get(\"MEMORY\")}')"Job 超時
Jobs 預設超時時間為 30 分鐘,之後會自動停止。對於模型訓練等長時間執行的任務,請使用 --timeout 選項設定自訂超時時間。
# Set timeout in seconds (default unit)
>>> hf jobs run --timeout 7200 python:3.12 python train.py
# Use time units: s (seconds), m (minutes), h (hours), d (days)
>>> hf jobs run --timeout 2h pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel python train.py
>>> hf jobs run --timeout 90m python:3.12 python process_data.py
>>> hf jobs run --timeout 1.5h python:3.12 python train.py # floats are supported--timeout 選項也適用於 UV 指令碼與排程 Job。
# UV script with timeout
>>> hf jobs uv run --timeout 2h training_script.py
# Scheduled job with timeout
>>> hf jobs scheduled run @daily --timeout 4h python:3.12 python daily_task.py如果您的 Job 超過超時時間,它將會自動終止。請務必針對長時間執行的任務設定適當的超時時間並保留緩衝,以避免意外終止。
硬體
可用的 --flavor 選項
- CPU:
cpu-basic、cpu-upgrade - GPU:
t4-small、t4-medium、l4x1、l4x4、a10g-small、a10g-large、a10g-largex2、a10g-largex4、a100-large - TPU:
v5e-1x1、v5e-2x2、v5e-2x4
(於 2025 年 7 月根據 Hugging Face suggested_hardware 文件 更新)
儲存卷 (Volumes)
使用 -v 或 --volume 在 Job 的磁碟上掛載儲存卷。
您可以使用 hf:// URL 配置掛載任何 Hugging Face 儲存庫(模型/資料集/空間)或 儲存桶。例如:
- 掛載模型儲存庫:
-v hf://openai/gpt-oss-120b:/model - 掛載資料集儲存庫:
-v hf://datasets/HuggingFaceFW/fineweb:/data - 掛載儲存桶:
-v hf://buckets/username/my-bucket:/mnt - 掛載 Space:
-v hf://spaces/username/my-space:/app - 掛載儲存庫內的子資料夾:
-v hf://datasets/org/ds/train:/data
接著,您可以將掛載的儲存卷當作本機目錄使用。
# Docker Job with a mounted volume as input
>>> hf jobs run -v hf://datasets/HuggingFaceFW/fineweb:/dataset \
... duckdb/duckdb duckdb -c "SELECT * FROM '/dataset/**/*.parquet' LIMIT 5"
# UV Job with a mounted volume to save checkpoints when training a model
>>> hf jobs uv run -v hf://buckets/username/my-bucket:/training-outputs \
... sft.py --output-dir /training-outputs/training-v3-final ...模型、資料集與 Space 始終以唯讀方式掛載。儲存桶預設為讀寫皆可 — 這對於經常變更的資料特別有用,因為檔案可以原地覆寫或刪除。
使用 :ro 啟用唯讀。
- 唯讀掛載儲存桶:
-v hf://buckets/username/my-bucket:/mnt:ro
標籤 (Labels)
使用 -l 或 --label 為 Job 新增標籤。標籤是套用於 Job 的鍵=值對。若要為 Job 加入兩個標籤,請重複使用標籤旗標(-l 或 --label)。
>>> hf jobs run -l my-label --label foo=bar ubuntu echo "This Job has multiple labels"my-label 鍵若未指定值,其預設值為空字串 ("")。
在 hf jobs ps 中使用 -f 或 --filter 來篩選符合特定標籤的 Jobs。
# Show fine-tuning Jobs
>>> hf jobs ps -a --filter label=fine-tuning
# Show Jobs that don't have the "prod" label and have a label that starts with "data-"
>>> hf jobs ps -a --filter label!=prod --filter "label=data-*"
# Show Jobs based on key=value labels
>>> hf jobs ps -a --filter label=model=Qwen3-06B --filter label=dataset!=CapybaraUV 指令碼(實驗性功能)
在 HF 基礎設施上執行 UV 指令碼(包含內嵌相依性的 Python 指令碼)。UV 指令碼是使用特殊註解語法將其相依性直接包含在檔案中的 Python 指令碼。
# Run a UV script (creates temporary repo)
>>> hf jobs uv run my_script.py
# Run with persistent repo
>>> hf jobs uv run my_script.py --repo my-uv-scripts
# Run with GPU
>>> hf jobs uv run ml_training.py --flavor gpu-t4-small
# Pass arguments to script
>>> hf jobs uv run process.py input.csv output.parquet
# Add dependencies
>>> hf jobs uv run --with transformers --with torch train.py
# Run a script directly from a URL
>>> hf jobs uv run https://huggingface.co/datasets/username/scripts/resolve/main/example.py
# Run a command
>>> hf jobs uv run --with lighteval python -c 'import lighteval'UV 指令碼使它們非常適合不需要複雜專案設定的獨立任務。在 UV 文件 中深入了解 UV 指令碼。
可以使用 -- 將指令與 jobs/uv 選項分開,以利清晰,例如 hf jobs uv run --flavor gpu-t4-small --with torch -- python -c '...'
hf jobs scheduled
在 HF 基礎設施上排程與管理任務。
排程時間應為 @annually、@yearly、@monthly、@weekly、@daily、@hourly 之一,或是 CRON 排程表示式(例如,每週一上午 9 點為 "0 9 * * 1")。
# Schedule a job that runs every hour
>>> hf jobs scheduled run @hourly python:3.12 python -c 'print("This runs every hour!")'
# Use the CRON syntax
>>> hf jobs scheduled run "*/5 * * * *" python:3.12 python -c 'print("This runs every 5 minutes!")'
# Schedule with GPU
>>> hf jobs scheduled run @hourly --flavor a10g-small pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel \
... python -c "import torch; print(f"This code ran with the following GPU: {torch.cuda.get_device_name()}")"
# Schedule a UV script
>>> hf jobs scheduled uv run @hourly my_script.py使用與 hf jobs run 相同的參數來傳遞環境變數、密鑰、超時時間等。
使用以下方式管理已排程的 Jobs:
# List your active scheduled jobs
>>> hf jobs scheduled ps
# Inspect the status of a job
>>> hf jobs scheduled inspect <scheduled_job_id>
# Suspend (pause) a scheduled job
>>> hf jobs scheduled suspend <scheduled_job_id>
# Resume a scheduled job
>>> hf jobs scheduled resume <scheduled_job_id>
# Delete a scheduled job
>>> hf jobs scheduled delete <scheduled_job_id>hf webhooks
hf webhooks 讓您能直接從終端機管理 Hugging Face Hub 上的 Webhooks。Webhooks 允許您監聽儲存庫、使用者或組織上的事件(推送、討論等)並觸發動作——透過 ping 遠端 URL 或在 Hugging Face 基礎設施上執行 Job。
列出 Webhooks
>>> hf webhooks ls
ID URL DISABLED DOMAINS WATCHED
wh-abc123 https://example.com/hook False repo model:bert-base-uncased
wh-def456 https://example.com/other-hook False repo org:HuggingFace使用 --format json 以取得機器可讀的輸出,或使用 -q 僅列印 ID。
>>> hf webhooks ls --format json
>>> hf webhooks ls -q取得 Webhook 資訊
>>> hf webhooks info wh-abc123以 JSON 格式列印完整的 Webhook 詳細資訊。
建立 Webhook
建立一個會將負載(payloads)傳送到特定 URL 的 Webhook
>>> hf webhooks create --url https://example.com/hook --watch model:bert-base-uncased
>>> hf webhooks create --url https://example.com/hook --watch org:HuggingFace --watch model:gpt2 --domain repo或是建立一個用來觸發 Job 的 Webhook
>>> hf webhooks create --job-id 687f911eaea852de79c4a50a --watch user:julien-c--watch 選項使用 type:name 格式,其中 type 為 model、dataset、space、org 或 user 之一。此選項可重複使用以監控多個項目。使用 --domain 將事件篩選為 repo 或 discussions,並使用 --secret 設定簽署金鑰(signing secret)。
更新 Webhook
>>> hf webhooks update wh-abc123 --url https://new-url.com/hook
>>> hf webhooks update wh-abc123 --watch model:gpt2 --domain repo僅會變更已提供的選項。請注意,若指定了 --watch,它將會取代整個監控列表。
啟用 / 停用 Webhook
>>> hf webhooks enable wh-abc123
>>> hf webhooks disable wh-abc123刪除 Webhook
>>> hf webhooks delete wh-abc123
Are you sure you want to delete webhook 'wh-abc123'? [y/N]: y
Webhook deleted: wh-abc123使用 --yes 跳過確認提示
>>> hf webhooks delete wh-abc123 --yes所有指令皆接受
--token以覆寫驗證資訊。對任何指令使用--help即可查看所有選項。
hf endpoints
使用 hf endpoints 可直接從終端機列出、部署、描述及管理 Inference Endpoints。舊版 hf inference-endpoints 別名仍可使用,以保持相容性。
# Lists endpoints in your namespace
>>> hf endpoints ls
# Deploy an endpoint from Model Catalog
>>> hf endpoints catalog deploy --repo openai/gpt-oss-120b --name my-endpoint
# Deploy an endpoint from the Hugging Face Hub
>>> hf endpoints deploy my-endpoint --repo gpt2 --framework pytorch --accelerator cpu --instance-size x2 --instance-type intel-icl
# List catalog entries
>>> hf endpoints catalog ls
# Show status and metadata
>>> hf endpoints describe my-endpoint
# Pause the endpoint
>>> hf endpoints pause my-endpoint
# Delete without confirmation prompt
>>> hf endpoints delete my-endpoint --yes加入
--namespace以鎖定特定組織,使用--token覆寫驗證資訊。
hf endpoints catalog
使用 hf endpoints catalog 與 Inference Endpoints 模型目錄互動。直接從目錄部署模型,並使用最佳化設定。
# List available catalog models
>>> hf endpoints catalog ls
# Deploy a model from the catalog
>>> hf endpoints catalog deploy --repo meta-llama/Llama-3.2-1B-Instruct
# Deploy with a custom name
>>> hf endpoints catalog deploy --repo meta-llama/Llama-3.2-1B-Instruct --name my-llama-endpoint