Update handler.py
Browse files- handler.py +15 -6
handler.py
CHANGED
@@ -1,8 +1,9 @@
|
|
|
|
1 |
from io import BytesIO
|
2 |
from typing import Any, List, Dict
|
3 |
|
4 |
from PIL import Image
|
5 |
-
from transformers import
|
6 |
|
7 |
|
8 |
class EndpointHandler():
|
@@ -16,18 +17,26 @@ class EndpointHandler():
|
|
16 |
self.tokenizer = tokenizer
|
17 |
|
18 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
19 |
-
|
20 |
-
question = data.pop("question", None)
|
21 |
-
image = Image.open(BytesIO(image_bytes))
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
1 |
+
import base64
|
2 |
from io import BytesIO
|
3 |
from typing import Any, List, Dict
|
4 |
|
5 |
from PIL import Image
|
6 |
+
from transformers import AutoTokenizer, AutoModel
|
7 |
|
8 |
|
9 |
class EndpointHandler():
|
|
|
17 |
self.tokenizer = tokenizer
|
18 |
|
19 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
20 |
+
inputs = data.pop("inputs", data)
|
|
|
|
|
21 |
|
22 |
+
image = inputs.pop("image", None) # base64 image as bytes
|
23 |
+
question = inputs.pop("question", None)
|
24 |
+
msgs = inputs.pop("msgs", None)
|
25 |
+
|
26 |
+
|
27 |
+
parameters = data.pop("parameters", {})
|
28 |
+
|
29 |
+
image = Image.open(BytesIO(base64.b64decode(image)))
|
30 |
+
|
31 |
+
if not msgs:
|
32 |
+
msgs = [{'role': 'user', 'content': question}]
|
33 |
|
34 |
res = self.model.chat(
|
35 |
image=image,
|
36 |
msgs=msgs,
|
37 |
tokenizer=self.tokenizer,
|
38 |
sampling=True, # if sampling=False, beam_search will be used by default
|
39 |
+
temperature=parameters.get('temperature', 0.7),
|
40 |
# system_prompt='' # pass system_prompt if needed
|
41 |
)
|
42 |
|