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.')