Hub Python 函式庫文件

Filesystem API

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Filesystem API

HfFileSystem 類別提供了一個基於 fsspec 的 Hugging Face Hub Python 風格檔案介面。

HfFileSystem

HfFileSystem 是基於 fsspec 建構的,因此它與該函式庫提供的大多數 API 相容。欲了解更多詳細資訊,請參閱我們的指南以及 fsspec 的 API 參考

class huggingface_hub.HfFileSystem

< >

( *args **kwargs )

參數

  • endpoint (str, 選填) — Hub 的端點。預設為 https://huggingface.co
  • token (boolstr, 選填) — 有效的使用者存取權杖(字串)。預設為本地儲存的權杖,這是推薦的身份驗證方法(請參閱 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。若要停用身份驗證,請傳入 False
  • block_size (int, 選填) — 讀取與寫入檔案的區塊大小。
  • expand_info (bool, 選填) — 是否展開檔案資訊。
  • **storage_options (dict, 選填) — 檔案系統的額外選項。請參閱 fsspec 文件

像存取本地檔案系統一樣存取遠端的 Hugging Face Hub 儲存庫。

[!WARNING][HfFileSystem](/docs/huggingface_hub/v1.16.2/en/package_reference/hf_file_system#huggingface_hub.HfFileSystem) 提供了 fsspec 相容性,這對於需要它的程式庫(例如直接使用 pandas 讀取 Hugging Face 資料集)很有用。然而,由於此相容層,它會帶來額外的開銷。為了獲得更好的效能與可靠性,建議在可能的情況下使用 HfApi 方法。

該檔案系統支援 hf:// 協定的路徑,遵循以下 URL 架構:

  • 模型、資料集與 Spaces 儲存庫
hf://<repo-id>[@<revision>]/<path/in/repo>
hf://datasets/<repo-id>[@<revision>]/<path/in/repo>
hf://spaces/<repo-id>[@<revision>]/<path/in/repo>
  • 儲存桶 (通用儲存)
hf://buckets/<bucket-id>/<path/in/bucket>

注意:直接使用 HfFileSystem 時,路徑中的 hf:// 協定字首是選填的。

用法

>>> from huggingface_hub import hffs

>>> # List files
>>> hffs.glob("my-username/my-model/*.bin")
['my-username/my-model/pytorch_model.bin']
>>> hffs.ls("datasets/my-username/my-dataset", detail=False)
['datasets/my-username/my-dataset/.gitattributes', 'datasets/my-username/my-dataset/README.md', 'datasets/my-username/my-dataset/data.json']

>>> # Read/write files
>>> with hffs.open("my-username/my-model/pytorch_model.bin") as f:
...     data = f.read()
>>> with hffs.open("my-username/my-model/pytorch_model.bin", "wb") as f:
...     f.write(data)

指定身份驗證用的權杖 (token)

>>> from huggingface_hub import HfFileSystem
>>> hffs = HfFileSystem(token=token)

cp_file

< >

( path1: str path2: str revision: str | None = None **kwargs )

參數

  • path1 (str) — 要複製的來源路徑。
  • path2 (str) — 要複製到的目標路徑。
  • revision (str, 選填) — 要從中複製的 git 修訂版本 (revision)。

在儲存庫內或儲存庫之間複製檔案。

注意:在可能的情況下,請使用 HfApi.upload_file() 以獲得更好的效能。

exists

< >

( path **kwargs ) bool

參數

  • path (str) — 要檢查的路徑。

返回

bool

若檔案存在則為 True,否則為 False。

檢查檔案是否存在。

欲了解更多詳細資訊,請參閱 fsspec 文件

注意:在可能的情況下,請使用 HfApi.file_exists() 以獲得更好的效能。

find

< >

( path: str maxdepth: int | None = None withdirs: bool = False detail: bool = False refresh: bool = False revision: str | None = None **kwargs ) Union[list[str], dict[str, dict[str, Any]]]

參數

  • path (str) — 用於列出檔案的根路徑。
  • maxdepth (int, 選填) — 深入子目錄的最大深度。
  • withdirs (bool, 選填) — 在輸出中包含目錄路徑。預設為 False。
  • detail (bool, 選填) — 若為 True,則回傳一個將路徑映射至檔案資訊的字典。預設為 False。
  • refresh (bool, 選填) — 若為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 選填) — 要列出的 git 修訂版本。

返回

Union[list[str], dict[str, dict[str, Any]]]

路徑清單或檔案資訊字典。

列出路徑下的所有檔案。

欲了解更多詳細資訊,請參閱 fsspec 文件

get_file

< >

( rpath lpath callback = <fsspec.callbacks.NoOpCallback object at 0x7f3f1823ffa0> outfile = None **kwargs )

參數

  • rpath (str) — 要下載的遠端路徑。
  • lpath (str) — 要下載到的本地路徑。
  • callback (Callback, 選填) — 用於追蹤下載進度的選擇性回呼。預設為不使用回呼。
  • outfile (IO, 選填) — 用於寫入的選擇性檔案物件。若已提供,則 lpath 會被忽略。

將單一遠端檔案複製到本地。

注意:在可能的情況下,請使用 HfApi.hf_hub_download()HfApi.download_bucket_files 以獲得更好的效能。

glob

< >

( path: str maxdepth: int | None = None **kwargs ) list[str]

參數

  • path (str) — 要匹配的路徑模式。

返回

list[str]

符合模式的路徑清單。

透過 glob 匹配尋找檔案。

欲了解更多詳細資訊,請參閱 fsspec 文件

info

< >

( path: str refresh: bool = False revision: str | None = None **kwargs ) dict[str, Any]

參數

  • path (str) — 要取得資訊的路徑。
  • refresh (bool, 選填) — 若為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 選填) — 要從中取得資訊的 git 修訂版本。

返回

dict[str, Any]

包含檔案資訊(類型、大小、提交資訊等)的字典。

取得有關檔案或目錄的資訊。

欲了解更多詳細資訊,請參閱 fsspec 文件

注意:在可能的情況下,請使用 HfApi.get_paths_info()HfApi.repo_info() 以獲得更好的效能(或者針對儲存桶使用 HfApi.get_bucket_paths_info()HfApi.bucket_info())。

invalidate_cache

< >

( path: str | None = None )

參數

  • path (str, 選填) — 要從快取中清除的路徑。若未提供,則清除整個快取。

清除給定路徑的快取。

欲了解更多詳細資訊,請參閱 fsspec 文件

isdir

< >

( path ) bool

參數

  • path (str) — 要檢查的路徑。

返回

bool

若路徑是目錄則為 True,否則為 False。

檢查路徑是否為目錄。

欲了解更多詳細資訊,請參閱 fsspec 文件

isfile

< >

( path ) bool

參數

  • path (str) — 要檢查的路徑。

返回

bool

若路徑是檔案則為 True,否則為 False。

檢查路徑是否為檔案。

欲了解更多詳細資訊,請參閱 fsspec 文件

ls

< >

( path: str detail: bool = True refresh: bool = False revision: str | None = None **kwargs ) list[Union[str, dict[str, Any]]]

參數

  • path (str) — 目錄路徑。
  • detail (bool, 選填) — 若為 True,則回傳包含檔案資訊的字典清單。若為 False,則回傳檔案路徑清單。預設為 True。
  • refresh (bool, 選填) — 若為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 選填) — 要列出的 git 修訂版本。

返回

list[Union[str, dict[str, Any]]]

檔案路徑清單(若 detail=False)或檔案資訊字典清單(若 detail=True)。

列出目錄內容。

欲了解更多詳細資訊,請參閱 fsspec 文件

注意:在可能的情況下,請使用 HfApi.list_repo_tree() 以獲得更好的效能。

modified

< >

( path: str **kwargs ) datetime

參數

  • path (str) — 檔案路徑。

返回

datetime

檔案最後修改時間。

取得檔案的最後修改時間。

欲了解更多詳細資訊,請參閱 fsspec 文件

resolve_path

< >

( path: str revision: str | None = None ) HfFileSystemResolvedPath

參數

  • path (str) — 要解析的路徑。
  • revision (str, 選填) — 要解析的儲存庫修訂版本。預設為路徑中指定的修訂版本。

返回

HfFileSystemResolvedPath

包含 repo_typerepo_idrevisionpath_in_repo 的解析路徑資訊。

引發

ValueErrorNotImplementedError

  • ValueError — 若路徑包含衝突的修訂版本資訊。
  • NotImplementedError — 若嘗試列出儲存庫。

將 Hugging Face 檔案系統路徑解析為其組成部分。

rm

< >

( path: str recursive: bool = False maxdepth: int | None = None revision: str | None = None **kwargs )

參數

  • path (str) — 要刪除的路徑。
  • recursive (bool, 選填) — 若為 True,則刪除目錄及其所有內容。預設為 False。
  • maxdepth (int, 選填) — 遞迴刪除時要存取的最深子目錄層數。
  • revision (str, 選填) — 要從中刪除的 git 版本。

從儲存庫中刪除檔案。

更多詳細資訊,請參閱 fsspec 文件

注意:若可能,請使用 HfApi.delete_file() 以獲得更好的效能。

url

< >

( path: str ) str

參數

  • path (str) — 要取得 URL 的路徑。

返回

str

用於存取 Hub 上檔案或目錄的 HTTP URL。

取得給定路徑的 HTTP URL。

walk

< >

( path: str *args **kwargs ) Iterator[tuple[str, list[str], list[str]]]

參數

  • path (str) — 要列出檔案的根路徑。

返回

Iterator[tuple[str, list[str], list[str]]]

一個包含 (路徑, 目錄名稱列表, 檔案名稱列表) 元組的迭代器。

傳回給定路徑下的所有檔案。

更多詳細資訊,請參閱 fsspec 文件

在 GitHub 上更新

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