File size: 1,124 Bytes
ae0228d
 
 
 
 
 
 
 
 
 
 
 
 
 
8852310
ae0228d
 
 
 
8852310
ae0228d
e7656af
ae0228d
e7656af
ae0228d
d793007
0e7cf0c
ae0228d
 
d793007
ae0228d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import Dict, List, Any
from PIL import Image
from io import BytesIO
from transformers import pipeline
import base64


class EndpointHandler():
    def __init__(self, path=""):
        self.pipeline=pipeline("zero-shot-image-classification",model="openai/clip-vit-large-patch14-336")

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        """
       data args:
            inputs (:obj:`string`)
            parameters (:obj:)
      Return:
            A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
        """
        image_data = data.pop("inputs", data)
        # decode base64 image to PIL
        image = Image.open(BytesIO(base64.b64decode(image_data)))

        parameters = data.pop("parameters", data)
        candidate_labels = parameters['candidate_labels']

        candidate_labels_array = list(map(str.strip, candidate_labels.split(',')))

        # run prediction one image wit provided candiates
        prediction = self.pipeline(images=[image], candidate_labels=candidate_labels_array)
        return prediction[0]