seyia92coding commited on
Commit
9ebe882
1 Parent(s): c049c60

Upload most_popular_albums_per_artist_with_gradio_v16_06_24.py

Browse files
most_popular_albums_per_artist_with_gradio_v16_06_24.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Most Popular Albums Per Artist With Gradio_V16-06-24.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1wx0n0CuWwG6Pn034qj-vQJPklYLab2-Y
8
+
9
+ # Set up Spotify credentials
10
+
11
+ Before getting started you need:
12
+
13
+ * Spotify API permissions & credentials that could apply for [here](https://developer.spotify.com/). Simply log in, go to your “dashboard” and select “create client id” and follow the instructions. Spotify are not too strict on providing permissions so put anything you like when they ask for commercial application.
14
+
15
+ * Python module — spotipy — imported
16
+ """
17
+
18
+ !pip install gradio
19
+
20
+ !pip install spotipy
21
+ import spotipy
22
+ #To access authorised Spotify data - https://developer.spotify.com/
23
+ from spotipy.oauth2 import SpotifyClientCredentials
24
+ !pip install fuzzywuzzy
25
+ from fuzzywuzzy import fuzz
26
+ import pandas as pd
27
+ import seaborn as sns
28
+ !pip install gradio
29
+ import gradio as gr
30
+ #https://gradio.app/docs/#i_slider
31
+ import matplotlib.pyplot as plt
32
+ import time
33
+ import numpy as np
34
+
35
+ #Create Function for identifying your artist
36
+
37
+ def choose_artist(name_input, sp):
38
+ results = sp.search(name_input)
39
+ #result_1 = result['tracks']['items'][0]['artists']
40
+ top_matches = []
41
+ counter = 0
42
+ #for each result item (max 10 I think)
43
+ for i in results['tracks']['items']:
44
+ #store current item
45
+ current_item = results['tracks']['items'][counter]['artists']
46
+ counter+=1
47
+ #for each item in that search_term
48
+ counter2 = 0
49
+ for i in current_item:
50
+ #append artist name to top_matches
51
+ #I will need to append something to identify the correct match, please update once I know
52
+ top_matches.append((current_item[counter2]['name'], current_item[counter2]['uri']))
53
+ counter2+=1
54
+
55
+ #remove duplicates by turning list into a set, then back into a list
56
+ top_matches = list(set(top_matches))
57
+
58
+ fuzzy_matches = []
59
+ #normal list doesn't need len(range)
60
+ for i in top_matches:
61
+ #put ratio result in variable to avoid errors
62
+ ratio = fuzz.ratio(name_input, i[0])
63
+ #store as tuple but will need to increase to 3 to include uid
64
+ fuzzy_matches.append((i[0], ratio, i[1]))
65
+ #sort fuzzy matches by ratio score
66
+ fuzzy_matches = sorted(fuzzy_matches, key=lambda tup: tup[1], reverse=True)
67
+ #store highest tuple's attributes in chosen variables
68
+ chosen = fuzzy_matches[0][0]
69
+ chosen_id = fuzzy_matches[0][1]
70
+ chosen_uri = fuzzy_matches[0][2]
71
+ print("The results are based on the artist: ", chosen)
72
+ return chosen, chosen_id, chosen_uri
73
+
74
+ #Function to Pull all of your artist's albums
75
+ def find_albums(artist_uri, sp):
76
+ sp_albums = sp.artist_albums(artist_uri, album_type='album', limit=50) #There's a 50 album limit
77
+ album_names = []
78
+ album_uris = []
79
+ for i in range(len(sp_albums['items'])):
80
+ #Keep names and uris in same order to keep track of duplicate albums
81
+ album_names.append(sp_albums['items'][i]['name'])
82
+ album_uris.append(sp_albums['items'][i]['uri'])
83
+ return album_uris, album_names
84
+
85
+ #Function to store all album details along with their song details
86
+ def albumSongs(album, sp, album_count, album_names, spotify_albums):
87
+ spotify_albums[album] = {} #Creates dictionary for that specific album
88
+ #Create keys-values of empty lists inside nested dictionary for album
89
+ spotify_albums[album]['album_name'] = [] #create empty list
90
+ spotify_albums[album]['track_number'] = []
91
+ spotify_albums[album]['song_id'] = []
92
+ spotify_albums[album]['song_name'] = []
93
+ spotify_albums[album]['song_uri'] = []
94
+
95
+ tracks = sp.album_tracks(album) #pull data on album tracks
96
+
97
+ for n in range(len(tracks['items'])): #for each song track
98
+ spotify_albums[album]['album_name'].append(album_names[album_count]) #append album name tracked via album_count
99
+ spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number'])
100
+ spotify_albums[album]['song_id'].append(tracks['items'][n]['id'])
101
+ spotify_albums[album]['song_name'].append(tracks['items'][n]['name'])
102
+ spotify_albums[album]['song_uri'].append(tracks['items'][n]['uri'])
103
+
104
+ #Add popularity category
105
+ def popularity(album, sp, spotify_albums):
106
+ #Add new key-values to store audio features
107
+ spotify_albums[album]['popularity'] = []
108
+ #create a track counter
109
+ track_count = 0
110
+ for track in spotify_albums[album]['song_uri']:
111
+ #pull audio features per track
112
+ pop = sp.track(track)
113
+ spotify_albums[album]['popularity'].append(pop['popularity'])
114
+ track_count+=1
115
+
116
+ def gradio_music_graph(client_id, client_secret, artist_name): #total_albums
117
+ #Insert your credentials
118
+ client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
119
+ sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) #spotify object to access API
120
+ #Choose your artist via input
121
+ chosen_artist, ratio_score, artist_uri = choose_artist(artist_name, sp=sp)
122
+ #Retrieve their album details
123
+ album_uris, album_names = find_albums(artist_uri, sp=sp)
124
+ #Create dictionary to store all the albums
125
+ spotify_albums = {}
126
+ #Album count tracker
127
+ album_count = 0
128
+ for i in album_uris: #for each album
129
+ albumSongs(i, sp=sp, album_count=album_count, album_names=album_names, spotify_albums=spotify_albums)
130
+ print("Songs from " + str(album_names[album_count]) + " have been added to spotify_albums dictionary")
131
+ album_count+=1 #Updates album count once all tracks have been added
132
+
133
+ #To avoid it timing out
134
+ sleep_min = 2
135
+ sleep_max = 5
136
+ start_time = time.time()
137
+ request_count = 0
138
+ #Update albums with popularity scores
139
+ for album in spotify_albums:
140
+ popularity(album, sp=sp, spotify_albums=spotify_albums)
141
+ request_count+=1
142
+ if request_count % 5 == 0:
143
+ # print(str(request_count) + " playlists completed")
144
+ time.sleep(np.random.uniform(sleep_min, sleep_max))
145
+ # print('Loop #: {}'.format(request_count))
146
+ # print('Elapsed Time: {} seconds'.format(time.time() - start_time))
147
+
148
+ #Create song dictonary to convert into Dataframe
149
+ dic_df = {}
150
+
151
+ dic_df['album_name'] = []
152
+ dic_df['track_number'] = []
153
+ dic_df['song_id'] = []
154
+ dic_df['song_name'] = []
155
+ dic_df['song_uri'] = []
156
+ dic_df['popularity'] = []
157
+
158
+ for album in spotify_albums:
159
+ for feature in spotify_albums[album]:
160
+ dic_df[feature].extend(spotify_albums[album][feature])
161
+ #Convert into dataframe
162
+
163
+ df = pd.DataFrame.from_dict(dic_df)
164
+ df = df.sort_values(by='popularity')
165
+ df = df.drop_duplicates(subset=['song_id'], keep=False)
166
+
167
+ #Parameters of a Original Plot, unfortunately, no longer works (for now)
168
+ # sns.set_style('ticks')
169
+
170
+ # fig, ax = plt.subplots()
171
+ # fig.set_size_inches(11, 8)
172
+ # ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
173
+ # plt.tight_layout()
174
+
175
+ # sns.boxplot(x=df["album_name"], y=df["popularity"], ax=ax)
176
+ # fig.savefig('artist_popular_albums.png')
177
+ # plt.show()
178
+
179
+ return df
180
+
181
+ plot = gr.ScatterPlot(x="album_name", y="popularity", width=600, height=350, title="Popular Songs By Album Box Plot Distribution on Spotify")
182
+ #Interface will include these buttons based on parameters in the function with a dataframe output
183
+
184
+ #Ignore, old code
185
+ #music_plots = gr.Interface(gradio_music_graph, ["text", "text", "text"],
186
+ # ["dataframe", "plot"], title="Popular Songs By Album Box Plot Distribution on Spotify", description="Using your Spotify API Access from https://developer.spotify.com/ you can see your favourite artist's most popular albums on Spotify")
187
+
188
+ music_plots = gr.Interface(fn=gradio_music_graph, inputs=["text","text","text"], outputs=plot)
189
+
190
+
191
+ music_plots.launch(debug=True)