Hub Python 庫文件
HfApi 客戶端
並獲得增強的文件體驗
開始使用
HfApi 客戶端
以下是 HfApi
類的文件,它是 Hugging Face Hub API 的 Python 包裝器。
HfApi
的所有方法也可以直接從包的根目錄訪問。下面詳細介紹了這兩種方法。
使用根方法更直接,但 HfApi 類為您提供了更大的靈活性。特別是,您可以傳遞一個令牌,該令牌將在所有 HTTP 呼叫中重複使用。這與 hf auth login
或 login() 不同,因為令牌不會持久化到機器上。還可以提供不同的端點或配置自定義的使用者代理。
from huggingface_hub import HfApi, list_models
# Use root method
models = list_models()
# Or configure a HfApi client
hf_api = HfApi(
endpoint="https://huggingface.co", # Can be a Private Hub endpoint.
token="hf_xxx", # Token is not persisted on the machine.
)
models = hf_api.list_models()
HfApi
class huggingface_hub.HfApi
< source >( endpoint: Optional[str] = None token: Union[str, bool, None] = None library_name: Optional[str] = None library_version: Optional[str] = None user_agent: Union[Dict, str, None] = None headers: Optional[Dict[str, str]] = None )
引數
- endpoint (
str
, 可選) — Hub 的端點。預設為 https://huggingface.co。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌 (字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法 (參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - library_name (
str
, 可選) — 發出 HTTP 請求的庫的名稱。將被新增到使用者代理頭中。例如:"transformers"
。 - library_version (
str
, 可選) — 發出 HTTP 請求的庫的版本。將被新增到使用者代理頭中。例如:"4.24.0"
。 - user_agent (
str
,dict
, 可選) — 使用者代理資訊,可以是字典或單個字串形式。它將補充已安裝軟體包的資訊。 - headers (
dict
, 可選) — 每次請求時傳送的附加標頭。例如:{"X-My-Header": "value"}
。此處傳遞的標頭優先於預設標頭。
透過 HTTP 與 Hugging Face Hub 互動的客戶端。
客戶端在初始化時會設定一些高階配置,這些配置將用於所有對 Hub 的請求(HF 端點、身份驗證、使用者代理等)。推薦使用 HfApi
客戶端,但不是強制性的,因為其所有公共方法都直接暴露在 huggingface_hub
的根目錄下。
accept_access_request
< source >( repo_id: str user: str repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要接受訪問請求的倉庫 ID。 - user (
str
) — 應接受其訪問請求的使用者的使用者名稱。 - repo_type (
str
, 可選) — 要接受訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌 (字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法 (參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
引發
HTTPError
接受使用者對給定門控倉庫的訪問請求。
請求被接受後,使用者將能夠下載倉庫的任何檔案並訪問社群選項卡。如果審批模式是自動的,則無需手動接受請求。已接受的請求可以隨時使用 cancel_access_request() 和 reject_access_request() 取消或拒絕。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
add_collection_item
< source >( collection_slug: str item_id: str item_type: CollectionItemType_T note: Optional[str] = None exists_ok: bool = False token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — 要更新的收藏集的 slug。例如:"TheBloke/recent-models-64f9a55bb3115b4f513ec026"
。 - item_id (
str
) — 要新增到收藏集的專案 ID。它可以是 Hub 上倉庫的 ID (例如"facebook/bart-large-mnli"
) 或論文 ID (例如"2307.09288"
)。 - item_type (
str
) — 要新增的專案的型別。可以是"model"
、"dataset"
、"space"
或"paper"
之一。 - note (
str
, 可選) — 附加到收藏集中專案的註釋。註釋的最大長度為 500 個字元。 - exists_ok (
bool
, 可選) — 如果為True
,則在專案已存在時不會引發錯誤。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌 (字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法 (參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
引發
HTTPError
向 Hub 上的收藏集新增一個專案。
返回: Collection
示例
>>> from huggingface_hub import add_collection_item
>>> collection = add_collection_item(
... collection_slug="davanstrien/climate-64f99dc2a5067f6b65531bab",
... item_id="pierre-loic/climate-news-articles",
... item_type="dataset"
... )
>>> collection.items[-1].item_id
"pierre-loic/climate-news-articles"
# ^item got added to the collection on last position
# Add item with a note
>>> add_collection_item(
... collection_slug="davanstrien/climate-64f99dc2a5067f6b65531bab",
... item_id="datasets/climate_fever",
... item_type="dataset"
... note="This dataset adopts the FEVER methodology that consists of 1,535 real-world claims regarding climate-change collected on the internet."
... )
(...)
add_space_secret
< source >( repo_id: str key: str value: str description: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - key (
str
) — 金鑰鍵。例如:"GITHUB_API_KEY"
- value (
str
) — 金鑰值。例如:"your_github_api_key"
。 - description (
str
, 可選) — 金鑰描述。例如:"用於訪問 Github API 的 Github API 金鑰"
。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌 (字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法 (參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
在 Space 中新增或更新一個金鑰。
金鑰允許您向 Space 設定金鑰或令牌,而無需對其進行硬編碼。更多詳情,請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets。
add_space_variable
< source >( repo_id: str key: str value: str description: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - key (
str
) — 變數鍵。例如:"MODEL_REPO_ID"
- value (
str
) — 變數值。例如:"the_model_repo_id"
。 - description (
str
) — 變數的描述。例如:"所實現模型的模型倉庫 ID"
。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌 (字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法 (參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
在 Space 中新增或更新一個變數。
變數允許您向 Space 設定環境變數,而無需對其進行硬編碼。更多詳情,請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets-and-environment-variables
auth_check
< source >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要檢查訪問許可權的倉庫。格式應為"user/repo_name"
。例如:"user/my-cool-model"
。 - repo_type (
str
, 可選) — 倉庫的型別。應為"model"
、"dataset"
或"space"
之一。如果未指定,預設為"model"
。 - token
(Union[bool, str, None]
, 可選) — 有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
以停用身份驗證。參考:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
-
RepositoryNotFoundError — 如果倉庫不存在、是私有的,或者使用者沒有訪問許可權,則會引發此異常。如果
repo_id
或repo_type
不正確,或者倉庫是私有的但使用者未進行身份驗證,則可能發生這種情況。 -
GatedRepoError — 如果倉庫存在但設定了門控,並且使用者無權訪問,則會引發此異常。
檢查提供的使用者令牌是否對 Hugging Face Hub 上的特定倉庫具有訪問許可權。
此方法驗證透過提供的令牌進行身份驗證的使用者是否對指定的倉庫具有訪問許可權。如果找不到倉庫或使用者缺少訪問所需的許可權,該方法將引發相應的異常。
示例
檢查使用者是否有權訪問倉庫
>>> from huggingface_hub import auth_check
>>> from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
try:
auth_check("user/my-cool-model")
except GatedRepoError:
# Handle gated repository error
print("You do not have permission to access this gated repository.")
except RepositoryNotFoundError:
# Handle repository not found error
print("The repository was not found or you do not have access.")
在這個例子中
- 如果使用者有訪問許可權,該方法將成功完成。
- 如果倉庫設定了門控或不存在,將引發相應的異常,允許使用者進行相應處理。
cancel_access_request
< source >( repo_id: str user: str repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要取消訪問請求的倉庫 ID。 - user (
str
) — 應取消其訪問請求的使用者的使用者名稱。 - repo_type (
str
, 可選) — 要取消訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
引發
HTTPError
取消使用者對給定受限倉庫的訪問請求。
已取消的請求將返回到待處理列表,使用者將失去對倉庫的訪問許可權。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
cancel_job
< source >( job_id: str namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- job_id (
str
) — 作業的 ID。 - namespace (
str
, 可選) — 作業執行的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
, 可選) — 一個有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
以停用身份驗證。請參閱:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
取消 Hugging Face 基礎設施上的計算作業。
change_discussion_status
< source >( repo_id: str discussion_num: int new_status: Literal['open', 'closed'] token: Union[bool, str, None] = None comment: Optional[str] = None repo_type: Optional[str] = None ) → DiscussionStatusChange
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,用/
分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - new_status (
str
) — 討論的新狀態,可以是"open"
或"closed"
。 - comment (
str
, 可選) — 一個可選的評論,隨狀態更改一起釋出。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
狀態更改事件
關閉或重新開啟一個討論或拉取請求。
示例
>>> new_title = "New title, fixing a typo"
>>> HfApi().rename_discussion(
... repo_id="username/repo_name",
... discussion_num=34
... new_title=new_title
... )
# DiscussionStatusChange(id='deadbeef0000000', type='status-change', ...)
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
comment_discussion
< source >( repo_id: str discussion_num: int comment: str token: Union[bool, str, None] = None repo_type: Optional[str] = None ) → DiscussionComment
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,用/
分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - comment (
str
) — 要建立的評論內容。評論支援 Markdown 格式。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
新建立的評論
在給定的討論中建立一條新評論。
示例
>>> comment = """
... Hello @otheruser!
...
... # This is a title
...
... **This is bold**, *this is italic* and ~this is strikethrough~
... And [this](http://url) is a link
... """
>>> HfApi().comment_discussion(
... repo_id="username/repo_name",
... discussion_num=34
... comment=comment
... )
# DiscussionComment(id='deadbeef0000000', type='comment', ...)
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
create_branch
< source >( repo_id: str branch: str revision: Optional[str] = None token: Union[bool, str, None] = None repo_type: Optional[str] = None exist_ok: bool = False )
引數
- repo_id (
str
) — 將在其中建立分支的倉庫。例如:"user/my-cool-model"
。 - branch (
str
) — 要建立的分支的名稱。 - revision (
str
, 可選) — 從其建立分支的 git 修訂版本。可以是一個分支名稱或提交的 OID/SHA,以十六進位制字串表示。預設為"main"
分支的頭部。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果在資料集或空間上建立分支,則設定為"dataset"
或"space"
;如果為模型打標籤,則為None
或"model"
。預設為None
。 - exist_ok (
bool
, 可選, 預設為False
) — 如果為True
,則如果分支已存在,不引發錯誤。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- BadRequestError — 如果分支的引用無效。例如:
refs/pr/5
或 ‘refs/foo/bar’。 - HfHubHTTPError — 如果分支已存在於倉庫中(錯誤 409)且
exist_ok
設定為False
。
在 Hub 上為倉庫建立一個新分支,從指定的修訂版本開始(預設為 main
)。要找到適合您需求的分支,您可以使用 list_repo_refs() 或 list_repo_commits()。
create_collection
< source >( title: str namespace: Optional[str] = None description: Optional[str] = None private: bool = False exists_ok: bool = False token: Union[bool, str, None] = None )
引數
- title (
str
) — 要建立的收藏集的標題。例如:"Recent models"
。 - namespace (
str
, 可選) — 要建立的收藏集的名稱空間(使用者名稱或組織)。將預設為所有者名稱。 - description (
str
, 可選) — 要建立的收藏集的描述。 - private (
bool
, 可選) — 收藏集是否應為私有。預設為False
(即公共收藏集)。 - exists_ok (
bool
, 可選) — 如果為True
,則如果收藏集已存在,不引發錯誤。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
在 Hub 上建立一個新的收藏集。
返回: Collection
create_commit
< source >( repo_id: str operations: Iterable[CommitOperation] commit_message: str commit_description: Optional[str] = None token: Union[str, bool, None] = None repo_type: Optional[str] = None revision: Optional[str] = None create_pr: Optional[bool] = None num_threads: int = 5 parent_commit: Optional[str] = None run_as_future: bool = False ) → CommitInfo or Future
引數
- repo_id (
str
) — 將在其中建立提交的倉庫,例如:"username/custom_transformers"
- operations (
Iterable
ofCommitOperation()
) — 一個包含在提交中的操作的可迭代物件,可以是:- CommitOperationAdd 用於上傳檔案
- CommitOperationDelete 用於刪除檔案
- CommitOperationCopy 用於複製檔案
操作物件將被修改以包含與上傳相關的資訊。請勿對多個提交重複使用相同的物件。
- commit_message (
str
) — 將建立的提交的摘要(第一行)。 - commit_description (
str
, 可選) — 將建立的提交的描述 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 從其提交的 git 修訂版本。預設為"main"
分支的頭部。 - create_pr (
boolean
, 可選) — 是否使用該提交建立一個拉取請求。預設為False
。如果revision
未設定,則針對"main"
分支開啟 PR。如果設定了revision
並且它是一個分支,則針對此分支開啟 PR。如果設定了revision
但不是一個分支名稱(例如:提交 oid),伺服器將返回RevisionNotFoundError
。 - num_threads (
int
, 可選) — 用於上傳檔案的併發執行緒數。預設為 5。將其設定為 2 意味著最多可以同時上傳 2 個檔案。 - parent_commit (
str
, 可選) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前 7 個字元)。如果指定且create_pr
為False
,則如果revision
未指向parent_commit
,提交將失敗。如果指定且create_pr
為True
,則將從parent_commit
建立拉取請求。指定parent_commit
可確保在提交更改之前倉庫沒有發生變化,這在倉庫被併發更新/提交時特別有用。 - run_as_future (
bool
, 可選) — 是否在後臺執行此方法。後臺作業按順序執行,不會阻塞主執行緒。傳遞run_as_future=True
將返回一個 Future 物件。預設為False
。
返回
CommitInfo or Future
CommitInfo 的例項,包含有關新建立的提交的資訊(提交雜湊、提交 URL、PR URL、提交訊息……)。如果傳遞了 run_as_future=True
,則返回一個 Future 物件,執行後將包含結果。
引發
ValueError
或 RepositoryNotFoundError
ValueError
— 如果提交訊息為空。ValueError
— 如果父提交不是有效的提交 OID。ValueError
— 如果提交了一個帶有無效元資料部分的 README.md 檔案。在這種情況下,提交將提前失敗,不會嘗試上傳任何檔案。ValueError
— 如果create_pr
為True
且 revision 既不是None
也不是"main"
。- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
在給定的倉庫中建立一個提交,根據需要刪除和上傳檔案。
輸入的 CommitOperation
列表將在提交過程中被修改。請勿對多個提交重複使用相同的物件。
create_commit
假定倉庫已存在於 Hub 上。如果您遇到客戶端 404 錯誤,請確保您已透過身份驗證,並且 repo_id
和 repo_type
設定正確。如果倉庫不存在,請先使用 create_repo() 建立它。
create_commit
對於 LFS 檔案限制為 25k 個,對於常規檔案限制為 1GB 的有效載荷。
create_discussion
< source >( repo_id: str title: str token: Union[bool, str, None] = None description: Optional[str] = None repo_type: Optional[str] = None pull_request: bool = False )
引數
- repo_id (
str
) — 名稱空間(使用者或組織)和倉庫名稱,以/
分隔。 - title (
str
) — 討論的標題。長度最多可達 200 個字元,且必須至少為 3 個字元。首尾的空白字元將被去除。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - description (
str
, 可選) — 拉取請求的可選描述。預設為"Discussion opened with the huggingface_hub Python library"
- pull_request (
bool
, 可選) — 是建立拉取請求還是討論。如果為True
,則建立拉取請求。如果為False
,則建立討論。預設為False
。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。
建立討論或拉取請求。
以程式設計方式建立的拉取請求將處於 "draft"
(草稿)狀態。
也可以使用 HfApi.create_commit() 一次性建立帶有更改的拉取請求。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
create_inference_endpoint
< 原始碼 >( name: str repository: str framework: str accelerator: str instance_size: str instance_type: str region: str vendor: str account_id: Optional[str] = None min_replica: int = 1 max_replica: int = 1 scale_to_zero_timeout: Optional[int] = None revision: Optional[str] = None task: Optional[str] = None custom_image: Optional[Dict] = None env: Optional[Dict[str, str]] = None secrets: Optional[Dict[str, str]] = None type: InferenceEndpointType = <InferenceEndpointType.PROTECTED: 'protected'> domain: Optional[str] = None path: Optional[str] = None cache_http_responses: Optional[bool] = None tags: Optional[List[str]] = None namespace: Optional[str] = None token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 新建推理端點的唯一名稱。 - repository (
str
) — 與推理端點關聯的模型倉庫名稱(例如"gpt2"
)。 - framework (
str
) — 模型使用的機器學習框架(例如"custom"
)。 - accelerator (
str
) — 用於推理的硬體加速器(例如"cpu"
)。 - instance_size (
str
) — 用於託管模型的例項的大小或型別(例如"x4"
)。 - instance_type (
str
) — 將部署推理端點的雲實例型別(例如"intel-icl"
)。 - region (
str
) — 將建立推理端點的雲區域(例如"us-east-1"
)。 - vendor (
str
) — 託管推理端點的雲提供商或供應商(例如"aws"
)。 - account_id (
str
, 可選) — 用於將 VPC 連結到私有推理端點的賬戶 ID(如果適用)。 - min_replica (
int
, 可選) — 為推理端點保持執行的最小副本(例項)數。要啟用縮減至零,請將此值設定為 0 並相應調整scale_to_zero_timeout
。預設為 1。 - max_replica (
int
, 可選) — 推理端點可擴充套件到的最大副本(例項)數。預設為 1。 - scale_to_zero_timeout (
int
, 可選) — 不活動的端點縮減至零之前的持續時間(分鐘),如果設定為 None 且min_replica
不為 0,則不縮減至零。預設為 None。 - revision (
str
, 可選) — 要在推理端點上部署的特定模型版本(例如"6c0e6080953db56375760c0471a8c5f2929baf11"
)。 - task (
str
, 可選) — 部署模型的任務(例如"text-classification"
)。 - custom_image (
Dict
, 可選) — 用於推理端點的自定義 Docker 映象。如果您想部署一個執行在text-generation-inference
(TGI) 框架上的推理端點,這將非常有用(請參閱示例)。 - env (
Dict[str, str]
, 可選) — 注入到容器環境中的非機密環境變數。 - secrets (
Dict[str, str]
, 可選) — 注入到容器環境中的機密值。 - type ([`InferenceEndpointType]
, *可選*) -- 推理端點的型別,可以是
“protected”(預設)、
“public”或
“private”`。 - domain (
str
, 可選) — 推理端點部署的自定義域名,如果設定,推理端點將在此域名上可用(例如"my-new-domain.cool-website.woof"
)。 - path (
str
, 可選) — 已部署模型的自定義路徑,應以/
開頭(例如"/models/google-bert/bert-base-uncased"
)。 - cache_http_responses (
bool
, 可選) — 是否快取來自推理端點的 HTTP 響應。預設為False
。 - tags (
List[str]
, 可選) — 與推理端點關聯的標籤列表。 - namespace (
str
, 可選) — 將建立推理端點的名稱空間。預設為當前使用者的名稱空間。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
有關更新後的推理端點的資訊。
建立一個新的推理端點。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> endpoint = api.create_inference_endpoint(
... "my-endpoint-name",
... repository="gpt2",
... framework="pytorch",
... task="text-generation",
... accelerator="cpu",
... vendor="aws",
... region="us-east-1",
... type="protected",
... instance_size="x2",
... instance_type="intel-icl",
... )
>>> endpoint
InferenceEndpoint(name='my-endpoint-name', status="pending",...)
# Run inference on the endpoint
>>> endpoint.client.text_generation(...)
"..."
# Start an Inference Endpoint running Zephyr-7b-beta on TGI
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> endpoint = api.create_inference_endpoint(
... "aws-zephyr-7b-beta-0486",
... repository="HuggingFaceH4/zephyr-7b-beta",
... framework="pytorch",
... task="text-generation",
... accelerator="gpu",
... vendor="aws",
... region="us-east-1",
... type="protected",
... instance_size="x1",
... instance_type="nvidia-a10g",
... env={
... "MAX_BATCH_PREFILL_TOKENS": "2048",
... "MAX_INPUT_LENGTH": "1024",
... "MAX_TOTAL_TOKENS": "1512",
... "MODEL_ID": "/repository"
... },
... custom_image={
... "health_route": "/health",
... "url": "ghcr.io/huggingface/text-generation-inference:1.1.0",
... },
... secrets={"MY_SECRET_KEY": "secret_value"},
... tags=["dev", "text-generation"],
... )
# Start an Inference Endpoint running ProsusAI/finbert while scaling to zero in 15 minutes
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> endpoint = api.create_inference_endpoint(
... "finbert-classifier",
... repository="ProsusAI/finbert",
... framework="pytorch",
... task="text-classification",
... min_replica=0,
... scale_to_zero_timeout=15,
... accelerator="cpu",
... vendor="aws",
... region="us-east-1",
... type="protected",
... instance_size="x2",
... instance_type="intel-icl",
... )
>>> endpoint.wait(timeout=300)
# Run inference on the endpoint
>>> endpoint.client.text_generation(...)
TextClassificationOutputElement(label='positive', score=0.8983615040779114)
create_inference_endpoint_from_catalog
< 原始碼 >( repo_id: str name: Optional[str] = None token: Union[bool, str, None] = None namespace: Optional[str] = None ) → InferenceEndpoint
引數
- repo_id (
str
) — 目錄中要部署為推理端點的模型的 ID。 - name (
str
, 可選) — 新建推理端點的唯一名稱。如果未提供,將生成一個隨機名稱。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。
- namespace (
str
, 可選) — 將建立推理端點的名稱空間。預設為當前使用者的名稱空間。
有關新建推理端點的資訊。
從 Hugging Face 推理目錄中的模型建立一個新的推理端點。
推理目錄的目標是提供一個經過篩選的模型列表,這些模型已針對推理進行了最佳化,並且其預設配置已經過測試。有關目錄中可用模型的列表,請參見 https://endpoints.huggingface.co/catalog。
create_inference_endpoint_from_catalog
是實驗性的。其 API 將來可能會更改。如果您有任何建議或請求,請提供反饋。
create_pull_request
< 原始碼 >( repo_id: str title: str token: Union[bool, str, None] = None description: Optional[str] = None repo_type: Optional[str] = None )
引數
- repo_id (
str
) — 名稱空間(使用者或組織)和倉庫名稱,以/
分隔。 - title (
str
) — 討論的標題。長度最多可達 200 個字元,且必須至少為 3 個字元。首尾的空白字元將被去除。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - description (
str
, 可選) — 拉取請求的可選描述。預設為"Discussion opened with the huggingface_hub Python library"
- repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。
建立一個拉取請求。以程式設計方式建立的拉取請求將處於 "draft"
(草稿)狀態。
也可以使用 HfApi.create_commit() 一次性建立帶有更改的拉取請求;
這是 HfApi.create_discussion() 的一個封裝。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
create_repo
< 原始碼 >( repo_id: str token: Union[str, bool, None] = None private: Optional[bool] = None repo_type: Optional[str] = None exist_ok: bool = False resource_group_id: Optional[str] = None space_sdk: Optional[str] = None space_hardware: Optional[SpaceHardware] = None space_storage: Optional[SpaceStorage] = None space_sleep_time: Optional[int] = None space_secrets: Optional[List[Dict[str, str]]] = None space_variables: Optional[List[Dict[str, str]]] = None ) → RepoUrl
引數
- repo_id (
str
) — 名稱空間(使用者或組織)和倉庫名稱,以/
分隔。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - private (
bool
, 可選) — 是否將倉庫設為私有。如果為None
(預設),倉庫將為公開,除非組織的預設設定為私有。如果倉庫已存在,此值將被忽略。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - exist_ok (
bool
, 可選, 預設為False
) — 如果為True
,則在倉庫已存在時不引發錯誤。 - resource_group_id (
str
, 可選) — 建立倉庫的資源組。資源組僅適用於企業版 Hub 組織,用於定義組織中哪些成員可以訪問該資源。資源組的 ID 可以在 Hub 上該資源的頁面 URL 中找到(例如"66670e5163145ca562cb1988"
)。要了解有關資源組的更多資訊,請參閱 https://huggingface.co/docs/hub/en/security-resource-groups。 - space_sdk (
str
, 可選) — 如果 repo_type 是 “space”,則選擇要使用的 SDK。可以是 “streamlit”、“gradio”、“docker” 或 “static”。 - space_hardware (
SpaceHardware
或str
, 可選) — 如果 repo_type 是 “space”,則選擇硬體。完整列表請參閱 SpaceHardware。 - space_storage (
SpaceStorage
或str
, 可選) — 持久化儲存層的選擇。示例:"small"
。完整列表請參閱 SpaceStorage。 - space_sleep_time (
int
, 可選) — 在 Space 進入休眠狀態之前等待的非活動秒數。如果你不希望你的 Space 休眠,請設定為-1
(升級硬體的預設行為)。對於免費硬體,你無法配置休眠時間(固定為 48 小時不活動)。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-gpus#sleep-time。 - space_secrets (
List[Dict[str, str]]
, 可選) — 在你的 Space 中設定的私密金鑰列表。每個專案都採用{"key": ..., "value": ..., "description": ...}
的形式,其中 description 是可選的。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets。 - space_variables (
List[Dict[str, str]]
, 可選) — 在你的 Space 中設定的公共環境變數列表。每個專案都採用{"key": ..., "value": ..., "description": ...}
的形式,其中 description 是可選的。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets-and-environment-variables。
返回
新建立倉庫的 URL。該值是 str
的子類,包含 endpoint
、repo_type
和 repo_id
等屬性。
在 HuggingFace Hub 上建立一個空倉庫。
create_tag
< 原始碼 >( repo_id: str tag: str tag_message: Optional[str] = None revision: Optional[str] = None token: Union[bool, str, None] = None repo_type: Optional[str] = None exist_ok: bool = False )
引數
- repo_id (
str
) — 將為其提交新增標籤的倉庫。示例:"user/my-cool-model"
。 - tag (
str
) — 要建立的標籤的名稱。 - tag_message (
str
, 可選) — 要建立的標籤的描述。 - revision (
str
, 可選) — 要標記的 git 版本。可以是一個分支名,也可以是提交的 OID/SHA 的十六進位制字串。也支援縮寫(前 7 個字元)。預設為"main"
分支的頭部。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果要給資料集或 Space 打標籤,則設定為"dataset"
或"space"
;如果要給模型打標籤,則為None
或"model"
。預設為None
。 - exist_ok (
bool
, 可選, 預設為False
) — 如果為True
,當標籤已存在時,不引發錯誤。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果在倉庫中找不到版本(錯誤 404)。
- HfHubHTTPError — 如果分支已存在於倉庫中(錯誤 409)且
exist_ok
設定為False
。
為 Hub 上的倉庫的給定提交新增標籤。
create_webhook
< 原始碼 >( url: str watched: List[Union[Dict, WebhookWatchedItem]] domains: Optional[List[constants.WEBHOOK_DOMAIN_T]] = None secret: Optional[str] = None token: Union[bool, str, None] = None ) → WebhookInfo
引數
- url (
str
) — 用於傳送負載的 URL。 - watched (
List[WebhookWatchedItem]
) — Webhook 將監視的 WebhookWatchedItem 列表。可以是使用者、組織、模型、資料集或 Spaces。監視的專案也可以作為普通字典提供。 - domains (
List[Literal["repo", "discussion"]]
, 可選) — 要監視的域列表。可以是 “repo”、“discussion” 或兩者。 - secret (
str
, 可選) — 用於簽署負載的金鑰。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於新建立的 webhook 的資訊。
建立一個新的 webhook。
示例
>>> from huggingface_hub import create_webhook
>>> payload = create_webhook(
... watched=[{"type": "user", "name": "julien-c"}, {"type": "org", "name": "HuggingFaceH4"}],
... url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
... domains=["repo", "discussion"],
... secret="my-secret",
... )
>>> print(payload)
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
domains=["repo", "discussion"],
secret="my-secret",
disabled=False,
)
dataset_info
< 原始碼 >( repo_id: str revision: Optional[str] = None timeout: Optional[float] = None files_metadata: bool = False expand: Optional[List[ExpandDatasetProperty_T]] = None token: Union[bool, str, None] = None ) → hf_api.DatasetInfo
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - revision (
str
, 可選) — 資料集倉庫的版本,用於獲取資訊。 - timeout (
float
, 可選) — 是否為向 Hub 的請求設定超時。 - files_metadata (
bool
, 可選) — 是否檢索倉庫中檔案的元資料(大小、LFS 元資料等)。預設為False
。 - expand (
List[ExpandDatasetProperty_T]
, 可選) — 在響應中返回的屬性列表。使用時,只有列表中的屬性會被返回。如果傳入了files_metadata
,則不能使用此引數。可能的值有"author"
、"cardData"
、"citation"
、"createdAt"
、"disabled"
、"description"
、"downloads"
、"downloadsAllTime"
、"gated"
、"lastModified"
、"likes"
、"paperswithcode_id"
、"private"
、"siblings"
、"sha"
、"tags"
、"trendingScore"
、"usedStorage"
、"resourceGroup"
和"xetEnabled"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
資料集倉庫資訊。
獲取 huggingface.co 上某個特定資料集的資訊。
如果傳遞了可接受的令牌,資料集可以是私有的。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
delete_branch
< 原始碼 >( repo_id: str branch: str token: Union[bool, str, None] = None repo_type: Optional[str] = None )
引數
- repo_id (
str
) — 將刪除分支的倉庫。示例:"user/my-cool-model"
。 - branch (
str
) — 要刪除的分支的名稱。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果在資料集或 Space 上建立分支,則設定為"dataset"
或"space"
;如果為模型打標籤,則為None
或"model"
。預設為None
。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- HfHubHTTPError — 如果試圖刪除一個受保護的分支。例如:
main
無法被刪除。 - HfHubHTTPError — 如果試圖刪除一個不存在的分支。
從 Hub 上的倉庫中刪除一個分支。
delete_collection
< 原始碼 >( collection_slug: str missing_ok: bool = False token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — 要刪除的收藏的 slug。示例:"TheBloke/recent-models-64f9a55bb3115b4f513ec026"
。 - missing_ok (
bool
, 可選) — 如果為True
,當收藏不存在時,不引發錯誤。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
在 Hub 上刪除一個收藏。
示例
>>> from huggingface_hub import delete_collection
>>> collection = delete_collection("username/useless-collection-64f9a55bb3115b4f513ec026", missing_ok=True)
這是一個不可逆轉的操作。被刪除的收藏無法恢復。
delete_collection_item
< 原始碼 >( collection_slug: str item_object_id: str missing_ok: bool = False token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — 要更新的收藏的 slug。示例:"TheBloke/recent-models-64f9a55bb3115b4f513ec026"
。 - item_object_id (
str
) — 收藏中專案的 ID。這不是 Hub 上專案(repo_id 或 paper id)的 ID。必須從 CollectionItem 物件中檢索。示例:collection.items[0].item_object_id
。 - missing_ok (
bool
, 可選) — 如果為True
,當專案不存在時,不引發錯誤。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
從收藏中刪除一個專案。
示例
>>> from huggingface_hub import get_collection, delete_collection_item
# Get collection first
>>> collection = get_collection("TheBloke/recent-models-64f9a55bb3115b4f513ec026")
# Delete item based on its ID
>>> delete_collection_item(
... collection_slug="TheBloke/recent-models-64f9a55bb3115b4f513ec026",
... item_object_id=collection.items[-1].item_object_id,
... )
delete_file
< 原始碼 >( path_in_repo: str repo_id: str token: Union[str, bool, None] = None repo_type: Optional[str] = None revision: Optional[str] = None commit_message: Optional[str] = None commit_description: Optional[str] = None create_pr: Optional[bool] = None parent_commit: Optional[str] = None )
引數
- path_in_repo (
str
) — 倉庫中的相對檔案路徑,例如:"checkpoints/1fec34a/weights.bin"
- repo_id (
str
) — 將從中刪除檔案的倉庫,例如:"username/custom_transformers"
- token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果檔案在資料集或 Space 中,則設定為"dataset"
或"space"
;如果在模型中,則為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 要提交的 git 修訂版本。預設為"main"
分支的頭部。 - commit_message (
str
, 可選) — 生成的提交的摘要/標題/第一行。預設為f"Delete {path_in_repo} with huggingface_hub"
。 - commit_description (
str
可選) — 生成的提交的描述。 - create_pr (
boolean
, 可選) — 是否為該提交建立一個拉取請求 (Pull Request)。預設為False
。如果未設定revision
,則 PR 將針對"main"
分支開啟。如果設定了revision
並且它是一個分支,則 PR 將針對此分支開啟。如果設定了revision
但它不是一個分支名(例如:一個提交 oid),伺服器將返回一個RevisionNotFoundError
。 - parent_commit (
str
, 可選) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前7個字元)。如果指定且create_pr
為False
,當revision
沒有指向parent_commit
時,提交將失敗。如果指定且create_pr
為True
,拉取請求將從parent_commit
建立。指定parent_commit
可以確保在提交更改之前倉庫沒有發生變化,這在併發更新/提交倉庫時特別有用。
在給定的倉庫中刪除一個檔案。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
- EntryNotFoundError 如果找不到要下載的檔案。
delete_files
< 原始碼 >( repo_id: str delete_patterns: List[str] token: Union[bool, str, None] = None repo_type: Optional[str] = None revision: Optional[str] = None commit_message: Optional[str] = None commit_description: Optional[str] = None create_pr: Optional[bool] = None parent_commit: Optional[str] = None )
引數
- repo_id (
str
) — 將從中刪除資料夾的倉庫,例如:"username/custom_transformers"
- delete_patterns (
List[str]
) — 要刪除的檔案或資料夾列表。每個字串可以是檔案路徑、資料夾路徑或 Unix shell 風格的萬用字元。例如["file.txt", "folder/", "data/*.parquet"]
- token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 要刪除檔案的倉庫型別。可以是"model"
、"dataset"
或"space"
。預設為"model"
。 - revision (
str
, 可選) — 要提交的 git 修訂版本。預設為"main"
分支的頭部。 - commit_message (
str
, 可選) — 生成的提交的摘要(第一行)。預設為f"Delete files using huggingface_hub"
。 - commit_description (
str
可選) — 生成的提交的描述。 - create_pr (
boolean
, 可選) — 是否為該提交建立一個拉取請求 (Pull Request)。預設為False
。如果未設定revision
,則 PR 將針對"main"
分支開啟。如果設定了revision
並且它是一個分支,則 PR 將針對此分支開啟。如果設定了revision
但它不是一個分支名(例如:一個提交 oid),伺服器將返回一個RevisionNotFoundError
。 - parent_commit (
str
, 可選) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前7個字元)。如果指定且create_pr
為False
,當revision
沒有指向parent_commit
時,提交將失敗。如果指定且create_pr
為True
,拉取請求將從parent_commit
建立。指定parent_commit
可以確保在提交更改之前倉庫沒有發生變化,這在併發更新/提交倉庫時特別有用。
從 Hub 上的倉庫刪除檔案。
如果提供資料夾路徑,則整個資料夾及其包含的所有檔案都將被刪除。
delete_folder
< 原始碼 >( path_in_repo: str repo_id: str token: Union[bool, str, None] = None repo_type: Optional[str] = None revision: Optional[str] = None commit_message: Optional[str] = None commit_description: Optional[str] = None create_pr: Optional[bool] = None parent_commit: Optional[str] = None )
引數
- path_in_repo (
str
) — 倉庫中的相對資料夾路徑,例如:"checkpoints/1fec34a"
。 - repo_id (
str
) — 將從中刪除資料夾的倉庫,例如:"username/custom_transformers"
- token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果資料夾位於資料集中,則設定為"dataset"
;如果位於空間中,則設定為"space"
;如果位於模型中,則為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 要提交的 git 修訂版本。預設為"main"
分支的頭部。 - commit_message (
str
, 可選) — 生成的提交的摘要/標題/第一行。預設為f"Delete folder {path_in_repo} with huggingface_hub"
。 - commit_description (
str
可選) — 生成的提交的描述。 - create_pr (
boolean
, 可選) — 是否為該提交建立一個拉取請求 (Pull Request)。預設為False
。如果未設定revision
,則 PR 將針對"main"
分支開啟。如果設定了revision
並且它是一個分支,則 PR 將針對此分支開啟。如果設定了revision
但它不是一個分支名(例如:一個提交 oid),伺服器將返回一個RevisionNotFoundError
。 - parent_commit (
str
, 可選) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前7個字元)。如果指定且create_pr
為False
,當revision
沒有指向parent_commit
時,提交將失敗。如果指定且create_pr
為True
,拉取請求將從parent_commit
建立。指定parent_commit
可以確保在提交更改之前倉庫沒有發生變化,這在併發更新/提交倉庫時特別有用。
在給定的倉庫中刪除一個資料夾。
create_commit() 方法的簡單包裝。
delete_inference_endpoint
< 原始碼 >( name: str namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- name (
str
) — 要刪除的推理端點的名稱。 - namespace (
str
, 可選) — 推理端點所在的名稱空間。預設為當前使用者。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
刪除一個推理端點。
此操作不可逆。如果您不想為一個推理端點付費,最好使用 pause_inference_endpoint() 暫停它,或使用 scale_to_zero_inference_endpoint() 將其縮減至零。
為方便起見,您還可以使用 InferenceEndpoint.delete() 刪除推理端點。
delete_repo
< 原始碼 >( repo_id: str token: Union[str, bool, None] = None repo_type: Optional[str] = None missing_ok: bool = False )
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,用/
分隔。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果上傳到資料集,則設定為"dataset"
;如果上傳到空間,則設定為"space"
;如果上傳到模型,則為None
或"model"
。 - missing_ok (
bool
, 可選, 預設為False
) — 如果為True
,當倉庫不存在時不會引發錯誤。
- RepositoryNotFoundError — 如果要刪除的倉庫找不到並且
missing_ok
設定為 False(預設值)。
從 HuggingFace Hub 刪除一個倉庫。注意:此操作不可逆。
delete_space_secret
< 原始碼 >( repo_id: str key: str token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - key (
str
) — 金鑰。例如:"GITHUB_API_KEY"
。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
從 Space 中刪除一個金鑰。
金鑰允許您向 Space 設定金鑰或令牌,而無需對其進行硬編碼。更多詳情,請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets。
delete_space_storage
< 原始碼 >( repo_id: str token: Union[bool, str, None] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要更新的 Space 的 ID。例如:"open-llm-leaderboard/open_llm_leaderboard"
。 - token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於 Space 的執行時資訊,包括 Space 的階段和硬體。
引發
BadRequestError
BadRequestError
— 如果空間沒有持久化儲存。
刪除 Space 的持久化儲存。
delete_space_variable
< 原始碼 >( repo_id: str key: str token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - key (
str
) — 變數鍵。例如:"MODEL_REPO_ID"
- token (Union[bool, str, None], 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
從 Space 中刪除一個變數。
變數允許您向 Space 設定環境變數,而無需對其進行硬編碼。更多詳情,請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets-and-environment-variables
delete_tag
< 原始碼 >( repo_id: str tag: str token: Union[bool, str, None] = None repo_type: Optional[str] = None )
引數
- repo_id (
str
) — 將在其中刪除標籤的倉庫。例如:"user/my-cool-model"
。 - tag (
str
) — 要刪除的標籤的名稱。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
- repo_type (
str
, 可選) — 如果為資料集或空間新增標籤,則設定為 `"dataset"` 或 `"space"`;如果為模型新增標籤,則設定為 `None` 或 `"model"`。預設為 `None`。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果未找到標籤。
從 Hub 上的倉庫中刪除一個標籤。
delete_webhook
< source >( webhook_id: str token: Union[bool, str, None] = None ) → None
引數
- webhook_id (
str
) — 要刪除的 Webhook 的唯一識別符號。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
返回
無
刪除一個 Webhook。
disable_webhook
< source >( webhook_id: str token: Union[bool, str, None] = None ) → WebhookInfo
引數
- webhook_id (
str
) — 要停用的 Webhook 的唯一識別符號。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
返回
有關已停用 Webhook 的資訊。
停用一個 Webhook(將其狀態設定為“disabled”)。
示例
>>> from huggingface_hub import disable_webhook
>>> disabled_webhook = disable_webhook("654bbbc16f2ec14d77f109cc")
>>> disabled_webhook
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
domains=["repo", "discussion"],
secret="my-secret",
disabled=True,
)
duplicate_space
< source >( from_id: str to_id: Optional[str] = None private: Optional[bool] = None token: Union[bool, str, None] = None exist_ok: bool = False hardware: Optional[SpaceHardware] = None storage: Optional[SpaceStorage] = None sleep_time: Optional[int] = None secrets: Optional[List[Dict[str, str]]] = None variables: Optional[List[Dict[str, str]]] = None ) → RepoUrl
引數
- from_id (
str
) — 要複製的 Space 的 ID。例如:"pharma/CLIP-Interrogator"
。 - to_id (
str
, 可選) — 新 Space 的 ID。例如:"dog/CLIP-Interrogator"
。如果未提供,新 Space 將與原始 Space 同名,但在您的帳戶下。 - private (
bool
, 可選) — 新 Space 是否應為私有。預設為與原始 Space 相同的隱私設定。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
- exist_ok (
bool
, 可選, 預設為False
) — 如果為 `True`,則在倉庫已存在時不會引發錯誤。 - hardware (
SpaceHardware
或str
, 可選) — 硬體選擇。例如:`"t4-medium"`。完整列表請參閱 SpaceHardware。 - storage (
SpaceStorage
或str
, 可選) — 持久儲存層級的選擇。例如:`"small"`。完整列表請參閱 SpaceStorage。 - sleep_time (
int
, 可選) — Space 進入休眠前等待的非活動秒數。如果您不希望 Space 休眠(升級硬體的預設行為),請設定為 `-1`。對於免費硬體,您無法配置休眠時間(固定為 48 小時不活動)。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-gpus#sleep-time。 - secrets (
List[Dict[str, str]]
, 可選) — 要在您的 Space 中設定的私密金鑰列表。每個專案格式為 `{"key": ..., "value": ..., "description": ...}`,其中 description 是可選的。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets。 - variables (
List[Dict[str, str]]
, 可選) — 要在您的 Space 中設定的公共環境變數列表。每個專案格式為 `{"key": ..., "value": ..., "description": ...}`,其中 description 是可選的。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets-and-environment-variables。
返回
新建立倉庫的 URL。該值是 str
的子類,包含 endpoint
、repo_type
和 repo_id
等屬性。
引發
RepositoryNotFoundError 或 `HTTPError`
- RepositoryNotFoundError — 如果找不到 `from_id` 或 `to_id` 中的一個。這可能是因為它不存在,或者它被設定為 `private` 而您沒有訪問許可權。
HTTPError
— 如果 HuggingFace API 返回錯誤。
複製一個 Space。
以程式設計方式複製一個 Space。新的 Space 將在您的帳戶中建立,並且狀態將與原始 Space 相同(執行中或已暫停)。無論 Space 當前的狀態如何,您都可以複製它。
示例
>>> from huggingface_hub import duplicate_space
# Duplicate a Space to your account
>>> duplicate_space("multimodalart/dreambooth-training")
RepoUrl('https://huggingface.co/spaces/nateraw/dreambooth-training',...)
# Can set custom destination id and visibility flag.
>>> duplicate_space("multimodalart/dreambooth-training", to_id="my-dreambooth", private=True)
RepoUrl('https://huggingface.co/spaces/nateraw/my-dreambooth',...)
edit_discussion_comment
< source >( repo_id: str discussion_num: int comment_id: str new_content: str token: Union[bool, str, None] = None repo_type: Optional[str] = None ) → DiscussionComment
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以 `/` 分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - comment_id (
str
) — 要編輯的評論的 ID。 - new_content (
str
) — 評論的新內容。評論支援 markdown 格式。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,則設定為 `"dataset"` 或 `"space"`;如果上傳到模型,則設定為 `None` 或 `"model"`。預設為 `None`。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
已編輯的評論
編輯討論/拉取請求中的評論。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
enable_webhook
< source >( webhook_id: str token: Union[bool, str, None] = None ) → WebhookInfo
引數
- webhook_id (
str
) — 要啟用的 Webhook 的唯一識別符號。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
返回
有關已啟用 Webhook 的資訊。
啟用一個 Webhook(將其狀態設定為“active”)。
示例
>>> from huggingface_hub import enable_webhook
>>> enabled_webhook = enable_webhook("654bbbc16f2ec14d77f109cc")
>>> enabled_webhook
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
domains=["repo", "discussion"],
secret="my-secret",
disabled=False,
)
fetch_job_logs
< source >( job_id: str namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- job_id (
str
) — 任務的 ID。 - namespace (
str
, 可選) — 任務執行的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
, 可選) — 一個有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為 `False` 以停用身份驗證。請參閱:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
從 Hugging Face 基礎設施上的計算任務中獲取所有日誌。
file_exists
< source >( repo_id: str filename: str repo_type: Optional[str] = None revision: Optional[str] = None token: Union[str, bool, None] = None )
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以 `/` 分隔。 - filename (
str
) — 要檢查的檔名,例如:`"config.json"` - repo_type (
str
, 可選) — 如果從資料集或空間獲取倉庫資訊,則設定為 `"dataset"` 或 `"space"`;如果從模型獲取倉庫資訊,則設定為 `None` 或 `"model"`。預設為 `None`。 - revision (
str
, 可選) — 獲取資訊的倉庫的版本。預設為 `"main"` 分支。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
檢查 Hugging Face Hub 上的倉庫中是否存在某個檔案。
get_collection
< source >( collection_slug: str token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — Hub 上集合的 slug。例如:`"TheBloke/recent-models-64f9a55bb3115b4f513ec026"`。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
獲取 Hub 上集合的資訊。
返回: Collection
示例
>>> from huggingface_hub import get_collection
>>> collection = get_collection("TheBloke/recent-models-64f9a55bb3115b4f513ec026")
>>> collection.title
'Recent models'
>>> len(collection.items)
37
>>> collection.items[0]
CollectionItem(
item_object_id='651446103cd773a050bf64c2',
item_id='TheBloke/U-Amethyst-20B-AWQ',
item_type='model',
position=88,
note=None
)
將所有有效的資料集標籤列為一個巢狀的名稱空間物件。
get_discussion_details
< source >( repo_id: str discussion_num: int repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以 `/` 分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,則設定為 `"dataset"` 或 `"space"`;如果上傳到模型,則設定為 `None` 或 `"model"`。預設為 `None`。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 `False`。
從 Hub 獲取討論/拉取請求的詳細資訊。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
get_full_repo_name
< source >( model_id: str organization: Optional[str] = None token: Union[bool, str, None] = None ) → str
引數
- model_id (
str
) — 模型的名稱。 - organization (
str
, 可選) — 如果傳入此引數,倉庫名稱將在組織名稱空間下,而不是使用者名稱空間下。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
字串
如果未傳入組織,則倉庫名稱在使用者名稱空間下 ({username}/{model_id}),否則在組織名稱空間下 ({organization}/{model_id})。
返回給定模型 ID 和可選組織的倉庫名稱。
get_hf_file_metadata
< source >( url: str token: Union[bool, str, None] = None proxies: Optional[Dict] = None timeout: Optional[float] = 10 )
引數
- url (
str
) — 檔案 URL,例如由 hf_hub_url() 返回。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。 - proxies (
dict
, 可選) — 協議到代理 URL 的字典對映,傳遞給requests.request
。 - timeout (
float
, 可選, 預設為 10) — 等待伺服器傳送元資料多少秒後放棄。
獲取給定 URL 在 Hub 上版本化的檔案的元資料。
get_inference_endpoint
< source >( name: str namespace: Optional[str] = None token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 要檢索資訊的推理端點的名稱。 - namespace (
str
, 可選) — 推理端點所在的名稱空間。預設為當前使用者。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
有關請求的推理端點的資訊。
獲取有關推理端點的資訊。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> endpoint = api.get_inference_endpoint("my-text-to-image")
>>> endpoint
InferenceEndpoint(name='my-text-to-image', ...)
# Get status
>>> endpoint.status
'running'
>>> endpoint.url
'https://my-text-to-image.region.vendor.endpoints.huggingface.cloud'
# Run inference
>>> endpoint.client.text_to_image(...)
以巢狀名稱空間物件的形式列出所有有效的模型標籤。
get_paths_info
< source >( repo_id: str paths: Union[List[str], str] expand: bool = False revision: Optional[str] = None repo_type: Optional[str] = None token: Union[str, bool, None] = None ) → List[Union[RepoFile, RepoFolder]]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - paths (
Union[List[str], str]
, 可選) — 要獲取資訊的路徑。如果路徑不存在,它將被忽略,而不會引發異常。 - expand (
bool
, 可選, 預設為False
) — 是否獲取有關路徑的更多資訊(例如,最後一次提交和檔案的安全掃描結果)。此操作對伺服器來說成本更高,因此每頁只返回 50 個結果(而不是 1000 個)。由於分頁是在huggingface_hub
中實現的,除了獲取結果所需的時間外,這對您是透明的。 - revision (
str
, 可選) — 要獲取資訊的倉庫版本。預設為"main"
分支。 - repo_type (
str
, 可選) — 要獲取資訊的倉庫型別 ("model"
,"dataset"
或"space"
)。預設為"model"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
List[Union[RepoFile, RepoFolder]]
路徑的資訊,作為 RepoFile
和 RepoFolder
物件的列表。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果在倉庫中找不到版本(錯誤 404)。
獲取有關倉庫路徑的資訊。
示例
>>> from huggingface_hub import get_paths_info
>>> paths_info = get_paths_info("allenai/c4", ["README.md", "en"], repo_type="dataset")
>>> paths_info
[
RepoFile(path='README.md', size=2379, blob_id='f84cb4c97182890fc1dbdeaf1a6a468fd27b4fff', lfs=None, last_commit=None, security=None),
RepoFolder(path='en', tree_id='dc943c4c40f53d02b31ced1defa7e5f438d5862e', last_commit=None)
]
get_repo_discussions
< source >( repo_id: str author: Optional[str] = None discussion_type: Optional[constants.DiscussionTypeFilter] = None discussion_status: Optional[constants.DiscussionStatusFilter] = None repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → Iterator[Discussion]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - author (
str
, 可選) — 傳入一個值以按討論作者進行篩選。None
表示不篩選。預設為None
。 - discussion_type (
str
, 可選) — 設定為"pull_request"
以僅獲取拉取請求,"discussion"
以僅獲取討論。設定為"all"
或None
以獲取兩者。預設為None
。 - discussion_status (
str
, 可選) — 設定為"open"
(或"closed"
) 以僅獲取開啟 (或關閉) 的討論。設定為"all"
或None
以獲取兩者。預設為None
。 - repo_type (
str
, 可選) — 如果從資料集或空間獲取,則設定為"dataset"
或"space"
,如果從模型獲取,則設定為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
Iterator[Discussion]
Discussion 物件的迭代器。
獲取給定倉庫的討論和拉取請求。
示例
get_safetensors_metadata
< source >( repo_id: str repo_type: Optional[str] = None revision: Optional[str] = None token: Union[bool, str, None] = None ) → SafetensorsRepoMetadata
引數
- repo_id (
str
) — 一個使用者或組織名稱和一個由/
分隔的倉庫名稱。 - repo_type (
str
, 可選) — 如果檔案在資料集或空間中,則設定為"dataset"
或"space"
,如果在模型中,則設定為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 獲取檔案的 git 版本。可以是一個分支名、一個標籤或一個提交雜湊。預設為"main"
分支的頭部。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
SafetensorsRepoMetadata
與 safetensors 倉庫相關的資訊。
引發
NotASafetensorsRepoError
或 SafetensorsParsingError
NotASafetensorsRepoError
— 如果倉庫不是 safetensors 倉庫,即沒有model.safetensors
或model.safetensors.index.json
檔案。SafetensorsParsingError
— 如果 safetensors 檔案頭無法正確解析。
解析 Hub 上 safetensors 倉庫的元資料。
我們首先檢查倉庫是單個 safetensors 檔案還是分片的 safetensors 倉庫。如果是單個 safetensors 檔案,我們從該檔案解析元資料。如果是分片的 safetensors 倉庫,我們從索引檔案解析元資料,然後從每個分片解析元資料。
要從單個 safetensors 檔案解析元資料,請使用 parse_safetensors_file_metadata()。
有關 safetensors 格式的更多詳細資訊,請檢視 https://huggingface.co/docs/safetensors/index#format。
示例
# Parse repo with single weights file
>>> metadata = get_safetensors_metadata("bigscience/bloomz-560m")
>>> metadata
SafetensorsRepoMetadata(
metadata=None,
sharded=False,
weight_map={'h.0.input_layernorm.bias': 'model.safetensors', ...},
files_metadata={'model.safetensors': SafetensorsFileMetadata(...)}
)
>>> metadata.files_metadata["model.safetensors"].metadata
{'format': 'pt'}
# Parse repo with sharded model
>>> metadata = get_safetensors_metadata("bigscience/bloom")
Parse safetensors files: 100%|██████████████████████████████████████████| 72/72 [00:12<00:00, 5.78it/s]
>>> metadata
SafetensorsRepoMetadata(metadata={'total_size': 352494542848}, sharded=True, weight_map={...}, files_metadata={...})
>>> len(metadata.files_metadata)
72 # All safetensors files have been fetched
# Parse repo with sharded model
>>> get_safetensors_metadata("runwayml/stable-diffusion-v1-5")
NotASafetensorsRepoError: 'runwayml/stable-diffusion-v1-5' is not a safetensors repo. Couldn't find 'model.safetensors.index.json' or 'model.safetensors' files.
get_space_runtime
< source >( repo_id: str token: Union[bool, str, None] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
關於 Space 的執行時資訊,包括 Space 的階段和硬體。
獲取有關 Space 的執行時資訊。
get_space_variables
< source >( repo_id: str token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要查詢的倉庫 ID。例如:"bigcode/in-the-stack"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
獲取 Space 的所有變數。
變數允許您向 Space 設定環境變數,而無需對其進行硬編碼。更多詳情,請參閱 https://huggingface.co/docs/hub/spaces-overview#managing-secrets-and-environment-variables
get_token_permission
< source >( token: Union[bool, str, None] = None ) → Literal["read", "write", "fineGrained", None]
引數
- token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
Literal["read", "write", "fineGrained", None]
令牌授予的許可權(“read” 或 “write”)。如果未傳入令牌、令牌無效或伺服器未返回角色,則返回 None
。這種情況通常發生在令牌是 OAuth 令牌時。
檢查給定的 token
是否有效,並返回其許可權。
此方法已棄用,並將在 1.0 版本中刪除。許可權比 get_token_permission
首次引入時更復雜。OAuth 和細粒度令牌允許更詳細的許可權。如果您需要了解與令牌關聯的許可權,請使用 whoami
並檢查 'auth'
鍵。
有關令牌的更多詳細資訊,請參閱 https://huggingface.co/docs/hub/security-tokens#what-are-user-access-tokens。
get_user_overview
< source >( username: str token: Union[bool, str, None] = None ) → User
引數
- username (
str
) — 要獲取概覽的使用者的使用者名稱。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
User
一個包含使用者概覽的 User 物件。
引發
HTTPError
HTTPError
— 如果使用者在 Hub 上不存在,則為 HTTP 404。
獲取 Hub 上使用者的概覽。
get_webhook
< source >( webhook_id: str token: Union[bool, str, None] = None ) → WebhookInfo
引數
- webhook_id (
str
) — 要獲取的 Webhook 的唯一識別符號。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳入
False
。
返回
關於 Webhook 的資訊。
透過其 ID 獲取一個 Webhook。
示例
>>> from huggingface_hub import get_webhook
>>> webhook = get_webhook("654bbbc16f2ec14d77f109cc")
>>> print(webhook)
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
secret="my-secret",
domains=["repo", "discussion"],
disabled=False,
)
grant_access
< source >( repo_id: str user: str repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要授予訪問許可權的倉庫 ID。 - user (
str
) — 要授予訪問許可權的使用者的使用者名稱。 - repo_type (
str
, optional) — 要授予訪問許可權的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
引發
HTTPError
為指定使用者授予對給定受限倉庫的訪問許可權。
授予訪問許可權不需要使用者自己傳送訪問請求。使用者會自動被新增到已接受列表中,這意味著他們可以下載檔案。您可以在任何時候使用 cancel_access_request() 或 reject_access_request() 撤銷已授予的訪問許可權。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
hf_hub_download
< source >( repo_id: str filename: str subfolder: Optional[str] = None repo_type: Optional[str] = None revision: Optional[str] = None cache_dir: Union[str, Path, None] = None local_dir: Union[str, Path, None] = None force_download: bool = False proxies: Optional[Dict] = None etag_timeout: float = 10 token: Union[bool, str, None] = None local_files_only: bool = False resume_download: Optional[bool] = None force_filename: Optional[str] = None local_dir_use_symlinks: Union[bool, Literal['auto']] = 'auto' ) → str
引數
- repo_id (
str
) — 使用者或組織名稱與倉庫名稱,由/
分隔。 - filename (
str
) — 倉庫中的檔名。 - subfolder (
str
, optional) — 一個可選值,對應倉庫內的資料夾。 - repo_type (
str
, optional) — 如果從資料集或空間下載,則設定為"dataset"
或"space"
;如果從模型下載,則為None
或"model"
。預設為None
。 - revision (
str
, optional) — 一個可選的 Git 修訂 ID,可以是分支名、標籤或提交雜湊。 - cache_dir (
str
,Path
, optional) — 儲存快取檔案的資料夾路徑。 - local_dir (
str
orPath
, optional) — 如果提供,下載的檔案將放置在該目錄下。 - force_download (
bool
, optional, defaults toFalse
) — 檔案是否應該被下載,即使它已經存在於本地快取中。 - proxies (
dict
, optional) — 協議到代理 URL 的對映字典,傳遞給requests.request
。 - etag_timeout (
float
, optional, defaults to10
) — 在獲取 ETag 時,在放棄之前等待伺服器傳送資料的秒數,該引數傳遞給requests.request
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - local_files_only (
bool
, optional, defaults toFalse
) — 如果為True
,則避免下載檔案,並在本地快取檔案存在時返回其路徑。
返回
字串
檔案的本地路徑,或者在網路關閉時,為磁碟上快取的最新版本檔案。
引發
RepositoryNotFoundError or RevisionNotFoundError or EntryNotFoundError or LocalEntryNotFoundError or EnvironmentError
or OSError
or ValueError
- RepositoryNotFoundError — 如果要下載的倉庫找不到。這可能是因為它不存在,或者它被設定為
private
而您沒有訪問許可權。 - RevisionNotFoundError — 如果要下載的修訂版本找不到。
- EntryNotFoundError — 如果要下載的檔案找不到。
- LocalEntryNotFoundError — 如果網路被停用或不可用,並且在快取中找不到檔案。
EnvironmentError
— 如果token=True
但找不到令牌。OSError
— 如果無法確定 ETag。ValueError
— 如果某些引數值無效。
如果檔案尚未存在於本地快取中,則下載給定檔案。
新的快取檔案佈局如下
- 快取目錄為每個 repo_id 包含一個子資料夾(按倉庫型別名稱空間)
- 在每個倉庫資料夾內
- refs 是最新已知修訂版本 => commit_hash 對的列表
- blobs 包含實際的檔案 blob(透過它們的 git-sha 或 sha256 標識,取決於它們是否是 LFS 檔案)
- snapshots 為每個提交包含一個子資料夾,每個“提交”包含在該特定提交時已解析的檔案子集。每個檔名都是指向該特定提交時 blob 的符號連結。
[ 96] .
└── [ 160] models--julien-c--EsperBERTo-small
├── [ 160] blobs
│ ├── [321M] 403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
│ ├── [ 398] 7cb18dc9bafbfcf74629a4b760af1b160957a83e
│ └── [1.4K] d7edf6bd2a681fb0175f7735299831ee1b22b812
├── [ 96] refs
│ └── [ 40] main
└── [ 128] snapshots
├── [ 128] 2439f60ef33a0d46d85da5001d52aeda5b00ce9f
│ ├── [ 52] README.md -> ../../blobs/d7edf6bd2a681fb0175f7735299831ee1b22b812
│ └── [ 76] pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
└── [ 128] bbc77c8132af1cc5cf678da3f1ddf2de43606d48
├── [ 52] README.md -> ../../blobs/7cb18dc9bafbfcf74629a4b760af1b160957a83e
└── [ 76] pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
如果提供了 local_dir
,倉庫的檔案結構將在此位置被複制。使用此選項時,不會使用 cache_dir
,而是在 local_dir
的根目錄下建立一個 .cache/huggingface/
資料夾,以儲存與下載檔案相關的一些元資料。雖然此機制不如主快取系統穩健,但它針對定期拉取倉庫最新版本進行了最佳化。
hide_discussion_comment
< source >( repo_id: str discussion_num: int comment_id: str token: Union[bool, str, None] = None repo_type: Optional[str] = None ) → DiscussionComment
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和倉庫名稱,由/
分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - comment_id (
str
) — 要編輯的評論的 ID。 - repo_type (
str
, optional) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
被隱藏的評論
隱藏討論/拉取請求中的評論。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
inspect_job
< source >( job_id: str namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- job_id (
str
) — 作業的 ID。 - namespace (
str
, optional) — 作業執行所在的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
, optional) — 一個有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
以停用身份驗證。參考:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
檢查 Hugging Face 基礎設施上的計算作業。
示例
>>> from huggingface_hub import inspect_job, run_job
>>> job = run_job("python:3.12", ["python", "-c" ,"print('Hello from HF compute!')"])
>>> inspect_job(job.job_id)
JobInfo(
id='68780d00bbe36d38803f645f',
created_at=datetime.datetime(2025, 7, 16, 20, 35, 12, 808000, tzinfo=datetime.timezone.utc),
docker_image='python:3.12',
space_id=None,
command=['python', '-c', "print('Hello from HF compute!')"],
arguments=[],
environment={},
secrets={},
flavor='cpu-basic',
status=JobStatus(stage='RUNNING', message=None)
)
list_accepted_access_requests
< source >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → List[AccessRequest]
引數
- repo_id (
str
) — 獲取訪問請求的倉庫 ID。 - repo_type (
str
, optional) — 獲取訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
List[AccessRequest]
AccessRequest
物件的列表。每個物件包含 username
、email
、status
和 timestamp
屬性。如果受限倉庫有自定義表單,fields
屬性將填充使用者的答案。
引發
HTTPError
獲取給定受限倉庫的已接受訪問請求。
已接受的請求意味著使用者已請求訪問倉庫且請求已被接受。使用者可以下載倉庫的任何檔案。如果批准模式是自動的,此列表預設應包含所有請求。已接受的請求可以隨時使用 cancel_access_request() 和 reject_access_request() 取消或拒絕。已取消的請求將返回到待處理列表,而已拒絕的請求將進入已拒絕列表。在這兩種情況下,使用者都將失去對倉庫的訪問許可權。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
示例
>>> from huggingface_hub import list_accepted_access_requests
>>> requests = list_accepted_access_requests("meta-llama/Llama-2-7b")
>>> len(requests)
411
>>> requests[0]
[
AccessRequest(
username='clem',
fullname='Clem 🤗',
email='***',
timestamp=datetime.datetime(2023, 11, 23, 18, 4, 53, 828000, tzinfo=datetime.timezone.utc),
status='accepted',
fields=None,
),
...
]
list_collections
< source >( owner: Union[List[str], str, None] = None item: Union[List[str], str, None] = None sort: Optional[Literal['lastModified', 'trending', 'upvotes']] = None limit: Optional[int] = None token: Union[bool, str, None] = None ) → Iterable[Collection]
引數
- owner (
List[str]
orstr
, optional) — 按所有者使用者名稱篩選。 - item (
List[str]
orstr
, optional) — 篩選包含特定專案的收藏。例如:"models/teknium/OpenHermes-2.5-Mistral-7B"
、"datasets/squad"
或"papers/2311.12983"
。 - sort (
Literal["lastModified", "trending", "upvotes"]
, optional) — 按最後修改時間、熱門趨勢或點贊數對收藏進行排序。 - limit (
int
, optional) — 要返回的收藏的最大數量。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[Collection]
一個可迭代的 Collection 物件。
根據給定的篩選條件,列出 Hugging Face Hub 上的收藏。
列出收藏時,每個收藏的專案列表最多被截斷為 4 個專案。要檢索收藏中的所有專案,必須使用 get_collection()。
list_datasets
< source >( filter: Union[str, Iterable[str], None] = None author: Optional[str] = None benchmark: Optional[Union[str, List[str]]] = None dataset_name: Optional[str] = None gated: Optional[bool] = None language_creators: Optional[Union[str, List[str]]] = None language: Optional[Union[str, List[str]]] = None multilinguality: Optional[Union[str, List[str]]] = None size_categories: Optional[Union[str, List[str]]] = None tags: Optional[Union[str, List[str]]] = None task_categories: Optional[Union[str, List[str]]] = None task_ids: Optional[Union[str, List[str]]] = None search: Optional[str] = None sort: Optional[Union[Literal['last_modified'], str]] = None direction: Optional[Literal[-1]] = None limit: Optional[int] = None expand: Optional[List[ExpandDatasetProperty_T]] = None full: Optional[bool] = None token: Union[bool, str, None] = None ) → Iterable[DatasetInfo]
引數
- filter (
str
orIterable[str]
, optional) — 用於篩選 Hub 上資料集的字串或字串列表。 - author (
str
, optional) — 標識返回資料集作者的字串。 - benchmark (
str
orList
, optional) — 一個字串或字串列表,可用於按其官方基準來識別 Hub 上的資料集。 - dataset_name (
str
, optional) — 一個字串或字串列表,可用於按名稱(如SQAC
或wikineural
)識別 Hub 上的資料集。 - gated (
bool
, optional) — 一個布林值,用於篩選 Hub 上是否受限的資料集。預設情況下,返回所有資料集。如果傳遞gated=True
,則只返回受限資料集。如果傳遞gated=False
,則只返回非受限資料集。 - language_creators (
str
orList
, optional) — 一個字串或字串列表,可用於根據資料策劃方式(如crowdsourced
或machine_generated
)識別 Hub 上的資料集。 - language (
str
orList
, optional) — 一個字串或字串列表,表示用於在 Hub 上篩選資料集的雙字元語言。 - multilinguality (
str
orList
, optional) — 一個字串或字串列表,表示用於篩選包含多種語言的資料集的篩選器。 - size_categories (
str
orList
, optional) — 一個字串或字串列表,可用於按資料集大小(如100K<n<1M
或1M<n<10M
)識別 Hub 上的資料集。 - tags (
str
或List
,可選) — 用於在 Hub 上篩選資料集的字串標籤或標籤列表。 - task_categories (
str
或List
,可選) — 可用於按設計任務(如audio_classification
或named_entity_recognition
)在 Hub 上識別資料集的字串或字串列表。 - task_ids (
str
或List
,可選) — 可用於按特定任務(如speech_emotion_recognition
或paraphrase
)在 Hub 上識別資料集的字串或字串列表。 - search (
str
,可選) — 返回的資料集中將包含的字串。 - sort (
Literal["last_modified"]
或str
,可選) — 用於對結果模型進行排序的鍵。可能的值包括“last_modified”、“trending_score”、“created_at”、“downloads”和“likes”。 - direction (
Literal[-1]
或int
,可選) — 排序方向。值-1
表示降序排序,而所有其他值表示升序排序。 - limit (
int
,可選) — 獲取資料集數量的限制。將此選項保留為None
將獲取所有資料集。 - expand (
List[ExpandDatasetProperty_T]
,可選) — 在響應中返回的屬性列表。使用時,只會返回列表中的屬性。如果傳遞了full
,則無法使用此引數。可能的值包括"author"
、"cardData"
、"citation"
、"createdAt"
、"disabled"
、"description"
、"downloads"
、"downloadsAllTime"
、"gated"
、"lastModified"
、"likes"
、"paperswithcode_id"
、"private"
、"siblings"
、"sha"
、"tags"
、"trendingScore"
、"usedStorage"
、"resourceGroup"
和"xetEnabled"
。 - full (
bool
,可選) — 是否獲取所有資料集資料,包括last_modified
、card_data
和檔案。可以包含有用的資訊,例如 PapersWithCode ID。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[DatasetInfo]
huggingface_hub.hf_api.DatasetInfo 物件的可迭代物件。
根據給定的篩選條件,列出 Hugging Face Hub 上託管的資料集。
使用 filter
引數的示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
# List all datasets
>>> api.list_datasets()
# List only the text classification datasets
>>> api.list_datasets(filter="task_categories:text-classification")
# List only the datasets in russian for language modeling
>>> api.list_datasets(
... filter=("language:ru", "task_ids:language-modeling")
... )
# List FiftyOne datasets (identified by the tag "fiftyone" in dataset card)
>>> api.list_datasets(tags="fiftyone")
list_inference_catalog
< 原始碼 >( token: Union[bool, str, None] = None ) → Liststr
引數
- token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。
返回
Liststr
目錄中可用模型 ID 的列表。
列出 Hugging Face 推理目錄(Inference Catalog)中可用的模型。
推理目錄的目標是提供一個經過篩選的模型列表,這些模型已針對推理進行了最佳化,並且其預設配置已經過測試。有關目錄中可用模型的列表,請參見 https://endpoints.huggingface.co/catalog。
使用 create_inference_endpoint_from_catalog() 從目錄部署模型。
list_inference_catalog
是實驗性的。其 API 未來可能會更改。如果您有任何建議或請求,請提供反饋。
list_inference_endpoints
< 原始碼 >( namespace: Optional[str] = None token: Union[bool, str, None] = None ) → ListInferenceEndpoint
引數
- namespace (
str
,可選) — 要列出端點的名稱空間。預設為當前使用者。設定為"*"
以列出所有名稱空間(即個人名稱空間和使用者所屬的所有組織)中的所有端點。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
給定名稱空間的所有推理端點的列表。
列出給定名稱空間的所有推理端點。
list_jobs
< 原始碼 >( timeout: Optional[int] = None namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- timeout (
float
,可選) — 是否為向 Hub 的請求設定超時。 - namespace (
str
,可選) — 從中列出作業的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
,可選) — 一個有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
以停用身份驗證。請參閱:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
列出 Hugging Face 基礎設施上的計算作業。
list_lfs_files
< 原始碼 >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → Iterable[LFSFileInfo]
引數
- repo_id (
str
) — 您正在列出 LFS 檔案的倉庫。 - repo_type (
str
,可選) — 倉庫型別。如果從資料集或空間列出,則設定為"dataset"
或"space"
,如果從模型列出,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[LFSFileInfo]
LFSFileInfo
物件的迭代器。
列出 Hub 上倉庫中的所有 LFS 檔案。
這主要用於計算倉庫使用了多少儲存空間,並最終使用 permanently_delete_lfs_files() 清理大檔案。請注意,這將是一個永久性操作,會影響所有引用此刪除檔案的提交,並且無法撤銷。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> lfs_files = api.list_lfs_files("username/my-cool-repo")
# Filter files files to delete based on a combination of `filename`, `pushed_at`, `ref` or `size`.
# e.g. select only LFS files in the "checkpoints" folder
>>> lfs_files_to_delete = (lfs_file for lfs_file in lfs_files if lfs_file.filename.startswith("checkpoints/"))
# Permanently delete LFS files
>>> api.permanently_delete_lfs_files("username/my-cool-repo", lfs_files_to_delete)
list_liked_repos
< 原始碼 >( user: Optional[str] = None token: Union[bool, str, None] = None ) → UserLikes
引數
- user (
str
,可選) — 您想要獲取其點讚的使用者名稱。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
包含使用者名稱和 3 個倉庫 ID 列表的物件(1 個用於模型,1 個用於資料集,1 個用於空間)。
引發
ValueError
ValueError
— 如果未傳遞user
且未找到令牌(無論是從引數還是從機器)。
列出使用者在 huggingface.co 上點讚的所有公共倉庫。
此列表是公開的,因此令牌是可選的。如果未傳遞 user
,則預設為已登入的使用者。
另請參閱 unlike()。
list_models
< 原始碼 >( filter: Union[str, Iterable[str], None] = None author: Optional[str] = None gated: Optional[bool] = None inference: Optional[Literal['warm']] = None inference_provider: Optional[Union[Literal['all'], 'PROVIDER_T', List['PROVIDER_T']]] = None library: Optional[Union[str, List[str]]] = None language: Optional[Union[str, List[str]]] = None model_name: Optional[str] = None task: Optional[Union[str, List[str]]] = None trained_dataset: Optional[Union[str, List[str]]] = None tags: Optional[Union[str, List[str]]] = None search: Optional[str] = None pipeline_tag: Optional[str] = None emissions_thresholds: Optional[Tuple[float, float]] = None sort: Union[Literal['last_modified'], str, None] = None direction: Optional[Literal[-1]] = None limit: Optional[int] = None expand: Optional[List[ExpandModelProperty_T]] = None full: Optional[bool] = None cardData: bool = False fetch_config: bool = False token: Union[bool, str, None] = None ) → Iterable[ModelInfo]
引數
- filter (
str
或Iterable[str]
,可選) — 用於在 Hub 上篩選模型的字串或字串列表。 - author (
str
,可選) — 識別返回模型作者(使用者或組織)的字串。 - gated (
bool
,可選) — 一個布林值,用於篩選 Hub 上是否受門控的模型。預設情況下,返回所有模型。如果傳遞gated=True
,則僅返回受門控的模型。如果傳遞gated=False
,則僅返回不受門控的模型。 - inference (
Literal["warm"]
,可選) — 如果為“warm”,則篩選 Hub 上當前至少由一個提供商服務的模型。 - inference_provider (
Literal["all"]
或str
,可選) — 用於篩選 Hub 上由特定提供商服務的模型的字串。傳遞"all"
以獲取所有由至少一個提供商服務的模型。 - library (
str
或List
,可選) — 模型最初訓練所使用的基礎庫的字串或字串列表,例如 pytorch、tensorflow 或 allennlp。 - language (
str
或List
,可選) — 語言的字串或字串列表,可以按名稱和國家程式碼,例如“en”或“English” - model_name (
str
,可選) — 包含 Hub 上模型完整或部分名稱的字串,例如“bert”或“bert-base-cased” - task (
str
或List
,可選) — 模型設計任務的字串或字串列表,例如:“fill-mask”或“automatic-speech-recognition” - trained_dataset (
str
或List
,可選) — Hub 上模型訓練資料集的字串標籤或字串標籤列表。 - tags (
str
或List
,可選) — 用於按 Hub 上的標籤篩選模型的字串標籤或標籤列表,例如text-generation
或spacy
。 - search (
str
,可選) — 將包含在返回的模型 ID 中的字串。 - pipeline_tag (
str
,可選) — 用於按 Hub 上的 pipeline 標籤篩選模型的字串,例如summarization
。 - emissions_thresholds (
Tuple
,可選) — 一個包含兩個整數或浮點數的元組,表示篩選結果模型的最小和最大碳足跡(以克為單位)。 - sort (
Literal["last_modified"]
或str
,可選) — 用於對結果模型進行排序的鍵。可能的值包括“last_modified”、“trending_score”、“created_at”、“downloads”和“likes”。 - direction (
Literal[-1]
或int
,可選) — 排序方向。值-1
表示降序排序,而所有其他值表示升序排序。 - limit (
int
,可選) — 獲取模型數量的限制。將此選項保留為None
將獲取所有模型。 - expand (
List[ExpandModelProperty_T]
,可選) — 在響應中返回的屬性列表。使用時,只會返回列表中的屬性。如果傳遞了full
、cardData
或fetch_config
,則無法使用此引數。可能的值包括"author"
、"cardData"
、"config"
、"createdAt"
、"disabled"
、"downloads"
、"downloadsAllTime"
、"gated"
、"gguf"
、"inference"
、"inferenceProviderMapping"
、"lastModified"
、"library_name"
、"likes"
、"mask_token"
、"model-index"
、"pipeline_tag"
、"private"
、"safetensors"
、"sha"
、"siblings"
、"spaces"
、"tags"
、"transformersInfo"
、"trendingScore"
、"widgetData"
、"resourceGroup"
和"xetEnabled"
。 - full (
bool
,可選) — 是否獲取所有模型資料,包括last_modified
、sha
、檔案和tags
。使用篩選器時,此項預設設定為True
。 - cardData (
bool
,可選) — 是否也獲取模型的元資料。可以包含有用的資訊,例如碳排放、指標和訓練資料集。 - fetch_config (
bool
,可選) — 是否也獲取模型配置。由於其大小,此項不包含在full
中。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[ModelInfo]
huggingface_hub.hf_api.ModelInfo 物件的可迭代物件。
根據給定的篩選條件,列出 Hugging Face Hub 上託管的模型。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
# List all models
>>> api.list_models()
# List text classification models
>>> api.list_models(filter="text-classification")
# List models from the KerasHub library
>>> api.list_models(filter="keras-hub")
# List models served by Cohere
>>> api.list_models(inference_provider="cohere")
# List models with "bert" in their name
>>> api.list_models(search="bert")
# List models with "bert" in their name and pushed by google
>>> api.list_models(search="bert", author="google")
list_organization_members
< 原始碼 >( organization: str token: Union[bool, str, None] = None ) → Iterable[User]
引數
- organization (
str
) — 要獲取其成員的組織名稱。 - token (Union[bool, str, None],可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[User]
包含組織成員的 User 物件列表。
引發
HTTPError
HTTPError
— HTTP 404 如果組織在 Hub 上不存在。
Hub 上組織的成員列表。
list_papers
< 原始碼 >( query: Optional[str] = None token: Union[bool, str, None] = None ) → Iterable[PaperInfo]
引數
- query (
str
,可選) — 用於查詢論文的搜尋查詢字串。如果提供,則返回與查詢匹配的論文。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[PaperInfo]
一個由 huggingface_hub.hf_api.PaperInfo
物件組成的可迭代物件。
根據給定的搜尋查詢,列出 Hugging Face Hub 上的每日論文。
list_pending_access_requests
< 原始碼 >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → List[AccessRequest]
引數
- repo_id (
str
) — 要獲取訪問請求的倉庫 ID。 - repo_type (
str
, 可選) — 要獲取訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
List[AccessRequest]
AccessRequest
物件的列表。每個物件包含 username
、email
、status
和 timestamp
屬性。如果受限倉庫有自定義表單,fields
屬性將填充使用者的答案。
引發
HTTPError
獲取給定封閉倉庫的待處理訪問請求。
待處理請求意味著使用者已請求訪問該倉庫,但請求尚未被處理。如果審批模式是自動的,此列表應為空。待處理的請求可以使用 accept_access_request() 和 reject_access_request() 來接受或拒絕。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
示例
>>> from huggingface_hub import list_pending_access_requests, accept_access_request
# List pending requests
>>> requests = list_pending_access_requests("meta-llama/Llama-2-7b")
>>> len(requests)
411
>>> requests[0]
[
AccessRequest(
username='clem',
fullname='Clem 🤗',
email='***',
timestamp=datetime.datetime(2023, 11, 23, 18, 4, 53, 828000, tzinfo=datetime.timezone.utc),
status='pending',
fields=None,
),
...
]
# Accept Clem's request
>>> accept_access_request("meta-llama/Llama-2-7b", "clem")
list_rejected_access_requests
< 原始碼 >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → List[AccessRequest]
引數
- repo_id (
str
) — 要獲取訪問請求的倉庫 ID。 - repo_type (
str
, 可選) — 要獲取訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
List[AccessRequest]
AccessRequest
物件的列表。每個物件包含 username
、email
、status
和 timestamp
屬性。如果受限倉庫有自定義表單,fields
屬性將填充使用者的答案。
引發
HTTPError
獲取給定封閉倉庫的已拒絕訪問請求。
已拒絕的請求意味著使用者已請求訪問該倉庫,並且該請求已被倉庫所有者(您或您組織的其他使用者)明確拒絕。該使用者無法下載該倉庫的任何檔案。被拒絕的請求可以隨時使用 accept_access_request() 和 cancel_access_request() 來接受或取消。已取消的請求將返回到待處理列表,而已接受的請求將進入已接受列表。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
示例
>>> from huggingface_hub import list_rejected_access_requests
>>> requests = list_rejected_access_requests("meta-llama/Llama-2-7b")
>>> len(requests)
411
>>> requests[0]
[
AccessRequest(
username='clem',
fullname='Clem 🤗',
email='***',
timestamp=datetime.datetime(2023, 11, 23, 18, 4, 53, 828000, tzinfo=datetime.timezone.utc),
status='rejected',
fields=None,
),
...
]
list_repo_commits
< 原始碼 >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None revision: Optional[str] = None formatted: bool = False ) → List[GitCommitInfo]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - repo_type (
str
, 可選) — 如果列出資料集或 Space 的提交,則設定為"dataset"
或"space"
;如果列出模型的提交,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - revision (
str
, 可選) — 要從中獲取提交的 git 修訂版本。預設為"main"
分支的 HEAD。 - formatted (
bool
) — 是否返回 HTML 格式的提交標題和描述。預設為 False。
返回
List[GitCommitInfo]
Hub 上倉庫提交資訊的物件列表。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果在倉庫中找不到版本(錯誤 404)。
獲取 Hub 上倉庫給定修訂版本的提交列表。
提交按日期排序(最近的提交在前)。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
# Commits are sorted by date (last commit first)
>>> initial_commit = api.list_repo_commits("gpt2")[-1]
# Initial commit is always a system commit containing the `.gitattributes` file.
>>> initial_commit
GitCommitInfo(
commit_id='9b865efde13a30c13e0a33e536cf3e4a5a9d71d8',
authors=['system'],
created_at=datetime.datetime(2019, 2, 18, 10, 36, 15, tzinfo=datetime.timezone.utc),
title='initial commit',
message='',
formatted_title=None,
formatted_message=None
)
# Create an empty branch by deriving from initial commit
>>> api.create_branch("gpt2", "new_empty_branch", revision=initial_commit.commit_id)
list_repo_files
< 原始碼 >( repo_id: str revision: Optional[str] = None repo_type: Optional[str] = None token: Union[str, bool, None] = None ) → List[str]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - revision (
str
, 可選) — 要從中獲取資訊的倉庫的修訂版本。 - repo_type (
str
, 可選) — 如果上傳到資料集或 Space,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
List[str]
給定倉庫中的檔案列表。
獲取給定倉庫中的檔案列表。
list_repo_likers
< 原始碼 >( repo_id: str repo_type: Optional[str] = None token: Union[bool, str, None] = None ) → Iterable[User]
引數
- repo_id (
str
) — 要檢索的倉庫。例如:"user/my-cool-model"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 如果上傳到資料集或 Space,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。
返回
Iterable[User]
一個由 huggingface_hub.hf_api.User 物件組成的可迭代物件。
列出所有在 Hugging Face Hub 上喜歡給定倉庫的使用者。
另請參閱 list_liked_repos()。
list_repo_refs
< 原始碼 >( repo_id: str repo_type: Optional[str] = None include_pull_requests: bool = False token: Union[str, bool, None] = None ) → GitRefs
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - repo_type (
str
, 可選) — 如果列出資料集或 Space 的引用,則設定為"dataset"
或"space"
;如果列出模型的引用,則為None
或"model"
。預設為None
。 - include_pull_requests (
bool
, 可選) — 是否在列表中包含拉取請求的引用。預設為False
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
包含 Hub 上倉庫所有分支和標籤資訊的物件。
獲取給定倉庫的引用列表(包括標籤和分支)。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> api.list_repo_refs("gpt2")
GitRefs(branches=[GitRefInfo(name='main', ref='refs/heads/main', target_commit='e7da7f221d5bf496a48136c0cd264e630fe9fcc8')], converts=[], tags=[])
>>> api.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')
]
)
list_repo_tree
< 原始碼 >( repo_id: str path_in_repo: Optional[str] = None recursive: bool = False expand: bool = False revision: Optional[str] = None repo_type: Optional[str] = None token: Union[str, bool, None] = None ) → Iterable[Union[RepoFile, RepoFolder]]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個由/
分隔的倉庫名稱。 - path_in_repo (
str
, 可選) — 倉庫中樹(資料夾)的相對路徑,例如:"checkpoints/1fec34a/results"
。將預設為倉庫的根樹(資料夾)。 - recursive (
bool
, 可選, 預設為False
) — 是否遞迴地列出樹的檔案和資料夾。 - expand (
bool
, 可選, 預設為False
) — 是否獲取有關樹的檔案和資料夾的更多資訊(例如,最後一次提交和檔案的安全掃描結果)。此操作對伺服器來說開銷更大,因此每頁只返回 50 個結果(而不是 1000 個)。由於分頁是在huggingface_hub
中實現的,這對您來說是透明的,除了獲取結果所需的時間。 - revision (
str
, 可選) — 要從中獲取樹的倉庫修訂版本。預設為"main"
分支。 - repo_type (
str
, 可選) — 要從中獲取樹的倉庫型別("model"
、"dataset"
或"space"
)。預設為"model"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[Union[RepoFile, RepoFolder]]
有關樹的檔案和資料夾的資訊,作為 RepoFile
和 RepoFolder
物件的可迭代物件。檔案和資料夾的順序不保證。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果在倉庫中找不到版本(錯誤 404)。
- EntryNotFoundError — 如果樹(資料夾)在倉庫中不存在(錯誤 404)。
列出倉庫樹的檔案和資料夾並獲取它們的資訊。
示例
獲取有關倉庫樹的資訊。
>>> from huggingface_hub import list_repo_tree
>>> repo_tree = list_repo_tree("lysandre/arxiv-nlp")
>>> repo_tree
<generator object HfApi.list_repo_tree at 0x7fa4088e1ac0>
>>> list(repo_tree)
[
RepoFile(path='.gitattributes', size=391, blob_id='ae8c63daedbd4206d7d40126955d4e6ab1c80f8f', lfs=None, last_commit=None, security=None),
RepoFile(path='README.md', size=391, blob_id='43bd404b159de6fba7c2f4d3264347668d43af25', lfs=None, last_commit=None, security=None),
RepoFile(path='config.json', size=554, blob_id='2f9618c3a19b9a61add74f70bfb121335aeef666', lfs=None, last_commit=None, security=None),
RepoFile(
path='flax_model.msgpack', size=497764107, blob_id='8095a62ccb4d806da7666fcda07467e2d150218e',
lfs={'size': 497764107, 'sha256': 'd88b0d6a6ff9c3f8151f9d3228f57092aaea997f09af009eefd7373a77b5abb9', 'pointer_size': 134}, last_commit=None, security=None
),
RepoFile(path='merges.txt', size=456318, blob_id='226b0752cac7789c48f0cb3ec53eda48b7be36cc', lfs=None, last_commit=None, security=None),
RepoFile(
path='pytorch_model.bin', size=548123560, blob_id='64eaa9c526867e404b68f2c5d66fd78e27026523',
lfs={'size': 548123560, 'sha256': '9be78edb5b928eba33aa88f431551348f7466ba9f5ef3daf1d552398722a5436', 'pointer_size': 134}, last_commit=None, security=None
),
RepoFile(path='vocab.json', size=898669, blob_id='b00361fece0387ca34b4b8b8539ed830d644dbeb', lfs=None, last_commit=None, security=None)]
]
獲取有關倉庫樹的更多資訊(最後一次提交和檔案的安全掃描結果)。
>>> from huggingface_hub import list_repo_tree
>>> repo_tree = list_repo_tree("prompthero/openjourney-v4", expand=True)
>>> list(repo_tree)
[
RepoFolder(
path='feature_extractor',
tree_id='aa536c4ea18073388b5b0bc791057a7296a00398',
last_commit={
'oid': '47b62b20b20e06b9de610e840282b7e6c3d51190',
'title': 'Upload diffusers weights (#48)',
'date': datetime.datetime(2023, 3, 21, 9, 5, 27, tzinfo=datetime.timezone.utc)
}
),
RepoFolder(
path='safety_checker',
tree_id='65aef9d787e5557373fdf714d6c34d4fcdd70440',
last_commit={
'oid': '47b62b20b20e06b9de610e840282b7e6c3d51190',
'title': 'Upload diffusers weights (#48)',
'date': datetime.datetime(2023, 3, 21, 9, 5, 27, tzinfo=datetime.timezone.utc)
}
),
RepoFile(
path='model_index.json',
size=582,
blob_id='d3d7c1e8c3e78eeb1640b8e2041ee256e24c9ee1',
lfs=None,
last_commit={
'oid': 'b195ed2d503f3eb29637050a886d77bd81d35f0e',
'title': 'Fix deprecation warning by changing `CLIPFeatureExtractor` to `CLIPImageProcessor`. (#54)',
'date': datetime.datetime(2023, 5, 15, 21, 41, 59, tzinfo=datetime.timezone.utc)
},
security={
'safe': True,
'av_scan': {'virusFound': False, 'virusNames': None},
'pickle_import_scan': None
}
)
...
]
list_spaces
< 原始碼 >( filter: Union[str, Iterable[str], None] = None author: Optional[str] = None search: Optional[str] = None datasets: Union[str, Iterable[str], None] = None models: Union[str, Iterable[str], None] = None linked: bool = False sort: Union[Literal['last_modified'], str, None] = None direction: Optional[Literal[-1]] = None limit: Optional[int] = None expand: Optional[List[ExpandSpaceProperty_T]] = None full: Optional[bool] = None token: Union[bool, str, None] = None ) → Iterable[SpaceInfo]
引數
- filter (
str
或Iterable
, 可選) — 一個字串標籤或標籤列表,可用於在 Hub 上識別 Spaces。 - author (
str
, 可選) — 一個標識返回 Spaces 作者的字串。 - search (
str
, 可選) — 將包含在返回的 Spaces 中的字串。 - datasets (
str
或Iterable
, 可選) — 是否返回使用了資料集的 Spaces。可以傳遞特定資料集的名稱作為字串。 - models (
str
或Iterable
, 可選) — 是否返回使用了模型的 Spaces。可以傳遞特定模型的名稱作為字串。 - linked (
bool
, 可選) — 是否返回使用了模型或資料集的 Spaces。 - sort (
Literal["last_modified"]
或str
, 可選) — 用於對結果模型進行排序的鍵。可能的值為 “last_modified”、“trending_score”、“created_at” 和 “likes”。 - direction (
Literal[-1]
或int
, 可選) — 排序方向。值-1
按降序排序,而所有其他值按升序排序。 - limit (
int
, 可選) — 獲取的 Spaces 數量限制。將此選項留為None
會獲取所有 Spaces。 - expand (
List[ExpandSpaceProperty_T]
, 可選) — 響應中要返回的屬性列表。使用時,只有列表中的屬性會被返回。如果傳遞了full
,則不能使用此引數。可能的值為"author"
、"cardData"
、"datasets"
、"disabled"
、"lastModified"
、"createdAt"
、"likes"
、"models"
、"private"
、"runtime"
、"sdk"
、"siblings"
、"sha"
、"subdomain"
、"tags"
、"trendingScore"
、"usedStorage"
、"resourceGroup"
和"xetEnabled"
。 - full (
bool
, 可選) — 是否獲取所有 Spaces 資料,包括last_modified
、siblings
和card_data
欄位。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[SpaceInfo]
一個由 huggingface_hub.hf_api.SpaceInfo 物件組成的可迭代物件。
根據給定的篩選條件,列出託管在 Hugging Face Hub 上的 Spaces。
list_user_followers
< 原始碼 >( username: str token: Union[bool, str, None] = None ) → Iterable[User]
引數
- username (
str
) — 要獲取其關注者的使用者的使用者名稱。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[User]
包含該使用者關注者的 User 物件列表。
引發
HTTPError
HTTPError
— 如果使用者在 Hub 上不存在,則為 HTTP 404。
獲取 Hub 上使用者的關注者列表。
list_user_following
< source >( username: str token: Union[bool, str, None] = None ) → Iterable[User]
引數
- username (
str
) — 使用者的使用者名稱,用於獲取該使用者關注的使用者列表。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Iterable[User]
一個包含該使用者所關注使用者的 User 物件列表。
引發
HTTPError
HTTPError
— 如果使用者在 Hub 上不存在,則為 HTTP 404。
獲取 Hub 上某個使用者所關注的使用者列表。
list_webhooks
< source >( token: Union[bool, str, None] = None ) → List[WebhookInfo]
引數
- token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
List[WebhookInfo]
Webhook 資訊物件列表。
列出所有已配置的 webhook。
示例
>>> from huggingface_hub import list_webhooks
>>> webhooks = list_webhooks()
>>> len(webhooks)
2
>>> webhooks[0]
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
url="https://webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
secret="my-secret",
domains=["repo", "discussion"],
disabled=False,
)
merge_pull_request
< source >( repo_id: str discussion_num: int token: Union[bool, str, None] = None comment: Optional[str] = None repo_type: Optional[str] = None ) → DiscussionStatusChange
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - discussion_num (
int
) — 討論或拉取請求的編號。必須是嚴格的正整數。 - comment (
str
, 可選) — 隨狀態變更一起釋出的可選評論。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
,如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
狀態更改事件
合併一個拉取請求。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
model_info
< source >( repo_id: str revision: Optional[str] = None timeout: Optional[float] = None securityStatus: Optional[bool] = None files_metadata: bool = False expand: Optional[List[ExpandModelProperty_T]] = None token: Union[bool, str, None] = None ) → huggingface_hub.hf_api.ModelInfo
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - revision (
str
, 可選) — 要獲取資訊的模型倉庫的版本。 - timeout (
float
, 可選) — 是否為向 Hub 的請求設定超時。 - securityStatus (
bool
, 可選) — 是否也從模型倉庫中檢索安全狀態。安全狀態將在security_repo_status
欄位中返回。 - files_metadata (
bool
, 可選) — 是否檢索倉庫中檔案的元資料(大小、LFS元資料等)。預設為False
。 - expand (
List[ExpandModelProperty_T]
, 可選) — 指定在響應中返回的屬性列表。使用時,僅返回列表中的屬性。如果傳遞了 `securityStatus` 或 `files_metadata`,則不能使用此引數。可能的值包括 ` "author" `、` "baseModels" `、` "cardData" `、` "childrenModelCount" `、` "config" `、` "createdAt" `、` "disabled" `、` "downloads" `、` "downloadsAllTime" `、` "gated" `、` "gguf" `、` "inference" `、` "inferenceProviderMapping" `、` "lastModified" `、` "library_name" `、` "likes" `、` "mask_token" `、` "model-index" `、` "pipeline_tag" `、` "private" `、` "safetensors" `、` "sha" `、` "siblings" `、` "spaces" `、` "tags" `、` "transformersInfo" `、` "trendingScore" `、` "widgetData" `、` "usedStorage" `、` "resourceGroup" ` 和 ` "xetEnabled" `。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
模型倉庫資訊。
獲取 huggingface.co 上特定模型的資訊
如果你傳遞了可接受的令牌或已登入,模型可以是私有的。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
move_repo
< source >( from_id: str to_id: str repo_type: Optional[str] = None token: Union[str, bool, None] = None )
引數
- from_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。原始倉庫識別符號。 - to_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。最終倉庫識別符號。 - repo_type (
str
, 可選) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
,如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
將一個倉庫從 namespace1/repo_name1 移動到 namespace2/repo_name2
請注意存在某些限制。有關移動倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/repositories-settings#renaming-or-transferring-a-repo。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
paper_info
< source >( id: str ) → PaperInfo
獲取 Hub 上一篇論文的資訊。
parse_safetensors_file_metadata
< source >( repo_id: str filename: str repo_type: Optional[str] = None revision: Optional[str] = None token: Union[bool, str, None] = None ) → SafetensorsFileMetadata
引數
- repo_id (
str
) — 一個使用者或組織名稱與一個倉庫名稱,以/
分隔。 - filename (
str
) — 倉庫中的檔名。 - repo_type (
str
, 可選) — 如果檔案在資料集或空間中,則設定為"dataset"
或"space"
;如果在模型中,則為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 獲取檔案的 git 修訂版本。可以是一個分支名、一個標籤或一個提交雜湊。預設為"main"
分支的頭部。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
SafetensorsFileMetadata
與 safetensors 檔案相關的資訊。
引發
NotASafetensorsRepoError
或 SafetensorsParsingError
NotASafetensorsRepoError
— 如果倉庫不是 safetensors 倉庫,即沒有model.safetensors
或model.safetensors.index.json
檔案。SafetensorsParsingError
— 如果 safetensors 檔案頭無法正確解析。
從 Hub 上的 safetensors 檔案中解析元資料。
要一次性解析倉庫中所有 safetensors 檔案的元資料,請使用 get_safetensors_metadata()。
有關 safetensors 格式的更多詳細資訊,請檢視 https://huggingface.co/docs/safetensors/index#format。
pause_inference_endpoint
< source >( name: str namespace: Optional[str] = None token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 要暫停的推理端點的名稱。 - namespace (
str
, 可選) — 推理端點所在的名稱空間。預設為當前使用者。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
關於已暫停的推理端點的資訊。
暫停一個推理端點。
暫停的推理端點不會收費。可以使用 resume_inference_endpoint() 在任何時候恢復它。這與使用 scale_to_zero_inference_endpoint() 將推理端點縮減至零不同,後者在收到請求時會自動重啟。
為方便起見,你也可以使用 pause_inference_endpoint() 暫停一個推理端點。
pause_space
< source >( repo_id: str token: Union[bool, str, None] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要暫停的空間的 ID。例如:"Salesforce/BLIP2"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於你的空間的執行時資訊,包括 `stage=PAUSED` 和請求的硬體。
- RepositoryNotFoundError — 如果你的空間未找到(錯誤 404)。很可能是 repo_id 錯誤,或者你的空間是私有的但你沒有進行身份驗證。
- HfHubHTTPError — 403 Forbidden: 只有空間的所有者才能暫停它。如果你想管理一個不屬於你的空間,可以發起一個討論請求所有者,或者複製該空間。
- BadRequestError — 如果你的空間是靜態空間。靜態空間始終執行且不收費。如果你想隱藏一個靜態空間,可以將其設定為私有。
暫停你的空間。
暫停的空間會停止執行,直到其所有者手動重啟。這與免費空間在 48 小時不活動後進入的休眠狀態不同。暫停的時間不會計入你的賬戶賬單,無論你選擇了何種硬體。要重啟你的空間,請使用 restart_space() 並訪問你的空間設定頁面。
更多詳情,請訪問文件。
permanently_delete_lfs_files
< source >( repo_id: str lfs_files: Iterable[LFSFileInfo] rewrite_history: bool = True repo_type: Optional[str] = None token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 你要列出 LFS 檔案的倉庫。 - lfs_files (
Iterable[LFSFileInfo]
) — 一個可迭代的LFSFileInfo
項,用於從倉庫中永久刪除。使用 list_lfs_files() 列出倉庫中的所有 LFS 檔案。 - rewrite_history (
bool
, 可選, 預設為True
) — 是否重寫倉庫歷史記錄以刪除引用已刪除 LFS 檔案的檔案指標(推薦)。 - repo_type (
str
, 可選) — 倉庫型別。如果要從資料集或空間列出,則設定為"dataset"
或"space"
;如果要從模型列出,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
從 Hub 上的倉庫中永久刪除 LFS 檔案。
這是一個永久性操作,將影響所有引用已刪除檔案的提交,並可能損壞你的倉庫。這是一個不可逆的操作。請僅在你清楚自己在做什麼時使用它。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> lfs_files = api.list_lfs_files("username/my-cool-repo")
# Filter files files to delete based on a combination of `filename`, `pushed_at`, `ref` or `size`.
# e.g. select only LFS files in the "checkpoints" folder
>>> lfs_files_to_delete = (lfs_file for lfs_file in lfs_files if lfs_file.filename.startswith("checkpoints/"))
# Permanently delete LFS files
>>> api.permanently_delete_lfs_files("username/my-cool-repo", lfs_files_to_delete)
preupload_lfs_files
< source >( repo_id: str additions: Iterable[CommitOperationAdd] token: Union[str, bool, None] = None repo_type: Optional[str] = None revision: Optional[str] = None create_pr: Optional[bool] = None num_threads: int = 5 free_memory: bool = True gitignore_content: Optional[str] = None )
引數
- repo_id (
str
) — 您將要提交檔案的倉庫,例如:"username/custom_transformers"
。 - operations (
Iterable
of CommitOperationAdd) — 要上傳的檔案列表。警告:此列表中的物件將被修改以包含與上傳相關的資訊。請勿對多次提交重用相同的物件。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, 可選) — 要上傳到的倉庫型別(例如"model"
-預設-、"dataset"
或"space"
)。 - revision (
str
, 可選) — 提交的 git 修訂版本。預設為"main"
分支的頭部。 - create_pr (
boolean
, 可選) — 是否計劃用該提交建立一個拉取請求。預設為False
。 - num_threads (
int
, optional) — 用於上傳檔案的併發執行緒數。預設為 5。將其設定為 2 意味著最多可以同時上傳 2 個檔案。 - gitignore_content (
str
, optional) —.gitignore
檔案的內容,用於瞭解哪些檔案應被忽略。優先順序順序是:首先檢查是否傳入了gitignore_content
,然後檢查.gitignore
檔案是否存在於要提交的檔案列表中,最後預設使用 Hub 上已託管的.gitignore
檔案(如果有的話)。
將 LFS 檔案預上傳到 S3,為將來的提交做準備。
如果您是動態生成要上傳的檔案,並且不希望在一次性上傳所有檔案之前將它們儲存在記憶體中,那麼此方法非常有用。
這是一個高階使用者方法。您不需要直接呼叫它來完成正常的提交。請直接使用 create_commit()。
在此過程中,提交操作(Commit operations)將被修改。特別是,附加的 path_or_fileobj
將在上傳後被移除以節省記憶體(並被一個空的 bytes
物件替換)。請不要重用相同的物件,除非是為了將它們傳遞給 create_commit()。如果您不想從提交操作物件中刪除附加的內容,請傳遞 free_memory=False
。
示例
>>> from huggingface_hub import CommitOperationAdd, preupload_lfs_files, create_commit, create_repo
>>> repo_id = create_repo("test_preupload").repo_id
# Generate and preupload LFS files one by one
>>> operations = [] # List of all `CommitOperationAdd` objects that will be generated
>>> for i in range(5):
... content = ... # generate binary content
... addition = CommitOperationAdd(path_in_repo=f"shard_{i}_of_5.bin", path_or_fileobj=content)
... preupload_lfs_files(repo_id, additions=[addition]) # upload + free memory
... operations.append(addition)
# Create commit
>>> create_commit(repo_id, operations=operations, commit_message="Commit all shards")
reject_access_request
< source >( repo_id: str user: str repo_type: Optional[str] = None rejection_reason: Optional[str] token: Union[bool, str, None] = None )
引數
- repo_id (
str
) — 要拒絕訪問請求的倉庫 ID。 - user (
str
) — 應拒絕其訪問請求的使用者的使用者名稱。 - repo_type (
str
, optional) — 要拒絕訪問請求的倉庫型別。必須是model
、dataset
或space
之一。預設為model
。 - rejection_reason (
str
, optional) — 可選的拒絕原因,將對使用者可見(最多 200 個字元)。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
引發
HTTPError
拒絕某個使用者對指定受限倉庫的訪問請求。
被拒絕的請求將進入被拒絕列表。使用者無法下載該倉庫的任何檔案。被拒絕的請求可以隨時使用 accept_access_request() 和 cancel_access_request() 來接受或取消。取消的請求將返回到待處理列表,而接受的請求將進入已接受列表。
有關門控倉庫的更多資訊,請參閱 https://huggingface.co/docs/hub/models-gated。
rename_discussion
< source >( repo_id: str discussion_num: int new_title: str token: Union[bool, str, None] = None repo_type: Optional[str] = None ) → DiscussionTitleChange
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - discussion_num (
int
) — 討論(Discussion)或拉取請求(Pull Request)的編號。必須是嚴格的正整數。 - new_title (
str
) — 討論的新標題。 - repo_type (
str
, optional) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
標題更改事件
重新命名一個討論。
示例
>>> new_title = "New title, fixing a typo"
>>> HfApi().rename_discussion(
... repo_id="username/repo_name",
... discussion_num=34
... new_title=new_title
... )
# DiscussionTitleChange(id='deadbeef0000000', type='title-change', ...)
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
repo_exists
< source >( repo_id: str repo_type: Optional[str] = None token: Union[str, bool, None] = None )
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - repo_type (
str
, optional) — 如果從資料集或空間獲取倉庫資訊,則設定為"dataset"
或"space"
;如果從模型獲取倉庫資訊,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
檢查 Hugging Face Hub 上是否存在某個倉庫。
repo_info
< source >( repo_id: str revision: Optional[str] = None repo_type: Optional[str] = None timeout: Optional[float] = None files_metadata: bool = False expand: Optional[Union[ExpandModelProperty_T, ExpandDatasetProperty_T, ExpandSpaceProperty_T]] = None token: Union[bool, str, None] = None ) → Union[SpaceInfo, DatasetInfo, ModelInfo]
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - revision (
str
, optional) — 要獲取資訊的倉庫版本。 - repo_type (
str
, optional) — 如果從資料集或空間獲取倉庫資訊,則設定為"dataset"
或"space"
;如果從模型獲取倉庫資訊,則為None
或"model"
。預設為None
。 - timeout (
float
, optional) — 是否為向 Hub 傳送的請求設定超時。 - expand (
ExpandModelProperty_T
orExpandDatasetProperty_T
orExpandSpaceProperty_T
, optional) — 在響應中返回的屬性列表。使用時,將只返回列表中的屬性。如果傳遞了files_metadata
,則不能使用此引數。有關可用屬性的詳盡列表,請檢視 model_info()、dataset_info() 或 space_info()。 - files_metadata (
bool
, optional) — 是否檢索倉庫中檔案的元資料(大小、LFS 元資料等)。預設為False
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
Union[SpaceInfo, DatasetInfo, ModelInfo]
倉庫資訊,作為 huggingface_hub.hf_api.DatasetInfo、huggingface_hub.hf_api.ModelInfo 或 huggingface_hub.hf_api.SpaceInfo 物件。
獲取給定型別的給定倉庫的資訊物件。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
request_space_hardware
< source >( repo_id: str hardware: SpaceHardware token: Union[bool, str, None] = None sleep_time: Optional[int] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - hardware (
str
or SpaceHardware) — 執行空間所需的硬體。例如:"t4-medium"
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - sleep_time (
int
, optional) — 在空間進入休眠狀態之前等待的非活動秒數。如果您不希望空間休眠,請設定為-1
(升級硬體的預設行為)。對於免費硬體,您無法配置休眠時間(固定為 48 小時不活動)。更多詳情請參見 https://huggingface.co/docs/hub/spaces-gpus#sleep-time。
返回
關於 Space 的執行時資訊,包括 Space 的階段和硬體。
為空間請求新硬體。
也可以在建立空間倉庫時直接請求硬體!詳情請參見 create_repo()。
request_space_storage
< source >( repo_id: str storage: SpaceStorage token: Union[bool, str, None] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要更新的空間 ID。例如:"open-llm-leaderboard/open_llm_leaderboard"
。 - storage (
str
or SpaceStorage) — 儲存層級。可以是 ‘small’、‘medium’ 或 ‘large’。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於 Space 的執行時資訊,包括 Space 的階段和硬體。
為空間請求持久儲存。
持久儲存一旦授予後,便無法減少。要減少儲存,您必須透過 delete_space_storage() 將其刪除。
restart_space
< source >( repo_id: str token: Union[bool, str, None] = None factory_reboot: bool = False ) → SpaceRuntime
引數
- repo_id (
str
) — 要重新啟動的空間 ID。例如:"Salesforce/BLIP2"
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - factory_reboot (
bool
, optional) — 如果為True
,空間將從頭開始重建,不快取任何依賴項。
返回
關於您的空間的執行時資訊。
- RepositoryNotFoundError — 如果你的空間未找到(錯誤 404)。很可能是 repo_id 錯誤,或者你的空間是私有的但你沒有進行身份驗證。
- HfHubHTTPError — 403 Forbidden: 只有空間的所有者才能重新啟動它。如果您想重新啟動一個不屬於您的空間,可以透過發起討論(Discussion)請求所有者,或者複製該空間。
- BadRequestError — 如果你的空間是靜態空間。靜態空間始終執行且不收費。如果你想隱藏一個靜態空間,可以將其設定為私有。
重新啟動您的空間。
如果您已將空間暫停(參見 pause_space()),這是以程式設計方式重新啟動空間的唯一方法。您必須是空間的所有者才能重新啟動它。如果您使用的是升級硬體,您的賬戶將在空間重新啟動後立即開始計費。無論空間當前處於何種狀態,您都可以觸發重新啟動。
更多詳情,請訪問文件。
resume_inference_endpoint
< source >( name: str namespace: Optional[str] = None running_ok: bool = True token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 要恢復的推理端點的名稱。 - namespace (
str
, optional) — 推理端點所在的名稱空間。預設為當前使用者。 - running_ok (
bool
, optional) — 如果為True
,當推理端點已在執行時,該方法不會引發錯誤。預設為True
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
關於已恢復的推理端點的資訊。
恢復一個推理端點。
為方便起見,您也可以使用 InferenceEndpoint.resume() 來恢復一個推理端點。
revision_exists
< source >( repo_id: str revision: str repo_type: Optional[str] = None token: Union[str, bool, None] = None )
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和一個倉庫名稱,以/
分隔。 - revision (
str
) — 要檢查的倉庫版本。 - repo_type (
str
, optional) — 如果從資料集或空間獲取倉庫資訊,則設定為"dataset"
或"space"
;如果從模型獲取倉庫資訊,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], optional) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
檢查 Hugging Face Hub 上的某個倉庫是否存在特定的版本。
run_as_future
< source >( fn: Callable[..., R] *args **kwargs ) → Future
在後臺執行一個方法並返回一個 Future 例項。
主要目標是在不阻塞主執行緒的情況下執行方法(例如,在訓練期間推送資料)。後臺作業會排隊以保持順序,但不會並行執行。如果您需要透過並行化大量 API 呼叫來加速指令碼,則必須設定並使用您自己的 ThreadPoolExecutor。
注意:像 upload_file()、upload_folder() 和 create_commit() 這樣最常用的方法有一個 run_as_future: bool
引數,可以直接在後臺呼叫它們。這等同於對它們呼叫 api.run_as_future(...)
,但更簡潔。
run_job
< source >( image: str command: List[str] env: Optional[Dict[str, Any]] = None secrets: Optional[Dict[str, Any]] = None flavor: Optional[SpaceHardware] = None timeout: Optional[Union[int, float, str]] = None namespace: Optional[str] = None token: Union[bool, str, None] = None )
引數
- image (
str
) — 要使用的 Docker 映象。例如:"ubuntu"
、"python:3.12"
、"pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel"
。使用來自 Space 的映象示例:"hf.co/spaces/lhoestq/duckdb"
。 - command (
List[str]
) — 要執行的命令。例如:["echo", "hello"]
。 - env (
Dict[str, Any]
, 可選) — 為作業定義環境變數。 - secrets (
Dict[str, Any]
, 可選) — 為作業定義機密環境變數。 - flavor (
str
, 可選) — 硬體的規格,與 Hugging Face Spaces 中的一樣。有關可能的值,請參閱 SpaceHardware。預設為"cpu-basic"
。 - timeout (
Union[int, float, str]
, 可選) — 作業的最大持續時間:int/float,單位為 s(秒,預設)、m(分鐘)、h(小時)或 d(天)。例如:300
或"5m"
表示 5 分鐘。 - namespace (
str
, 可選) — 將在其中建立作業的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
, 可選) — 有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
可停用身份驗證。請參考:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
在 Hugging Face 基礎設施上執行計算作業。
示例
run_uv_job
< source >( script: str script_args: Optional[List[str]] = None dependencies: Optional[List[str]] = None python: Optional[str] = None image: Optional[str] = None env: Optional[Dict[str, Any]] = None secrets: Optional[Dict[str, Any]] = None flavor: Optional[SpaceHardware] = None timeout: Optional[Union[int, float, str]] = None namespace: Optional[str] = None token: Union[bool, str, None] = None _repo: Optional[str] = None )
引數
- script (
str
) — UV 指令碼的路徑或 URL。 - script_args (
List[str]
, 可選) — 傳遞給指令碼的引數。 - dependencies (
List[str]
, 可選) — 執行 UV 指令碼所使用的依賴項。 - python (
str
, 可選) — 使用特定的 Python 版本。預設為 3.12。 - image (
str
, 可選,預設為 “ghcr.io/astral-sh/uv —python3.12-bookworm”): 使用一個已安裝uv
的自定義 Docker 映象。 - env (
Dict[str, Any]
, 可選) — 為作業定義環境變數。 - secrets (
Dict[str, Any]
, 可選) — 為作業定義機密環境變數。 - flavor (
str
, 可選) — 硬體的規格,與 Hugging Face Spaces 中的一樣。有關可能的值,請參閱 SpaceHardware。預設為"cpu-basic"
。 - timeout (
Union[int, float, str]
, 可選) — 作業的最大持續時間:int/float,單位為 s(秒,預設)、m(分鐘)、h(小時)或 d(天)。例如:300
或"5m"
表示 5 分鐘。 - namespace (
str
, 可選) — 將在其中建立作業的名稱空間。預設為當前使用者的名稱空間。 - token
(Union[bool, str, None]
, 可選) — 有效的使用者訪問令牌。如果未提供,將使用本地儲存的令牌,這是推薦的身份驗證方法。設定為False
可停用身份驗證。請參考:https://huggingface.co/docs/huggingface_hub/quick-start#authentication。
在 Hugging Face 基礎設施上執行 UV 指令碼作業。
scale_to_zero_inference_endpoint
< source >( name: str namespace: Optional[str] = None token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 要縮減至零的推理端點的名稱。 - namespace (
str
, 可選) — 推理端點所在的名稱空間。預設為當前使用者。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
關於縮減至零的推理端點的資訊。
將推理端點縮減至零。
縮減至零的推理端點將不會被計費。它將在下一次請求時恢復,並有冷啟動延遲。這與使用 pause_inference_endpoint() 暫停推理端點不同,後者需要使用 resume_inference_endpoint() 手動恢復。
為方便起見,您還可以使用 InferenceEndpoint.scale_to_zero() 將推理端點縮減至零。
set_space_sleep_time
< source >( repo_id: str sleep_time: int token: Union[bool, str, None] = None ) → SpaceRuntime
引數
- repo_id (
str
) — 要更新的倉庫 ID。例如:"bigcode/in-the-stack"
。 - sleep_time (
int
, 可選) — 在 Space 進入休眠狀態前等待的非活動秒數。如果您不希望您的 Space 暫停,請設定為-1
(升級硬體的預設行為)。對於免費硬體,您無法配置休眠時間(該值固定為 48 小時不活動)。更多詳情請參閱 https://huggingface.co/docs/hub/spaces-gpus#sleep-time。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於 Space 的執行時資訊,包括 Space 的階段和硬體。
為在升級硬體上執行的 Space 設定自定義休眠時間。
您的 Space 將在 X 秒不活動後進入休眠狀態。當您的 Space 處於“休眠”模式時,您不會被計費。如果有新訪客訪問您的 Space,它將會“喚醒”。只有升級的硬體才能配置休眠時間。要了解有關休眠階段的更多資訊,請參考 https://huggingface.co/docs/hub/spaces-gpus#sleep-time。
在使用 request_space_hardware() 請求硬體時,也可以設定自定義休眠時間。
snapshot_download
< source >( repo_id: str repo_type: Optional[str] = None revision: Optional[str] = None cache_dir: Union[str, Path, None] = None local_dir: Union[str, Path, None] = None proxies: Optional[Dict] = None etag_timeout: float = 10 force_download: bool = False token: Union[bool, str, None] = None local_files_only: bool = False allow_patterns: Optional[Union[List[str], str]] = None ignore_patterns: Optional[Union[List[str], str]] = None max_workers: int = 8 tqdm_class: Optional[Type[base_tqdm]] = None local_dir_use_symlinks: Union[bool, Literal['auto']] = 'auto' resume_download: Optional[bool] = None ) → str
引數
- repo_id (
str
) — 使用者或組織名稱和倉庫名稱,用/
分隔。 - repo_type (
str
, 可選) — 如果從資料集或空間下載,則設定為"dataset"
或"space"
;如果從模型下載,則為None
或"model"
。預設為None
。 - revision (
str
, 可選) — 可選的 Git 修訂版 ID,可以是一個分支名稱、一個標籤或一個提交雜湊值。 - cache_dir (
str
,Path
, 可選) — 儲存快取檔案的資料夾路徑。 - local_dir (
str
或Path
, 可選) — 如果提供,下載的檔案將放置在此目錄下。 - proxies (
dict
, 可選) — 協議到代理 URL 的字典對映,傳遞給requests.request
。 - etag_timeout (
float
, 可選, 預設為10
) — 獲取 ETag 時,在放棄之前等待伺服器傳送資料的秒數,傳遞給requests.request
。 - force_download (
bool
, 可選, 預設為False
) — 即使檔案已存在於本地快取中,是否也應下載。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - local_files_only (
bool
, 可選, 預設為False
) — 如果為True
,則避免下載檔案,如果本地快取檔案存在,則返回其路徑。 - allow_patterns (
List[str]
或str
, 可選) — 如果提供,則僅下載與至少一個模式匹配的檔案。 - ignore_patterns (
List[str]
或str
, 可選) — 如果提供,則不下載與任何模式匹配的檔案。 - max_workers (
int
, 可選) — 下載檔案的併發執行緒數(1 個執行緒 = 1 個檔案下載)。預設為 8。 - tqdm_class (
tqdm
, 可選) — 如果提供,將覆蓋進度條的預設行為。傳遞的引數必須繼承自tqdm.auto.tqdm
或至少模仿其行為。請注意,tqdm_class
不會傳遞給每個單獨的下載。預設為自定義的 HF 進度條,可以透過設定HF_HUB_DISABLE_PROGRESS_BARS
環境變數來停用。
返回
字串
倉庫快照的資料夾路徑。
引發
RepositoryNotFoundError 或 RevisionNotFoundError 或 EnvironmentError
或 OSError
或 ValueError
- RepositoryNotFoundError — 如果要下載的倉庫找不到。這可能是因為它不存在,或者它被設定為
private
而您沒有訪問許可權。 - RevisionNotFoundError — 如果要下載的修訂版本找不到。
EnvironmentError
— 如果token=True
且找不到令牌。OSError
— 如果無法確定 ETag。ValueError
— 如果某些引數值無效。
下載倉庫檔案。
下載指定修訂版中倉庫檔案的完整快照。當您需要倉庫中的所有檔案,但事先不知道具體需要哪些檔案時,這非常有用。所有檔案都巢狀在一個資料夾內,以保持它們相對於該資料夾的實際檔名。您還可以使用 allow_patterns
和 ignore_patterns
過濾要下載的檔案。
如果提供了 local_dir
,倉庫的檔案結構將在此位置被複制。使用此選項時,將不使用 cache_dir
,並將在 local_dir
的根目錄下建立一個 .cache/huggingface/
資料夾,以儲存與下載檔案相關的一些元資料。雖然此機制不如主快取系統穩健,但它為定期拉取倉庫的最新版本進行了最佳化。
另一種方法是克隆倉庫,但這需要安裝並正確配置 git 和 git-lfs。使用 git 克隆倉庫時,也無法過濾要下載的檔案。
space_info
< source >( repo_id: str revision: Optional[str] = None timeout: Optional[float] = None files_metadata: bool = False expand: Optional[List[ExpandSpaceProperty_T]] = None token: Union[bool, str, None] = None ) → SpaceInfo
引數
- repo_id (
str
) — 一個名稱空間(使用者或組織)和倉庫名稱,用/
分隔。 - revision (
str
, 可選) — 要獲取資訊的空間倉庫的修訂版。 - timeout (
float
, 可選) — 是否為向 Hub 的請求設定超時。 - files_metadata (
bool
, 可選) — 是否檢索倉庫中檔案的元資料(大小、LFS 元資料等)。預設為False
。 - expand (
List[ExpandSpaceProperty_T]
, 可選) — 在響應中返回的屬性列表。使用時,只有列表中的屬性會被返回。如果傳遞了full
,則不能使用此引數。可能的值為"author"
、"cardData"
、"createdAt"
、"datasets"
、"disabled"
、"lastModified"
、"likes"
、"models"
、"private"
、"runtime"
、"sdk"
、"siblings"
、"sha"
、"subdomain"
、"tags"
、"trendingScore"
、"usedStorage"
、"resourceGroup"
和"xetEnabled"
。 - token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
空間倉庫資訊。
獲取 huggingface.co 上某個特定 Space 的資訊。
如果傳遞了可接受的令牌,Space 可以是私有的。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
super_squash_history
< source >( repo_id: str branch: Optional[str] = None commit_message: Optional[str] = None repo_type: Optional[str] = None token: Union[str, bool, None] = None )
引數
- repo_id (
str
) — 名稱空間(使用者或組織)和倉庫名稱,以/
分隔。 - branch (
str
, optional) — 要壓縮歷史的分支。預設為"main"
分支的頭部。 - commit_message (
str
, optional) — 用於壓縮提交的提交資訊。 - repo_type (
str
, optional) — 如果是列出資料集或 Space 的提交,則設定為"dataset"
或"space"
;如果是列出模型的提交,則為None
或"model"
。預設為None
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
- RevisionNotFoundError — 如果找不到要壓縮的分支。
- BadRequestError — 如果分支的引用無效。您不能在標籤上壓縮歷史記錄。
在 Hub 上的倉庫的某個分支上壓縮提交歷史。
當您知道將進行數百次提交併且不想讓歷史變得混亂時,壓縮倉庫歷史非常有用。壓縮提交只能在分支的頭部執行。
一旦壓縮,提交歷史將無法恢復。這是一個不可逆的操作。
一旦某個分支的歷史被壓縮,就無法將其合併回另一個分支,因為它們的歷史已經分叉。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
# Create repo
>>> repo_id = api.create_repo("test-squash").repo_id
# Make a lot of commits.
>>> api.upload_file(repo_id=repo_id, path_in_repo="file.txt", path_or_fileobj=b"content")
>>> api.upload_file(repo_id=repo_id, path_in_repo="lfs.bin", path_or_fileobj=b"content")
>>> api.upload_file(repo_id=repo_id, path_in_repo="file.txt", path_or_fileobj=b"another_content")
# Squash history
>>> api.super_squash_history(repo_id=repo_id)
unlike
< 原始碼 >( repo_id: str token: Union[bool, str, None] = None repo_type: Optional[str] = None )
引數
- repo_id (
str
) — 要取消點讚的倉庫。示例:"user/my-cool-model"
。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, optional) — 如果要取消點讚的是資料集或 Space,則設定為"dataset"
或"space"
;如果要取消點讚的是模型,則為None
或"model"
。預設為None
。
- RepositoryNotFoundError — 如果未找到倉庫(錯誤 404):錯誤的 repo_id/repo_type,私有倉庫但未認證,或倉庫不存在。
取消對 Hub 上指定倉庫的點贊(例如,從收藏列表中移除)。
為防止垃圾郵件行為,無法透過指令碼 點贊
倉庫。
另請參閱 list_liked_repos()。
update_collection_item
< 原始碼 >( collection_slug: str item_object_id: str note: Optional[str] = None position: Optional[int] = None token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — 要更新的合集的 slug。示例:"TheBloke/recent-models-64f9a55bb3115b4f513ec026"
。 - item_object_id (
str
) — 合集中專案的 ID。這不是 Hub 上專案(repo_id 或 paper id)的 ID。它必須從 CollectionItem 物件中檢索。示例:collection.items[0].item_object_id
。 - note (
str
, optional) — 附加到合集中專案的備註。備註的最大長度為 500 個字元。 - position (
int
, optional) — 專案在合集中的新位置。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
更新合集中的一個專案。
示例
>>> from huggingface_hub import get_collection, update_collection_item
# Get collection first
>>> collection = get_collection("TheBloke/recent-models-64f9a55bb3115b4f513ec026")
# Update item based on its ID (add note + update position)
>>> update_collection_item(
... collection_slug="TheBloke/recent-models-64f9a55bb3115b4f513ec026",
... item_object_id=collection.items[-1].item_object_id,
... note="Newly updated model!"
... position=0,
... )
update_collection_metadata
< 原始碼 >( collection_slug: str title: Optional[str] = None description: Optional[str] = None position: Optional[int] = None private: Optional[bool] = None theme: Optional[str] = None token: Union[bool, str, None] = None )
引數
- collection_slug (
str
) — 要更新的合集的 slug。示例:"TheBloke/recent-models-64f9a55bb3115b4f513ec026"
。 - title (
str
) — 要更新的合集的標題。 - description (
str
, optional) — 要更新的合集的描述。 - position (
int
, optional) — 合集在使用者合集列表中的新位置。 - private (
bool
, optional) — 合集是否應為私有。 - theme (
str
, optional) — Hub 上合集的主題。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
更新 Hub 上合集的元資料。
所有引數都是可選的。只有提供的元資料才會被更新。
返回: Collection
示例
>>> from huggingface_hub import update_collection_metadata
>>> collection = update_collection_metadata(
... collection_slug="username/iccv-2023-64f9a55bb3115b4f513ec026",
... title="ICCV Oct. 2023"
... description="Portfolio of models, datasets, papers and demos I presented at ICCV Oct. 2023",
... private=False,
... theme="pink",
... )
>>> collection.slug
"username/iccv-oct-2023-64f9a55bb3115b4f513ec026"
# ^collection slug got updated but not the trailing ID
update_inference_endpoint
< 原始碼 >( name: str accelerator: Optional[str] = None instance_size: Optional[str] = None instance_type: Optional[str] = None min_replica: Optional[int] = None max_replica: Optional[int] = None scale_to_zero_timeout: Optional[int] = None repository: Optional[str] = None framework: Optional[str] = None revision: Optional[str] = None task: Optional[str] = None custom_image: Optional[Dict] = None env: Optional[Dict[str, str]] = None secrets: Optional[Dict[str, str]] = None domain: Optional[str] = None path: Optional[str] = None cache_http_responses: Optional[bool] = None tags: Optional[List[str]] = None namespace: Optional[str] = None token: Union[bool, str, None] = None ) → InferenceEndpoint
引數
- name (
str
) — 要更新的推理端點的名稱。 - accelerator (
str
, optional) — 用於推理的硬體加速器(例如"cpu"
)。 - instance_size (
str
, optional) — 用於託管模型的例項的大小或型別(例如"x4"
)。 - instance_type (
str
, optional) — 推理端點將部署的雲實例型別(例如"intel-icl"
)。 - min_replica (
int
, optional) — 推理端點保持執行的最小副本(例項)數量。 - max_replica (
int
, optional) — 推理端點可以擴充套件到的最大副本(例項)數量。 - scale_to_zero_timeout (
int
, optional) — 非活動端點縮減至零之前的持續時間(分鐘)。 - repository (
str
, optional) — 與推理端點關聯的模型倉庫的名稱(例如"gpt2"
)。 - framework (
str
, optional) — 用於模型的機器學習框架(例如"custom"
)。 - revision (
str
, optional) — 在推理端點上部署的特定模型修訂版(例如"6c0e6080953db56375760c0471a8c5f2929baf11"
)。 - task (
str
, optional) — 部署模型的任務(例如"text-classification"
)。 - custom_image (
Dict
, optional) — 用於推理端點的自定義 Docker 映象。如果您想部署一個執行在text-generation-inference
(TGI) 框架上的推理端點,這將非常有用(請參閱示例)。 - env (
Dict[str, str]
, optional) — 注入到容器環境中的非機密環境變數 - secrets (
Dict[str, str]
, optional) — 注入到容器環境中的機密值。 - domain (
str
, optional) — 推理端點部署的自定義域名,如果設定,推理端點將在此域名下可用(例如"my-new-domain.cool-website.woof"
)。 - path (
str
, optional) — 已部署模型的自定義路徑,應以/
開頭(例如"/models/google-bert/bert-base-uncased"
)。 - cache_http_responses (
bool
, optional) — 是否快取來自推理端點的 HTTP 響應。 - tags (
List[str]
, optional) — 與推理端點關聯的標籤列表。 - namespace (
str
, optional) — 將要更新推理端點的名稱空間。預設為當前使用者的名稱空間。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
有關更新後的推理端點的資訊。
更新一個推理端點。
此方法允許更新計算配置、部署的模型、路由或它們的任意組合。所有引數都是可選的,但至少必須提供一個。
為了方便起見,您也可以使用 InferenceEndpoint.update() 來更新推理端點。
update_repo_settings
< 原始碼 >( repo_id: str gated: Optional[Literal['auto', 'manual', False]] = None private: Optional[bool] = None token: Union[str, bool, None] = None repo_type: Optional[str] = None xet_enabled: Optional[bool] = None )
引數
- repo_id (
str
) — 名稱空間(使用者或組織)和倉庫名稱,以 / 分隔。 - gated (
Literal["auto", "manual", False]
, optional) — 儲存庫的訪問控制狀態。如果設定為None
(預設值),則不會更新儲存庫的gated
設定。- “auto”:儲存庫受訪問控制,訪問請求會根據預定義標準自動批准或拒絕。
- “manual”:儲存庫受訪問控制,訪問請求需要手動批准。
- False:儲存庫不受訪問控制,任何人都可以訪問。
- private (
bool
, optional) — 儲存庫是否應為私有。 - token (
Union[str, bool, None]
, optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞 False。 - repo_type (
str
, optional) — 要更新設定的儲存庫型別("model"
、"dataset"
或"space"
)。預設為"model"
。 - xet_enabled (
bool
, optional) — 是否為儲存庫啟用 Xet 儲存。
引發
ValueError
或 HfHubHTTPError 或 RepositoryNotFoundError
ValueError
— 如果 gated 不是“auto”、“manual”或 False 之一。ValueError
— 如果 repo_type 不是 constants.REPO_TYPES 中的值之一。- HfHubHTTPError — 如果向 Hugging Face Hub API 的請求失敗。
- RepositoryNotFoundError — 如果要下載的倉庫找不到。這可能是因為它不存在,或者它被設定為
private
而您沒有訪問許可權。
更新儲存庫的設定,包括訪問控制和可見性。
為了更好地控制儲存庫的使用方式,Hub 允許儲存庫作者為其儲存庫啟用訪問請求,並將儲存庫的可見性設定為私有。
update_repo_visibility
< 原始碼 >( repo_id: str private: bool = False token: Union[str, bool, None] = None repo_type: Optional[str] = None )
引數
- repo_id (
str
, optional) — 一個名稱空間(使用者或組織)和一個儲存庫名稱,由/
分隔。 - private (
bool
, optional, defaults toFalse
) — 儲存庫是否應為私有。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, optional) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。
更新儲存庫的可見性設定。
已棄用。請改用 update_repo_settings
。
引發以下錯誤
- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
update_webhook
< 原始碼 >( webhook_id: str url: Optional[str] = None watched: Optional[List[Union[Dict, WebhookWatchedItem]]] = None domains: Optional[List[constants.WEBHOOK_DOMAIN_T]] = None secret: Optional[str] = None token: Union[bool, str, None] = None ) → WebhookInfo
引數
- webhook_id (
str
) — 要更新的 webhook 的唯一識別符號。 - url (
str
, optional) — 將傳送有效負載的 URL。 - watched (
List[WebhookWatchedItem]
, optional) — 要監視的專案列表。可以是使用者、組織、模型、資料集或空間。更多詳情請參閱 WebhookWatchedItem。監視的專案也可以作為普通字典提供。 - domains (
List[Literal["repo", "discussion"]]
, optional) — 要監視的域。可以包括“repo”、“discussion”或兩者。 - secret (
str
, optional) — 用於簽署有效負載的金鑰,提供額外的安全層。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
返回
關於已更新的 webhook 的資訊。
更新現有的 webhook。
示例
>>> from huggingface_hub import update_webhook
>>> updated_payload = update_webhook(
... webhook_id="654bbbc16f2ec14d77f109cc",
... url="https://new.webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
... watched=[{"type": "user", "name": "julien-c"}, {"type": "org", "name": "HuggingFaceH4"}],
... domains=["repo"],
... secret="my-secret",
... )
>>> print(updated_payload)
WebhookInfo(
id="654bbbc16f2ec14d77f109cc",
url="https://new.webhook.site/a2176e82-5720-43ee-9e06-f91cb4c91548",
watched=[WebhookWatchedItem(type="user", name="julien-c"), WebhookWatchedItem(type="org", name="HuggingFaceH4")],
domains=["repo"],
secret="my-secret",
disabled=False,
upload_file
< 原始碼 >( path_or_fileobj: Union[str, Path, bytes, BinaryIO] path_in_repo: str repo_id: str token: Union[str, bool, None] = None repo_type: Optional[str] = None revision: Optional[str] = None commit_message: Optional[str] = None commit_description: Optional[str] = None create_pr: Optional[bool] = None parent_commit: Optional[str] = None run_as_future: bool = False ) → CommitInfo or Future
引數
- path_or_fileobj (
str
,Path
,bytes
, orIO
) — 本地機器上的檔案路徑或二進位制資料流/檔案物件/緩衝區。 - path_in_repo (
str
) — 儲存庫中的相對檔案路徑,例如:"checkpoints/1fec34a/weights.bin"
- repo_id (
str
) — 檔案將上傳到的儲存庫,例如:"username/custom_transformers"
- token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, optional) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - revision (
str
, optional) — 要提交的 git 修訂版本。預設為"main"
分支的頭部。 - commit_message (
str
, optional) — 生成的提交的摘要/標題/第一行 - commit_description (
str
optional) — 生成的提交的描述 - create_pr (
boolean
, optional) — 是否為此提交建立一個拉取請求。預設為False
。如果未設定revision
,則針對"main"
分支開啟拉取請求。如果設定了revision
並且它是一個分支,則針對此分支開啟拉取請求。如果設定了revision
但不是分支名稱(例如:提交的 oid),伺服器將返回RevisionNotFoundError
。 - parent_commit (
str
, optional) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前 7 個字元)。如果指定且create_pr
為False
,則當revision
未指向parent_commit
時,提交將失敗。如果指定且create_pr
為True
,則拉取請求將從parent_commit
建立。指定parent_commit
可確保在提交更改之前儲存庫未發生變化,這在儲存庫被併發更新/提交時尤其有用。 - run_as_future (
bool
, optional) — 是否在後臺執行此方法。後臺作業按順序執行,不阻塞主執行緒。傳遞run_as_future=True
將返回一個 Future 物件。預設為False
。
返回
CommitInfo or Future
CommitInfo 的例項,包含有關新建立的提交的資訊(提交雜湊、提交 URL、PR URL、提交訊息……)。如果傳遞了 run_as_future=True
,則返回一個 Future 物件,執行後將包含結果。
將本地檔案(最大 50 GB)上傳到給定的儲存庫。上傳透過 HTTP post 請求完成,不需要安裝 git 或 git-lfs。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效- RepositoryNotFoundError 如果找不到要下載的倉庫。這可能是因為它不存在,或者是私有倉庫而您沒有訪問許可權。
- RevisionNotFoundError 如果找不到要下載的版本。
upload_file
假定儲存庫已存在於 Hub 上。如果您收到客戶端錯誤 404,請確保您已透過身份驗證,並且 repo_id
和 repo_type
設定正確。如果儲存庫不存在,請先使用 create_repo() 建立它。
示例
>>> from huggingface_hub import upload_file
>>> with open("./local/filepath", "rb") as fobj:
... upload_file(
... path_or_fileobj=fileobj,
... path_in_repo="remote/file/path.h5",
... repo_id="username/my-dataset",
... repo_type="dataset",
... token="my_token",
... )
"https://huggingface.co/datasets/username/my-dataset/blob/main/remote/file/path.h5"
>>> upload_file(
... path_or_fileobj=".\\local\\file\\path",
... path_in_repo="remote/file/path.h5",
... repo_id="username/my-model",
... token="my_token",
... )
"https://huggingface.co/username/my-model/blob/main/remote/file/path.h5"
>>> upload_file(
... path_or_fileobj=".\\local\\file\\path",
... path_in_repo="remote/file/path.h5",
... repo_id="username/my-model",
... token="my_token",
... create_pr=True,
... )
"https://huggingface.co/username/my-model/blob/refs%2Fpr%2F1/remote/file/path.h5"
upload_folder
< 原始碼 >( repo_id: str folder_path: Union[str, Path] path_in_repo: Optional[str] = None commit_message: Optional[str] = None commit_description: Optional[str] = None token: Union[str, bool, None] = None repo_type: Optional[str] = None revision: Optional[str] = None create_pr: Optional[bool] = None parent_commit: Optional[str] = None allow_patterns: Optional[Union[List[str], str]] = None ignore_patterns: Optional[Union[List[str], str]] = None delete_patterns: Optional[Union[List[str], str]] = None run_as_future: bool = False ) → CommitInfo or Future
引數
- repo_id (
str
) — 檔案將上傳到的儲存庫,例如:"username/custom_transformers"
- folder_path (
str
orPath
) — 本地檔案系統上要上傳的資料夾的路徑 - path_in_repo (
str
, optional) — 儲存庫中目錄的相對路徑,例如:"checkpoints/1fec34a/results"
。將預設為儲存庫的根資料夾。 - token (Union[bool, str, None], optional) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。 - repo_type (
str
, optional) — 如果上傳到資料集或空間,則設定為"dataset"
或"space"
;如果上傳到模型,則為None
或"model"
。預設為None
。 - revision (
str
, optional) — 要提交的 git 修訂版本。預設為"main"
分支的頭部。 - commit_message (
str
, optional) — 生成的提交的摘要/標題/第一行。預設為:f"Upload {path_in_repo} with huggingface_hub"
- commit_description (
str
optional) — 生成的提交的描述 - create_pr (
boolean
, optional) — 是否為此提交建立一個拉取請求。預設為False
。如果未設定revision
,則針對"main"
分支開啟拉取請求。如果設定了revision
並且它是一個分支,則針對此分支開啟拉取請求。如果設定了revision
但不是分支名稱(例如:提交的 oid),伺服器將返回RevisionNotFoundError
。 - parent_commit (
str
, optional) — 父提交的 OID / SHA,以十六進位制字串表示。也支援簡寫(前 7 個字元)。如果指定且create_pr
為False
,則當revision
未指向parent_commit
時,提交將失敗。如果指定且create_pr
為True
,則拉取請求將從parent_commit
建立。指定parent_commit
可確保在提交更改之前儲存庫未發生變化,這在儲存庫被併發更新/提交時尤其有用。 - allow_patterns (
List[str]
orstr
, optional) — 如果提供,則僅上傳至少匹配一個模式的檔案。 - ignore_patterns (
List[str]
orstr
, optional) — 如果提供,則不上傳匹配任何模式的檔案。 - delete_patterns (
List[str]
orstr
, optional) — 如果提供,則在提交新檔案時,匹配任何模式的遠端檔案將從儲存庫中刪除。如果您不知道哪些檔案已經上傳,這會很有用。注意:為避免不一致,即使.gitattributes
檔案匹配模式,也不會被刪除。 - run_as_future (
bool
, optional) — 是否在後臺執行此方法。後臺作業按順序執行,不阻塞主執行緒。傳遞run_as_future=True
將返回一個 Future 物件。預設為False
。
返回
CommitInfo or Future
CommitInfo 的例項,包含有關新建立的提交的資訊(提交雜湊、提交 URL、PR URL、提交訊息……)。如果傳遞了 run_as_future=True
,則返回一個 Future 物件,執行後將包含結果。
將本地資料夾上傳到給定的儲存庫。上傳透過 HTTP 請求完成,不需要安裝 git 或 git-lfs。
資料夾的結構將被保留。儲存庫中已存在的同名檔案將被覆蓋。其他檔案將保持不變。
使用 allow_patterns
和 ignore_patterns
引數指定要上傳的檔案。這些引數接受單個模式或模式列表。模式是標準萬用字元(globbing 模式),如此處所述。如果同時提供了 allow_patterns
和 ignore_patterns
,則兩個約束都將適用。預設情況下,將上傳資料夾中的所有檔案。
使用 delete_patterns
引數指定要刪除的遠端檔案。輸入型別與 allow_patterns
相同(見上文)。如果還提供了 path_in_repo
,則模式將與相對於此資料夾的路徑匹配。例如,upload_folder(..., path_in_repo="experiment", delete_patterns="logs/*")
將刪除 ./experiment/logs/
下的任何遠端檔案。請注意,即使 .gitattributes
檔案匹配模式,也不會被刪除。
任何子目錄中存在的 .git/
資料夾都將被忽略。但是,請注意,.gitignore
檔案不會被考慮。
內部使用 HfApi.create_commit
。
引發以下錯誤
HTTPError
如果 HuggingFace API 返回錯誤ValueError
如果某些引數值無效
upload_folder
假定儲存庫已存在於 Hub 上。如果您收到客戶端錯誤 404,請確保您已透過身份驗證,並且 repo_id
和 repo_type
設定正確。如果儲存庫不存在,請先使用 create_repo() 建立它。
在處理大型資料夾(數千個檔案或數百GB)時,我們建議改用 upload_large_folder()。
示例
# Upload checkpoints folder except the log files
>>> upload_folder(
... folder_path="local/checkpoints",
... path_in_repo="remote/experiment/checkpoints",
... repo_id="username/my-dataset",
... repo_type="datasets",
... token="my_token",
... ignore_patterns="**/logs/*.txt",
... )
# "https://huggingface.co/datasets/username/my-dataset/tree/main/remote/experiment/checkpoints"
# Upload checkpoints folder including logs while deleting existing logs from the repo
# Useful if you don't know exactly which log files have already being pushed
>>> upload_folder(
... folder_path="local/checkpoints",
... path_in_repo="remote/experiment/checkpoints",
... repo_id="username/my-dataset",
... repo_type="datasets",
... token="my_token",
... delete_patterns="**/logs/*.txt",
... )
"https://huggingface.co/datasets/username/my-dataset/tree/main/remote/experiment/checkpoints"
# Upload checkpoints folder while creating a PR
>>> upload_folder(
... folder_path="local/checkpoints",
... path_in_repo="remote/experiment/checkpoints",
... repo_id="username/my-dataset",
... repo_type="datasets",
... token="my_token",
... create_pr=True,
... )
"https://huggingface.co/datasets/username/my-dataset/tree/refs%2Fpr%2F1/remote/experiment/checkpoints"
upload_large_folder
< 原始碼 >( repo_id: str folder_path: Union[str, Path] repo_type: str revision: Optional[str] = None private: Optional[bool] = None allow_patterns: Optional[Union[List[str], str]] = None ignore_patterns: Optional[Union[List[str], str]] = None num_workers: Optional[int] = None print_report: bool = True print_report_every: int = 60 )
引數
- repo_id (
str
) — 檔案將上傳到的倉庫。例如"HuggingFaceTB/smollm-corpus"
。 - folder_path (
str
或Path
) — 本地檔案系統上要上傳的資料夾路徑。 - repo_type (
str
) — 倉庫的型別。必須是"model"
、"dataset"
或"space"
之一。與所有其他HfApi
方法不同,此處明確要求repo_type
。這是為了避免在向 Hub 上傳大型資料夾時出現任何錯誤,從而防止需要重新上傳所有內容。 - revision (
str
,可選
) — 要提交到的分支。如果未提供,將使用main
分支。 - private (
bool
,可選
) — 倉庫是否應為私有。如果為None
(預設值),倉庫將是公開的,除非組織的預設設定為私有。 - allow_patterns (
List[str]
或str
, 可選) — 如果提供,則只上傳匹配至少一個模式的檔案。 - ignore_patterns (
List[str]
或str
, 可選) — 如果提供,匹配任何模式的檔案都不會被上傳。 - num_workers (
int
, 可選) — 啟動的工作程序數。預設為os.cpu_count() - 2
(最少為 2)。如果你的機器允許,更多的工作程序可能會加快處理速度。然而,在網路連線較慢的機器上,建議保持較低的工作程序數以確保更好的續傳能力。因為如果程序中斷,部分上傳的檔案將不得不完全重新上傳。 - print_report (
bool
, 可選) — 是否列印上傳進度的報告。預設為 True。報告每 X 秒(預設為 60 秒)列印到sys.stdout
,並覆蓋之前的報告。 - print_report_every (
int
, 可選) — 列印報告的頻率。預設為 60 秒。
以最穩健的方式將大型資料夾上傳到 Hub。
啟動多個工作程序以最佳化方式上傳檔案。在提交到倉庫之前,檔案必須被雜湊,並且如果是 LFS 檔案,則必須預上傳。工作程序將為資料夾中的每個檔案執行這些任務。在每一步中,有關上傳過程的一些元資料資訊會儲存在資料夾下的 .cache/.huggingface/
中,以便在程序中斷時能夠恢復。整個過程可能會產生多次提交。
需要注意的幾點
- 倉庫限制仍然適用:https://huggingface.co/docs/hub/repositories-recommendations
- 不要並行啟動多個程序。
- 你可以隨時中斷和恢復該過程。
- 不要將同一個資料夾上傳到多個倉庫。如果需要這樣做,必須先刪除本地的
.cache/.huggingface/
資料夾。
儘管 upload_large_folder
在上傳大型資料夾方面更為穩健,但在功能上比 upload_folder() 更受限。具體來說:
- 你不能設定自定義的
path_in_repo
。如果想上傳到子資料夾,你需要在本地設定好相應的結構。 - 你不能設定自定義的
commit_message
和commit_description
,因為會建立多次提交。 - 上傳時不能從倉庫中刪除檔案。請先單獨進行一次提交來刪除檔案。
- 你不能直接建立 PR。請先建立 PR(透過 UI 或使用 create_pull_request()),然後透過傳遞
revision
引數來向其提交。
技術細節
upload_large_folder
過程如下:
- (檢查引數並設定。)
- 如果倉庫不存在,則建立倉庫。
- 列出本地待上傳的檔案。
- 執行驗證檢查,如果可能超出倉庫限制,則顯示警告。
- 如果檔案總數超過 10 萬個(推薦限制),則發出警告。
- 如果任何資料夾包含超過 1 萬個檔案(推薦限制),則發出警告。
- 對大於 20GB(推薦)或 50GB(硬限制)的檔案發出警告。
- 啟動工作程序。工作程序可以執行以下任務:
- 雜湊一個檔案。
- 獲取檔案列表的上傳模式(常規或 LFS)。
- 預上傳一個 LFS 檔案。
- 提交一批檔案。一旦一個工作程序完成一項任務,它將根據優先順序列表(見下文)移至下一個任務,直到所有檔案都已上傳並提交。
- 工作程序執行時,定期向 sys.stdout 列印報告。
優先順序順序
- 如果距離上次提交嘗試已超過 5 分鐘(且至少有 1 個檔案),則提交。
- 如果至少有 150 個檔案準備好提交,則提交。
- 如果至少有 10 個檔案已被雜湊,則獲取上傳模式。
- 如果至少有 1 個檔案且沒有工作程序在預上傳,則預上傳 LFS 檔案。
- 如果至少有 1 個檔案且沒有工作程序在雜湊,則雜湊檔案。
- 如果至少有 1 個檔案且沒有工作程序在獲取上傳模式,則獲取上傳模式。
- 如果至少有 1 個檔案,則預上傳 LFS 檔案(例外:如果啟用了 hf_transfer,一次只能有 1 個工作程序預上傳 LFS)。
- 如果至少有 1 個檔案要雜湊,則雜湊檔案。
- 如果至少有 1 個檔案要獲取上傳模式,則獲取上傳模式。
- 如果至少有 1 個檔案要提交,並且距離上次提交嘗試至少 1 分鐘,則提交。
- 如果至少有 1 個檔案要提交,並且所有其他佇列都為空,則提交。
特殊規則
- 如果啟用了
hf_transfer
,一次只能有 1 個 LFS 上傳器。否則 CPU 會被hf_transfer
佔用過多。 - 一次只能有一個工作程序進行提交。
- 如果沒有可用任務,工作程序會等待 10 秒後再次檢查。
whoami
< 原始碼 >( token: Union[bool, str, None] = None )
引數
- token (Union[bool, str, None], 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞
False
。
呼叫 HF API 以瞭解“whoami”。
API 資料類
AccessRequest
class huggingface_hub.hf_api.AccessRequest
< 原始碼 >( username: str fullname: str email: Optional[str] timestamp: datetime status: Literal['pending', 'accepted', 'rejected'] fields: Optional[Dict[str, Any]] = None )
引數
- username (
str
) — 請求訪問的使用者的使用者名稱。 - fullname (
str
) — 請求訪問的使用者的全名。 - email (
Optional[str]
) — 請求訪問的使用者的電子郵件。只有在使用者被手動授予訪問許可權的情況下,在 /accepted 列表中該值才可能為 `None`。 - timestamp (
datetime
) — 請求的時間戳。 - status (
Literal["pending", "accepted", "rejected"]
) — 請求的狀態。可以是["pending", "accepted", "rejected"]
之一。 - fields (
Dict[str, Any]
, 可選) — 使用者在准入表單中填寫的附加欄位。
包含使用者訪問請求資訊的資料結構。
CommitInfo
class huggingface_hub.CommitInfo
< 原始碼 >( *args commit_url: str _url: Optional[str] = None **kwargs )
引數
- commit_url (
str
) — 提交所在的 URL。 - commit_message (
str
) — 已建立提交的摘要(第一行)。 - commit_description (
str
) — 已建立提交的描述。可以為空。 - oid (
str
) — 提交的雜湊 ID。例如:"91c54ad1727ee830252e457677f467be0bfd8a57"
。 - pr_url (
str
, 可選) — 已建立的 PR 的 URL(如果有的話)。當傳遞 `create_pr=True` 時會填充此欄位。 - pr_revision (
str
, 可選) — 已建立的 PR 的修訂版本(如果有的話)。當傳遞 `create_pr=True` 時會填充此欄位。例如:`"refs/pr/1"`。 - pr_num (
int
, 可選) — 已建立的 PR 討論的編號(如果有的話)。當傳遞 `create_pr=True` 時會填充此欄位。可以作為 `discussion_num` 傳遞給 get_discussion_details()。例如:`1`。 - repo_url (
RepoUrl
) — 提交的倉庫 URL,包含 repo_id、repo_type 等資訊。 - _url (
str
, 可選) — 用於str
相容性的舊版 URL。可以是指向 Hub 上已上傳檔案的 URL(如果由 upload_file() 返回)、指向 Hub 上已上傳資料夾的 URL(如果由 upload_folder() 返回)或指向 Hub 上提交的 URL(如果由 create_commit() 返回)。預設為 `commit_url`。使用此屬性已被棄用。請改用 `commit_url`。
包含有關新建立的提交資訊的資料結構。
由任何在 Hub 上建立提交的方法返回:create_commit()、upload_file()、upload_folder()、delete_file()、delete_folder()。為了向後相容,它繼承自 str
,但使用特定於 str
的方法已被棄用。
DatasetInfo
class huggingface_hub.DatasetInfo
< 原始碼 >( **kwargs )
引數
- id (
str
) — 資料集的 ID。 - author (
str
) — 資料集的作者。 - sha (
str
) — 此特定修訂版本的倉庫 SHA。 - created_at (
datetime
, 可選) — 倉庫在 Hub 上的建立日期。請注意,最低值為 `2022-03-02T23:29:04.000Z`,對應於我們開始儲存建立日期的日期。 - last_modified (
datetime
, 可選) — 倉庫的最後一次提交日期。 - private (
bool
) — 倉庫是否為私有。 - disabled (
bool
, 可選) — 倉庫是否被停用。 - gated (
Literal["auto", "manual", False]
, 可選) — 倉庫是否有準入限制。如果是,是手動批准還是自動批准。 - downloads (
int
) — 資料集在過去 30 天內的下載次數。 - downloads_all_time (
int
) — 模型自建立以來的累計下載次數。 - likes (
int
) — 資料集的點贊數。 - tags (
List[str]
) — 資料集的標籤列表。 - card_data (
DatasetCardData
, 可選) — 作為 huggingface_hub.repocard_data.DatasetCardData 物件的模型卡元資料。 - siblings (
List[RepoSibling]
) — 構成資料集的 huggingface_hub.hf_api.RepoSibling 物件列表。 - paperswithcode_id (
str
, 可選) — 資料集的 Papers with code ID。 - trending_score (
int
, optional) — 資料集的熱度得分。
包含 Hub 上資料集的資訊。此物件由 dataset_info() 和 list_datasets() 返回。
此類別的大多數屬性都是可選的。這是因為 Hub 返回的資料取決於所做的查詢。通常,查詢越具體,返回的資訊就越多。相反,當使用 list_datasets() 列出資料集時,只返回屬性的一個子集。
GitRefInfo
class huggingface_hub.GitRefInfo
< 原始碼 >( name: str ref: str target_commit: str )
包含 Hub 上倉庫的 git 引用資訊。
GitCommitInfo
class huggingface_hub.GitCommitInfo
< 原始碼 >( commit_id: str authors: List[str] created_at: datetime title: str message: str formatted_title: Optional[str] formatted_message: Optional[str] )
引數
- commit_id (
str
) — 提交的 OID(例如"e7da7f221d5bf496a48136c0cd264e630fe9fcc8"
)。 - authors (
List[str]
) — 提交的作者列表。 - created_at (
datetime
) — 提交的建立日期和時間。 - title (
str
) — 提交的標題。這是一個由作者輸入的自由文字值。 - message (
str
) — 提交的描述。這是一個由作者輸入的自由文字值。 - formatted_title (
str
) — 格式化為 HTML 的提交標題。僅在設定formatted=True
時返回。 - formatted_message (
str
) — 格式化為 HTML 的提交描述。僅在設定formatted=True
時返回。
包含 Hub 上倉庫的 git 提交資訊。更多詳情請檢視 list_repo_commits()。
GitRefs
class huggingface_hub.GitRefs
< 原始碼 >( branches: List[GitRefInfo] converts: List[GitRefInfo] tags: List[GitRefInfo] pull_requests: Optional[List[GitRefInfo]] = None )
引數
- branches (
List[GitRefInfo]
) — 一個包含倉庫分支資訊的 GitRefInfo 列表。 - converts (
List[GitRefInfo]
) — 一個包含倉庫“convert”引用資訊的 GitRefInfo 列表。Converts 是用於在資料集倉庫中推送預處理資料的引用(內部使用)。 - tags (
List[GitRefInfo]
) — 一個包含倉庫標籤資訊的 GitRefInfo 列表。 - pull_requests (
List[GitRefInfo]
, optional) — 一個包含倉庫拉取請求資訊的 GitRefInfo 列表。僅在設定include_prs=True
時返回。
包含 Hub 上倉庫的所有 git 引用資訊。
此物件由 list_repo_refs() 返回。
InferenceProviderMapping
LFSFileInfo
class huggingface_hub.hf_api.LFSFileInfo
< 原始碼 >( **kwargs )
包含有關 Hub 上倉庫中儲存為 LFS 的檔案的資訊。
用於列出和永久刪除倉庫中的 LFS 檔案以釋放空間。更多詳情請檢視 list_lfs_files() 和 permanently_delete_lfs_files()。
Git LFS 檔案使用 SHA-256 物件 ID 而非檔案路徑進行跟蹤,以最佳化效能。這種方法是必要的,因為單個物件可以被不同提交中的多個路徑引用,這使得搜尋和解析這些連線變得不切實際。請檢視我們的文件,瞭解如何知道每個 SHA 關聯的檔名。
示例
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> lfs_files = api.list_lfs_files("username/my-cool-repo")
# Filter files files to delete based on a combination of `filename`, `pushed_at`, `ref` or `size`.
# e.g. select only LFS files in the "checkpoints" folder
>>> lfs_files_to_delete = (lfs_file for lfs_file in lfs_files if lfs_file.filename.startswith("checkpoints/"))
# Permanently delete LFS files
>>> api.permanently_delete_lfs_files("username/my-cool-repo", lfs_files_to_delete)
ModelInfo
class huggingface_hub.ModelInfo
< 原始碼 >( **kwargs )
引數
- id (
str
) — 模型 ID。 - author (
str
, optional) — 模型的作者。 - sha (
str
, optional) — 在特定修訂版本下的倉庫 SHA。 - created_at (
datetime
, optional) — 在 Hub 上建立倉庫的日期。請注意,最小值是2022-03-02T23:29:04.000Z
,對應於我們開始儲存建立日期的日期。 - last_modified (
datetime
, optional) — 最後一次提交到倉庫的日期。 - private (
bool
) — 倉庫是否為私有。 - disabled (
bool
, optional) — 倉庫是否被停用。 - downloads (
int
) — 過去 30 天內模型的下載次數。 - downloads_all_time (
int
) — 模型自建立以來的累計下載次數。 - gated (
Literal["auto", "manual", False]
, optional) — 倉庫是否有訪問限制。如果是,是手動批准還是自動批准。 - gguf (
Dict
, optional) — 模型的 GGUF 資訊。 - inference (
Literal["warm"]
, optional) — 模型在推理提供商上的狀態。如果模型由至少一個提供商提供服務,則為 Warm。 - inference_provider_mapping (
List[InferenceProviderMapping]
, optional) — 一個按使用者提供商順序排列的InferenceProviderMapping
列表。 - likes (
int
) — 模型的點贊數。 - library_name (
str
, optional) — 與模型關聯的庫。 - tags (
List[str]
) — 模型的標籤列表。與card_data.tags
相比,包含由 Hub 計算的額外標籤(例如支援的庫、模型的 arXiv)。 - pipeline_tag (
str
, optional) — 與模型關聯的流水線標籤。 - mask_token (
str
, optional) — 模型使用的掩碼標記。 - widget_data (
Any
, optional) — 與模型關聯的小元件資料。 - model_index (
Dict
, optional) — 用於評估的模型索引。 - config (
Dict
, optional) — 模型配置。 - transformers_info (
TransformersInfo
, optional) — 與模型關聯的 Transformers 特定資訊(自動類、處理器等)。 - trending_score (
int
, optional) — 模型的熱度得分。 - card_data (
ModelCardData
, optional) — 模型卡元資料,作為 huggingface_hub.repocard_data.ModelCardData 物件。 - siblings (
List[RepoSibling]
) — 構成模型的 huggingface_hub.hf_api.RepoSibling 物件列表。 - spaces (
List[str]
, 可選) — 使用該模型的 Space 列表。 - safetensors (
SafeTensorsInfo
, 可選) — 模型的 safetensors 資訊。 - security_repo_status (
Dict
, 可選) — 模型的安全掃描狀態。
包含有關 Hub 上模型的資訊。此物件由 model_info() 和 list_models() 返回。
此類別的大多數屬性都是可選的。這是因為 Hub 返回的資料取決於所做的查詢。通常,查詢越具體,返回的資訊就越多。相反,當使用 list_models() 列出模型時,僅返回屬性的子集。
RepoSibling
class huggingface_hub.hf_api.RepoSibling
< 原始碼 >( rfilename: str size: Optional[int] = None blob_id: Optional[str] = None lfs: Optional[BlobLfsInfo] = None )
引數
- rfilename (str) — 檔名,相對於倉庫根目錄。
- size (
int
, 可選) — 檔案大小,以位元組為單位。當 repo_info() 的 `files_metadata` 引數設定為 `True` 時,此屬性被定義。否則為 `None`。 - blob_id (
str
, 可選) — 檔案的 git OID。當 repo_info() 的 `files_metadata` 引數設定為 `True` 時,此屬性被定義。否則為 `None`。 - lfs (
BlobLfsInfo
, 可選) — 檔案的 LFS 元資料。當 repo_info() 的 `files_metadata` 引數設定為 `True` 且檔案使用 Git LFS 儲存時,此屬性被定義。否則為 `None`。
包含有關 Hub 上倉庫中檔案的基本資訊。
除了 `rfilename` 外,此類的所有屬性都是可選的。這是因為在 Hub 上列出倉庫時(使用 list_models()、list_datasets() 或 list_spaces()),只返回檔名。如果您需要更多資訊,如檔案大小、blob id 或 lfs 詳細資訊,您必須一次從一個倉庫中專門請求它們(使用 model_info()、dataset_info() 或 space_info()),因為這會給後端伺服器檢索這些資訊帶來更多約束。
RepoFile
class huggingface_hub.hf_api.RepoFile
< 原始碼 >( **kwargs )
引數
- path (str) — 相對於倉庫根目錄的檔案路徑。
- size (
int
) — 檔案大小,以位元組為單位。 - blob_id (
str
) — 檔案的 git OID。 - lfs (
BlobLfsInfo
) — 檔案的 LFS 元資料。 - last_commit (
LastCommitInfo
, 可選) — 檔案的最後一次提交元資料。僅當使用 `expand=True` 呼叫 list_repo_tree() 和 get_paths_info() 時定義。 - security (
BlobSecurityInfo
, 可選) — 檔案的安全掃描元資料。僅當使用 `expand=True` 呼叫 list_repo_tree() 和 get_paths_info() 時定義。
包含有關 Hub 上檔案的資訊。
RepoUrl
class huggingface_hub.RepoUrl
< 原始碼 >( url: Any endpoint: Optional[str] = None )
引數
- url (
Any
) — 倉庫 URL 的字串值。 - endpoint (
str
, 可選) — Hub 的端點。預設為 https://huggingface.co。
引發
ValueError
ValueError
— 如果 URL 無法解析。ValueError
— 如果 `repo_type` 未知。
str
的子類,描述 Hub 上的倉庫 URL。
`RepoUrl` 由 `HfApi.create_repo` 返回。為了向後相容,它繼承自 `str`。在初始化時,會解析 URL 以填充屬性
- endpoint (`str`)
- namespace (`Optional[str]`)
- repo_name (`str`)
- repo_id (`str`)
- repo_type (`Literal["model", "dataset", "space"]`)
- url (`str`)
示例
>>> RepoUrl('https://huggingface.co/gpt2')
RepoUrl('https://huggingface.co/gpt2', endpoint='https://huggingface.co', repo_type='model', repo_id='gpt2')
>>> RepoUrl('https://hub-ci.huggingface.co/datasets/dummy_user/dummy_dataset', endpoint='https://hub-ci.huggingface.co')
RepoUrl('https://hub-ci.huggingface.co/datasets/dummy_user/dummy_dataset', endpoint='https://hub-ci.huggingface.co', repo_type='dataset', repo_id='dummy_user/dummy_dataset')
>>> RepoUrl('hf://datasets/my-user/my-dataset')
RepoUrl('hf://datasets/my-user/my-dataset', endpoint='https://huggingface.co', repo_type='dataset', repo_id='user/dataset')
>>> HfApi.create_repo("dummy_model")
RepoUrl('https://huggingface.co/Wauplin/dummy_model', endpoint='https://huggingface.co', repo_type='model', repo_id='Wauplin/dummy_model')
SafetensorsRepoMetadata
class huggingface_hub.utils.SafetensorsRepoMetadata
< 原始碼 >( metadata: typing.Optional[typing.Dict] sharded: bool weight_map: typing.Dict[str, str] files_metadata: typing.Dict[str, huggingface_hub.utils._safetensors.SafetensorsFileMetadata] )
引數
- metadata (
Dict
, 可選) — 包含在 ‘model.safetensors.index.json’ 檔案中的元資料,如果該檔案存在。僅對分片模型填充。 - sharded (
bool
) — 倉庫是否包含分片模型。 - weight_map (
Dict[str, str]
) — 所有權重的對映。鍵是張量名稱,值是包含這些張量的檔案的檔名。 - files_metadata (
Dict[str, SafetensorsFileMetadata]
) — 所有檔案元資料的對映。鍵是檔名,值是相應檔案的元資料,作為 `SafetensorsFileMetadata` 物件。 - parameter_count (
Dict[str, int]
) — 每種資料型別的引數數量對映。鍵是資料型別,值是該資料型別的引數數量。
Safetensors 倉庫的元資料。
如果一個倉庫的根目錄下包含 ‘model.safetensors’ 權重檔案(非共享模型)或 ‘model.safetensors.index.json’ 索引檔案(分片模型),則該倉庫被視為 Safetensors 倉庫。
此類由 get_safetensors_metadata() 返回。
有關 safetensors 格式的更多詳細資訊,請檢視 https://huggingface.co/docs/safetensors/index#format。
SafetensorsFileMetadata
class huggingface_hub.utils.SafetensorsFileMetadata
< 原始碼 >( metadata: typing.Dict[str, str] tensors: typing.Dict[str, huggingface_hub.utils._safetensors.TensorInfo] )
託管在 Hub 上的 Safetensors 檔案的元資料。
此類由 parse_safetensors_file_metadata() 返回。
有關 safetensors 格式的更多詳細資訊,請檢視 https://huggingface.co/docs/safetensors/index#format。
SpaceInfo
class huggingface_hub.SpaceInfo
< 原始碼 >( **kwargs )
引數
- id (
str
) — Space 的 ID。 - author (
str
, 可選) — Space 的作者。 - sha (
str
, 可選) — 倉庫在此特定修訂版的 SHA。 - created_at (
datetime
, 可選) — 倉庫在 Hub 上建立的日期。請注意,最低值為 `2022-03-02T23:29:04.000Z`,對應於我們開始儲存建立日期的日期。 - last_modified (
datetime
, 可選) — 對倉庫的最後一次提交日期。 - private (
bool
) — 倉庫是否為私有。 - gated (
Literal["auto", "manual", False]
, 可選) — 倉庫是否受限。如果是,是手動批准還是自動批准。 - disabled (
bool
, 可選) — Space 是否被停用。 - host (
str
, 可選) — Space 的主機 URL。 - subdomain (
str
, 可選) — Space 的子域。 - likes (
int
) — Space 的點贊數。 - tags (
List[str]
) — Space 的標籤列表。 - siblings (
List[RepoSibling]
) — 構成 Space 的 huggingface_hub.hf_api.RepoSibling 物件列表。 - card_data (
SpaceCardData
, 可選) — Space 卡片元資料,作為 huggingface_hub.repocard_data.SpaceCardData 物件。 - runtime (
SpaceRuntime
, 可選) — Space 執行時資訊,作為 huggingface_hub.hf_api.SpaceRuntime 物件。 - sdk (
str
, 可選) — Space 使用的 SDK。 - models (
List[str]
, 可選) — Space 使用的模型列表。 - datasets (
List[str]
, 可選) — Space 使用的資料集列表。 - trending_score (
int
, 可選) — Space 的熱門分數。
包含有關 Hub 上 Space 的資訊。此物件由 space_info() 和 list_spaces() 返回。
此類別的大多數屬性都是可選的。這是因為 Hub 返回的資料取決於所做的查詢。通常,查詢越具體,返回的資訊就越多。相反,當使用 list_spaces() 列出 Space 時,僅返回屬性的子集。
TensorInfo
class huggingface_hub.utils.TensorInfo
< 原始碼 >( dtype: typing.Literal['F64', 'F32', 'F16', 'BF16', 'I64', 'I32', 'I16', 'I8', 'U8', 'BOOL'] shape: typing.List[int] data_offsets: typing.Tuple[int, int] )
關於張量的資訊。
有關 safetensors 格式的更多詳細資訊,請檢視 https://huggingface.co/docs/safetensors/index#format。
User
class huggingface_hub.User
< source >( **kwargs )
引數
- username (
str
) — 使用者在 Hub 上的名稱(唯一)。 - fullname (
str
) — 使用者的全名。 - avatar_url (
str
) — 使用者頭像的 URL。 - details (
str
, optional) — 使用者的詳細資訊。 - is_following (
bool
, optional) — 認證使用者是否正在關注此使用者。 - is_pro (
bool
, optional) — 使用者是否是專業版使用者。 - num_models (
int
, optional) — 使用者建立的模型數量。 - num_datasets (
int
, optional) — 使用者建立的資料集數量。 - num_spaces (
int
, optional) — 使用者建立的 Spaces 數量。 - num_discussions (
int
, optional) — 使用者發起的討論數量。 - num_papers (
int
, optional) — 使用者撰寫的論文數量。 - num_upvotes (
int
, optional) — 使用者收到的點贊數。 - num_likes (
int
, optional) — 使用者給出的點贊數量。 - num_following (
int
, optional) — 該使用者正在關注的使用者數量。 - num_followers (
int
, optional) — 關注該使用者的使用者數量。 - orgs (
Organization
的列表) — 使用者所屬組織的列表。
包含有關 Hub 上使用者的資訊。
UserLikes
class huggingface_hub.UserLikes
< source >( user: str total: int datasets: List[str] models: List[str] spaces: List[str] )
包含有關使用者在 Hub 上點讚的資訊。
WebhookInfo
class huggingface_hub.WebhookInfo
< source >( id: str url: str watched: List[WebhookWatchedItem] domains: List[constants.WEBHOOK_DOMAIN_T] secret: Optional[str] disabled: bool )
引數
- id (
str
) — Webhook 的 ID。 - url (
str
) — Webhook 的 URL。 - watched (
List[WebhookWatchedItem]
) — Webhook 監視的專案列表,請參閱 WebhookWatchedItem。 - domains (
List[WEBHOOK_DOMAIN_T]
) — Webhook 正在監視的域列表。可以是["repo", "discussions"]
中的一個。 - secret (
str
, optional) — Webhook 的金鑰。 - disabled (
bool
) — Webhook 是否被停用。
包含有關 Webhook 資訊的資料結構。
WebhookWatchedItem
class huggingface_hub.WebhookWatchedItem
< source >( type: Literal['dataset', 'model', 'org', 'space', 'user'] name: str )
包含有關 Webhook 監視的專案資訊的資料結構。
CommitOperation
以下是 `CommitOperation()` 支援的值
class huggingface_hub.CommitOperationAdd
< source >( path_in_repo: str path_or_fileobj: typing.Union[str, pathlib.Path, bytes, typing.BinaryIO] )
引數
- path_in_repo (
str
) — 倉庫中的相對檔案路徑,例如:"checkpoints/1fec34a/weights.bin"
- path_or_fileobj (
str
,Path
,bytes
, orBinaryIO
) — 以下任一:- 要上傳的本地檔案的路徑(作為 `str` 或 `pathlib.Path`)
- 儲存要上傳檔案內容的位元組緩衝區(`bytes`)
- 一個“檔案物件”(`io.BufferedIOBase` 的子類),通常透過 `open(path, "rb")` 獲取。它必須支援 `seek()` 和 `tell()` 方法。
引發
ValueError
ValueError
— 如果 `path_or_fileobj` 不是 `str`、`Path`、`bytes` 或 `io.BufferedIOBase` 中的一種。ValueError
— 如果 `path_or_fileobj` 是一個 `str` 或 `Path` 但不是一個已存在檔案的路徑。ValueError
— 如果 `path_or_fileobj` 是一個 `io.BufferedIOBase` 但它不同時支援 `seek()` 和 `tell()`。
包含將檔案上傳到 Hub 倉庫所需資訊的資料結構。
as_file
< source >( with_tqdm: bool = False )
一個上下文管理器,它產生一個類檔案物件,允許讀取 `path_or_fileobj` 後面的底層資料。
示例
>>> operation = CommitOperationAdd(
... path_in_repo="remote/dir/weights.h5",
... path_or_fileobj="./local/weights.h5",
... )
CommitOperationAdd(path_in_repo='remote/dir/weights.h5', path_or_fileobj='./local/weights.h5')
>>> with operation.as_file() as file:
... content = file.read()
>>> with operation.as_file(with_tqdm=True) as file:
... while True:
... data = file.read(1024)
... if not data:
... break
config.json: 100%|█████████████████████████| 8.19k/8.19k [00:02<00:00, 3.72kB/s]
>>> with operation.as_file(with_tqdm=True) as file:
... requests.put(..., data=file)
config.json: 100%|█████████████████████████| 8.19k/8.19k [00:02<00:00, 3.72kB/s]
class huggingface_hub.CommitOperationDelete
< source >( path_in_repo: str is_folder: typing.Union[bool, typing.Literal['auto']] = 'auto' )
包含從 Hub 倉庫中刪除檔案或資料夾所需資訊的資料結構。
class huggingface_hub.CommitOperationCopy
< source >( src_path_in_repo: str path_in_repo: str src_revision: typing.Optional[str] = None _src_oid: typing.Optional[str] = None _dest_oid: typing.Optional[str] = None )
包含在 Hub 倉庫中複製檔案所需資訊的資料結構。
侷限性
- 只有 LFS 檔案可以被複制。要複製常規檔案,您需要先將其下載到本地再重新上傳。
- 不支援跨倉庫複製。
注意:您可以組合使用 CommitOperationCopy 和 CommitOperationDelete 在 Hub 上重新命名 LFS 檔案。
CommitScheduler
class huggingface_hub.CommitScheduler
< source >( repo_id: str folder_path: typing.Union[str, pathlib.Path] every: typing.Union[int, float] = 5 path_in_repo: typing.Optional[str] = None repo_type: typing.Optional[str] = None revision: typing.Optional[str] = None private: typing.Optional[bool] = None token: typing.Optional[str] = None allow_patterns: typing.Union[typing.List[str], str, NoneType] = None ignore_patterns: typing.Union[typing.List[str], str, NoneType] = None squash_history: bool = False hf_api: typing.Optional[ForwardRef('HfApi')] = None )
引數
- repo_id (
str
) — 要提交到的倉庫的 ID。 - folder_path (
str
或Path
) — 需要定期上傳的本地資料夾路徑。 - every (
int
或float
, 可選) — 每次提交之間間隔的分鐘數。預設為 5 分鐘。 - path_in_repo (
str
, 可選) — 倉庫中目錄的相對路徑,例如:"checkpoints/"
。預設為倉庫的根資料夾。 - repo_type (
str
, 可選) — 要提交到的倉庫型別。預設為model
。 - revision (
str
, 可選) — 要提交到的倉庫的版本。預設為main
。 - private (
bool
, 可選) — 是否將倉庫設為私有。如果為None
(預設值),倉庫將是公開的,除非組織的預設設定是私有。如果倉庫已存在,此值將被忽略。 - token (
str
, 可選) — 用於提交到倉庫的令牌。預設為儲存在本地計算機上的令牌。 - allow_patterns (
List[str]
或str
, 可選) — 如果提供,則只上傳至少匹配一個模式的檔案。 - ignore_patterns (
List[str]
或str
, 可選) — 如果提供,則不上傳任何匹配模式的檔案。 - squash_history (
bool
, 可選) — 是否在每次提交後壓縮倉庫的歷史記錄。預設為False
。當倉庫變得過大時,壓縮提交歷史有助於避免效能下降。 - hf_api (
HfApi
, 可選) — 用於向 Hub 提交的 HfApi 客戶端。可以設定自定義配置(使用者代理、令牌等)。
用於定期將本地資料夾上傳到 Hub 的排程器(例如,每 5 分鐘推送到 Hub)。
推薦使用排程器作為上下文管理器。這可以確保排程器在指令碼結束時被正確停止並觸發最後一次提交。排程器也可以使用 stop
方法手動停止。請檢視上傳指南以瞭解更多關於如何使用它的資訊。
示例
>>> from pathlib import Path
>>> from huggingface_hub import CommitScheduler
# Scheduler uploads every 10 minutes
>>> csv_path = Path("watched_folder/data.csv")
>>> CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path=csv_path.parent, every=10)
>>> with csv_path.open("a") as f:
... f.write("first line")
# Some time later (...)
>>> with csv_path.open("a") as f:
... f.write("second line")
使用上下文管理器的示例
>>> from pathlib import Path
>>> from huggingface_hub import CommitScheduler
>>> with CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path="watched_folder", every=10) as scheduler:
... csv_path = Path("watched_folder/data.csv")
... with csv_path.open("a") as f:
... f.write("first line")
... (...)
... with csv_path.open("a") as f:
... f.write("second line")
# Scheduler is now stopped and last commit have been triggered
將資料夾推送到 Hub 並返回提交資訊。
此方法不應直接呼叫。它由排程器在後臺執行,並遵循佇列機制以避免併發提交。直接呼叫此方法可能導致併發問題。
push_to_hub
的預設行為是假設資料夾為只追加模式。它會列出資料夾中的所有檔案,並只上傳已更改的檔案。如果未發現更改,該方法將返回而不進行任何提交。如果你想改變這種行為,可以繼承 CommitScheduler 並重寫此方法。例如,這在提交前將資料壓縮到單個檔案中會很有用。更多細節和示例,請檢視我們的整合指南。
觸發一個 push_to_hub
並返回一個 future 物件。
此方法每隔 every
分鐘自動呼叫一次。你也可以手動呼叫它來立即觸發一次提交,而無需等待下一次計劃的提交。