Hub Python 庫文件

檔案系統 API

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

檔案系統 API

HfFileSystem 類提供了一個基於 fsspec 的 Hugging Face Hub 的 Pythonic 檔案介面。

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.4.0/zh/package_reference/hf_file_system#huggingface_hub.HfFileSystem) 提供 fsspec 相容性,這對於需要它的庫(例如,直接使用 pandas 讀取 Hugging Face 資料集)很有用。但是,由於這個相容層,它會引入額外的開銷。為了獲得更好的效能和可靠性,建議在可能的情況下使用 HfApi 方法。

用法

>>> 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)

指定一個令牌進行身份驗證

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

cp_file

< >

( path1: str path2: str revision: typing.Optional[str] = None **kwargs )

引數

  • path1 (str) — 源路徑,用於複製。
  • path2 (str) — 目標路徑,用於複製。
  • revision (str, 可選) — 要從中複製的 git 版本。

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

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

exists

< >

( path **kwargs ) bool

引數

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

返回

布林值

如果檔案存在則為 True,否則為 False。

檢查檔案是否存在。

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

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

find

< >

( path: str maxdepth: typing.Optional[int] = None withdirs: bool = False detail: bool = False refresh: bool = False revision: typing.Optional[str] = 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 0x7ff1eb9ec700> outfile = None **kwargs )

引數

  • rpath (str) — 要從中下載的遠端路徑。
  • lpath (str) — 要下載到的本地路徑。
  • callback (Callback, optional) — 用於跟蹤下載進度的可選回撥。預設為無回撥。
  • outfile (IO, optional) — 要寫入的類檔案物件。如果提供,則忽略 lpath

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

注意:儘可能使用 HfApi.hf_hub_download() 以獲得更好的效能。

glob

< >

( path: str maxdepth: typing.Optional[int] = None **kwargs ) list[str]

引數

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

返回

list[str]

匹配模式的路徑列表。

透過 glob 匹配查詢檔案。

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

info

< >

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

引數

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

返回

dict[str, Any]

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

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

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

注意:儘可能使用 HfApi.get_paths_info()HfApi.repo_info() 以獲得更好的效能。

invalidate_cache

< >

( path: typing.Optional[str] = None )

引數

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

清除給定路徑的快取。

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

isdir

< >

( path ) bool

引數

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

返回

布林值

如果 path 是目錄,則返回 True,否則返回 False。

檢查路徑是否為目錄。

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

isfile

< >

( path ) bool

引數

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

返回

布林值

如果 path 是檔案,則返回 True,否則返回 False。

檢查路徑是否為檔案。

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

ls

< >

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

引數

  • path (str) — 目錄的路徑。
  • detail (bool, optional) — 如果為 True,則返回包含檔案資訊的字典列表。如果為 False,則返回檔案路徑列表。預設為 True。
  • refresh (bool, optional) — 如果為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, optional) — 要列出內容的 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: typing.Optional[str] = None ) HfFileSystemResolvedPath

引數

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

返回

HfFileSystemResolvedPath

已解析的路徑資訊,包含 repo_typerepo_idrevisionpath_in_repo

引發

ValueErrorNotImplementedError

  • ValueError — 如果路徑包含衝突的修訂資訊。
  • NotImplementedError — 如果嘗試列出倉庫。

解析 Hugging Face 檔案系統路徑到其元件。

rm

< >

( path: str recursive: bool = False maxdepth: typing.Optional[int] = None revision: typing.Optional[str] = None **kwargs )

引數

  • path (str) — 要刪除的路徑。
  • recursive (bool, optional) — 如果為 True,則刪除目錄及其所有內容。預設為 False。
  • maxdepth (int, optional) — 遞迴刪除時要訪問的子目錄的最大數量。
  • revision (str, optional) — 要從中刪除的 git 版本。

從倉庫中刪除檔案。

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

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

url

< >

( path: str ) str

引數

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

返回

字串

用於在 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.