custom deployment files
Browse files- handler.py +28 -0
- requirements.txt +2 -0
handler.py
ADDED
|
@@ -0,0 +1,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 |
+
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}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
lmdeploy
|
| 2 |
+
timm
|