Eyalyoli commited on
Commit
c26a43c
1 Parent(s): b816ba2

add handler

Browse files
Files changed (2) hide show
  1. handler.py +32 -0
  2. requirements.txt +1 -0
handler.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from InstructorEmbedding import INSTRUCTOR
3
+
4
+
5
+ INSTRUCTION_SEPARATOR = "|||"
6
+
7
+
8
+ class EndpointHandler:
9
+ def __init__(self, path=""):
10
+ # load model
11
+ self.model = INSTRUCTOR(path, device="cuda")
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
+ # get inputs
21
+ texts = data.pop("texts", data)
22
+ instruction = data.pop("instruction", "Represent this sentence:")
23
+ # if isinstance(inputs, str):
24
+ # inputs = [inputs]
25
+
26
+ # run normal prediction
27
+ # scores = self.model.predict_proba(inputs)[0]
28
+
29
+ # return [{"label": self.id2label[i], "score": score.item()} for i, score in enumerate(scores)]
30
+ instructions = [[instruction, text] for text in texts]
31
+ embeddings = self.model.encode(instructions)
32
+ return embeddings.tolist()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ InstructorEmbedding~=1.0.1