File size: 10,819 Bytes
f4c8685 dad89e4 f4c8685 7c4e98c f4c8685 6868ece f4c8685 0d5194e f4c8685 dad89e4 e239738 83c9d16 e239738 f7e616d e239738 f4c8685 83c9d16 f4c8685 7c4e98c 0670634 9baeb63 83c9d16 3020ad1 506e32d 5c5363c 9baeb63 0670634 9baeb63 7570efe 9baeb63 0670634 dad89e4 9baeb63 dad89e4 6868ece 83c9d16 6868ece 83c9d16 6868ece 9baeb63 6868ece 0670634 7570efe 0670634 c66809e 0670634 e239738 7570efe 9baeb63 0670634 c66809e 7570efe c66809e 7570efe 9baeb63 7570efe 0670634 f4c8685 |
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
import json
from urllib import request
from fastapi import FastAPI
from starlette.middleware.sessions import SessionMiddleware
from starlette.responses import HTMLResponse, RedirectResponse
from starlette.requests import Request
import gradio as gr
import uvicorn
from fastapi.responses import HTMLResponse
from fastapi.responses import RedirectResponse
import pandas as pd
import spotipy
from spotipy import oauth2
import heatmap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, RegularPolygon
from matplotlib.path import Path
from matplotlib.projections.polar import PolarAxes
from matplotlib.projections import register_projection
from matplotlib.spines import Spine
from matplotlib.transforms import Affine2D
import matplotlib
matplotlib.use('SVG')
def get_features2(spotify):
features = []
for index in range(0, 10):
results = spotify.current_user_saved_tracks(offset=index*50, limit=50)
track_ids = [item['track']['id'] for item in results['items']]
features.extend(spotify.audio_features(track_ids))
df = pd.DataFrame(data=features)
names = [
'danceability',
'energy',
# 'loudness',
'speechiness',
'acousticness',
'instrumentalness',
'liveness',
'valence',
]
features_means = df[names].mean()
return names, features_means.values
def radar_factory(num_vars, frame='circle'):
"""
Create a radar chart with `num_vars` axes.
This function creates a RadarAxes projection and registers it.
Parameters
----------
num_vars : int
Number of variables for radar chart.
frame : {'circle', 'polygon'}
Shape of frame surrounding axes.
"""
# calculate evenly-spaced axis angles
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
class RadarTransform(PolarAxes.PolarTransform):
def transform_path_non_affine(self, path):
# Paths with non-unit interpolation steps correspond to gridlines,
# in which case we force interpolation (to defeat PolarTransform's
# autoconversion to circular arcs).
if path._interpolation_steps > 1:
path = path.interpolated(num_vars)
return Path(self.transform(path.vertices), path.codes)
class RadarAxes(PolarAxes):
name = 'radar'
PolarTransform = RadarTransform
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# rotate plot such that the first axis is at the top
self.set_theta_zero_location('N')
def fill(self, *args, closed=True, **kwargs):
"""Override fill so that line is closed by default"""
return super().fill(closed=closed, *args, **kwargs)
def plot(self, *args, **kwargs):
"""Override plot so that line is closed by default"""
lines = super().plot(*args, **kwargs)
for line in lines:
self._close_line(line)
def _close_line(self, line):
x, y = line.get_data()
# FIXME: markers at x[0], y[0] get doubled-up
if x[0] != x[-1]:
x = np.append(x, x[0])
y = np.append(y, y[0])
line.set_data(x, y)
def set_varlabels(self, labels):
self.set_thetagrids(np.degrees(theta), labels)
def _gen_axes_patch(self):
# The Axes patch must be centered at (0.5, 0.5) and of radius 0.5
# in axes coordinates.
if frame == 'circle':
return Circle((0.5, 0.5), 0.5)
elif frame == 'polygon':
return RegularPolygon((0.5, 0.5), num_vars,
radius=.5, edgecolor="k")
else:
raise ValueError("Unknown value for 'frame': %s" % frame)
def _gen_axes_spines(self):
if frame == 'circle':
return super()._gen_axes_spines()
elif frame == 'polygon':
# spine_type must be 'left'/'right'/'top'/'bottom'/'circle'.
spine = Spine(axes=self,
spine_type='circle',
path=Path.unit_regular_polygon(num_vars))
# unit_regular_polygon gives a polygon of radius 1 centered at
# (0, 0) but we want a polygon of radius 0.5 centered at (0.5,
# 0.5) in axes coordinates.
spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
+ self.transAxes)
return {'polar': spine}
else:
raise ValueError("Unknown value for 'frame': %s" % frame)
register_projection(RadarAxes)
return theta
def get_spider_plot(request: gr.Request):
token = request.request.session.get('token')
sp = spotipy.Spotify(token)
names, data = get_features2(sp)
theta = radar_factory(len(names), frame='polygon')
fig = plt.figure(figsize=(9, 9))
ax = fig.add_axes([0, 0, 1, 1], projection='radar')
# Plot the four cases from the example data on separate axes
title = 'test'
ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
horizontalalignment='center', verticalalignment='center')
ax.plot(theta, data)
ax.fill(theta, data, alpha=0.25, label='_nolegend_')
ax.set_varlabels(names)
return fig
PORT_NUMBER = 8080
SPOTIPY_CLIENT_ID = 'c087fa97cebb4f67b6f08ba841ed8378'
SPOTIPY_CLIENT_SECRET = 'ae27d6916d114ac4bb948bb6c58a72d9'
SPOTIPY_REDIRECT_URI = 'https://hf-hackathon-2023-01-spotify.hf.space'
SCOPE = 'ugc-image-upload user-read-playback-state user-modify-playback-state user-read-currently-playing app-remote-control streaming playlist-read-private playlist-read-collaborative playlist-modify-private playlist-modify-public user-follow-modify user-follow-read user-read-playback-position user-top-read user-read-recently-played user-library-modify user-library-read user-read-email user-read-private'
sp_oauth = oauth2.SpotifyOAuth(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI, scope=SCOPE)
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="w.o.w")
@app.get('/', response_class=HTMLResponse)
async def homepage(request: Request):
token = request.session.get('token')
if token:
return RedirectResponse("/gradio")
url = str(request.url)
code = sp_oauth.parse_response_code(url)
if code != url:
token_info = sp_oauth.get_access_token(code)
request.session['token'] = token_info['access_token']
return RedirectResponse("/gradio")
auth_url = sp_oauth.get_authorize_url()
return "<a href='" + auth_url + "'>Login to Spotify</a>"
from vega_datasets import data
iris = data.iris()
def scatter_plot_fn_energy(request: gr.Request):
token = request.request.session.get('token')
if token:
sp = spotipy.Spotify(token)
results = sp.current_user()
print(results)
df = get_features(sp)
return gr.ScatterPlot(
value=df,
x="danceability",
y="energy"
)
def scatter_plot_fn_liveness(request: gr.Request):
token = request.request.session.get('token')
if token:
sp = spotipy.Spotify(token)
results = sp.current_user()
print(results)
df = get_features(sp)
print(df)
return gr.ScatterPlot(
value=df,
x="acousticness",
y="liveness"
)
def heatmap_plot_fn(request: gr.Request):
token = request.request.session.get('token')
if token:
sp = spotipy.Spotify(token)
data = heatmap.build_heatmap(heatmap.fetch_recent_songs(sp))
fig, ax = heatmap.plot(data)
return fig
def get_features(spotify):
features = []
for index in range(0, 10):
results = spotify.current_user_saved_tracks(offset=index*50, limit=50)
track_ids = [item['track']['id'] for item in results['items']]
features.extend(spotify.audio_features(track_ids))
df = pd.DataFrame(data=features)
names = [
'danceability',
'energy',
'loudness',
'speechiness',
'acousticness',
'instrumentalness',
'liveness',
'valence',
'tempo',
]
# print (features_means.to_json())
return df
def get_features(spotify):
features = []
for index in range(0, 10):
results = spotify.current_user_saved_tracks(offset=index*50, limit=50)
track_ids = [item['track']['id'] for item in results['items']]
features.extend(spotify.audio_features(track_ids))
df = pd.DataFrame(data=features)
names = [
'danceability',
'energy',
'loudness',
'speechiness',
'acousticness',
'instrumentalness',
'liveness',
'valence',
'tempo',
]
features_means = df[names].mean()
# print (features_means.to_json())
return features_means
##########
def get_started():
# redirects to spotify and comes back
# then generates plots
return
with gr.Blocks() as demo:
gr.Markdown(" ## Spotify Analyzer 🥳🎉")
gr.Markdown("This app analyzes how cool your music taste is. We dare you to take this challenge!")
with gr.Row():
get_started_btn = gr.Button("Get Started")
with gr.Row():
spider_plot = gr.Plot()
# with gr.Row():
# with gr.Column():
# with gr.Row():
# with gr.Column():
# energy_plot = gr.ScatterPlot(show_label=False).style(container=True)
# with gr.Column():
# liveness_plot = gr.ScatterPlot(show_label=False).style(container=True)
with gr.Row():
gr.Markdown(" ### We have recommendations for you!")
with gr.Row():
heatmap_plot = gr.Plot()
with gr.Row():
gr.Markdown(" ### We have recommendations for you!")
with gr.Row():
gr.Dataframe(
headers=["Song", "Album", "Artist"],
datatype=["str", "str", "str"],
label="Reccomended Songs",
value=[["Fired Up", "Fired Up", "Randy Houser"], ["Something Just Like This", "Memories... Do Not Open", "The Chainsmokers"]] # TODO: replace with actual reccomendations once get_started() is implemeted.
)
demo.load(fn=get_spider_plot, outputs = spider_plot)
demo.load(fn=heatmap_plot_fn, outputs = heatmap_plot)
# demo.load(fn=scatter_plot_fn_energy, outputs = energy_plot)
# demo.load(fn=scatter_plot_fn_liveness, outputs = liveness_plot)
gradio_app = gr.mount_gradio_app(app, demo, "/gradio")
uvicorn.run(app, host="0.0.0.0", port=7860)
|