forked from Aditya020224/Music-Recommendation-Sys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp1.py
More file actions
206 lines (177 loc) · 8.2 KB
/
app1.py
File metadata and controls
206 lines (177 loc) · 8.2 KB
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
import streamlit as st
import pandas as pd
from sklearn.neighbors import NearestNeighbors
import plotly.express as px
import uuid
import streamlit.components.v1 as components
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
st.set_page_config(page_title="Music Recommendation", layout="wide")
@st.cache_data()
def load_data():
df = pd.read_csv("data/processed_track_df.csv")
df['genres'] = df.genres.apply(lambda x: [i[1:-1] for i in str(x)[1:-1].split(", ")])
exploded_track_df = df.explode("genres")
return exploded_track_df
genre_names = ['Dance Pop', 'Electronic', 'Electropop', 'Hip Hop', 'Jazz', 'K-pop', 'Latin', 'Pop', 'Pop Rap', 'R&B', 'Rock']
audio_feats = ["acousticness", "danceability", "energy", "instrumentalness", "valence", "tempo"]
exploded_track_df = load_data()
def n_neighbors_uri_audio(genre, start_year, end_year, test_feat):
genre = genre.lower()
genre_data = exploded_track_df[(exploded_track_df["genres"]==genre) & (exploded_track_df["release_year"]>=start_year) & (exploded_track_df["release_year"]<=end_year)]
genre_data = genre_data.sort_values(by='popularity', ascending=False)[:500]
neigh = NearestNeighbors()
neigh.fit(genre_data[audio_feats].to_numpy())
n_neighbors = neigh.kneighbors([test_feat], n_neighbors=len(genre_data), return_distance=False)[0]
uris = genre_data.iloc[n_neighbors]["uri"].tolist()
audios = genre_data.iloc[n_neighbors][audio_feats].to_numpy()
return uris, audios
def login_page():
st.title("User Login")
username = st.text_input("Username", key="login_username")
password = st.text_input("Password", type="password", key="login_password")
if st.button("Login"):
user_data = pd.read_csv("user_data.csv")
if username in user_data["Username"].values and password in user_data["Password"].values:
st.success("Login successful!")
st.session_state["is_logged_in"] = True
else:
st.warning("Invalid username or password.")
st.markdown("Don't have an account? [Create one](?register=true)")
def register_page():
st.write("Enter your details below to create a new account.")
first_name = st.text_input("First name", key="register_first_name")
last_name = st.text_input("Last name", key="register_last_name")
email = st.text_input("Email", key="register_email")
password = st.text_input("Password", type="password", key="register_password")
if st.button("Create Account", key="register_button"):
if first_name and last_name and email and password:
user_data = {
"Username": [first_name],
"Password": [password]
}
df = pd.DataFrame(user_data)
df.to_csv("user_data.csv", index=False)
st.success("Account created successfully! Please log in.")
st.experimental_rerun()
else:
st.warning("Please enter all of the required details.")
def recommendation_page():
title = "Music Recommendation System"
st.title(title)
st.write("Welcome! Here you can customize what you want to listen to based on genres. And listen to the songs recommended by our system!")
st.markdown("##")
with st.container():
col1, col2,col3,col4 = st.columns((2,0.5,0.5,0.5))
with col1:
st.markdown("***Customize Features :***")
start_year, end_year = st.slider(
'Select the year range',
1990, 2019, (2010, 2019)
)
acousticness = st.slider(
'Acousticness',
0.0, 1.0, 0.5
)
danceability = st.slider(
'Danceability',
0.0, 1.0, 0.5
)
energy = st.slider(
'Energy',
0.0, 1.0, 0.5
)
instrumentalness = st.slider(
'Instrumentalness',
0.0, 1.0, 0.0
)
valence = st.slider(
'Valence',
0.0, 1.0, 0.45
)
tempo = st.slider(
'Tempo',
0.0, 244.0, 118.0
)
with col3:
st.sidebar.header("***Select genre:***")
genre = st.sidebar.radio(
"",
genre_names, index=genre_names.index("K-pop"))
tracks_per_page = 8
test_feat = [acousticness, danceability, energy, instrumentalness, valence, tempo]
uris, audios = n_neighbors_uri_audio(genre, start_year, end_year, test_feat)
tracks = []
for uri in uris:
track = """<iframe src="https://open.spotify.com/embed/track/{}" width="260" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>""".format(uri)
tracks.append(track)
if 'previous_inputs' not in st.session_state:
st.session_state['previous_inputs'] = [genre, start_year, end_year] + test_feat
current_inputs = [genre, start_year, end_year] + test_feat
if current_inputs != st.session_state['previous_inputs']:
if 'start_track_i' in st.session_state:
st.session_state['start_track_i'] = 0
st.session_state['previous_inputs'] = current_inputs
if 'start_track_i' not in st.session_state:
st.session_state['start_track_i'] = 0
with st.container():
col1, col2, col3 = st.columns([2,1,2])
if st.button("Recommend More Songs"):
if st.session_state['start_track_i'] < len(tracks):
st.session_state['start_track_i'] += tracks_per_page
current_tracks = tracks[st.session_state['start_track_i']: st.session_state['start_track_i'] + tracks_per_page]
current_audios = audios[st.session_state['start_track_i']: st.session_state['start_track_i'] + tracks_per_page]
if st.session_state['start_track_i'] < len(tracks):
for i, (track, audio) in enumerate(zip(current_tracks, current_audios)):
if i%2==0:
with col1:
components.html(
track,
height=400,
)
with st.expander("See more details"):
df = pd.DataFrame(dict(
r=audio[:5],
theta=audio_feats[:5]))
fig = px.line_polar(df, r='r', theta='theta', line_close=True)
fig.update_layout(height=400, width=340)
fig.update_traces(fill='toself')
st.plotly_chart(fig)
else:
with col3:
components.html(
track,
height=400,
)
with st.expander("See more details"):
df = pd.DataFrame(dict(
r=audio[:5],
theta=audio_feats[:5]))
fig = px.line_polar(df, r='r', theta='theta', line_close=True)
fig.update_layout(height=400, width=340)
fig.update_traces(fill='toself')
st.plotly_chart(fig)
else:
st.write("No songs left to recommend")
def main():
st.session_state.setdefault("is_logged_in", False)
if not st.session_state["is_logged_in"]:
if "register" in st.experimental_get_query_params():
st.session_state["register"] = True
register_page()
st.stop()
else:
if not st.session_state.get("is_csv_created", False):
df = pd.DataFrame(columns=["Username", "Password"])
df.to_csv("user_data.csv", index=False)
st.session_state["is_csv_created"] = True
login_page()
if "register" in st.session_state and st.session_state["register"]:
register_page()
st.stop()
if st.session_state["is_logged_in"]:
recommendation_page()
else:
recommendation_page()
if __name__ == "__main__":
main()