DaniilAlpha commited on
Commit
c542da9
·
1 Parent(s): 26790f6

Create mapper.py

Browse files
Files changed (1) hide show
  1. mapper.py +18 -0
mapper.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer, util
2
+
3
+ class Mapper:
4
+ def __init__(self, repo: str, model: str):
5
+ self.__model = SentenceTransformer(f"{repo}/{model}")
6
+
7
+ def __call__(self, query: str, *data):
8
+ query_emb = self.__model.encode(query)
9
+ data_emb = self.__model.encode(data)
10
+
11
+ scores = util.dot_score(query_emb, data_emb)[0].cpu().tolist()
12
+ data_score_pairs = list(zip(data, scores))
13
+
14
+ return sorted(
15
+ data_score_pairs,
16
+ key=lambda x: x[1],
17
+ reverse=True,
18
+ )