Hub Python 庫文件
管理本地和線上儲存庫
並獲得增強的文件體驗
開始使用
管理本地和線上儲存庫
Repository
類是一個包裝 git
和 git-lfs
命令的輔助類。它提供了適用於管理可能非常大的儲存庫的工具。
一旦涉及任何 git
操作,或者當協作將成為儲存庫本身的重點時,它是推薦的工具。
Repository 類
class huggingface_hub.Repository
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
用於包裝 git 和 git-lfs 命令的輔助類。
目的是方便與 huggingface.co 託管的模型或資料集倉庫進行互動,儘管這裡(如果有的話)並沒有多少是 huggingface.co 特有的。
Repository 已棄用,取而代之的是 HfApi 中實現的基於 HTTP 的替代方案。鑑於其在舊程式碼中的廣泛採用,Repository 的完全移除只會在 v1.0
版本中進行。有關更多詳細資訊,請閱讀 https://huggingface.co/docs/huggingface_hub/concepts/git_vs_http。
__init__
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
引數
- local_dir (
str
或Path
) — 本地目錄的路徑(例如'my_trained_model/'
),Repository
將在此處初始化。 - clone_from (
str
, 可選) — 倉庫 URL 或repo_id
。示例:"https://huggingface.co/philschmid/playground-tests"
"philschmid/playground-tests"
- repo_type (
str
, 可選) — 從 repo_id 克隆倉庫時設定。預設為模型。 - token (
bool
或str
, 可選) — 有效的身份驗證令牌(請參閱 https://huggingface.co/settings/token)。如果為None
或True
且機器已登入(透過hf auth login
或 login()),則將從快取中檢索令牌。如果為False
,則請求頭中不傳送令牌。 - git_user (
str
, 可選) — 將覆蓋git config user.name
,用於提交和推送檔案到 Hub。 - git_email (
str
, 可選) — 將覆蓋git config user.email
,用於提交和推送檔案到 Hub。 - revision (
str
, 可選) — 初始化倉庫後要檢出的修訂版本。如果修訂版本不存在,將從預設分支的當前 HEAD 建立一個具有該修訂版本名稱的分支。 - skip_lfs_files (
bool
, 可選, 預設為False
) — 是否跳過 git-LFS 檔案。 - client (
HfApi
, 可選) — 呼叫 HF Hub API 時使用的 HfApi 例項。如果保留為None
,將建立一個新例項。
引發
EnvironmentError
EnvironmentError
— 如果clone_from
中設定的遠端倉庫不存在。
例項化 git 倉庫的本地克隆。
如果設定了 clone_from
,倉庫將從現有遠端倉庫克隆。如果遠端倉庫不存在,將丟擲 EnvironmentError
異常。請先使用 create_repo() 建立遠端倉庫。
Repository
預設使用本地 git 憑據。如果明確設定,將使用 token
或 git_user
/git_email
對。
返回當前檢出的分支。
add_tag
< 源 >( tag_name: str message: typing.Optional[str] = None remote: typing.Optional[str] = None )
在當前 HEAD 新增標籤並推送
如果 remote 為 None,則只會在本地更新
如果沒有提供訊息,標籤將是輕量級的。如果提供了訊息,標籤將是帶註釋的。
auto_track_binary_files
< 源 >( pattern: str = '.' ) → List[str]
使用 git-lfs 自動跟蹤二進位制檔案。
auto_track_large_files
< 源 >( pattern: str = '.' ) → List[str]
使用 git-lfs 自動跟蹤大檔案(超過 10MB 的檔案)。
檢查 git
和 git-lfs
是否可執行。
clone_from
< 源 >( repo_url: str token: typing.Union[bool, str, NoneType] = None )
從遠端倉庫克隆。如果資料夾已存在,將嘗試在其內部克隆倉庫。
如果此資料夾是帶有連結歷史記錄的 git 倉庫,將嘗試更新該倉庫。
引發以下錯誤
ValueError
如果傳遞了組織令牌(以“api_org”開頭)。必須使用您自己的個人訪問令牌(請參閱 https://huggingface.co/settings/tokens)。EnvironmentError
如果您嘗試將倉庫克隆到非空資料夾中,或者如果git
操作引發錯誤。
commit
< 源 >( commit_message: str branch: typing.Optional[str] = None track_large_files: bool = True blocking: bool = True auto_lfs_prune: bool = False )
上下文管理器實用工具,用於處理對倉庫的提交。它會自動使用 git-lfs 跟蹤大檔案(>10Mb)。如果您希望忽略此行為,請將 track_large_files
引數設定為 False
。
示例
>>> with Repository(
... "text-files",
... clone_from="<user>/text-files",
... token=True,
>>> ).commit("My first file :)"):
... with open("file.txt", "w+") as f:
... f.write(json.dumps({"hey": 8}))
>>> import torch
>>> model = torch.nn.Transformer()
>>> with Repository(
... "torch-model",
... clone_from="<user>/torch-model",
... token=True,
>>> ).commit("My cool model :)"):
... torch.save(model.state_dict(), "model.pt")
delete_tag
< 源 >( tag_name: str remote: typing.Optional[str] = None ) → bool
如果標籤存在,則刪除本地和遠端標籤
git_add
< 源 >( pattern: str = '.' auto_lfs_track: bool = False )
git add
將 auto_lfs_track
引數設定為 True
將自動使用 git-lfs
跟蹤大於 10MB 的檔案。
git_checkout
< 源 >( revision: str create_branch_ok: bool = False )
git 檢出給定修訂版本
將 create_branch_ok
指定為 True
將在給定修訂版本不存在的情況下建立該修訂版本的分支。
git_commit
< 源 >( commit_message: str = 'commit files to HF hub' )
git commit
git_config_username_and_email
< 源 >( git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None )
設定 git 使用者名稱和電子郵件(僅在當前儲存庫中)。
將 git 憑證助手設定為 store
獲取 HEAD 上最後一次提交的 URL。我們假設它已被推送,並且 URL 方案與 GitHub 或 HuggingFace 的相同。
獲取 HEAD 頂部的提交 SHA。
git_pull
< 源 >( rebase: bool = False lfs: bool = False )
git pull
git_push
< 源 >( upstream: typing.Optional[str] = None blocking: bool = True auto_lfs_prune: bool = False )
git push
如果使用時未設定 blocking
,將返回遠端倉庫上提交的 URL。如果使用 blocking=True
,將返回一個元組,其中包含提交的 URL 和用於獲取程序資訊的命令物件。
獲取源遠端的 URL。
返回 git 狀態是否乾淨
HF 特有。這支援上傳大於 5GB 的檔案。
lfs_prune
< 源 >( recent = False )
引數
- recent (
bool
, 可選, 預設為False
) — 是否即使檔案被最近的提交引用也修剪它們。有關更多資訊,請參閱以下 連結。
git lfs prune
lfs_track
< 源 >( patterns: typing.Union[str, typing.List[str]] filename: bool = False )
告訴 git-lfs 根據模式跟蹤檔案。
將 filename
引數設定為 True
將把引數視為字面檔名,而不是模式。檔名中的任何特殊全域性字元在寫入 .gitattributes
檔案時都將被轉義。
lfs_untrack
< 源 >( patterns: typing.Union[str, typing.List[str]] )
告訴 git-lfs 取消跟蹤這些檔案。
返回工作目錄或索引中已刪除的檔案列表。
push_to_hub
< 源 >( commit_message: str = 'commit files to HF hub' blocking: bool = True clean_ok: bool = True auto_lfs_prune: bool = False )
用於將檔案新增到遠端儲存庫並提交和推送的助手,位於 HuggingFace Hub 上。將自動跟蹤大型檔案(>10MB)。
tag_exists
< 源 >( tag_name: str remote: typing.Optional[str] = None ) → bool
檢查標籤是否存在。
阻塞方法:阻塞所有後續執行,直到所有命令都已處理。
幫助方法
huggingface_hub.repository.is_git_repo
< 源 >( folder: typing.Union[str, pathlib.Path] ) → bool
檢查資料夾是否為 git 儲存庫的根目錄或一部分
huggingface_hub.repository.is_local_clone
< 源 >( folder: typing.Union[str, pathlib.Path] remote_url: str ) → bool
檢查資料夾是否是 remote_url 的本地克隆
huggingface_hub.repository.is_tracked_with_lfs
< 源 >( filename: typing.Union[str, pathlib.Path] ) → bool
檢查傳入的檔案是否使用 git-lfs 跟蹤。
huggingface_hub.repository.is_git_ignored
< 源 >( filename: typing.Union[str, pathlib.Path] ) → bool
檢查檔案是否被 git 忽略。支援巢狀的 .gitignore 檔案。
huggingface_hub.repository.files_to_be_staged
< 源 >( pattern: str = '.' folder: typing.Union[str, pathlib.Path, NoneType] = None ) → List[str]
返回要暫存的檔名列表。
huggingface_hub.repository.is_tracked_upstream
< 來源 >( folder: typing.Union[str, pathlib.Path] ) → bool
檢查當前簽出的分支是否被上游跟蹤。
huggingface_hub.repository.commits_to_push
< 來源 >( folder: typing.Union[str, pathlib.Path] upstream: typing.Optional[str] = None ) → int
檢查將被推送到上游的提交數量
用於比較的上游倉庫的名稱。
跟蹤非同步命令
Repository
工具提供了幾個可以非同步啟動的方法
git_push
git_pull
push_to_hub
commit
上下文管理器
有關管理此類非同步實用程式的資訊,請參閱下文。
class huggingface_hub.Repository
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
用於包裝 git 和 git-lfs 命令的輔助類。
目的是方便與 huggingface.co 託管的模型或資料集倉庫進行互動,儘管這裡(如果有的話)並沒有多少是 huggingface.co 特有的。
Repository 已棄用,取而代之的是 HfApi 中實現的基於 HTTP 的替代方案。鑑於其在舊程式碼中的廣泛採用,Repository 的完全移除只會在 v1.0
版本中進行。有關更多詳細資訊,請閱讀 https://huggingface.co/docs/huggingface_hub/concepts/git_vs_http。
返回失敗的非同步命令。
返回當前正在進行的非同步命令。
阻塞方法:阻塞所有後續執行,直到所有命令都已處理。
class huggingface_hub.repository.CommandInProgress
< 來源 >( title: str is_done_method: typing.Callable status_method: typing.Callable process: Popen post_method: typing.Optional[typing.Callable] = None )
用於跟蹤非同步啟動命令的實用程式。