Hub Python 库文档
管理本地和在线仓库
并获得增强的文档体验
开始使用
管理本地和在线仓库
Repository
类是一个辅助类,它封装了 git
和 git-lfs
命令。它提供了适用于管理可能非常大的仓库的工具。
只要涉及到任何 git
操作,或者当协作将成为仓库本身的重点时,它是推荐的工具。
Repository 类
class huggingface_hub.Repository
< source >( 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__
< source >( 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 克隆仓库时设置。默认为 model。 - token (
bool
或str
, 可选) — 有效的身份验证令牌(请参阅 https://huggingface.co/settings/token)。如果为None
或True
且机器已登录(通过huggingface-cli 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
< source >( tag_name: str message: typing.Optional[str] = None remote: typing.Optional[str] = None )
在当前头部添加标签并推送它
如果 remote 为 None,则仅在本地更新
如果没有提供消息,标签将是轻量级的。如果提供了消息,标签将是带注释的。
auto_track_binary_files
< source >( pattern: str = '.' ) → List[str]
使用 git-lfs 自动追踪二进制文件。
auto_track_large_files
< source >( pattern: str = '.' ) → List[str]
使用 git-lfs 自动追踪大文件(重量超过 10MB 的文件)。
检查 git
和 git-lfs
是否可以运行。
clone_from
< source >( repo_url: str token: typing.Union[bool, str, NoneType] = None )
从远程克隆。如果文件夹已存在,将尝试在其中克隆仓库。
如果此文件夹是具有链接历史记录的 git 仓库,将尝试更新仓库。
抛出以下错误
ValueError
如果传递了组织令牌(以“api_org”开头)。必须使用您自己的个人访问令牌(请参阅 https://huggingface.co/settings/tokens)。EnvironmentError
如果您尝试在非空文件夹中克隆仓库,或者git
操作引发错误。
commit
< source >( 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
< source >( tag_name: str remote: typing.Optional[str] = None ) → bool
如果标签存在,则删除本地和远程标签
git_add
< source >( pattern: str = '.' auto_lfs_track: bool = False )
git add
将 auto_lfs_track
参数设置为 True
将自动使用 git-lfs
追踪大于 10MB 的文件。
git_checkout
< source >( revision: str create_branch_ok: bool = False )
git checkout 给定版本
如果指定 create_branch_ok
为 True
,如果该版本不存在,则会创建到给定版本的分支。
git_commit
< source >( commit_message: str = 'commit files to HF hub' )
git commit
git_config_username_and_email
< source >( git_user: typing.Optional[str] = None git_email: typing.Optional[str] = None )
设置 git 用户名和电子邮件(仅在当前仓库中)。
将 git 凭据助手设置为 store
获取 HEAD 上最后一次提交的 URL。我们假设它已被推送,并且 URL 方案与 GitHub 或 HuggingFace 的方案相同。
获取 HEAD 顶部的提交 SHA。
git_pull
< source >( rebase: bool = False lfs: bool = False )
git pull
git_push
< source >( upstream: typing.Optional[str] = None blocking: bool = True auto_lfs_prune: bool = False )
git push
如果使用时不设置 blocking
,将返回远程仓库上提交的 URL。 如果使用 blocking=True
,将返回一个元组,其中包含提交的 URL 和命令对象,以便跟踪有关该过程的信息。
获取 origin 远程仓库的 URL。
返回 git 状态是否干净
HF 专用。 这启用了大于 5GB 的文件上传支持。
lfs_prune
< source >( recent = False )
参数
- recent (
bool
, optional, defaults toFalse
) — 是否即使文件被最近的提交引用也修剪文件。 有关更多信息,请参见以下链接。
git lfs prune
lfs_track
< source >( patterns: typing.Union[str, typing.List[str]] filename: bool = False )
告诉 git-lfs 根据模式跟踪文件。
将 filename
参数设置为 True
会将参数视为文字文件名,而不是模式。 写入 .gitattributes
文件时,文件名中的任何特殊 glob 字符都将被转义。
lfs_untrack
< source >( patterns: typing.Union[str, typing.List[str]] )
告诉 git-lfs 取消跟踪这些文件。
返回工作目录或索引中已删除的文件列表。
push_to_hub
< source >( 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
< source >( tag_name: str remote: typing.Optional[str] = None ) → bool
检查标签是否存在。
阻塞方法:阻塞所有后续执行,直到所有命令都已处理完毕。
辅助方法
huggingface_hub.repository.is_git_repo
< source >( folder: typing.Union[str, pathlib.Path] ) → bool
检查文件夹是否是 git 仓库的根目录或一部分
huggingface_hub.repository.is_local_clone
< source >( folder: typing.Union[str, pathlib.Path] remote_url: str ) → bool
检查文件夹是否是 remote_url 的本地克隆
huggingface_hub.repository.is_tracked_with_lfs
< source >( filename: typing.Union[str, pathlib.Path] ) → bool
检查传递的文件是否通过 git-lfs 跟踪。
huggingface_hub.repository.is_git_ignored
< source >( filename: typing.Union[str, pathlib.Path] ) → bool
检查文件是否被 git 忽略。支持嵌套的 .gitignore 文件。
huggingface_hub.repository.files_to_be_staged
< source >( pattern: str = '.' folder: typing.Union[str, pathlib.Path, NoneType] = None ) → List[str]
返回要暂存的文件名列表。
huggingface_hub.repository.is_tracked_upstream
< source >( folder: typing.Union[str, pathlib.Path] ) → bool
检查当前检出的分支是否已在上游跟踪。
huggingface_hub.repository.commits_to_push
< source >( 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
< source >( 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
< source >( title: str is_done_method: typing.Callable status_method: typing.Callable process: Popen post_method: typing.Optional[typing.Callable] = None )
用于跟踪异步启动的命令的实用工具。