Hub Python Library documentation
迁移到 huggingface_hub v2.0
快速入门
操作指南
概念指南
参考
迁移到 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 迁移计划。