Hub Python 库文档
推理端点
并获得增强的文档体验
开始使用
推理端点
推理端点提供了一个安全的生产解决方案,可以轻松地将模型部署到 Hugging Face 管理的专用且自动扩展的基础设施上。推理端点是根据来自 Hub 的模型构建的。此页面是 huggingface_hub 与推理端点集成的参考。有关推理端点产品的更多信息,请查看其 官方文档。
查看相关指南,了解如何使用
huggingface_hub以编程方式管理您的推理端点。
推理端点可以通过 API 完全管理。端点使用 Swagger 进行文档化。huggingface_hub 中的 InferenceEndpoint 类是一个基于此 API 构建的简单包装器。
方法
推理端点功能的子集已在 HfApi 中实现
- get_inference_endpoint() 和 list_inference_endpoints() 用于获取有关您的推理端点的信息
- create_inference_endpoint()、update_inference_endpoint() 和 delete_inference_endpoint() 用于部署和管理推理端点
- pause_inference_endpoint() 和 resume_inference_endpoint() 用于暂停和恢复推理端点
- scale_to_zero_inference_endpoint() 用于手动将端点缩减到 0 个副本
InferenceEndpoint
主要的类是 InferenceEndpoint。它包含已部署的 InferenceEndpoint 的信息,包括其配置和当前状态。部署后,您可以使用 InferenceEndpoint.client 和 InferenceEndpoint.async_client 属性分别返回 InferenceClient 和 AsyncInferenceClient 对象来运行端点上的推理。
class huggingface_hub.InferenceEndpoint
< 来源 >( namespace: str raw: dict _token: typing.Union[str, bool, NoneType] _api: HfApi )
参数
- name (
str) — 推理端点的唯一名称。 - namespace (
str) — 推理端点所在的命名空间。 - repository (
str) — 在此推理端点上部署的模型名称。 - status (InferenceEndpointStatus) — 推理端点的当前状态。
- url (
str, optional) — 推理端点的 URL,如果可用。仅已部署的推理端点才有 URL。 - framework (
str) — 用于模型的机器学习框架。 - revision (
str) — 在推理端点上部署的具体模型修订版本。 - task (
str) — 与已部署模型相关的任务。 - created_at (
datetime.datetime) — 推理端点创建的时间戳。 - updated_at (
datetime.datetime) — 推理端点最后更新的时间戳。 - type (InferenceEndpointType) — 推理端点的类型(公共、受保护、私有)。
- raw (
dict) — 从 API 返回的原始字典数据。 - token (
strorbool, optional) — 用于推理端点的认证令牌,在请求 API 时设置。如果未提供,则默认为本地保存的令牌。如果不想将令牌发送到服务器,请传递token=False。
包含已部署的推理端点的信息。
示例
>>> from huggingface_hub import get_inference_endpoint
>>> endpoint = get_inference_endpoint("my-text-to-image")
>>> endpoint
InferenceEndpoint(name='my-text-to-image', ...)
# Get status
>>> endpoint.status
'running'
>>> endpoint.url
'https://my-text-to-image.region.vendor.endpoints.huggingface.cloud'
# Run inference
>>> endpoint.client.text_to_image(...)
# Pause endpoint to save $$$
>>> endpoint.pause()
# ...
# Resume and wait for deployment
>>> endpoint.resume()
>>> endpoint.wait()
>>> endpoint.client.text_to_image(...)from_raw
< 来源 >( raw: dict namespace: str token: typing.Union[str, bool, NoneType] = None api: typing.Optional[ForwardRef('HfApi')] = None )
从原始字典初始化对象。
client
< 来源 >( ) → InferenceClient
返回一个客户端,用于在此推理端点上进行预测。
async_client
< 来源 >( ) → AsyncInferenceClient
返回一个客户端,用于在此推理端点上进行预测。
删除推理端点。
此操作不可逆。如果您不想为推理端点付费,建议使用 InferenceEndpoint.pause() 暂停它,或使用 InferenceEndpoint.scale_to_zero() 将其缩减到零。
获取人脸识别终端的最新信息。
暂停人脸识别终端。
暂停的人脸识别终端将不会被收费。可以使用 InferenceEndpoint.resume() 随时恢复。这与使用 InferenceEndpoint.scale_to_zero() 将人脸识别终端缩减到零不同,后者会在收到请求时自动重启。
这是 HfApi.pause_inference_endpoint() 的别名。当前对象会从服务器获取最新数据并原地更新。
恢复人脸识别终端。
这是 HfApi.resume_inference_endpoint() 的别名。当前对象会从服务器获取最新数据并原地更新。
将人脸识别终端缩减到零。
缩减到零的人脸识别终端将不会被收费。下次收到请求时,它会以冷启动延迟恢复。这与使用 InferenceEndpoint.pause() 暂停人脸识别终端不同,后者需要使用 InferenceEndpoint.resume() 手动恢复。
这是 HfApi.scale_to_zero_inference_endpoint() 的别名。当前对象会从服务器获取最新数据并原地更新。
update
< source >( accelerator: typing.Optional[str] = None instance_size: typing.Optional[str] = None instance_type: typing.Optional[str] = None min_replica: typing.Optional[int] = None max_replica: typing.Optional[int] = None scale_to_zero_timeout: typing.Optional[int] = None repository: typing.Optional[str] = None framework: typing.Optional[str] = None revision: typing.Optional[str] = None task: typing.Optional[str] = None custom_image: typing.Optional[dict] = None secrets: typing.Optional[dict[str, str]] = None ) → InferenceEndpoint
参数
- accelerator (
str, optional) — 用于推理的硬件加速器(例如"cpu")。 - instance_size (
str, optional) — 用于托管模型的实例的大小或类型(例如"x4")。 - instance_type (
str, optional) — 将部署人脸识别终端的云实例类型(例如"intel-icl")。 - min_replica (
int, optional) — 要为人脸识别终端保留的最小副本(实例)数。 - max_replica (
int, optional) — 人脸识别终端要扩展到的最大副本(实例)数。 - scale_to_zero_timeout (
int, optional) — 不活动终端缩减到零之前的持续时间(分钟)。 - repository (
str, optional) — 与人脸识别终端关联的模型存储库名称(例如"gpt2")。 - framework (
str, optional) — 用于模型的机器学习框架(例如"custom")。 - revision (
str, optional) — 要在人脸识别终端上部署的具体模型修订版(例如"6c0e6080953db56375760c0471a8c5f2929baf11")。 - task (
str, optional) — 部署模型的任务(例如"text-classification")。 - custom_image (
dict, optional) — 用于人脸识别终端的自定义 Docker 镜像。如果你想部署运行text-generation-inference(TGI) 框架的人脸识别终端,这会很有用(参见示例)。 - secrets (
dict[str, str], optional) — 注入到容器环境中的秘密值。
同一个人脸识别终端,原地更新为最新数据。
更新人脸识别终端。
此方法允许更新计算配置、部署的模型或两者。所有参数都是可选的,但必须至少提供一个。
这是 HfApi.update_inference_endpoint() 的别名。当前对象会从服务器获取最新数据并原地更新。
wait
< source >( timeout: typing.Optional[int] = None refresh_every: int = 5 ) → InferenceEndpoint
参数
- timeout (
int, optional) — 等待人脸识别终端部署的最长时间(秒)。如果为None,则无限等待。 - refresh_every (
int, optional) — 每次获取人脸识别终端状态之间等待的时间(秒)。默认为 5 秒。
同一个人脸识别终端,原地更新为最新数据。
引发
InferenceEndpointError 或 InferenceEndpointTimeoutError
- InferenceEndpointError — 如果人脸识别终端最终处于失败状态。
InferenceEndpointTimeoutError— 如果人脸识别终端在timeout秒后仍未部署。
等待人脸识别终端部署。
服务器信息将每 1 秒获取一次。如果在 timeout 秒后人脸识别终端仍未部署,将引发 InferenceEndpointTimeoutError。 InferenceEndpoint 将在原地进行变异,以反映最新的数据。
InferenceEndpointStatus
class huggingface_hub.InferenceEndpointStatus
< source >( value names = None module = None qualname = None type = None start = 1 )
一个枚举。
InferenceEndpointType
class huggingface_hub.InferenceEndpointType
< source >( value names = None module = None qualname = None type = None start = 1 )
一个枚举。
InferenceEndpointError
处理推理端点时发生的通用异常。