Hub Python 庫文件
檔案系統 API
並獲得增強的文件體驗
開始使用
檔案系統 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 (
bool或str, 可選) — 一個有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 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)cp_file
< 原始碼 >( path1: str path2: str revision: typing.Optional[str] = None **kwargs )
在儲存庫內或儲存庫之間複製檔案。
注意:如果可能,請使用
HfApi.upload_file()以獲得更好的效能。
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 )
將單個遠端檔案複製到本地。
注意:儘可能使用
HfApi.hf_hub_download()以獲得更好的效能。
glob
< 源 >( path: str maxdepth: typing.Optional[int] = None **kwargs ) → list[str]
透過 glob 匹配查詢檔案。
有關更多詳細資訊,請參閱 fsspec 文件。
info
< 源 >( path: str refresh: bool = False revision: typing.Optional[str] = None **kwargs ) → dict[str, Any]
獲取有關檔案或目錄的資訊。
有關更多詳細資訊,請參閱 fsspec 文件。
注意:儘可能使用
HfApi.get_paths_info()或HfApi.repo_info()以獲得更好的效能。
檢查路徑是否為目錄。
有關更多詳細資訊,請參閱 fsspec 文件。
檢查路徑是否為檔案。
有關更多詳細資訊,請參閱 fsspec 文件。
ls
< 源 >( path: str detail: bool = True refresh: bool = False revision: typing.Optional[str] = None **kwargs ) → list[Union[str, dict[str, Any]]]
列出目錄的內容。
有關更多詳細資訊,請參閱 fsspec 文件。
注意:儘可能使用
HfApi.list_repo_tree()以獲得更好的效能。
獲取檔案的最後修改時間。
有關更多詳細資訊,請參閱 fsspec 文件。
resolve_path
< 源 >( path: str revision: typing.Optional[str] = None ) → HfFileSystemResolvedPath
解析 Hugging Face 檔案系統路徑到其元件。
rm
< 源 >( path: str recursive: bool = False maxdepth: typing.Optional[int] = None revision: typing.Optional[str] = None **kwargs )
從倉庫中刪除檔案。
有關更多詳細資訊,請參閱 fsspec 文件。
注意:如果可能,請使用
HfApi.delete_file()以獲得更好的效能。
獲取給定路徑的 HTTP URL。
walk
< source >( path: str *args **kwargs ) → Iterator[tuple[str, list[str], list[str]]]
返回給定路徑下的所有檔案。
有關更多詳細資訊,請參閱 fsspec 文件。