forked from tkuar/project3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
97 lines (62 loc) · 3.19 KB
/
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
#import dependencies
from flask import Flask, jsonify, render_template, redirect
from keras.models import load_model
import pandas as pd
from pickle import load
import numpy as np
from scipy.spatial import distance
import random
billboard_model = load_model('./Models/billb_spot_DL.h5')
X_scaler = load(open('scaler.pkl', 'rb'))
centers = pd.read_csv('data/kmeans_centers_scaled.csv').drop(columns=['Unnamed: 0'])
clusters = pd.read_csv('data/kmeans_clusters_scaled.csv')
centers_array = centers.to_numpy()
app = Flask(__name__)
@app.route("/")
def deepBillBoard():
return render_template("index.html")
@app.route("/deepBillboard/song_param=<song_params>")
def deepSong(song_params):
song_param = song_params.split(',')
minput = pd.DataFrame({'genre': song_param[0],
'spotify_track_explicit': song_param[1],
'spotify_track_duration_ms': song_param[2],
'spotify_track_popularity': song_param[3],
'danceability': song_param[4],
'energy': song_param[5],
'key': song_param[6],
'loudness': song_param[6],
'mode': song_param[7],
'speechiness': song_param[8],
'acousticness': song_param[9],
'instrumentalness': song_param[10],
'liveness': song_param[11],
'valence': song_param[12],
'tempo': song_param[13],
'time_signature': song_param[14],
'week_pos': song_param[15],
'instance': song_param[16],
'weeks_on_chart':song_param[17]},
index = [0])
minput['genre'] = minput['genre'].replace('pop',float(0)).replace('country',float(1)).replace('hiphop',float(2)).replace('other',float(3)).replace('latin',float(4)).replace('house',float(5)).replace('folk',float(6)).replace('r&b',float(7)).replace('adult standards',float(8)).replace('rock',float(9)).replace('metal',float(10)).replace('show tunes',float(11)).replace('soul',float(12)).replace('rap',float(13)).replace('jazz',float(14))
minput = minput.astype(np.float)
minput_scaled = X_scaler.transform(minput)
nn = billboard_model(minput_scaled, training=False)
maxi = np.argmax(nn)
report = f'A song like this might land in the {(maxi+1)*10}th Percentile of the Billboard Chart! '
dists = []
for i in range(len(centers_array)):
dist = distance.euclidean(minput_scaled,centers_array[i])
dists.append(dist)
selection = np.argmin(dists)
similarSongs = clusters.loc[clusters['cluster']==selection]['track'].values.tolist()[0:11]
similarArtists = clusters.loc[clusters['cluster']==selection]['artist'].values.tolist()[0:11]
similarPeaks = clusters.loc[clusters['cluster']==selection]['peak_pos'].values.tolist()[0:11]
clusterAvg = clusters.loc[clusters['cluster']==selection]['peak_pos'].mean().round()
clusterSize = clusters.loc[clusters['cluster']==selection]['track_id'].nunique()
selections = random.sample(range(0,11),2)
reportout = {'peak_decile':str((maxi+1)*10), 'report':report, 'similar_songs': similarSongs, 'similar_artists':similarArtists, 'similar_peaks':similarPeaks,'cluster_avg':clusterAvg, 'selection':selections, 'cluster':f'{selection}','cluster_size':f'{clusterSize}'}
# return render_template("index.html",dict=reportout)
return(reportout)
if __name__ == "__main__":
app.run(debug=True)