Upload handler.py
Browse files- handler.py +19 -26
handler.py
CHANGED
@@ -1,34 +1,27 @@
|
|
1 |
-
from typing import
|
2 |
-
from
|
3 |
-
from
|
|
|
|
|
4 |
|
5 |
|
6 |
class EndpointHandler():
|
7 |
def __init__(self, path=""):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# create inference pipeline
|
12 |
-
self.pipeline = pipeline("zero-shot-image-classification", model=model, tokenizer=tokenizer)
|
13 |
-
|
14 |
-
|
15 |
-
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
16 |
"""
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
A :obj:`list`:. The
|
22 |
-
- "label": A string representing what the label/class is. There can be multiple labels.
|
23 |
-
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
24 |
"""
|
25 |
inputs = data.pop("inputs", data)
|
26 |
-
parameters = data.pop("parameters", None)
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
return prediction
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
from transformers import pipeline
|
5 |
+
import base64
|
6 |
|
7 |
|
8 |
class 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 PIL
|
23 |
+
image = Image.open(BytesIO(base64.b64decode(inputs['image'])))
|
24 |
+
|
25 |
+
# run prediction one image wit provided candiates
|
26 |
+
prediction = self.pipeline(images=[image], candidate_labels=inputs["candiates"])
|
27 |
+
return prediction[0]
|
|