File size: 9,618 Bytes
9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 545f761 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 9b6c8d0 eebbb84 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
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("<br>", 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()
|