aradootle commited on
Commit
f49d008
1 Parent(s): c2fbc3b

fixed indentation

Browse files
Files changed (1) hide show
  1. handler.py +20 -24
handler.py CHANGED
@@ -8,41 +8,37 @@ class EndpointHandler():
8
  def __init__(self, path=""):
9
  # Preload all the elements you are going to need at inference.
10
  model_type = "vit_b"
11
- # prefix = "/opt/ml/model"
12
- model_path = "tf_model.h5"
13
- # model_checkpoint_path = os.path.join(prefix, "sam_vit_h_4b8939.pth")
14
 
15
- sam = sam_model_registry[model_type](checkpoint=model_path)
16
- predictor = SamPredictor(sam)
17
 
18
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
19
  """
20
- data args:
21
  inputs (:obj: `str` | `PIL.Image` | `np.array`)
22
  kwargs
23
- Return:
24
  A :obj:`list` | `dict`: will be serialized and returned
25
  """
26
 
27
- inputs = data.pop("inputs", data)
28
- image_url = inputs.pop("imageUrl", none)
29
 
30
- if not image_url:
31
- return jsonify({"error": "image_url not provided"}), 400
32
 
33
- try:
34
- response = requests.get(image_url)
35
- response.raise_for_status()
36
- image = response.content
37
- except requests.RequestException as e:
38
- return jsonify({"error": f"Error downloading image: {str(e)}"}), 500
39
 
 
40
 
41
- predictor.set_image(image)
42
 
43
- image_embedding = predictor.get_image_embedding().cpu().numpy().toList()
44
-
45
- return jsonify(image_embedding)
46
-
47
- # pseudo
48
- # self.model(input)
 
8
  def __init__(self, path=""):
9
  # Preload all the elements you are going to need at inference.
10
  model_type = "vit_b"
11
+ # prefix = "/opt/ml/model"
12
+ model_path = "tf_model.h5"
13
+ # model_checkpoint_path = os.path.join(prefix, "sam_vit_h_4b8939.pth")
14
 
15
+ sam = sam_model_registry[model_type](checkpoint=model_path)
16
+ self.predictor = SamPredictor(sam)
17
 
18
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
19
  """
20
+ data args:
21
  inputs (:obj: `str` | `PIL.Image` | `np.array`)
22
  kwargs
23
+ Return:
24
  A :obj:`list` | `dict`: will be serialized and returned
25
  """
26
 
27
+ inputs = data.pop("inputs", data)
28
+ image_url = inputs.pop("imageUrl", None)
29
 
30
+ if not image_url:
31
+ return jsonify({"error": "image_url not provided"}), 400
32
 
33
+ try:
34
+ response = requests.get(image_url)
35
+ response.raise_for_status()
36
+ image = response.content
37
+ except requests.RequestException as e:
38
+ return jsonify({"error": f"Error downloading image: {str(e)}"}), 500
39
 
40
+ self.predictor.set_image(image)
41
 
42
+ image_embedding = self.predictor.get_image_embedding().cpu().numpy().tolist()
43
 
44
+ return jsonify(image_embedding)