Hub Python 庫文件
建立和管理倉庫
並獲得增強的文件體驗
開始使用
建立和管理倉庫
Hugging Face Hub 是一個由 Git 倉庫組成的集合。 Git 是軟體開發中廣泛使用的工具,用於在協作時輕鬆地對專案進行版本控制。本指南將向您展示如何與 Hub 上的倉庫進行互動,尤其是
- 建立和刪除倉庫。
- 管理分支和標籤。
- 重新命名您的倉庫。
- 更新您的倉庫可見性。
- 管理您倉庫的本地副本。
如果您習慣使用 GitLab/GitHub/Bitbucket 等平臺,您可能會首先想到使用
gitCLI 來克隆您的倉庫(git clone)、提交更改(git add, git commit)並推送它們(git push)。在使用 Hugging Face Hub 時,這都是可行的。但是,軟體工程和機器學習對儲存大型模型檔案的要求和工作流程不同。模型倉庫可能維護著針對不同框架和工具的大型模型權重檔案,因此克隆倉庫可能會導致您維護著大量本地資料夾。因此,使用我們的自定義 HTTP 方法可能會更有效率。您可以閱讀我們的 Git vs HTTP 範例 解釋頁面以獲取更多詳細資訊。
如果您想在 Hub 上建立和管理倉庫,您的機器必須已登入。如果您尚未登入,請參閱 此部分。在本指南的其餘部分,我們將假定您的機器已登入。
倉庫建立和刪除
第一步是瞭解如何建立和刪除倉庫。您只能管理您擁有的倉庫(在您的使用者名稱名稱空間下)或您擁有寫入許可權的組織中的倉庫。
建立倉庫
使用 create_repo() 建立一個空倉庫,並透過 repo_id 引數為其命名。repo_id 是您的名稱空間加上倉庫名稱:username_or_org/repo_name。
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-model")
'https://huggingface.co/lysandre/test-model'或透過 CLI
>>> hf repo create lysandre/test-model Successfully created lysandre/test-model on the Hub. Your repo is now available at https://huggingface.co/lysandre/test-model
預設情況下,create_repo() 建立的是一個模型倉庫。但您可以使用 repo_type 引數指定其他倉庫型別。例如,如果您想建立一個數據集倉庫
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-dataset", repo_type="dataset")
'https://huggingface.co/datasets/lysandre/test-dataset'或透過 CLI
>>> hf repo create lysandre/test-dataset --repo-type dataset
建立倉庫時,您可以使用 private 引數設定倉庫的可見性。
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-private", private=True)或透過 CLI
>>> hf repo create lysandre/test-private --private
如果您想稍後更改倉庫的可見性,可以使用 update_repo_settings() 函式。
如果您是一個擁有企業版計劃的組織的成員,您可以將
resource_group_id作為引數傳遞給 create_repo() 來在一個特定的資源組中建立倉庫。資源組是一種安全功能,用於控制組織內的哪些成員可以訪問給定資源。您可以透過從 Hub 上的組織設定頁面的 URL 中複製資源組 ID 來獲取它(例如,"https://huggingface.co/organizations/huggingface/settings/resource-groups/66670e5163145ca562cb1988"=>"66670e5163145ca562cb1988")。有關資源組的更多詳細資訊,請參閱此 指南。
刪除倉庫
使用 delete_repo() 刪除倉庫。請確保您要刪除倉庫,因為這是一個不可逆的過程!
指定您要刪除的倉庫的 repo_id
>>> delete_repo(repo_id="lysandre/my-corrupted-dataset", repo_type="dataset")或透過 CLI
>>> hf repo delete lysandre/my-corrupted-dataset --repo-type dataset
複製倉庫(僅限 Spaces)
在某些情況下,您想複製他人的倉庫以適應您的用例。對於 Spaces,可以使用 duplicate_space() 方法來實現。它會複製整個倉庫。您仍然需要配置自己的設定(硬體、睡眠時間、儲存、變數和金鑰)。有關更多詳細資訊,請參閱我們的 管理你的 Space 指南。
>>> from huggingface_hub import duplicate_space
>>> duplicate_space("multimodalart/dreambooth-training", private=False)
RepoUrl('https://huggingface.co/spaces/nateraw/dreambooth-training',...)上傳和下載檔案
現在您已經建立了倉庫,您可能想將更改推送到它或從中下載檔案。
這兩個主題值得擁有自己的指南。請參閱 上傳 和 下載 指南,瞭解如何使用您的倉庫。
分支和標籤
Git 倉庫通常使用分支來儲存同一倉庫的不同版本。標籤也可以用來標記倉庫的特定狀態,例如,在釋出版本時。更一般地說,分支和標籤被稱為 Git 引用。
建立分支和標籤
您可以使用 create_branch() 和 create_tag() 建立新的分支和標籤。
>>> from huggingface_hub import create_branch, create_tag
# Create a branch on a Space repo from `main` branch
>>> create_branch("Matthijs/speecht5-tts-demo", repo_type="space", branch="handle-dog-speaker")
# Create a tag on a Dataset repo from `v0.1-release` branch
>>> create_tag("bigcode/the-stack", repo_type="dataset", revision="v0.1-release", tag="v0.1.1", tag_message="Bump release version.")或透過 CLI
>>> hf repo branch create Matthijs/speecht5-tts-demo handle-dog-speaker --repo-type space
>>> hf repo tag create bigcode/the-stack v0.1.1 --repo-type dataset --revision v0.1-release -m "Bump release version."您可以使用 delete_branch() 和 delete_tag() 函式以同樣的方式刪除分支或標籤,或者在 CLI 中分別使用 hf repo branch delete 和 hf repo tag delete。
列出所有分支和標籤
您也可以使用 list_repo_refs() 列出倉庫中現有的 Git 引用。
>>> from huggingface_hub import list_repo_refs
>>> list_repo_refs("bigcode/the-stack", repo_type="dataset")
GitRefs(
branches=[
GitRefInfo(name='main', ref='refs/heads/main', target_commit='18edc1591d9ce72aa82f56c4431b3c969b210ae3'),
GitRefInfo(name='v1.1.a1', ref='refs/heads/v1.1.a1', target_commit='f9826b862d1567f3822d3d25649b0d6d22ace714')
],
converts=[],
tags=[
GitRefInfo(name='v1.0', ref='refs/tags/v1.0', target_commit='c37a8cd1e382064d8aced5e05543c5f7753834da')
]
)更改倉庫設定
倉庫帶有一些您可以配置的設定。大多數情況下,您會想在瀏覽器中的倉庫設定頁面手動進行配置。您必須擁有倉庫的寫入許可權才能配置它(要麼擁有它,要麼是組織的一部分)。在本節中,我們將介紹您也可以透過 huggingface_hub 以程式設計方式配置的設定。
某些設定特定於 Spaces(硬體、環境變數等)。要配置這些設定,請參閱我們的 管理你的 Spaces 指南。
更新可見性
倉庫可以是公開或私有的。私有倉庫僅對您或倉庫所在組織的成員可見。像下面這樣將倉庫更改為私有
>>> from huggingface_hub import update_repo_settings
>>> update_repo_settings(repo_id=repo_id, private=True)或透過 CLI
>>> hf repo settings lysandre/test-private --private true設定受限訪問
為了更好地控制倉庫的使用方式,Hub 允許倉庫作者為其倉庫啟用訪問請求。啟用後,使用者必須同意與倉庫作者共享其聯絡資訊(使用者名稱和電子郵件地址)才能訪問檔案。啟用訪問請求的倉庫稱為受限倉庫。
您可以使用 update_repo_settings() 將倉庫設定為受限狀態。
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> api.update_repo_settings(repo_id=repo_id, gated="auto") # Set automatic gating for a model或透過 CLI
>>> hf repo settings lysandre/test-private --gated auto
重新命名您的倉庫
您可以使用 move_repo() 在 Hub 上重新命名您的倉庫。使用此方法,您還可以將倉庫從使用者移動到組織。這樣做時,有一些 侷限性 需要注意。例如,您不能將倉庫轉移給其他使用者。
>>> from huggingface_hub import move_repo
>>> move_repo(from_id="Wauplin/cool-model", to_id="huggingface/cool-model")或透過 CLI
>>> hf repo move Wauplin/cool-model huggingface/cool-model