Update handler.py
Browse files- handler.py +31 -28
handler.py
CHANGED
@@ -1,28 +1,31 @@
|
|
1 |
-
from typing import Dict, List, Any
|
2 |
-
from lmdeploy import pipeline
|
3 |
-
from lmdeploy.vl import load_image
|
4 |
-
from lmdeploy.messages import TurbomindEngineConfig
|
5 |
-
|
6 |
-
class EndpointHandler():
|
7 |
-
def __init__(self, path):
|
8 |
-
# Preload the model at initialization
|
9 |
-
backend_config = TurbomindEngineConfig(model_name ="OpenGVLab/InternVL-Chat-V1-5",model_format='hf',tp=1)
|
10 |
-
self.pipe = pipeline(f"{path}", backend_config=backend_config, log_level='INFO')
|
11 |
-
|
12 |
-
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
13 |
-
"""
|
14 |
-
data args:
|
15 |
-
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
16 |
-
kwargs
|
17 |
-
Return:
|
18 |
-
A :obj:`list` | `dict`: will be serialized and returned
|
19 |
-
"""
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from lmdeploy import pipeline
|
3 |
+
from lmdeploy.vl import load_image
|
4 |
+
from lmdeploy.messages import TurbomindEngineConfig
|
5 |
+
|
6 |
+
class EndpointHandler():
|
7 |
+
def __init__(self, path):
|
8 |
+
# Preload the model at initialization
|
9 |
+
backend_config = TurbomindEngineConfig(model_name ="OpenGVLab/InternVL-Chat-V1-5",model_format='hf',tp=1)
|
10 |
+
self.pipe = pipeline(f"{path}", backend_config=backend_config, log_level='INFO')
|
11 |
+
|
12 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
13 |
+
"""
|
14 |
+
data args:
|
15 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
16 |
+
kwargs
|
17 |
+
Return:
|
18 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
19 |
+
"""
|
20 |
+
try:
|
21 |
+
image_url = data.get('image_url', "")
|
22 |
+
prompt = data.get("prompt", "")
|
23 |
+
if not image_url:
|
24 |
+
return {'error': 'No image URL provided'}
|
25 |
+
|
26 |
+
image = load_image(image_url)
|
27 |
+
|
28 |
+
response = self.pipe((prompt, image))
|
29 |
+
return {'response': response.text}
|
30 |
+
except Exception as err:
|
31 |
+
return {"error", str(err)}
|