Heatmap wip
Browse files- app.py +14 -2
- heatmap.py +40 -0
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from starlette.middleware.sessions import SessionMiddleware
|
| 4 |
from starlette.responses import HTMLResponse, RedirectResponse
|
|
@@ -12,6 +13,8 @@ import pandas as pd
|
|
| 12 |
import spotipy
|
| 13 |
from spotipy import oauth2
|
| 14 |
|
|
|
|
|
|
|
| 15 |
PORT_NUMBER = 8080
|
| 16 |
SPOTIPY_CLIENT_ID = 'c087fa97cebb4f67b6f08ba841ed8378'
|
| 17 |
SPOTIPY_CLIENT_SECRET = 'ae27d6916d114ac4bb948bb6c58a72d9'
|
|
@@ -56,6 +59,15 @@ def scatter_plot_fn(request: gr.Request):
|
|
| 56 |
value=iris,
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def get_features(spotify):
|
| 60 |
features = []
|
| 61 |
for index in range(0, 10):
|
|
@@ -95,7 +107,7 @@ with gr.Blocks() as demo:
|
|
| 95 |
with gr.Column():
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
-
|
| 99 |
with gr.Column():
|
| 100 |
plot = gr.ScatterPlot(show_label=False).style(container=True)
|
| 101 |
with gr.Row():
|
|
@@ -113,7 +125,7 @@ with gr.Blocks() as demo:
|
|
| 113 |
value=[["something", "something", "something"], ["something", "something", "something"]] # TODO: replace with actual reccomendations once get_started() is implemeted.
|
| 114 |
)
|
| 115 |
demo.load(fn=scatter_plot_fn, outputs=plot)
|
| 116 |
-
|
| 117 |
|
| 118 |
|
| 119 |
gradio_app = gr.mount_gradio_app(app, demo, "/gradio")
|
|
|
|
| 1 |
import json
|
| 2 |
+
from urllib import request
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from starlette.middleware.sessions import SessionMiddleware
|
| 5 |
from starlette.responses import HTMLResponse, RedirectResponse
|
|
|
|
| 13 |
import spotipy
|
| 14 |
from spotipy import oauth2
|
| 15 |
|
| 16 |
+
import heatmap
|
| 17 |
+
|
| 18 |
PORT_NUMBER = 8080
|
| 19 |
SPOTIPY_CLIENT_ID = 'c087fa97cebb4f67b6f08ba841ed8378'
|
| 20 |
SPOTIPY_CLIENT_SECRET = 'ae27d6916d114ac4bb948bb6c58a72d9'
|
|
|
|
| 59 |
value=iris,
|
| 60 |
)
|
| 61 |
|
| 62 |
+
def heatmap_plot_fn(request: gr.Request):
|
| 63 |
+
token = request.request.session.get('token')
|
| 64 |
+
if token:
|
| 65 |
+
sp = spotipy.Spotify(token)
|
| 66 |
+
data = heatmap.build_heatmap(heatmap.fetch_recent_songs(sp))
|
| 67 |
+
fig, _ = heatmap.plot(data)
|
| 68 |
+
return fig
|
| 69 |
+
|
| 70 |
+
|
| 71 |
def get_features(spotify):
|
| 72 |
features = []
|
| 73 |
for index in range(0, 10):
|
|
|
|
| 107 |
with gr.Column():
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column():
|
| 110 |
+
hm_plot = gr.Plot().style(container=True)
|
| 111 |
with gr.Column():
|
| 112 |
plot = gr.ScatterPlot(show_label=False).style(container=True)
|
| 113 |
with gr.Row():
|
|
|
|
| 125 |
value=[["something", "something", "something"], ["something", "something", "something"]] # TODO: replace with actual reccomendations once get_started() is implemeted.
|
| 126 |
)
|
| 127 |
demo.load(fn=scatter_plot_fn, outputs=plot)
|
| 128 |
+
demo.load(fn=heatmap_plot_fn, output=hm_plot)
|
| 129 |
|
| 130 |
|
| 131 |
gradio_app = gr.mount_gradio_app(app, demo, "/gradio")
|
heatmap.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import List
|
| 3 |
+
import numpy as np
|
| 4 |
+
from spotipy import Spotify
|
| 5 |
+
from dateutil.parser import parse
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
def fetch_recent_songs(client: Spotify):
|
| 9 |
+
cursor = client.current_user_recently_played()
|
| 10 |
+
recently_played: List[dict] = cursor["items"]
|
| 11 |
+
|
| 12 |
+
max_iterations = 30
|
| 13 |
+
it = 0
|
| 14 |
+
while it < max_iterations and cursor["cursors"] is not None:
|
| 15 |
+
cursor = cursor["cursors"]["before"]
|
| 16 |
+
recently_played.extend(cursor["items"])
|
| 17 |
+
|
| 18 |
+
return recently_played
|
| 19 |
+
|
| 20 |
+
def build_heatmap(recent_songs: List[dict]) -> np.ndarray:
|
| 21 |
+
heatmap = np.zeros((7, 20))
|
| 22 |
+
now = datetime.now()
|
| 23 |
+
|
| 24 |
+
for track in recent_songs:
|
| 25 |
+
played_at = parse(track["played_at"])
|
| 26 |
+
weekday = datetime.weekday(played_at)
|
| 27 |
+
week_offset = (now - played_at).days // 7
|
| 28 |
+
print(weekday, week_offset)
|
| 29 |
+
|
| 30 |
+
heatmap[weekday, -week_offset]
|
| 31 |
+
|
| 32 |
+
return heatmap
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def plot(heatmap: np.ndarray):
|
| 36 |
+
fig, ax = plt.subplots()
|
| 37 |
+
|
| 38 |
+
ax.imshow(heatmap, cmap="Greens")
|
| 39 |
+
ax.set_yticklabels(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
|
| 40 |
+
return fig, ax
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ vega-datasets==0.9.0
|
|
| 3 |
Authlib==1.2.0
|
| 4 |
flask==2.2.2
|
| 5 |
pandas==1.5.2
|
|
|
|
|
|
| 3 |
Authlib==1.2.0
|
| 4 |
flask==2.2.2
|
| 5 |
pandas==1.5.2
|
| 6 |
+
matplotlib
|