Hub Python 庫文件

管理本地和線上儲存庫

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

管理本地和線上儲存庫

Repository 類是一個包裝 gitgit-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 (strPath) — 本地目錄的路徑(例如 '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 (boolstr, 可選) — 有效的身份驗證令牌(請參閱 https://huggingface.co/settings/token)。如果為 NoneTrue 且機器已登入(透過 hf auth loginlogin()),則將從快取中檢索令牌。如果為 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

例項化 git 倉庫的本地克隆。

如果設定了 clone_from,倉庫將從現有遠端倉庫克隆。如果遠端倉庫不存在,將丟擲 EnvironmentError 異常。請先使用 create_repo() 建立遠端倉庫。

Repository 預設使用本地 git 憑據。如果明確設定,將使用 tokengit_user/git_email 對。

current_branch

< >

( ) str

返回

字串

當前檢出的分支。

返回當前檢出的分支。

add_tag

< >

( tag_name: str message: typing.Optional[str] = None remote: typing.Optional[str] = None )

引數

  • tag_name (str) — 要新增的標籤名稱。
  • message (str, 可選) — 伴隨標籤的訊息。如果傳遞了訊息,標籤將變為帶註釋的標籤。
  • remote (str, 可選) — 要在其上新增標籤的遠端倉庫。

在當前 HEAD 新增標籤並推送

如果 remote 為 None,則只會在本地更新

如果沒有提供訊息,標籤將是輕量級的。如果提供了訊息,標籤將是帶註釋的。

auto_track_binary_files

< >

( pattern: str = '.' ) List[str]

引數

  • pattern (str, 可選, 預設為 ”.“) — 用於跟蹤二進位制檔案的模式。

返回

List[str]

因二進位制檔案而被跟蹤的檔名列表

使用 git-lfs 自動跟蹤二進位制檔案。

auto_track_large_files

< >

( pattern: str = '.' ) List[str]

引數

  • pattern (str, 可選, 預設為 ”.“) — 用於跟蹤超過 10MB 的檔案的模式。

返回

List[str]

因檔案大小而被跟蹤的檔名列表。

使用 git-lfs 自動跟蹤大檔案(超過 10MB 的檔案)。

check_git_versions

< >

( )

引發

EnvironmentError

檢查 gitgit-lfs 是否可執行。

clone_from

< >

( repo_url: str token: typing.Union[bool, str, NoneType] = None )

引數

  • repo_url (str) — 克隆倉庫的 URL
  • token (Union[str, bool], 可選) — 是否使用身份驗證令牌。它可以是:
    • 令牌本身字串
    • False,表示不使用身份驗證令牌
    • True,表示從本地資料夾獲取身份驗證令牌並使用(這需要您登入才能工作)。
    • None,表示檢索 self.huggingface_token 的值。

從遠端倉庫克隆。如果資料夾已存在,將嘗試在其內部克隆倉庫。

如果此資料夾是帶有連結歷史記錄的 git 倉庫,將嘗試更新該倉庫。

引發以下錯誤

commit

< >

( commit_message: str branch: typing.Optional[str] = None track_large_files: bool = True blocking: bool = True auto_lfs_prune: bool = False )

引數

  • commit_message (str) — 用於提交的訊息。
  • branch (str, 可選) — 提交將顯示在哪個分支上。在此操作之前,將檢出此分支。
  • track_large_files (bool, 可選, 預設為 True) — 是否自動跟蹤大檔案。預設情況下會這樣做。
  • blocking (bool, 可選, 預設為 True) — 函式是否應僅在 git push 完成後返回。
  • auto_lfs_prune (bool, 預設為 True) — 推送到遠端倉庫後是否自動修剪檔案。

上下文管理器實用工具,用於處理對倉庫的提交。它會自動使用 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

引數

  • tag_name (str) — 要刪除的標籤名稱。
  • remote (str, 可選) — 要在其上刪除標籤的遠端倉庫。

返回

布林值

如果已刪除則為 True,如果標籤不存在則為 False。如果沒有傳遞遠端,則只會在本地更新

如果標籤存在,則刪除本地和遠端標籤

git_add

< >

( pattern: str = '.' auto_lfs_track: bool = False )

引數

  • pattern (str, 可選, 預設為 ”.“) — 用於將檔案新增到暫存區的模式。
  • auto_lfs_track (bool, 可選, 預設為 False) — 是否使用 git-lfs 自動跟蹤大檔案和二進位制檔案。任何大小超過 10MB 或二進位制格式的檔案都將自動跟蹤。

git add

auto_lfs_track 引數設定為 True 將自動使用 git-lfs 跟蹤大於 10MB 的檔案。

git_checkout

< >

( revision: str create_branch_ok: bool = False )

引數

  • revision (str) — 要檢出的修訂版本。
  • create_branch_ok (str, 可選, 預設為 False) — 如果 revision 不是現有修訂版本,是否允許在當前檢出的引用上建立以 revision 命名的分支。

git 檢出給定修訂版本

create_branch_ok 指定為 True 將在給定修訂版本不存在的情況下建立該修訂版本的分支。

git_commit

< >

( commit_message: str = 'commit files to HF hub' )

引數

  • 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_user (str, 可選) — 要透過 git 註冊的使用者名稱。
  • git_email (str, 可選) — 要透過 git 註冊的電子郵件。

設定 git 使用者名稱和電子郵件(僅在當前儲存庫中)。

git_credential_helper_store

< >

( )

將 git 憑證助手設定為 store

git_head_commit_url

< >

( ) str

返回

字串

當前檢出提交的 URL。

獲取 HEAD 上最後一次提交的 URL。我們假設它已被推送,並且 URL 方案與 GitHub 或 HuggingFace 的相同。

git_head_hash

< >

( ) str

返回

字串

當前檢出提交的 SHA。

獲取 HEAD 頂部的提交 SHA。

git_pull

< >

( rebase: bool = False lfs: bool = False )

引數

  • rebase (bool, 可選, 預設為 False) — 是否在拉取後將當前分支變基到上游分支之上。
  • lfs (bool, 可選, 預設為 False) — 是否也獲取 LFS 檔案。此選項僅在未獲取 LFS 檔案的情況下克隆儲存庫時更改行為;呼叫 repo.git_pull(lfs=True) 將從遠端儲存庫獲取 LFS 檔案。

git pull

git_push

< >

( upstream: typing.Optional[str] = None blocking: bool = True auto_lfs_prune: bool = False )

引數

  • upstream (str, 可選) — 推送到的上游。如果未指定,將推送到最後定義上游或預設上游(origin main)。
  • blocking (bool, 可選, 預設為 True) — 函式是否應僅在推送完成後返回。將其設定為 False 將返回一個 CommandInProgress 物件,該物件具有 is_done 屬性。當推送完成後,此屬性將設定為 True
  • auto_lfs_prune (bool, 可選, 預設為 False) — 是否在檔案推送到遠端後自動修剪檔案。

git push

如果使用時未設定 blocking,將返回遠端倉庫上提交的 URL。如果使用 blocking=True,將返回一個元組,其中包含提交的 URL 和用於獲取程序資訊的命令物件。

git_remote_url

< >

( ) str

返回

字串

origin 遠端的 URL。

獲取源遠端的 URL。

is_repo_clean

< >

( ) bool

返回

布林值

如果 git 狀態乾淨,則為 True,否則為 False

返回 git 狀態是否乾淨

lfs_enable_largefiles

< >

( )

HF 特有。這支援上傳大於 5GB 的檔案。

lfs_prune

< >

( recent = False )

引數

  • recent (bool, 可選, 預設為 False) — 是否即使檔案被最近的提交引用也修剪它們。有關更多資訊,請參閱以下 連結

git lfs prune

lfs_track

< >

( patterns: typing.Union[str, typing.List[str]] filename: bool = False )

引數

  • patterns (Union[str, List[str]]) — 要用 git-lfs 跟蹤的模式或模式列表。
  • filename (bool, 可選, 預設為 False) — 是否將模式用作字面檔名。

告訴 git-lfs 根據模式跟蹤檔案。

filename 引數設定為 True 將把引數視為字面檔名,而不是模式。檔名中的任何特殊全域性字元在寫入 .gitattributes 檔案時都將被轉義。

lfs_untrack

< >

( patterns: typing.Union[str, typing.List[str]] )

引數

  • patterns (Union[str, List[str]]) — 要用 git-lfs 取消跟蹤的模式或模式列表。

告訴 git-lfs 取消跟蹤這些檔案。

list_deleted_files

< >

( ) List[str]

返回

List[str]

工作目錄或索引中已刪除的檔案列表。

返回工作目錄或索引中已刪除的檔案列表。

push_to_hub

< >

( commit_message: str = 'commit files to HF hub' blocking: bool = True clean_ok: bool = True auto_lfs_prune: bool = False )

引數

  • commit_message (str) — 用於提交的訊息。
  • blocking (bool, 可選, 預設為 True) — 函式是否應僅在 git push 完成後返回。
  • clean_ok (bool, 可選, 預設為 True) — 如果為 True,則如果倉庫未更改,此函式將返回 None。預設行為是失敗,因為 git 命令會失敗。
  • auto_lfs_prune (bool, 可選, 預設為 False) — 檔案推送到遠端後是否自動修剪檔案。

用於將檔案新增到遠端儲存庫並提交和推送的助手,位於 HuggingFace Hub 上。將自動跟蹤大型檔案(>10MB)。

tag_exists

< >

( tag_name: str remote: typing.Optional[str] = None ) bool

引數

  • tag_name (str) — 要檢查的標籤名稱。
  • remote (str, 可選) — 是否檢查標籤是否存在於遠端。此引數應為遠端的識別符號。

返回

布林值

標籤是否存在。

檢查標籤是否存在。

wait_for_commands

< >

( )

阻塞方法:阻塞所有後續執行,直到所有命令都已處理。

幫助方法

huggingface_hub.repository.is_git_repo

< >

( folder: typing.Union[str, pathlib.Path] ) bool

引數

  • folder (str) — 執行命令的資料夾。

返回

布林值

如果儲存庫是儲存庫的一部分,則為 True,否則為 False

檢查資料夾是否為 git 儲存庫的根目錄或一部分

huggingface_hub.repository.is_local_clone

< >

( folder: typing.Union[str, pathlib.Path] remote_url: str ) bool

引數

  • folder (strPath) — 執行命令的資料夾。
  • remote_url (str) — Git 倉庫的 URL。

返回

布林值

如果倉庫是指定遠端倉庫的本地克隆,則為 True,否則為 False

檢查資料夾是否是 remote_url 的本地克隆

huggingface_hub.repository.is_tracked_with_lfs

< >

( filename: typing.Union[str, pathlib.Path] ) bool

引數

  • filename (strPath) — 要檢查的檔名。

返回

布林值

如果傳入的檔案使用 git-lfs 跟蹤,則為 True,否則為 False

檢查傳入的檔案是否使用 git-lfs 跟蹤。

huggingface_hub.repository.is_git_ignored

< >

( filename: typing.Union[str, pathlib.Path] ) bool

引數

  • filename (strPath) — 要檢查的檔名。

返回

布林值

如果傳入的檔案被 git 忽略,則為 True,否則為 False

檢查檔案是否被 git 忽略。支援巢狀的 .gitignore 檔案。

huggingface_hub.repository.files_to_be_staged

< >

( pattern: str = '.' folder: typing.Union[str, pathlib.Path, NoneType] = None ) List[str]

引數

  • pattern (strPath) — 要檢查的檔名模式。輸入 . 以獲取所有檔案。
  • folder (strPath) — 執行命令的資料夾。

返回

List[str]

要暫存的檔案列表。

返回要暫存的檔名列表。

huggingface_hub.repository.is_tracked_upstream

< >

( folder: typing.Union[str, pathlib.Path] ) bool

引數

  • folder (strPath) — 執行命令的資料夾。

返回

布林值

如果當前簽出的分支被上游跟蹤,則為 True,否則為 False

檢查當前簽出的分支是否被上游跟蹤。

huggingface_hub.repository.commits_to_push

< >

( folder: typing.Union[str, pathlib.Path] upstream: typing.Optional[str] = None ) int

引數

  • folder (strPath) — 執行命令的資料夾。
  • upstream (str, 可選) —

返回

int

如果執行 `git push` 命令,將被推送到上游的提交數量。

檢查將被推送到上游的提交數量

用於比較的上游倉庫的名稱。

跟蹤非同步命令

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

commands_failed

< >

( )

返回失敗的非同步命令。

commands_in_progress

< >

( )

返回當前正在進行的非同步命令。

wait_for_commands

< >

( )

阻塞方法:阻塞所有後續執行,直到所有命令都已處理。

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 )

用於跟蹤非同步啟動命令的實用程式。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.