espejelomar
commited on
Commit
•
6178d48
1
Parent(s):
0facd56
Upload inference.py
Browse files- inference.py +25 -0
inference.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from backend.utils import load_model, load_embeddings, load_texts
|
3 |
+
|
4 |
+
# Search
|
5 |
+
def query_search(query: str, n_answers: int, model_name: str):
|
6 |
+
model = load_model(model_name)
|
7 |
+
|
8 |
+
# Creating embeddings
|
9 |
+
# query_emb = model.encode(query, convert_to_tensor=True)[None, :]
|
10 |
+
query_emb = model.encode(query, convert_to_tensor=True)
|
11 |
+
|
12 |
+
print("loading embedding")
|
13 |
+
corpus_emb = load_embeddings()
|
14 |
+
corpus_texts = load_texts()
|
15 |
+
|
16 |
+
# Getting hits
|
17 |
+
hits = torch.nn.functional.cosine_similarity(
|
18 |
+
query_emb[None, :], corpus_emb, dim=1, eps=1e-8
|
19 |
+
)
|
20 |
+
|
21 |
+
corpus_texts["Similarity"] = hits.tolist()
|
22 |
+
|
23 |
+
return corpus_texts.sort_values(by="Similarity", ascending=False).head(n_answers)[
|
24 |
+
["Description", "Code"]
|
25 |
+
]
|