-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_app.py
165 lines (119 loc) · 5.74 KB
/
streamlit_app.py
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
import streamlit as st
import utils.functions as f
# Define model to use
model_name = 'kmeans_17'
def update_popularity():
# Dictionary to convert popularity slider values to min and max values
pop_dict = {'Hidden Gem': 0,
'Underrated': 25,
'Trending': 50,
'Popular': 75,
'Super hit': 100}
st.session_state['popularity_min'] = pop_dict[st.session_state['pop_slider'][0]]
st.session_state['popularity_max'] = pop_dict[st.session_state['pop_slider'][1]]
option_song_clicked() # Call function to update recommendations
def start_again():
for key in st.session_state.keys():
del st.session_state[key]
def like_song_clicked():
track_name = st.session_state['track_name']
artist = st.session_state['artist']
if track_name != "":
sp_song_data = f.search_song(track_name, artist)
st.session_state['sp_song_data'] = sp_song_data
if len(sp_song_data) == 0:
st.session_state['current_page'] = 1
st.info('No songs found.')
else:
st.session_state['current_page'] = 2
def option_song_clicked():
for item in st.session_state.items():
if (item[1] == True) & (item[0][:2] == 'so'): # if it is search option button and its true
st.session_state['selected_song_index'] = int(item[0][-1])
st.session_state['current_page'] = 3
st.session_state['track_id'] = st.session_state['sp_song_data'][st.session_state['selected_song_index']]['track_id']
st.session_state['recommended_songs'] = f.recommend_song(model_name,
st.session_state['track_id'],
st.session_state['popularity_min'],
st.session_state['popularity_max']).to_dict(orient="records")
def main():
if 'current_page' not in st.session_state:
st.session_state['current_page'] = 1
if 'track_name' not in st.session_state:
st.session_state['track_name'] = ""
if 'artist' not in st.session_state:
st.session_state['artist'] = ""
if 'sp_song_data' not in st.session_state:
st.session_state['sp_song_data'] = []
if 'popularity_min' not in st.session_state:
st.session_state['popularity_min'] = 0
st.session_state['popularity_max'] = 100
# Logo
st.image('./resources/melodymind_logo_v5.png', width=150)
st.divider()
# --- PAGE 1 ---
if st.session_state['current_page'] == 1:
# Track name input box
st.text_input('Type a song that you like', key='track_name')
# Artist input box
st.text_input('Type the artist (optional)', key='artist')
# Search button
search_button = st.button('Search song', on_click=like_song_clicked)
if search_button:
if st.session_state['track_name'] == "":
st.error("You must input a song.")
# --- PAGE 2 ---
elif st.session_state['current_page'] == 2:
# --- HEADER ---
st.subheader('Choose a song...')
st.caption("Click on 'I like this song!' to get recommendations.")
st.divider()
# --- SONG SEARCH RESULTS ---
for index, song in enumerate(st.session_state['sp_song_data']):
col1, col2 = st.columns(2)
with col1:
st.subheader(song['track_name'])
st.caption(song['artist'])
st.write(f.convert_seconds_to_min_seconds(song['track_duration']))
st.write('Album: *'+str(song['album_name'])+'*')
st.write('Released: ', song['album_release_year'])
with col2:
st.image(song['album_image'],width=125)
st.button('I like this song!', on_click=option_song_clicked, key='so_button_'+str(index))
st.divider()
# --- PAGE 3 ---
elif st.session_state['current_page'] == 3:
# --- HEADER ---
col1, col2 = st.columns([3, 1])
with col1:
st.subheader('You will also like these songs!')
st.caption('Find below a list of recommended songs based on your selection.')
with col2:
st.button('Start again', on_click=start_again)
# --- POPULARITY FILTER ---
st.caption('Use the popularity filter to narrow down your results:')
st.select_slider('',
options=['Hidden Gem', 'Underrated', 'Trending', 'Popular', 'Super hit'],
value=('Hidden Gem', 'Super hit'),
on_change=update_popularity,
key='pop_slider')
st.divider()
# --- RECOMMENDED SONGS ---
recommended_songs = st.session_state['recommended_songs']
if len(recommended_songs) == 0:
st.info('No recommendations available. Consider widening the popularity filter.')
for index, song in enumerate(recommended_songs):
col1, col2 = st.columns(2)
with col1:
st.subheader(song['track_name'])
st.caption(song['artist'])
st.write(f.convert_seconds_to_min_seconds(song['track_duration']))
st.write('Album: *'+str(song['album_name'])+'*')
st.write('Released: ', song['album_release_year'])
st.write('Popularity: ', song['popularity'])
with col2:
st.image(song['album_image'], width=125)
st.markdown('[Listen on Spotify!](' + song['track_link'] + ')')
st.divider()
if __name__ == "__main__":
main()