This repository implements a custom task for zero-shot-image-classification for ? Inference Endpoints. The code for the customized pipeline is in the pipeline.py .
To use deploy this model a an Inference Endpoint you have to select Custom as task to use the pipeline.py file. -> double check if it is selected
{ "image": "/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAMCAgICAgMC....", // base64 image as bytes "candiates":["sea","palace","car","ship"] }
below is an example on how to run a request using Python and requests .
!wget https://huggingface.co/datasets/mishig/sample_images/resolve/main/palace.jpg
import json from typing import List import requests as r import base64 ENDPOINT_URL = "" HF_TOKEN = "" def predict(path_to_image: str = None, candiates: List[str] = None): with open(path_to_image, "rb") as i: b64 = base64.b64encode(i.read()) payload = {"inputs": {"image": b64.decode("utf-8"), "candiates": candiates}} response = r.post( ENDPOINT_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"}, json=payload ) return response.json() prediction = predict( path_to_image="palace.jpg", candiates=["sea", "palace", "car", "ship"] )
expected output
[{'label': 'palace', 'score': 0.9996134638786316}, {'label': 'car', 'score': 0.0002602009626571089}, {'label': 'ship', 'score': 0.00011758189066313207}, {'label': 'sea', 'score': 8.666840585647151e-06}]
该仓库实现了?推理端点的零射击图像分类任务的自定义任务。定制的 pipeline 代码位于 pipeline.py 中。
要使用部署此模型作为推理端点,您必须选择 Custom 作为任务,并使用 pipeline.py 文件。-> 请确认是否已选择。
{ "image": "/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAMCAgICAgMC....", // base64 image as bytes "candiates":["sea","palace","car","ship"] }
以下是使用 Python 和 requests 运行请求的示例。
!wget https://huggingface.co/datasets/mishig/sample_images/resolve/main/palace.jpg
import json from typing import List import requests as r import base64 ENDPOINT_URL = "" HF_TOKEN = "" def predict(path_to_image: str = None, candiates: List[str] = None): with open(path_to_image, "rb") as i: b64 = base64.b64encode(i.read()) payload = {"inputs": {"image": b64.decode("utf-8"), "candiates": candiates}} response = r.post( ENDPOINT_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"}, json=payload ) return response.json() prediction = predict( path_to_image="palace.jpg", candiates=["sea", "palace", "car", "ship"] )
预期的输出
[{'label': 'palace', 'score': 0.9996134638786316}, {'label': 'car', 'score': 0.0002602009626571089}, {'label': 'ship', 'score': 0.00011758189066313207}, {'label': 'sea', 'score': 8.666840585647151e-06}]