Spaces:
Build error
Build error
Abhay Mishra
commited on
Commit
·
16c64bc
1
Parent(s):
d5c3587
initial commit
Browse files- app.py +26 -0
- title_to_content_embed.pickle +0 -0
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
8 |
+
|
9 |
+
with open("title_to_content_embed.pickle", "rb") as handle:
|
10 |
+
loaded_map = pickle.load(handle)
|
11 |
+
|
12 |
+
course_titles = list(loaded_map.keys())
|
13 |
+
course_content_embeddings = np.array(list(loaded_map.values()), dtype=np.float32)
|
14 |
+
|
15 |
+
cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
|
16 |
+
|
17 |
+
def give_best_match(query):
|
18 |
+
embed = model.encode(query)
|
19 |
+
result = cos(torch.from_numpy(course_content_embeddings),torch.from_numpy(embed))
|
20 |
+
indices = reversed(np.argsort(result))
|
21 |
+
predictions = {course_titles[i] : float(result[i]) for i in indices}
|
22 |
+
return predictions
|
23 |
+
|
24 |
+
|
25 |
+
demo = gr.Interface(fn = give_best_match, inputs="text",outputs=gr.Label(num_top_classes=5))
|
26 |
+
demo.launch()
|
title_to_content_embed.pickle
ADDED
Binary file (265 kB). View file
|
|