added error handling
Browse files- handler.py +22 -12
handler.py
CHANGED
@@ -41,18 +41,28 @@ class EndpointHandler:
|
|
41 |
|
42 |
image = data["inputs"]
|
43 |
inputs = self.processor(image, return_tensors="pt")
|
44 |
-
with torch.no_grad():
|
45 |
-
outputs = self.model(**inputs)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
|
|
|
41 |
|
42 |
image = data["inputs"]
|
43 |
inputs = self.processor(image, return_tensors="pt")
|
|
|
|
|
44 |
|
45 |
+
try:
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = self.model(**inputs)
|
48 |
+
|
49 |
+
print(subprocess.run(["nvidia-smi"]))
|
50 |
+
|
51 |
+
output = outputs.reconstruction.data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
52 |
+
output = np.moveaxis(output, source=0, destination=-1)
|
53 |
+
output = (output * 255.0).round().astype(np.uint8)
|
54 |
+
|
55 |
+
img = Image.fromarray(output)
|
56 |
+
buffered = BytesIO()
|
57 |
+
img.save(buffered, format="JPEG")
|
58 |
+
img_str = base64.b64encode(buffered.getvalue())
|
59 |
+
|
60 |
+
return img_str.decode()
|
61 |
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(str(e))
|
64 |
+
del inputs
|
65 |
+
gc.collect()
|
66 |
+
torch.cuda.empty_cache()
|
67 |
|
68 |
+
return {"error": str(e)}
|