import streamlit as st from streamlit_option_menu import option_menu import streamlit.components.v1 as components import time from model import * import pandas as pd # Make sure to import pandas if you're using it if 'model' not in st.session_state: st.session_state.model = 'Model 1' def update_radio2(): st.session_state.model = st.session_state.radio2 if 'genre' not in st.session_state: st.session_state.genre = 3 def update_num_genre(): st.session_state.genre = st.session_state.num_genre if 'artist' not in st.session_state: st.session_state.artist = 5 def update_same_art(): st.session_state.artist = st.session_state.same_art if 'model2' not in st.session_state: st.session_state.model2 = 'Spotify model' def update_radio1(): st.session_state.model2 = st.session_state.radio1 if 'Region' not in st.session_state: st.session_state.rg = "US" def update_Region(): st.session_state.rg = st.session_state.Region if 'radio' not in st.session_state: st.session_state.feature = "Playlist" def update_radio0(): st.session_state.feature = st.session_state.radio if 'p_url' not in st.session_state: st.session_state.p_url = 'Example: https://open.spotify.com/playlist/37i9dQZF1DX8FwnYE6PRvL?si=06ff6b38d4124af0' def update_playlist_url(): st.session_state.p_url = st.session_state.playlist_url if 's_url' not in st.session_state: st.session_state.s_url = 'Example: https://open.spotify.com/track/5CQ30WqJwcep0pYcV4AMNc?si=ed4b04f153a24531' def update_song_url(): st.session_state.s_url = st.session_state.song_url if 'a_url' not in st.session_state: st.session_state.a_url = 'Example: https://open.spotify.com/artist/3RNrq3jvMZxD9ZyoOZbQOD?si=UNAsX20kRpG89bxOO8o7ew' def update_artist_url(): st.session_state.a_url = st.session_state.artist_url def play_recomm(): if 'rs' in st.session_state: del st.session_state.rs, st.session_state.err try: if len(pd.read_csv('Data/new_tracks.csv')) >= 200: with st.spinner('Updating the dataset...'): x = update_dataset() st.success('{} New tracks were added to the dataset.'.format(x)) except: st.error("The dataset update failed.") with st.spinner('Getting Recommendations...'): res, err = playlist_model(st.session_state.p_url, st.session_state.model, st.session_state.genre, st.session_state.artist) st.session_state.rs = res st.session_state.err = err if len(st.session_state.rs) >= 1: if st.session_state.model == 'Model 1' or st.session_state.model == 'Model 2': st.success('Go to the Result page to view the top {} recommendations'.format(len(st.session_state.rs))) else: st.success('Go to the Result page to view the Spotify recommendations') else: st.error('Model failed. Check the log for more information.') def art_recomm(): if 'rs' in st.session_state: del st.session_state.rs, st.session_state.err with st.spinner('Getting Recommendations...'): res, err = top_tracks(st.session_state.a_url, st.session_state.rg) st.session_state.rs = res st.session_state.err = err if len(st.session_state.rs) >= 1: st.success("Go to the Result page to view the Artist's top tracks") else: st.error('Model failed. Check the log for more information.') def song_recomm(): if 'rs' in st.session_state: del st.session_state.rs, st.session_state.err with st.spinner('Getting Recommendations...'): res, err = song_model(st.session_state.s_url, st.session_state.model, st.session_state.genre, st.session_state.artist) st.session_state.rs = res st.session_state.err = err if len(st.session_state.rs) >= 1: if st.session_state.model == 'Model 1' or st.session_state.model == 'Model 2': st.success('Go to the Result page to view the top {} recommendations'.format(len(st.session_state.rs))) else: st.success('Go to the Result page to view the Spotify recommendations') else: st.error('Model failed. Check the log for more information.') def playlist_page(): st.subheader("User Playlist") st.markdown('---') playlist_uri = (st.session_state.playlist_url).split('/')[-1].split('?')[0] uri_link = 'https://open.spotify.com/embed/playlist/' + playlist_uri components.iframe(uri_link, height=300) def song_page(): st.subheader("User Song") st.markdown('---') song_uri = (st.session_state.song_url).split('/')[-1].split('?')[0] uri_link = 'https://open.spotify.com/embed/track/' + song_uri components.iframe(uri_link, height=100) def artist_page(): st.subheader("User Artist") st.markdown('---') artist_uri = (st.session_state.artist_url).split('/')[-1].split('?')[0] uri_link = 'https://open.spotify.com/embed/artist/' + artist_uri components.iframe(uri_link, height=80) def spr_sidebar(): menu = option_menu( menu_title=None, options=['Home', 'Result'], # Removed 'About' and 'Log' icons=['house', 'book'], menu_icon='cast', default_index=0, orientation='horizontal' ) if menu == 'Home': st.session_state.app_mode = 'Home' elif menu == 'Result': st.session_state.app_mode = 'Result' def home_page(): st.session_state.radio = st.session_state.feature st.session_state.radio2 = st.session_state.model st.session_state.num_genre = st.session_state.genre st.session_state.same_art = st.session_state.artist st.session_state.Region = st.session_state.rg st.title('SpotifAI') col, col2, col3 = st.columns([2, 2, 3]) radio = col.radio("Feature", options=("Playlist", "Song", "Artist Top Tracks"), key='radio', on_change=update_radio0) if radio == "Artist Top Tracks": radio1 = col2.radio("Model", options=["Spotify model"], key='radio1', on_change=update_radio1) Region = col3.selectbox("Please Choose Region", index=58, key='Region', on_change=update_Region, options=('AD', 'AR', 'AU', 'AT', 'BE', 'BO', 'BR', 'BG', 'CA', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DK', 'DO', 'EC', 'SV', 'EE', 'FI', 'FR', 'DE', 'GR', 'GT', 'HN', 'HK', 'HU', 'IS', 'ID', 'IE', 'IT', 'JP', 'LV', 'LI', 'LT', 'LU', 'MY', 'MT', 'MX', 'MC', 'NL', 'NZ', 'NI', 'NO', 'PA', 'PY', 'PE', 'PH', 'PL', 'PT', 'SG', 'ES', 'SK', 'SE', 'CH', 'TW', 'TR', 'GB', 'US', 'UY')) elif radio == "Playlist" or radio == "Song": radio2 = col2.radio("Model", options=("Model 1", "Model 2", "Spotify Model"), key='radio2', on_change=update_radio2) if st.session_state.radio2 == "Model 1" or st.session_state.radio2 == "Model 2": num_genre = col3.selectbox("choose a number of genres to focus on", options=(1, 2, 3, 4, 5, 6, 7), index=2, key='num_genre', on_change=update_num_genre) same_art = col3.selectbox("How many recommendations by the same artist", options=(1, 2, 3, 4, 5, 7, 10, 15), index=3, key='same_art', on_change=update_same_art) st.markdown("
", unsafe_allow_html=True) if radio == "Playlist": st.session_state.playlist_url = st.session_state.p_url Url = st.text_input(label="Playlist Url", key='playlist_url', on_change=update_playlist_url) playlist_page() state = st.button('Get Recommendations') with st.expander("Here's how to find any Playlist URL in Spotify"): st.write(""" - Search for Playlist on the Spotify app - Right Click on the Playlist you like - Click "Share" - Choose "Copy link to playlist" """) if state: play_recomm() elif radio == "Song": st.session_state.song_url = st.session_state.s_url Url = st.text_input(label="Song Url", key='song_url', on_change=update_song_url) song_page() state = st.button('Get Recommendations') with st.expander("Here's how to find any Song URL in Spotify"): st.write(""" - Search for Song on the Spotify app - Right Click on the Song you like - Click "Share" - Choose "Copy link to song" """) if state: song_recomm() elif radio == "Artist Top Tracks": st.session_state.artist_url = st.session_state.a_url Url = st.text_input(label="Artist Url", key='artist_url', on_change=update_artist_url) artist_page() state = st.button('Get Recommendations') with st.expander("Here's how to find any Artist URL in Spotify"): st.write(""" - Search for Artist on the Spotify app - Right Click on the Artist you like - Click "Share" - Choose "Copy link to artist" """) if state: art_recomm() def result_page(): if 'rs' in st.session_state: df = st.session_state.rs if st.session_state.radio == "Playlist": st.subheader("Playlist Recommendations") elif st.session_state.radio == "Song": st.subheader("Song Recommendations") elif st.session_state.radio == "Artist Top Tracks": st.subheader("Artist Top Tracks Recommendations") else: st.subheader("Recommendations") st.dataframe(df) def main(): spr_sidebar() if st.session_state.app_mode == 'Home': home_page() if st.session_state.app_mode == 'Result': result_page() if __name__ == '__main__': main()