Upload 2 files
Browse files- handler.py +25 -0
- requirements.txt +2 -0
handler.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
|
4 |
+
class EndpointHandler():
|
5 |
+
def __init__(self, path=""):
|
6 |
+
self.model = SentenceTransformer(path, trust_remote_code=True).cuda()
|
7 |
+
|
8 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
9 |
+
"""
|
10 |
+
data args:
|
11 |
+
inputs (:obj: List[`str`])
|
12 |
+
type (:obj: `str`) 'query' || 'doc'
|
13 |
+
Return:
|
14 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
15 |
+
"""
|
16 |
+
# get inputs
|
17 |
+
inputs = data.pop("inputs",data)
|
18 |
+
request_type = data.pop("type", 'doc')
|
19 |
+
|
20 |
+
if request_type == 'query':
|
21 |
+
return self.model.encode(inputs, prompt_name='s2p_query')
|
22 |
+
elif request_type == 'doc':
|
23 |
+
return self.model.encode(inputs)
|
24 |
+
else:
|
25 |
+
raise Exception("Invalid request type")
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
flash_attn
|
2 |
+
sentence_transformers
|