zesquirrelnator commited on
Commit
55e0e41
·
verified ·
1 Parent(s): 00945c9

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +49 -48
handler.py CHANGED
@@ -4,53 +4,54 @@ import torch
4
  from io import BytesIO
5
  import base64
6
 
7
- # Initialize the model and tokenizer
8
- model_id = "zesquirrelnator/moondream2-finetuneV2"
9
- model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
10
- tokenizer = AutoTokenizer.from_pretrained("vikhyatk/moondream2", trust_remote_code=True)
11
-
12
- # Check if CUDA (GPU support) is available and then set the device to GPU or CPU
13
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
- model.to(device)
15
-
16
- def preprocess_image(encoded_image):
17
- """Decode and preprocess the input image."""
18
- decoded_image = base64.b64decode(encoded_image)
19
- img = Image.open(BytesIO(decoded_image)).convert("RGB")
20
- return img
21
-
22
- def handler(event, context):
23
- """Handle the incoming request."""
24
- try:
25
- # Extract the base64-encoded image and question from the event
26
- input_image = event['body']['image']
27
- question = event['body'].get('question', "move to the red ball")
28
-
29
- # Preprocess the image
30
- img = preprocess_image(input_image)
31
-
32
- # Perform inference
33
- enc_image = model.encode_image(img).to(device)
34
- answer = model.answer_question(enc_image, question, tokenizer)
35
-
36
- # If the output is a tensor, move it back to CPU and convert to list
37
- if isinstance(answer, torch.Tensor):
38
- answer = answer.cpu().numpy().tolist()
39
-
40
- # Create the response
41
- response = {
42
- "statusCode": 200,
43
- "body": {
44
- "answer": answer
 
 
45
  }
46
- }
47
- return response
48
- except Exception as e:
49
- # Handle any errors
50
- response = {
51
- "statusCode": 500,
52
- "body": {
53
- "error": str(e)
54
  }
55
- }
56
- return response
 
4
  from io import BytesIO
5
  import base64
6
 
7
+ class EndpointHandler:
8
+ def __init__(self, model_dir):
9
+ self.model_id = "zesquirrelnator/moondream2-finetuneV2"
10
+ self.model = AutoModelForCausalLM.from_pretrained(self.model_id, trust_remote_code=True)
11
+ self.tokenizer = AutoTokenizer.from_pretrained("vikhyatk/moondream2", trust_remote_code=True)
12
+
13
+ # Check if CUDA (GPU support) is available and then set the device to GPU or CPU
14
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ self.model.to(self.device)
16
+
17
+ def preprocess_image(self, encoded_image):
18
+ """Decode and preprocess the input image."""
19
+ decoded_image = base64.b64decode(encoded_image)
20
+ img = Image.open(BytesIO(decoded_image)).convert("RGB")
21
+ return img
22
+
23
+ def handle(self, event, context):
24
+ """Handle the incoming request."""
25
+ try:
26
+ # Extract the base64-encoded image and question from the event
27
+ input_image = event['body']['image']
28
+ question = event['body'].get('question', "move to the red ball")
29
+
30
+ # Preprocess the image
31
+ img = self.preprocess_image(input_image)
32
+
33
+ # Perform inference
34
+ enc_image = self.model.encode_image(img).to(self.device)
35
+ answer = self.model.answer_question(enc_image, question, self.tokenizer)
36
+
37
+ # If the output is a tensor, move it back to CPU and convert to list
38
+ if isinstance(answer, torch.Tensor):
39
+ answer = answer.cpu().numpy().tolist()
40
+
41
+ # Create the response
42
+ response = {
43
+ "statusCode": 200,
44
+ "body": {
45
+ "answer": answer
46
+ }
47
  }
48
+ return response
49
+ except Exception as e:
50
+ # Handle any errors
51
+ response = {
52
+ "statusCode": 500,
53
+ "body": {
54
+ "error": str(e)
55
+ }
56
  }
57
+ return response