Hub Python 庫文件
檔案系統 API
並獲得增強的文件體驗
開始使用
檔案系統 API
HfFileSystem
類提供了一個基於 fsspec
的 Hugging Face Hub Python 檔案介面。
HfFileSystem
HfFileSystem
基於 fsspec,因此與它提供的大多數 API 相容。有關更多詳細資訊,請檢視我們的指南和 fsspec 的API 參考。
class huggingface_hub.HfFileSystem
< 源 >( *args **kwargs )
引數
- token (
str
或bool
, 可選) — 有效的使用者訪問令牌(字串)。預設為本地儲存的令牌,這是推薦的身份驗證方法(參見 https://huggingface.co/docs/huggingface_hub/quick-start#authentication)。要停用身份驗證,請傳遞False
。 - endpoint (
str
, 可選) — Hub 的端點。預設為 https://huggingface.co。
訪問遠端 Hugging Face Hub 倉庫,如同訪問本地檔案系統。
HfFileSystem 提供了 fsspec 相容性,這對於需要它的庫(例如,直接使用 pandas
讀取 Hugging Face 資料集)非常有用。但是,由於此相容層會引入額外的開銷。為了獲得更好的效能和可靠性,建議儘可能使用 HfApi
方法。
用法
>>> from huggingface_hub import HfFileSystem
>>> fs = HfFileSystem()
>>> # List files
>>> fs.glob("my-username/my-model/*.bin")
['my-username/my-model/pytorch_model.bin']
>>> fs.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 fs.open("my-username/my-model/pytorch_model.bin") as f:
... data = f.read()
>>> with fs.open("my-username/my-model/pytorch_model.bin", "wb") as f:
... f.write(data)
__init__
< 源 >( *args endpoint: typing.Optional[str] = None token: typing.Union[bool, str, NoneType] = None **storage_options )
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 0x7fd63b7cafb0> outfile = None **kwargs )
將單個遠端檔案複製到本地。
注意:如果可能,請使用 HfApi.hf_hub_download()
以獲得更好的效能。
透過 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。