-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
167 lines (115 loc) · 4.81 KB
/
main.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
166
167
import json
import joblib
import os
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
def get_song_data(path):
try:
song_data = pd.read_csv(path)
return song_data
except FileNotFoundError:
raise ValueError("No file found.")
def retrieve_song_features(song_data):
song_data_features = song_data[['valence',
'year',
'acousticness',
'danceability',
'energy',
'instrumentalness',
'loudness',
'popularity',
'speechiness',
'tempo']]
return song_data_features
def train_pipeline(songs_features):
pipeline = Pipeline([('scaler', StandardScaler()), ('kmeans', KMeans(n_clusters=5))])
pipeline.fit(songs_features)
return pipeline
def label_songs(songs_features, pipeline):
songs_features['k_mean'] = pipeline.predict(songs_features)
return songs_features
def search_by_artist(artist):
searched_songs = spotify.search(q='artist=%s' % artist)
if searched_songs['tracks']['total'] == 0:
raise ValueError('No song searched.')
songs_df = []
for song in searched_songs['tracks']['items']:
track_id = song['id']
features = spotify.audio_features(track_id)[0]
songs_data = {
'valence': features['valence'],
'year': song['album']['release_date'],
'acousticness': features['acousticness'],
'danceability': features['danceability'],
'energy': features['energy'],
'instrumentalness': features['instrumentalness'],
'loudness': features['loudness'],
'popularity': song['popularity'],
'speechiness': features['speechiness'],
'tempo': features['tempo'],
}
songs_df.append(songs_data)
songs_df = pd.DataFrame(songs_df)
songs_df['year'] = songs_df['year'].str[:4].astype(int)
songs_mean = songs_df.mean(axis=0)
return pd.DataFrame(songs_mean).transpose()
def classify_song(song, pipeline):
return pipeline.predict(song)
def get_recommended_cluster(artists, pipeline):
cluster_idx = []
for artist in artists:
cluster_idx.append(classify_song(search_by_artist(artist), pipeline))
count = []
for i in range(5):
count.append(cluster_idx.count(i))
max_val = max(count)
cluster_idx = [i for i, v in enumerate(count) if v == max_val]
return cluster_idx[0]
def recommend_songs(pipeline, songs, labeled_songs):
artists = input('Type your favorite artists with comma: ')
try:
n = int(input('How many songs do you want to get?: '))
except ValueError:
raise ValueError("It's not a number.")
if ',' in artists:
artists = artists.split(',')
else:
artists = [artists]
cluster_idx = get_recommended_cluster(artists, pipeline)
sample_songs = labeled_songs.loc[labeled_songs['k_mean'] == cluster_idx].sample(n)
sample_songs_idx = sample_songs.index.values
sample_songs = songs.loc[sample_songs_idx]
sample_songs = sample_songs[['name', 'artists', 'release_date']]
cluster_songs = []
for j in sample_songs_idx:
cluster_songs.append(sample_songs.loc[j].tolist())
return cluster_songs, cluster_idx
if __name__ == '__main__':
current_path = os.path.dirname(os.path.abspath(__file__))
secret_file_path = os.path.join(current_path, 'secret.json')
with open(secret_file_path, 'r') as secret_file:
secret_data = json.load(secret_file)
client_id = secret_data.get("client_id", None)
client_secret = secret_data.get("client_secret", None)
spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
client_id=client_id,
client_secret=client_secret)
)
songs = get_song_data(current_path + '/data.csv')
songs_features = retrieve_song_features(songs)
pipeline = train_pipeline(songs_features)
# joblib.dump(pipeline, 'trained_pipeline.pkl')
labeled_songs = label_songs(songs_features, pipeline)
cluster_dic = {0: 'upbeat', 1: 'decent', 2: 'mood', 3: 'unique', 4: 'club'}
recommended_songs, cluster_idx = recommend_songs(pipeline, songs, labeled_songs)
print(f'Seems like you like {cluster_dic[cluster_idx]} songs!')
print("Here's some song recommendations for you!")
for song in recommended_songs:
print("*" * 10)
print(f"Title: {song[0]}")
print(f"Artist: {song[1][1:-1]}")
print(f"Released date: {song[2]}")