Hub Python 库文档
管理本地和在线存储库
并获得增强的文档体验
开始使用
管理本地和在线存储库
Repository
类是一个包装 git
和 git-lfs
命令的辅助类。它提供了适用于管理可能非常大的存储库的工具。
一旦涉及任何 git
操作,或者当协作将成为存储库本身的重点时,它是推荐的工具。
Repository 类
class huggingface_hub.Repository
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
用于包装 git 和 git-lfs 命令的辅助类。
目的是方便与 huggingface.co 托管的模型或数据集仓库进行交互,尽管这里(如果有的话)并没有多少是 huggingface.co 特有的。
Repository 已弃用,取而代之的是 HfApi 中实现的基于 HTTP 的替代方案。鉴于其在旧代码中的广泛采用,Repository 的完全移除只会在 v1.0
版本中进行。有关更多详细信息,请阅读 https://huggingface.co/docs/huggingface_hub/concepts/git_vs_http。
__init__
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
参数
- local_dir (
str
或Path
) — 本地目录的路径(例如'my_trained_model/'
),Repository
将在此处初始化。 - clone_from (
str
, 可选) — 仓库 URL 或repo_id
。示例:"https://huggingface.co/philschmid/playground-tests"
"philschmid/playground-tests"
- repo_type (
str
, 可选) — 从 repo_id 克隆仓库时设置。默认为模型。 - token (
bool
或str
, 可选) — 有效的身份验证令牌(请参阅 https://huggingface.co/settings/token)。如果为None
或True
且机器已登录(通过hf auth login
或 login()),则将从缓存中检索令牌。如果为False
,则请求头中不发送令牌。 - git_user (
str
, 可选) — 将覆盖git config user.name
,用于提交和推送文件到 Hub。 - git_email (
str
, 可选) — 将覆盖git config user.email
,用于提交和推送文件到 Hub。 - revision (
str
, 可选) — 初始化仓库后要检出的修订版本。如果修订版本不存在,将从默认分支的当前 HEAD 创建一个具有该修订版本名称的分支。 - skip_lfs_files (
bool
, 可选, 默认为False
) — 是否跳过 git-LFS 文件。 - client (
HfApi
, 可选) — 调用 HF Hub API 时使用的 HfApi 实例。如果保留为None
,将创建一个新实例。
引发
EnvironmentError
EnvironmentError
— 如果clone_from
中设置的远程仓库不存在。
实例化 git 仓库的本地克隆。
如果设置了 clone_from
,仓库将从现有远程仓库克隆。如果远程仓库不存在,将抛出 EnvironmentError
异常。请先使用 create_repo() 创建远程仓库。
Repository
默认使用本地 git 凭据。如果明确设置,将使用 token
或 git_user
/git_email
对。
返回当前检出的分支。
add_tag
< 源 >( tag_name: str message: typing.Optional[str] = None remote: typing.Optional[str] = None )
在当前 HEAD 添加标签并推送
如果 remote 为 None,则只会在本地更新
如果没有提供消息,标签将是轻量级的。如果提供了消息,标签将是带注释的。
auto_track_binary_files
< 源 >( pattern: str = '.' ) → List[str]
使用 git-lfs 自动跟踪二进制文件。
auto_track_large_files
< 源 >( pattern: str = '.' ) → List[str]
使用 git-lfs 自动跟踪大文件(超过 10MB 的文件)。
检查 git
和 git-lfs
是否可运行。
clone_from
< 源 >( repo_url: str token: typing.Union[bool, str, NoneType] = None )
从远程仓库克隆。如果文件夹已存在,将尝试在其内部克隆仓库。
如果此文件夹是带有链接历史记录的 git 仓库,将尝试更新该仓库。
引发以下错误
ValueError
如果传递了组织令牌(以“api_org”开头)。必须使用您自己的个人访问令牌(请参阅 https://huggingface.co/settings/tokens)。EnvironmentError
如果您尝试将仓库克隆到非空文件夹中,或者如果git
操作引发错误。
commit
< 源 >( commit_message: str branch: typing.Optional[str] = None track_large_files: bool = True blocking: bool = True auto_lfs_prune: bool = False )
上下文管理器实用工具,用于处理对仓库的提交。它会自动使用 git-lfs 跟踪大文件(>10Mb)。如果您希望忽略此行为,请将 track_large_files
参数设置为 False
。
示例
>>> with Repository(
... "text-files",
... clone_from="<user>/text-files",
... token=True,
>>> ).commit("My first file :)"):
... with open("file.txt", "w+") as f:
... f.write(json.dumps({"hey": 8}))
>>> import torch
>>> model = torch.nn.Transformer()
>>> with Repository(
... "torch-model",
... clone_from="<user>/torch-model",
... token=True,
>>> ).commit("My cool model :)"):
... torch.save(model.state_dict(), "model.pt")
delete_tag
< 源 >( tag_name: str remote: typing.Optional[str] = None ) → bool
如果标签存在,则删除本地和远程标签
git_add
< 源 >( pattern: str = '.' auto_lfs_track: bool = False )
git add
将 auto_lfs_track
参数设置为 True
将自动使用 git-lfs
跟踪大于 10MB 的文件。
git_checkout
< 源 >( revision: str create_branch_ok: bool = False )
git 检出给定修订版本
将 create_branch_ok
指定为 True
将在给定修订版本不存在的情况下创建该修订版本的分支。
git_commit
< 源 >( commit_message: str = 'commit files to HF hub' )
git commit
git_config_username_and_email
< 源 >( git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None )
设置 git 用户名和电子邮件(仅在当前存储库中)。
将 git 凭证助手设置为 store
获取 HEAD 上最后一次提交的 URL。我们假设它已被推送,并且 URL 方案与 GitHub 或 HuggingFace 的相同。
获取 HEAD 顶部的提交 SHA。
git_pull
< 源 >( rebase: bool = False lfs: bool = False )
git pull
git_push
< 源 >( upstream: typing.Optional[str] = None blocking: bool = True auto_lfs_prune: bool = False )
git push
如果使用时未设置 blocking
,将返回远程仓库上提交的 URL。如果使用 blocking=True
,将返回一个元组,其中包含提交的 URL 和用于获取进程信息的命令对象。
获取源远程的 URL。
返回 git 状态是否干净
HF 特有。这支持上传大于 5GB 的文件。
lfs_prune
< 源 >( recent = False )
参数
- recent (
bool
, 可选, 默认为False
) — 是否即使文件被最近的提交引用也修剪它们。有关更多信息,请参阅以下 链接。
git lfs prune
lfs_track
< 源 >( patterns: typing.Union[str, typing.List[str]] filename: bool = False )
告诉 git-lfs 根据模式跟踪文件。
将 filename
参数设置为 True
将把参数视为字面文件名,而不是模式。文件名中的任何特殊全局字符在写入 .gitattributes
文件时都将被转义。
lfs_untrack
< 源 >( patterns: typing.Union[str, typing.List[str]] )
告诉 git-lfs 取消跟踪这些文件。
返回工作目录或索引中已删除的文件列表。
push_to_hub
< 源 >( commit_message: str = 'commit files to HF hub' blocking: bool = True clean_ok: bool = True auto_lfs_prune: bool = False )
用于将文件添加到远程存储库并提交和推送的助手,位于 HuggingFace Hub 上。将自动跟踪大型文件(>10MB)。
tag_exists
< 源 >( tag_name: str remote: typing.Optional[str] = None ) → bool
检查标签是否存在。
阻塞方法:阻塞所有后续执行,直到所有命令都已处理。
帮助方法
huggingface_hub.repository.is_git_repo
< 源 >( folder: typing.Union[str, pathlib.Path] ) → bool
检查文件夹是否为 git 存储库的根目录或一部分
huggingface_hub.repository.is_local_clone
< 源 >( folder: typing.Union[str, pathlib.Path] remote_url: str ) → bool
检查文件夹是否是 remote_url 的本地克隆
huggingface_hub.repository.is_tracked_with_lfs
< 源 >( filename: typing.Union[str, pathlib.Path] ) → bool
检查传入的文件是否使用 git-lfs 跟踪。
huggingface_hub.repository.is_git_ignored
< 源 >( filename: typing.Union[str, pathlib.Path] ) → bool
检查文件是否被 git 忽略。支持嵌套的 .gitignore 文件。
huggingface_hub.repository.files_to_be_staged
< 源 >( pattern: str = '.' folder: typing.Union[str, pathlib.Path, NoneType] = None ) → List[str]
返回要暂存的文件名列表。
huggingface_hub.repository.is_tracked_upstream
< 来源 >( folder: typing.Union[str, pathlib.Path] ) → bool
检查当前签出的分支是否被上游跟踪。
huggingface_hub.repository.commits_to_push
< 来源 >( folder: typing.Union[str, pathlib.Path] upstream: typing.Optional[str] = None ) → int
检查将被推送到上游的提交数量
用于比较的上游仓库的名称。
跟踪异步命令
Repository
工具提供了几个可以异步启动的方法
git_push
git_pull
push_to_hub
commit
上下文管理器
有关管理此类异步实用程序的信息,请参阅下文。
class huggingface_hub.Repository
< 源 >( local_dir: typing.Union[str, pathlib.Path] clone_from: typing.Optional[str] = None repo_type: typing.Optional[str] = None token: typing.Union[bool, str] = True git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None revision: typing.Optional[str] = None skip_lfs_files: bool = False client: typing.Optional[huggingface_hub.hf_api.HfApi] = None )
用于包装 git 和 git-lfs 命令的辅助类。
目的是方便与 huggingface.co 托管的模型或数据集仓库进行交互,尽管这里(如果有的话)并没有多少是 huggingface.co 特有的。
Repository 已弃用,取而代之的是 HfApi 中实现的基于 HTTP 的替代方案。鉴于其在旧代码中的广泛采用,Repository 的完全移除只会在 v1.0
版本中进行。有关更多详细信息,请阅读 https://huggingface.co/docs/huggingface_hub/concepts/git_vs_http。
返回失败的异步命令。
返回当前正在进行的异步命令。
阻塞方法:阻塞所有后续执行,直到所有命令都已处理。
class huggingface_hub.repository.CommandInProgress
< 来源 >( title: str is_done_method: typing.Callable status_method: typing.Callable process: Popen post_method: typing.Optional[typing.Callable] = None )
用于跟踪异步启动命令的实用程序。