aradootle commited on
Commit
7f7a4fc
1 Parent(s): f49d008

fixed tabs and spaces

Browse files
Files changed (1) hide show
  1. handler.py +36 -37
handler.py CHANGED
@@ -5,40 +5,39 @@ from flask import Flask, Response, request, jsonify
5
  from segment_anything import SamPredictor, sam_model_registry
6
 
7
  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
- 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)
 
5
  from segment_anything import SamPredictor, sam_model_registry
6
 
7
  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
+ sam = sam_model_registry[model_type](checkpoint=model_path)
15
+ self.predictor = SamPredictor(sam)
16
+
17
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
18
+ """
19
+ data args:
20
+ inputs (:obj: `str` | `PIL.Image` | `np.array`)
21
+ kwargs
22
+ Return:
23
+ A :obj:`list` | `dict`: will be serialized and returned
24
+ """
25
+
26
+ inputs = data.pop("inputs", data)
27
+ image_url = inputs.pop("imageUrl", None)
28
+
29
+ if not image_url:
30
+ return jsonify({"error": "image_url not provided"}), 400
31
+
32
+ try:
33
+ response = requests.get(image_url)
34
+ response.raise_for_status()
35
+ image = response.content
36
+ except requests.RequestException as e:
37
+ return jsonify({"error": f"Error downloading image: {str(e)}"}), 500
38
+
39
+ self.predictor.set_image(image)
40
+
41
+ image_embedding = self.predictor.get_image_embedding().cpu().numpy().tolist()
42
+
43
+ return jsonify(image_embedding)