Ahmed Essam
commited on
Update handler.py
Browse files- handler.py +37 -36
handler.py
CHANGED
|
@@ -1,36 +1,37 @@
|
|
| 1 |
-
from typing import Dict, Any
|
| 2 |
-
import torch
|
| 3 |
-
import base64
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
from model import Model
|
| 6 |
-
from PIL import Image
|
| 7 |
-
# set device
|
| 8 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
-
|
| 10 |
-
if device.type != 'cuda':
|
| 11 |
-
raise ValueError("need to run on GPU")
|
| 12 |
-
|
| 13 |
-
class EndpointHandler():
|
| 14 |
-
def __init__(self, path=""):
|
| 15 |
-
# load the optimized model
|
| 16 |
-
self.model = Model()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def __call__(self, data: Any) -> Any:
|
| 20 |
-
"""
|
| 21 |
-
Args:
|
| 22 |
-
data (:obj:):
|
| 23 |
-
includes the input data and the parameters for the inference.
|
| 24 |
-
Return:
|
| 25 |
-
A :obj:`dict`:. base64 encoded image
|
| 26 |
-
"""
|
| 27 |
-
inputs = data.pop("
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
import torch
|
| 3 |
+
import base64
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from model import Model
|
| 6 |
+
from PIL import Image
|
| 7 |
+
# set device
|
| 8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
+
|
| 10 |
+
if device.type != 'cuda':
|
| 11 |
+
raise ValueError("need to run on GPU")
|
| 12 |
+
|
| 13 |
+
class EndpointHandler():
|
| 14 |
+
def __init__(self, path=""):
|
| 15 |
+
# load the optimized model
|
| 16 |
+
self.model = Model()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def __call__(self, data: Any) -> Any:
|
| 20 |
+
"""
|
| 21 |
+
Args:
|
| 22 |
+
data (:obj:):
|
| 23 |
+
includes the input data and the parameters for the inference.
|
| 24 |
+
Return:
|
| 25 |
+
A :obj:`dict`:. base64 encoded image
|
| 26 |
+
"""
|
| 27 |
+
inputs = data.pop("inputs", data)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
image = Image.open(BytesIO(base64.b64decode(inputs['image'])))
|
| 31 |
+
|
| 32 |
+
# run inference pipeline
|
| 33 |
+
_, res = self.model.process_lineart(image)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# encoding image as base 64 is done by the default toolkit
|
| 37 |
+
return res
|