File size: 3,620 Bytes
d562319
 
 
 
 
 
 
72f2133
 
d562319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd698d1
a906503
d562319
d9a6e1a
bd698d1
a906503
d562319
 
 
 
 
 
 
 
 
 
 
 
72f2133
d9a6e1a
 
 
d562319
 
 
 
72f2133
 
d562319
 
 
 
72f2133
d9a6e1a
 
 
d562319
 
 
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
69
70
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_new.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('')
st.write("Do you like the tech + social impact focus of CS51? Excited by film-centered courses like FILMEDIA245B? Saw a cool study-abroad course (OSPISTAN76) and want to study that topic on campus? Enter DiscoverCourses. Just pick a course and get dozens of similarly interesting recommendations based on its title or description.")
st.write('')

selected_course = st.selectbox('Pick a course from the dropdown (or click on it and start typing to search).',course_title_list)
#st.write("Description: "+coursedf.iloc[np.where((coursedf['ref']+": "+coursedf['title'])==selected_course)[0][0],3])
#st.write('')

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(coursedf.iloc[index,3]) #Using the new coursedf because it has proper descriptions for each course
        link1 = "[ExploreCourses ↗](https://explorecourses.stanford.edu/search?q="+course_id+"+"+result.replace(" ","+")+")"
        link2 = "[Carta ↗](https://carta-beta.stanford.edu/results/"+course_id+")"
        st.markdown(link1+"   "+link2, 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=coursedf.iloc[index_new,2]
    for result in rec_list[1:]:
        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(coursedf.iloc[index,3]) #Using the new coursedf because it has proper descriptions for each course
        link1 = "[ExploreCourses ↗](https://explorecourses.stanford.edu/search?q="+course_id+"+"+result.replace(" ","+")+")"
        link2 = "[Carta ↗](https://carta-beta.stanford.edu/results/"+course_id+")"
        st.markdown("<span style='color:white'>"+link1+"   "+link2+"</span>", 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.')