Serkan Ozturk commited on
Commit
073b383
·
1 Parent(s): 0a34c89
Files changed (1) hide show
  1. handler.py +13 -4
handler.py CHANGED
@@ -1,9 +1,19 @@
1
  from typing import Dict, List, Any
 
 
2
  import torch
3
  import numpy as np
4
  import torch.nn.functional as F
5
  from serkan import SimpleUpscaleModel
6
  import os
 
 
 
 
 
 
 
 
7
  class EndpointHandler():
8
  def __init__(self, path="."):
9
  # load the optimized model
@@ -12,7 +22,7 @@ class EndpointHandler():
12
  self.model.load_state_dict(torch.load(model_path))
13
 
14
 
15
-
16
 
17
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
18
  """
@@ -26,10 +36,9 @@ class EndpointHandler():
26
  """
27
  inputs = data.pop("inputs", data)
28
  img = inputs["image"]
29
-
 
30
  # Load the image
31
- img = np.float32(img)
32
-
33
  upscaled = self.model(img)
34
  # postprocess the prediction
35
  return "OKAY"
 
1
  from typing import Dict, List, Any
2
+ import base64
3
+ import io
4
  import torch
5
  import numpy as np
6
  import torch.nn.functional as F
7
  from serkan import SimpleUpscaleModel
8
  import os
9
+ from PIL import Image
10
+
11
+ def decode_image(self, base64_str: str) -> np.ndarray:
12
+ """Decode base64 string to an image (numpy array)"""
13
+ image_data = base64.b64decode(base64_str)
14
+ image = Image.open(io.BytesIO(image_data))
15
+ return np.array(image)
16
+
17
  class EndpointHandler():
18
  def __init__(self, path="."):
19
  # load the optimized model
 
22
  self.model.load_state_dict(torch.load(model_path))
23
 
24
 
25
+
26
 
27
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
28
  """
 
36
  """
37
  inputs = data.pop("inputs", data)
38
  img = inputs["image"]
39
+ img = self.decode_image(img)
40
+ img = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0).float()
41
  # Load the image
 
 
42
  upscaled = self.model(img)
43
  # postprocess the prediction
44
  return "OKAY"