File size: 3,119 Bytes
17d7d2e
 
 
efda6d0
8970242
17d7d2e
 
bf3fe21
38e0a38
17d7d2e
1303ac0
17d7d2e
 
c077474
38e0a38
17d7d2e
56a13a2
17d7d2e
 
 
 
 
 
 
 
 
 
94fc387
fc60bd5
4d2b4be
ba18f11
fc60bd5
17d7d2e
2815635
912fc79
4d2b4be
bd80b62
4ec5d64
7cdbd6f
73a25a9
011e348
73a25a9
1907c60
94fc387
38e0a38
28831ec
9cc277c
979381b
2b57cad
f68a5ef
bf3fe21
4ec5d64
bf3fe21
38e0a38
 
fc60bd5
 
bf3fe21
94fc387
38e0a38
28831ec
bf3fe21
979381b
2b57cad
4d2b4be
 
38e0a38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import pickle
import pandas as pd
import torch
import numpy as np

cosine_scores = pickle.load(open('cosine_scores.pkl','rb'))
coursedf = pd.read_pickle('course_df.pkl')          # course_df uses titles to generate course recommendations
course_df_new = pd.read_pickle('course_df_new.pkl') #course_df_new makes recommendations using the entire description

course_title_list = [i + ": " + j for i, j in zip(coursedf['ref'].to_list(), coursedf['title'].to_list())]

def get_random_course():
    row=coursedf.sample(1)
    return row['ref'], row['title'] 

def recommend(index):
    pairs = {}

    for i in range(len(coursedf)):
        pairs[coursedf.iloc[i,1]]=cosine_scores[index][i]

    sorttemp = sorted(pairs.items(), key=lambda x:x[1], reverse=True)
    sorted_final = dict(sorttemp[1:31])

    return list(sorted_final.keys())

st.set_page_config(page_title='DiscoverCourses', page_icon=':bird:')
st.header('DiscoverCourses')
st.write('')

selected_course = st.selectbox('Pick a course from the dropdown:',course_title_list)

container = st.container()
maincol1, maincol2 = container.columns(2)
st.write('')

if maincol1.button('Recommend by title',use_container_width=True):
    output=recommend(np.where((coursedf['ref']+": "+coursedf['title']) == selected_course)[0][0])
    for result in output:
        index=np.where(coursedf['title'] == result)[0][0]
        course_id=coursedf.iloc[index,0]
        st.subheader(course_id+": "+result)
        with st.expander("See description"):
            st.write(course_df_new.iloc[index,3]) #Using the new coursedf because it has proper descriptions for each course
        link = "[ExploreCourses](https://explorecourses.stanford.edu/search?q="+course_id+"+"+result.replace(" ","+")+")"
        st.markdown(link, unsafe_allow_html=True)
        link = "[Carta](https://carta-beta.stanford.edu/results/"+course_id+")"
        st.markdown(link, unsafe_allow_html=True)
        st.divider()
        
if maincol2.button('Recommend by description',use_container_width=True):
    index_new=np.where((coursedf['ref']+": "+coursedf['title']) == selected_course)[0][0]
    rec_list=course_df_new.iloc[index_new,2]
    for result in rec_list:
        index=np.where(coursedf['title'] == result)[0][0]
        course_id=coursedf.iloc[index,0]
        st.subheader(course_id+": "+result)
        with st.expander("See description"):
            st.write(course_df_new.iloc[index,3]) #Using the new coursedf because it has proper descriptions for each course
        link = "[ExploreCourses](https://explorecourses.stanford.edu/search?q="+course_id+"+"+result.replace(" ","+")+")"
        st.markdown(link, unsafe_allow_html=True)
        link = "[Carta](https://carta-beta.stanford.edu/results/"+course_id+")"
        st.markdown(link, unsafe_allow_html=True)
        st.divider()

st.write('© 2023 Rushank Goyal. All rights reserved. Source for the all-MiniLM-L6-v2 model: Wang, Wenhui, et al. "MiniLM: Deep Self-Attention Distillation for Task-Agnostic Compression of Pre-Trained Transformers." arXiv, 25 Feb. 2020, doi:10.48550/arXiv.2002.10957.')