Eyalyoli commited on
Commit
ef01036
1 Parent(s): 0a60aec

clean handler

Browse files
Files changed (1) hide show
  1. handler.py +8 -10
handler.py CHANGED
@@ -2,15 +2,12 @@ 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`)
@@ -22,13 +19,14 @@ class EndpointHandler:
22
  texts = inputs.pop("texts", None)
23
  instruction = inputs.pop("instruction", None)
24
 
25
- # if isinstance(inputs, str):
26
- # inputs = [inputs]
27
 
28
- # run normal prediction
29
- # scores = self.model.predict_proba(inputs)[0]
 
30
 
31
- # return [{"label": self.id2label[i], "score": score.item()} for i, score in enumerate(scores)]
32
  instructions = [[instruction, text] for text in texts]
33
  embeddings = self.model.encode(instructions)
 
34
  return embeddings.tolist()
 
2
  from InstructorEmbedding import INSTRUCTOR
3
 
4
 
 
 
 
5
  class EndpointHandler:
6
  def __init__(self, path=""):
7
+ # load model on gpu
8
  self.model = INSTRUCTOR(path, device="cuda")
9
 
10
+ def __call__(self, data: Dict[str, Any]) -> List[List[float]]:
11
  """
12
  data args:
13
  inputs (:obj: `str`)
 
19
  texts = inputs.pop("texts", None)
20
  instruction = inputs.pop("instruction", None)
21
 
22
+ if not texts or not instruction:
23
+ raise ValueError("Please provide texts and instruction")
24
 
25
+ # make sure texts is a list
26
+ if not isinstance(texts, list):
27
+ texts = [texts]
28
 
 
29
  instructions = [[instruction, text] for text in texts]
30
  embeddings = self.model.encode(instructions)
31
+
32
  return embeddings.tolist()