Hub Python 库文档

推理端点

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

推理端点

推理端点提供了一个安全的生产解决方案,可以在 Hugging Face 管理的专用和自动扩展基础设施上轻松部署模型。推理端点是从 Hub 中的模型构建的。此页面是 huggingface_hub 与推理端点集成的参考文档。有关推理端点产品的更多信息,请查看其官方文档

查看相关指南,了解如何以编程方式使用 huggingface_hub 管理您的推理端点。

推理端点可以通过 API 完全管理。这些端点的文档使用 Swagger 编写。InferenceEndpoint 类是构建在此 API 之上的一个简单包装器。

方法

推理端点功能的子集在 HfApi 中实现

InferenceEndpoint

主要的 dataclass 是 InferenceEndpoint。它包含有关已部署 InferenceEndpoint 的信息,包括其配置和当前状态。部署后,您可以使用 InferenceEndpoint.clientInferenceEndpoint.async_client 属性在端点上运行推理,它们分别返回 InferenceClientAsyncInferenceClient 对象。

class huggingface_hub.InferenceEndpoint

< >

( namespace: str raw: typing.Dict _token: typing.Union[str, bool, NoneType] _api: HfApi )

参数

  • name (str) — 推理端点的唯一名称。
  • namespace (str) — 推理端点所在的命名空间。
  • repository (str) — 在此推理端点上部署的模型仓库的名称。
  • status (InferenceEndpointStatus) — 推理端点的当前状态。
  • url (str, 可选) — 推理端点的 URL(如果可用)。只有已部署的推理端点才会有 URL。
  • framework (str) — 用于模型的机器学习框架。
  • revision (str) — 推理端点上部署的特定模型修订版本。
  • task (str) — 与已部署模型关联的任务。
  • created_at (datetime.datetime) — 推理端点创建的时间戳。
  • updated_at (datetime.datetime) — 推理端点上次更新的时间戳。
  • type (InferenceEndpointType) — 推理端点的类型(公共、受保护、私有)。
  • raw (Dict) — 从 API 返回的原始字典数据。
  • token (strbool, 可选) — 推理端点的身份验证令牌,如果在请求 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: typing.Dict namespace: str token: typing.Union[str, bool, NoneType] = None api: typing.Optional[ForwardRef('HfApi')] = None )

从原始字典初始化对象。

client

< >

( ) InferenceClient

返回

InferenceClient

指向已部署端点的推理客户端。

抛出

InferenceEndpointError

返回一个客户端,用于在此推理端点上进行预测。

async_client

< >

( ) AsyncInferenceClient

返回

AsyncInferenceClient

一个与 asyncio 兼容的推理客户端,指向已部署的端点。

抛出

InferenceEndpointError

返回一个客户端,用于在此推理端点上进行预测。

删除

< >

( )

删除推理端点。

此操作不可逆。如果您不想为推理端点付费,最好使用 InferenceEndpoint.pause() 暂停它,或者使用 InferenceEndpoint.scale_to_zero() 将其缩放至零。

这是 HfApi.delete_inference_endpoint() 的别名。

获取

< >

( ) InferenceEndpoint

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

获取关于 Inference Endpoint 的最新信息。

暂停

< >

( ) InferenceEndpoint

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

暂停推理端点。

暂停的推理端点将不会收费。可以使用 InferenceEndpoint.resume() 随时恢复。这与使用 InferenceEndpoint.scale_to_zero() 将推理端点缩放至零不同,后者会在收到请求时自动重启。

这是 HfApi.pause_inference_endpoint() 的别名。当前对象会使用来自服务器的最新数据就地更新。

恢复

< >

( running_ok: bool = True ) InferenceEndpoint

参数

  • running_ok (bool, 可选) — 如果为 True,则当推理端点已在运行时,此方法不会引发错误。默认为 True

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

恢复推理端点。

这是 HfApi.resume_inference_endpoint() 的别名。当前对象会使用来自服务器的最新数据就地更新。

缩放至零

< >

( ) InferenceEndpoint

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

将推理端点缩放至零。

缩放至零的推理端点将不会收费。它将在收到下一个请求时恢复,但会有冷启动延迟。这与使用 InferenceEndpoint.pause() 暂停推理端点不同,后者需要使用 InferenceEndpoint.resume() 手动恢复。

这是 HfApi.scale_to_zero_inference_endpoint() 的别名。当前对象会使用来自服务器的最新数据就地更新。

更新

< >

( 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[typing.Dict] = None secrets: typing.Optional[typing.Dict[str, str]] = None ) InferenceEndpoint

参数

  • accelerator (str, 可选) — 用于推理的硬件加速器 (例如 "cpu")。
  • instance_size (str, 可选) — 用于托管模型实例的大小或类型 (例如 "x4")。
  • instance_type (str, 可选) — 将部署推理端点的云实例类型 (例如 "intel-icl")。
  • min_replica (int, 可选) — 为推理端点保持运行的最小副本(实例)数。
  • max_replica (int, 可选) — 推理端点要扩展到的最大副本(实例)数。
  • scale_to_zero_timeout (int, 可选) — 不活动端点在缩放至零之前的持续时间(分钟)。
  • repository (str, 可选) — 与推理端点关联的模型仓库的名称 (例如 "gpt2")。
  • framework (str, 可选) — 用于模型的机器学习框架 (例如 "custom")。
  • revision (str, 可选) — 要在推理端点上部署的特定模型修订版本 (例如 "6c0e6080953db56375760c0471a8c5f2929baf11")。
  • task (str, 可选) — 要在其上部署模型的任务 (例如 "text-classification")。
  • custom_image (Dict, 可选) — 用于推理端点的自定义 Docker 镜像。如果您想部署在 text-generation-inference (TGI) 框架上运行的推理端点,这将非常有用(请参阅示例)。
  • secrets (Dict[str, str], 可选) — 要注入到容器环境中的密钥值。

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

更新推理端点。

此方法允许更新计算配置、已部署的模型或两者。所有参数都是可选的,但必须至少提供一个。

这是 HfApi.update_inference_endpoint() 的别名。当前对象会使用来自服务器的最新数据就地更新。

等待

< >

( timeout: typing.Optional[int] = None refresh_every: int = 5 ) InferenceEndpoint

参数

  • timeout (int, 可选) — 等待推理端点部署的最大时间,以秒为单位。如果为 None,将无限期等待。
  • refresh_every (int, 可选) — 每次获取推理端点状态之间等待的时间,以秒为单位。默认为 5 秒。

返回

InferenceEndpoint

同一个 Inference Endpoint,使用最新数据就地更新。

抛出

InferenceEndpointErrorInferenceEndpointTimeoutError

  • InferenceEndpointError — 如果推理端点最终处于失败状态。
  • InferenceEndpointTimeoutError — 如果推理端点在 timeout 秒后仍未部署。

等待推理端点完成部署。

服务器的信息将每 1 秒获取一次。如果推理端点在 timeout 秒后仍未部署,将引发 InferenceEndpointTimeoutError 异常。InferenceEndpoint 将会使用最新的数据进行原地修改。

InferenceEndpointStatus

class huggingface_hub.InferenceEndpointStatus

< >

( value names = None module = None qualname = None type = None start = 1 )

枚举。

InferenceEndpointType

class huggingface_hub.InferenceEndpointType

< >

( value names = None module = None qualname = None type = None start = 1 )

枚举。

InferenceEndpointError

class huggingface_hub.InferenceEndpointError

< >

( )

处理推理端点时发生的通用异常。

< > GitHub 上更新