Abhay Mishra commited on
Commit
63cb040
·
1 Parent(s): 1b9a1bf

support department selection

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -10,17 +10,41 @@ with open("dep_course_title_to_content_embed.pickle", "rb") as handle:
10
  loaded_map = pickle.load(handle)
11
 
12
  dep_name_course_name = list(loaded_map.keys())
13
- dep_name = [x for (x,y) in dep_name_course_name]
14
- course_titles = [y for (x,y) in dep_name_course_name]
15
- course_content_embeddings = np.array(list(loaded_map.values()), dtype=np.float32)
 
 
 
 
 
 
 
 
 
16
 
17
  cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
18
- def give_best_match(query):
 
 
 
 
 
 
 
 
 
19
  embed = model.encode(query)
20
  result = cos(torch.from_numpy(course_content_embeddings),torch.from_numpy(embed))
21
  indices = reversed(np.argsort(result))
22
  predictions = {course_titles[i] : float(result[i]) for i in indices}
23
  return predictions
24
 
25
- demo = gr.Interface(fn = give_best_match, inputs=gr.Textbox(label="Describe the course",lines = 5, placeholder = "Type anything related to course/s\n\nTitle, Topics/Sub Topics, Refernce books, Questions asked in exams or some random fun stuff.") ,outputs=gr.Label(label = "Most Relevant Courses", num_top_classes=5))
 
 
 
 
 
 
26
  demo.launch()
 
10
  loaded_map = pickle.load(handle)
11
 
12
  dep_name_course_name = list(loaded_map.keys())
13
+ deps = list(set([x for (x,y) in dep_name_course_name]))
14
+ dep_to_course_name = {}
15
+ dep_to_course_embedding = {}
16
+
17
+ for dep in deps:
18
+ dep_to_course_name[dep] = []
19
+ dep_to_course_embedding[dep] = []
20
+
21
+ for (dep_name, course_name), embedding in loaded_map.items():
22
+ # print(embedding.shape)
23
+ dep_to_course_name[dep_name].append(course_name)
24
+ dep_to_course_embedding[dep_name].append(np.array(embedding, dtype = np.float32))
25
 
26
  cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
27
+
28
+ def give_best_match(query, Department):
29
+ if not Department:
30
+ Department = deps
31
+ course_titles = []
32
+ course_content_embeddings = []
33
+ for dep in Department:
34
+ course_titles += dep_to_course_name[dep]
35
+ course_content_embeddings += dep_to_course_embedding[dep]
36
+ course_content_embeddings = np.stack(course_content_embeddings)
37
  embed = model.encode(query)
38
  result = cos(torch.from_numpy(course_content_embeddings),torch.from_numpy(embed))
39
  indices = reversed(np.argsort(result))
40
  predictions = {course_titles[i] : float(result[i]) for i in indices}
41
  return predictions
42
 
43
+ demo = gr.Interface(fn = give_best_match,
44
+ inputs=[
45
+ gr.Textbox(label="Describe the course",lines = 5, placeholder = "Type anything related to course/s\n\nTitle, Topics/Sub Topics, Refernce books, Questions asked in exams or some random fun stuff."),
46
+ gr.CheckboxGroup(deps),
47
+ ],
48
+ outputs=gr.Label(label = "Most Relevant Courses", num_top_classes=5)
49
+ )
50
  demo.launch()