Hub Python Library documentation

迁移到 huggingface_hub v2.0

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

迁移到 huggingface_hub v2.0

huggingface_hub v2.0 将其 HTTP 依赖项替换为 httpx2,并移除了已弃用的 1.x API。HTTP 客户端和异常现在来自一个独立的包。

为了同时支持 huggingface_hub v1.x(从 v1.30.0 开始)和 v2.x,请从 huggingface_hub.utils 导入 HTTP 模块:

from huggingface_hub.utils import get_session, httpx

try:
    response = get_session().get("https://huggingface.co/api/models/gpt2")
    response.raise_for_status()
except httpx.HTTPError:
    ...

在为 set_client_factory() 或 set_async_client_factory() 创建自定义客户端时,请使用相同的导入。旧 HTTP 包中的客户端和异常与 httpx2 中的不同,不能互换使用。

httpx2 默认使用操作系统的证书信任存储。通过 SSL_CERT_FILE 或 SSL_CERT_DIR 配置的自定义 CA 证书包仍然受支持。日志配置应针对 httpx2 和 httpcore2,而不是旧的日志记录器名称。有关详细信息,请参阅 上游迁移指南。

oauth 附加组件现在需要 authlib>=1.8.0 以支持 httpx2。

移除了已弃用的 API

  • 请使用 upload_folder 代替 upload_large_folder 或 HfApi.upload_large_folder。
  • 请使用 duplicate_repo、set_space_volumes 和 delete_space_volumes 来替代 duplicate_space、request_space_storage 和 delete_space_storage。
  • 请使用 parse_hf_uri 代替 repo_type_and_id_from_hf_id,并使用 list_models(search=...) 代替 model_name=...。
  • 在 create_repo 和 duplicate_repo 中,请使用 space_volumes 代替 space_storage。
  • 请使用 InferenceEndpointType.AUTHENTICATED 或 type="authenticated" 代替 PROTECTED 或 type="protected"。
  • 请使用 text_generation(stop=...) 代替 stop_sequences=...,并向 InferenceClient 传递一个 token 字符串或 None,而不是布尔值。
  • 请使用 hf 代替 huggingface-cli;hf repo 和 hf repo-files delete 已被 hf repos 和 hf repos delete-files 取代。
  • 请使用 hf upload 代替 hf upload-large-folder;在创建和复制仓库时,请使用 --volume 代替 --storage,并使用 --status 或 --label 代替 hf jobs ps --filter。
  • hf skills add 和 hf skills update 上已弃用的 --claude 选项已被移除。技能会自动为 Claude Code 安装。

有关更多背景信息,请参阅 httpx2 项目、httpx2 文档、上游的 从 httpx 到 httpx2 的迁移指南,以及 huggingface_hub 迁移计划。

Update on GitHub