philschmid's picture
Create new file
58d0174
raw
history blame
853 Bytes
from typing import Dict, List, Any
from PIL import Image
from io import BytesIO
from transformers import pipeline
class PreTrainedPipeline():
def __init__(self, path=""):
self.pipeline=pipeline("zero-shot-image-classification",model=path)
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
images (:obj:`PIL.Image`)
candiates (:obj:`list`)
Return:
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
"""
# decode base64 image to PIL
image = Image.open(BytesIO(base64.b64decode(data['image'])))
# run prediction one image wit provided candiates
prediction = self.pipeline(images=[image], candidate_labels=data["candiates"])
return prediction[0]