SwordElucidator commited on
Commit
e66647f
1 Parent(s): 2a4cbf8

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +34 -0
handler.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from typing import Any, List, Dict
3
+
4
+ from PIL import Image
5
+ from transformers import AutoModel, AutoTokenizer
6
+
7
+
8
+ class EndpointHandler():
9
+ def __init__(self, path=""):
10
+ # Use a pipeline as a high-level helper
11
+ model_name = "SwordElucidator/MiniCPM-Llama3-V-2_5-int4"
12
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
14
+ model.eval()
15
+ self.model = model
16
+ self.tokenizer = tokenizer
17
+
18
+ def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
19
+ image_bytes = data.pop("image_bytes", None)
20
+ question = data.pop("question", None)
21
+ image = Image.open(BytesIO(image_bytes))
22
+
23
+ msgs = [{'role': 'user', 'content': question}]
24
+
25
+ res = self.model.chat(
26
+ image=image,
27
+ msgs=msgs,
28
+ tokenizer=self.tokenizer,
29
+ sampling=True, # if sampling=False, beam_search will be used by default
30
+ temperature=0.7,
31
+ # system_prompt='' # pass system_prompt if needed
32
+ )
33
+
34
+ return res