Load dolly-v2 model with remote code trusted and full text returned (so it's usable with langchain)
Browse files- handler.py +29 -0
handler.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any, List
|
2 |
+
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
|
6 |
+
class EndpointHandler:
|
7 |
+
def __init__(
|
8 |
+
self,
|
9 |
+
path: str,
|
10 |
+
) -> None:
|
11 |
+
self.pipeline = pipeline(model=path, trust_remote_code=True, return_full_text=True)
|
12 |
+
|
13 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
14 |
+
"""
|
15 |
+
data args:
|
16 |
+
inputs (:obj: `str`)
|
17 |
+
Return:
|
18 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
19 |
+
"""
|
20 |
+
inputs = data.pop("inputs", data)
|
21 |
+
parameters = data.pop("parameters", None)
|
22 |
+
|
23 |
+
# pass inputs with all kwargs in data
|
24 |
+
if parameters is not None:
|
25 |
+
prediction = self.pipeline(inputs, **parameters)
|
26 |
+
else:
|
27 |
+
prediction = self.pipeline(inputs)
|
28 |
+
# postprocess the prediction
|
29 |
+
return prediction
|