Sentence Similarity
sentence-transformers
PyTorch
Transformers
English
t5
text-embedding
embeddings
information-retrieval
beir
text-classification
language-model
text-clustering
text-semantic-similarity
text-evaluation
prompt-retrieval
text-reranking
feature-extraction
English
Sentence Similarity
natural_questions
ms_marco
fever
hotpot_qa
mteb
Eval Results
Inference Endpoints
clean handler
Browse files- 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[
|
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 |
-
|
26 |
-
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
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()
|