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
處理推理端點時發生的通用異常。