kblevins commited on
Commit
de7f614
1 Parent(s): 560f31c

update handler

Browse files
Files changed (1) hide show
  1. handler.py +26 -0
handler.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from InstructorEmbedding import INSTRUCTOR
3
+
4
+ class EndpointHandler:
5
+ def __init__(self, path=""):
6
+ # load model on gpu
7
+ self.model = INSTRUCTOR(path, device="cuda")
8
+
9
+ def __call__(self, data: Dict[str, Any]) -> List[List[float]]:
10
+ """
11
+ data args:
12
+ inputs (:obj: `str`)
13
+ Return:
14
+ A :obj:`list` | `dict`: will be serialized and returned
15
+ """
16
+ # get inputs
17
+ inputs: dict = data.pop("inputs", data)
18
+
19
+ # add instruction
20
+ query = [['Retrieve documents that can help answer the question:',
21
+ inputs]]
22
+
23
+ # encode
24
+ embedding = self.model.encode(query)
25
+
26
+ return embedding[0]