Thomasboosinger
commited on
Commit
•
9b99880
1
Parent(s):
634e34a
Create handler.py
Browse files- handler.py +22 -0
handler.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
class EndpointHandler():
|
4 |
+
def __init__(self, path=""):
|
5 |
+
self.pipeline = pipeline(task="zero-shot-object-detection",model=path, device = 0 ) #device = 0 to use GPU rather than -1 which would be CPU
|
6 |
+
|
7 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
8 |
+
"""
|
9 |
+
data args:
|
10 |
+
images (:obj:`string`)
|
11 |
+
candiates (:obj:`list`)
|
12 |
+
Return:
|
13 |
+
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
|
14 |
+
"""
|
15 |
+
inputs = data.pop("inputs", data)
|
16 |
+
|
17 |
+
# decode base64 image to PIL
|
18 |
+
image = Image.open(BytesIO(base64.b64decode(inputs['image'])))
|
19 |
+
|
20 |
+
# run prediction one image wit provided candiates
|
21 |
+
detector = self.pipeline(images=[image], candidate_labels=inputs["candiates"])
|
22 |
+
return detector
|