Devops-hestabit commited on
Commit
6c72430
·
verified ·
1 Parent(s): 78585b3

Update handler.py

Browse files
Files changed (1) hide show
  1. 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
- image_url = data.get('image_url')
21
- if not image_url:
22
- return {'error': 'No image URL provided'}
23
-
24
- image = load_image(image_url)
25
- prompt = """Please analyze the provided image to determine if it contains any form of analytical representation, such as a chart, graph, or table. If such a representation is present, extract all the information into a textual, relational data format and provide a detailed description of the image. If the image does not contain any analytical representation, give only "none" without any additional words."""
26
-
27
- response = self.pipe((prompt, image))
28
- return {'response': response.text}
 
 
 
 
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)}