Update handler.py
Browse files- handler.py +20 -13
handler.py
CHANGED
@@ -29,18 +29,25 @@ class EndpointHandler:
|
|
29 |
def __call__(self, data: Any):
|
30 |
image = data["inputs"]
|
31 |
inputs = self.processor(image, return_tensors="pt")
|
32 |
-
with torch.no_grad():
|
33 |
-
outputs = self.model(**inputs)
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
img = Image.fromarray(output)
|
42 |
-
buffered = BytesIO()
|
43 |
-
img.save(buffered, format="JPEG")
|
44 |
-
img_str = base64.b64encode(buffered.getvalue())
|
45 |
-
|
46 |
-
return img_str.decode()
|
|
|
29 |
def __call__(self, data: Any):
|
30 |
image = data["inputs"]
|
31 |
inputs = self.processor(image, return_tensors="pt")
|
|
|
|
|
32 |
|
33 |
+
try:
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = self.model(**inputs)
|
36 |
+
|
37 |
+
print(subprocess.run(["nvidia-smi"]))
|
38 |
+
|
39 |
+
output = outputs.reconstruction.data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
40 |
+
output = np.moveaxis(output, source=0, destination=-1)
|
41 |
+
output = (output * 255.0).round().astype(np.uint8)
|
42 |
+
|
43 |
+
img = Image.fromarray(output)
|
44 |
+
buffered = BytesIO()
|
45 |
+
img.save(buffered, format="JPEG")
|
46 |
+
img_str = base64.b64encode(buffered.getvalue())
|
47 |
+
|
48 |
+
return img_str.decode()
|
49 |
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(str(e))
|
52 |
+
del inputs
|
53 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|