philschmid/clip-zero-shot-image-classification
1643
1from typing import Dict, List, Any2from PIL import Image3from io import BytesIO4from transformers import pipeline5import base646 7 8class EndpointHandler():9 def __init__(self, path=""):10 self.pipeline=pipeline("zero-shot-image-classification",model=path)11 12 def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:13 """14 data args:15 images (:obj:`string`)16 candiates (:obj:`list`)17 Return:18 A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}19 """20 inputs = data.pop("inputs", data)21 22 # decode base64 image to PIL23 image = Image.open(BytesIO(base64.b64decode(inputs['image'])))24 25 # run prediction one image wit provided candiates26 prediction = self.pipeline(images=[image], candidate_labels=inputs["candiates"])27 return prediction[0]