-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-ml.py
More file actions
263 lines (233 loc) · 12.9 KB
/
Copy pathapp-ml.py
File metadata and controls
263 lines (233 loc) · 12.9 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import streamlit as st
import pickle
import numpy as np
model=pickle.load(open('spotify_skip_prediction_model.pkl','rb'))
def predict(session_position, session_length,context_switch,
no_pause_before_play, short_pause_before_play,
long_pause_before_play, hist_user_behavior_n_seekfwd,
hist_user_behavior_n_seekback, hist_user_behavior_is_shuffle,
hour_of_day, premium, context_type_editorial_playlist,
context_type_user_collection, context_type_radio,
context_type_personalized_playlist, context_type_catalog,
context_type_charts, hist_user_behavior_reason_start_trackdone,
hist_user_behavior_reason_start_fwdbtn,
hist_user_behavior_reason_start_backbtn,
hist_user_behavior_reason_start_clickrow,
hist_user_behavior_reason_start_appload,
hist_user_behavior_reason_start_playbtn,
hist_user_behavior_reason_start_remote,
hist_user_behavior_reason_start_trackerror,
hist_user_behavior_reason_start_endplay,
hist_user_behavior_reason_end_trackdone,
hist_user_behavior_reason_end_fwdbtn,
hist_user_behavior_reason_end_backbtn,
hist_user_behavior_reason_end_endplay,
hist_user_behavior_reason_end_logout,
hist_user_behavior_reason_end_remote, duration, release_year,
us_popularity_estimate, acousticness, beat_strength, bounciness,
danceability, dyn_range_mean, energy, flatness,
instrumentalness, key, liveness, loudness, mechanism, mode,
organism, speechiness, tempo, time_signature, valence,
day_of_week):
input=np.array([[session_position, session_length,context_switch,
no_pause_before_play, short_pause_before_play,
long_pause_before_play, hist_user_behavior_n_seekfwd,
hist_user_behavior_n_seekback, hist_user_behavior_is_shuffle,
hour_of_day, premium, context_type_editorial_playlist,
context_type_user_collection, context_type_radio,
context_type_personalized_playlist, context_type_catalog,
context_type_charts, hist_user_behavior_reason_start_trackdone,
hist_user_behavior_reason_start_fwdbtn,
hist_user_behavior_reason_start_backbtn,
hist_user_behavior_reason_start_clickrow,
hist_user_behavior_reason_start_appload,
hist_user_behavior_reason_start_playbtn,
hist_user_behavior_reason_start_remote,
hist_user_behavior_reason_start_trackerror,
hist_user_behavior_reason_start_endplay,
hist_user_behavior_reason_end_trackdone,
hist_user_behavior_reason_end_fwdbtn,
hist_user_behavior_reason_end_backbtn,
hist_user_behavior_reason_end_endplay,
hist_user_behavior_reason_end_logout,
hist_user_behavior_reason_end_remote, duration, release_year,
us_popularity_estimate, acousticness, beat_strength, bounciness,
danceability, dyn_range_mean, energy, flatness,
instrumentalness, key, liveness, loudness, mechanism, mode,
organism, speechiness, tempo, time_signature, valence,
day_of_week]]).astype(np.float64)
prediction=model.predict(input)
return float(prediction)
def main():
st.title("Spotify music skip prediction")
html_temp = """
<div style="background-color:#025246 ;padding:10px">
<h2 style="color:white;text-align:center;">Spotify Skip Prediction ML App</h2>
</div>
"""
st.markdown(html_temp, unsafe_allow_html=True)
session_position = float(st.slider("Session position",1, 20,1))
session_length = float(st.slider("Session length",10, 20,10))
context_switch = float(st.slider("Context Switch",0, 1,0))
no_pause_before_play = float(st.slider("No Pause before play",0, 1,0))
short_pause_before_play = float(st.slider("Short pause before play",0, 1,0))
long_pause_before_play = float(st.slider("Long pause before play",0, 1,0))
hist_user_behavior_n_seekfwd = float(st.slider("Number of seek forward times",0, 60,0))
hist_user_behavior_n_seekback = float(st.slider("Number of seek back times",0, 150,0))
hist_user_behavior_is_shuffle = float(st.slider("User encountered this track while shuffle was on",0, 1,0))
hour_of_day = float(st.slider("Hour of the day",0, 23,0))
premium = float(st.slider("Premium User",0, 1,0))
context_type_editorial_playlist = 0.0
context_type_user_collection = 0.0
context_type_radio = 0.0
context_type_personalized_playlist = 0.0
context_type_catalog = 0.0
context_type_charts = 0.0
context_type = st.radio("what type of context the playback occurred within",('Editorial Playlist', 'User Collection', 'Radio','Personalized Playlist','Catalog','Charts'))
if context_type=='Editorial Playlist':
context_type_editorial_playlist=1.0
elif context_type == 'User Collection':
context_type_user_collection = 1.0
elif context_type== 'Radio':
context_type_radio = 1.0
elif context_type== 'Personalized Playlist':
context_type_personalized_playlist=1.0
elif context_type=='Catalog':
context_type_catalog = 1.0
else:
context_type_charts = 1.0
# context_type_editorial_playlist = st.slider("Editorial Playlist?",0, 1,0)
# context_type_user_collection = st.slider("User Collection?",0, 1,0)
# context_type_radio = st.slider("Radio?",0, 1,0)
# context_type_personalized_playlist = st.slider("Personalized Playlist?",0, 1,0)
# context_type_catalog = st.slider("Catalog?",0, 1,0)
# context_type_charts = st.slider("Charts?",0, 1,0)
hist_user_behavior_reason_start_trackdone = 0.0
hist_user_behavior_reason_start_fwdbtn = 0.0
hist_user_behavior_reason_start_backbtn=0.0
hist_user_behavior_reason_start_clickrow=0.0
hist_user_behavior_reason_start_appload= 0.0
hist_user_behavior_reason_start_playbtn= 0.0
hist_user_behavior_reason_start_remote= 0.0
hist_user_behavior_reason_start_trackerror= 0.0
hist_user_behavior_reason_start_endplay= 0.0
hist_user_behavior_reason_start = st.radio("The user action which led to the current track being played:",('Trackdone', 'Forward Button', 'Back Button','Click','Appload','Play Button','Remote','Track Error', 'End Play' ))
if hist_user_behavior_reason_start == 'Trackdone':
hist_user_behavior_reason_start_trackdone = 1.0
elif hist_user_behavior_reason_start == 'Forward Button':
hist_user_behavior_reason_start_fwdbtn = 1.0
elif hist_user_behavior_reason_start == 'Back Button':
hist_user_behavior_reason_start_backbtn = 1.0
elif hist_user_behavior_reason_start == 'Click':
hist_user_behavior_reason_start_clickrow = 1.0
elif hist_user_behavior_reason_start == 'Appload':
hist_user_behavior_reason_start_appload =1.0
elif hist_user_behavior_reason_start == 'Play Button':
hist_user_behavior_reason_start_playbtn =1.0
elif hist_user_behavior_reason_start == 'Remote':
hist_user_behavior_reason_start_remote =1.0
elif hist_user_behavior_reason_start == 'Track Error':
hist_user_behavior_reason_start_trackerror =1.0
else:
hist_user_behavior_reason_start_endplay =1
# hist_user_behavior_reason_start_trackdone = st.slider("Reason to start is trackdone?",0, 1,0)
# hist_user_behavior_reason_start_fwdbtn = st.slider("Reason to start is forward button?",0, 1,0)
# hist_user_behavior_reason_start_backbtn= st.slider("Reason to start is back button?",0, 1,0)
# hist_user_behavior_reason_start_clickrow= st.slider("Reason to start is click?",0, 1,0)
# hist_user_behavior_reason_start_appload= st.slider("Reason to start is appload?",0, 1,0)
# hist_user_behavior_reason_start_playbtn= st.slider("Reason to start is play button?",0, 1,0)
# hist_user_behavior_reason_start_remote= st.slider("Reason to start is remote?",0, 1,0)
# hist_user_behavior_reason_start_trackerror= st.slider("Reason to start is trackerror?",0, 1,0)
# hist_user_behavior_reason_start_endplay= st.slider("Reason to start is endplay?",0, 1,0)
hist_user_behavior_reason_end_trackdone= 0.0
hist_user_behavior_reason_end_fwdbtn= 0.0
hist_user_behavior_reason_end_backbtn= 0.0
hist_user_behavior_reason_end_endplay= 0.0
hist_user_behavior_reason_end_logout= 0.0
hist_user_behavior_reason_end_remote= 0.0
hist_user_behavior_reason_end = st.radio("The user action which led to the current track playback ending:",('Trackdone', 'Forward Button', 'Back Button','Remote','Logout', 'End Play' ))
if hist_user_behavior_reason_end == 'Trackdone':
hist_user_behavior_reason_end_trackdone = 1.0
elif hist_user_behavior_reason_end == 'Forward Button':
hist_user_behavior_reason_end_fwdbtn = 1.0
elif hist_user_behavior_reason_end == 'Back Button':
hist_user_behavior_reason_end_backbtn = 1.0
elif hist_user_behavior_reason_end == 'Remote':
hist_user_behavior_reason_end_remote =1.0
elif hist_user_behavior_reason_end == 'End Play':
hist_user_behavior_reason_end_endplay =1.0
elif hist_user_behavior_reason_end == 'Logout':
hist_user_behavior_reason_end_logout =1.0
# hist_user_behavior_reason_end_trackdone= st.slider("Reason to end is trackdone?",0, 1,0)
# hist_user_behavior_reason_end_fwdbtn= st.slider("Reason to end is forward button?",0, 1,0)
# hist_user_behavior_reason_end_backbtn= st.slider("Reason to end is back button?",0, 1,0)
# hist_user_behavior_reason_end_endplay= st.slider("Reason to end is end play?",0, 1,0)
# hist_user_behavior_reason_end_logout= st.slider("Reason to end is logout?",0, 1,0)
# hist_user_behavior_reason_end_remote= st.slider("Reason to end is remote?",0, 1,0)
duration = float(st.slider("Duration",30, 1790,30))
release_year = float(st.slider("Release year",1950, 2018,1950))
us_popularity_estimate= float(st.slider("US popularity estimate",90, 100,90))
acousticness = float(st.slider("Acoustiness",0, 1,0))
bounciness = float(st.slider("Bounciness",0, 1,0))
beat_strength = float(st.slider("Beat strength",0, 1,0))
danceability = float(st.slider("Danceability",0, 1,0))
dyn_range_mean = float(st.slider("Dyn range mean",0, 1,0))
energy = float(st.slider("Energy",0, 1,0))
flatness = float(st.slider("Flaness",0, 1,0))
instrumentalness = float(st.slider("Instrumentalness",0, 1,0))
key = float(st.slider("Key",0, 11,0))
liveness = float(st.slider("Liveness",0, 1,0))
loudness = float(st.slider("Loudness",-60, 1,-60))
mechanism = float(st.slider("Mechanism",0, 1,0))
mode = float(st.slider("Mode",0, 1,0))
organism = float(st.slider("Organism",0, 1,0))
speechiness = float(st.slider("Speechiness",0, 1,0))
tempo = float(st.slider("Tempo",0, 218,0))
time_signature = float(st.slider("Time signature",0, 5,0))
valence = float(st.slider("Valence",0, 1,0))
day_of_week = float(st.slider("Day of week", 0,6,0))
safe_html="""
<div style="background-color:#F4D03F;padding:10px >
<h2 style="color:white;text-align:center;"> Not Skipped</h2>
</div>
"""
danger_html="""
<div style="background-color:#F08080;padding:10px >
<h2 style="color:black ;text-align:center;"> Skipped</h2>
</div>
"""
if st.button("Predict"):
output=predict(session_position, session_length,context_switch,
no_pause_before_play, short_pause_before_play,
long_pause_before_play, hist_user_behavior_n_seekfwd,
hist_user_behavior_n_seekback, hist_user_behavior_is_shuffle,
hour_of_day, premium, context_type_editorial_playlist,
context_type_user_collection, context_type_radio,
context_type_personalized_playlist, context_type_catalog,
context_type_charts, hist_user_behavior_reason_start_trackdone,
hist_user_behavior_reason_start_fwdbtn,
hist_user_behavior_reason_start_backbtn,
hist_user_behavior_reason_start_clickrow,
hist_user_behavior_reason_start_appload,
hist_user_behavior_reason_start_playbtn,
hist_user_behavior_reason_start_remote,
hist_user_behavior_reason_start_trackerror,
hist_user_behavior_reason_start_endplay,
hist_user_behavior_reason_end_trackdone,
hist_user_behavior_reason_end_fwdbtn,
hist_user_behavior_reason_end_backbtn,
hist_user_behavior_reason_end_endplay,
hist_user_behavior_reason_end_logout,
hist_user_behavior_reason_end_remote, duration, release_year,
us_popularity_estimate, acousticness, beat_strength, bounciness,
danceability, dyn_range_mean, energy, flatness,
instrumentalness, key, liveness, loudness, mechanism, mode,
organism, speechiness, tempo, time_signature, valence,
day_of_week)
# st.success('The probability of user skipping the song is {}'.format(output))
if output > 0.5 :
st.markdown(danger_html,unsafe_allow_html=True)
else:
st.markdown(safe_html,unsafe_allow_html=True)
if __name__=='__main__':
main()