Hub Python 库文档
文件系统 API
并获得增强的文档体验
开始
文件系统 API
HfFileSystem
类基于 fsspec
为 Hugging Face Hub 提供了一个 pythonic 文件接口。
HfFileSystem
HfFileSystem
基于 fsspec,因此它与 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()
以获得更好的性能。
exists
< 源代码 >( path **kwargs ) → bool
检查文件是否存在。
更多详情,请参阅 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
, optional) — 要从中列出的 git 修订版本(可选)。
返回值
Union[List[str], Dict[str, Dict[str, Any]]]
路径列表或文件信息字典。
列出路径下的所有文件。
有关更多详细信息,请参阅 fsspec 文档。
get_file
< source >( rpath lpath callback = <fsspec.callbacks.NoOpCallback object at 0x7f61cb9b2740> outfile = None **kwargs )
将单个远程文件复制到本地。
注意:如果可能,请使用 HfApi.hf_hub_download()
以获得更好的性能。
info
< source >( path: str refresh: bool = False revision: typing.Optional[str] = None **kwargs ) → Dict[str, Any]
获取有关文件或目录的信息。
有关更多详细信息,请参阅 fsspec 文档。
注意:如果可能,请使用 HfApi.get_paths_info()
或 HfApi.repo_info()
以获得更好的性能。
invalidate_cache
< source >( path: typing.Optional[str] = None )
清除给定路径的缓存。
有关更多详细信息,请参阅 fsspec 文档。
检查路径是否为目录。
有关更多详细信息,请参阅 fsspec 文档。
检查路径是否为文件。
有关更多详细信息,请参阅 fsspec 文档。
ls
< source >( 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
< source >( path: str revision: typing.Optional[str] = None ) → HfFileSystemResolvedPath
将 Hugging Face 文件系统路径解析为其组件。
rm
< source >( 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 文档。