Create handler.py
Browse files- handler.py +30 -0
handler.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import pipeline
|
3 |
+
import transformers
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
print('TRANSFORMERS VERSION')
|
9 |
+
print(transformers.__version__)
|
10 |
+
|
11 |
+
class EndpointHandler():
|
12 |
+
def __init__(self, path=""):
|
13 |
+
# load pipe
|
14 |
+
self.pipe = pipeline(task="depth-estimation", model=path)
|
15 |
+
|
16 |
+
|
17 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
+
base64_image = data.pop("inputs",data)
|
19 |
+
if base64_image is None:
|
20 |
+
raise ValueError("No image provided")
|
21 |
+
|
22 |
+
if base64_image.startswith('data:image/jpeg;base64,'):
|
23 |
+
base64_image = base64_image.replace('data:image/jpeg;base64,', '')
|
24 |
+
|
25 |
+
image_bytes = base64.b64decode(base64_image)
|
26 |
+
image = Image.open(BytesIO(image_bytes))
|
27 |
+
|
28 |
+
depth = self.pipe(image)["depth"]
|
29 |
+
|
30 |
+
return depth
|