-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.py
121 lines (103 loc) · 4 KB
/
stream.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
import streamlit as st
import altair as alt
import numpy as np
import pandas as pd
import time
#our modules
import helper
import prodplay
import spotify
import algos
from songdataset import SongDataset
st.title("Mood-Dynamic Playlist")
st.write("By [Shaurya Gaur](https://github.com/shaurgaur/)")
st.write("For [Soundbendor Lab](https://soundbendor.org/), Oregon State University")
# st.write("Last updated November 2nd, 2022")
#get important personal information from Spotify API
datasetpath = "static/deezer-std-all.csv"
info = helper.loadConfig("config.json")
dataset = SongDataset(
name="Deezer",
cols=info["cols"]["deezer"] + info["cols"]["spotify"],
path=datasetpath, knn=False, verbose=False,
feat_index = 5, arousal = 4, valence = 3,
)
dist_options = {
"Cosine": algos.cosine_score,
"Euclidean": algos.euclidean_score,
"Manhattan": algos.manhattan_score,
"Jaccard": algos.jaccard_score,
"Random": algos.neighbors_rand
}
sp, spo = spotify.Spotify(
info["auth"]["client_id"],
info["auth"]["client_secret"],
info["auth"]["redirect_uri"],
info["auth"]["username"],
info["auth"]["scope"]
)
songoptionobj = {
"label": [],
"id": []
}
for i in range(64):
songoptionobj["label"].append("({},{}), id: {} ... {} - {} ".format(
np.around(dataset.full_df.iloc[i]["valence"], decimals=2),
np.around(dataset.full_df.iloc[i]["arousal"], decimals=2),
list(dataset.full_df.index.values)[i],
dataset.full_df.iloc[i]["artist_name"],
dataset.full_df.iloc[i]["track_name"],
))
songoptionobj["id"].append(list(dataset.full_df.index.values)[i])
optiondf = pd.DataFrame(songoptionobj)
st.write("## Let's make a playlist!")
form = st.form("playlist_form")
form.write("### Required Options")
origstr = form.selectbox("Choose a song to start with:", optiondf, index=52)
deststr = form.selectbox("Choose a song to end with:", optiondf, index=8)
nsongs = form.slider("Number of songs:", 3, 15, value=7, step=1)
form.write("### Advanced Options")
distmetric = form.radio("Distance metric:", dist_options.keys(), index=1, horizontal=True)
neighbors_k = form.slider("Neighbors:", 3, 31, value=11, step=2)
dataset_cols = form.multiselect(
"Audio dataset(s) to use:",
["Spotify", "MSD"],
["Spotify"]
)
spotyn = form.checkbox("Make the playlist on Spotify.")
submitted = form.form_submit_button("Make my playlist!", type='primary')
if submitted:
orig = int(origstr.split(" ... ")[0].split(" id: ")[1])
dest = int(deststr.split(" ... ")[0].split(" id: ")[1])
if orig == dest:
st.write("Songs are the same! Try again.")
submitted = False
else:
cols = info["cols"]["deezer"]
for c in dataset_cols:
cols += info["cols"][c.lower()]
dataset = SongDataset(
name="Deezer",
cols=cols,
path=datasetpath, knn=True, verbose=False,
feat_index = 5, arousal = 4, valence = 3,
)
playlistDF = prodplay.makePlaylist(
dataset, orig, dest, nsongs,
score=dist_options[distmetric],
neighbors=neighbors_k
)
st.write("## Here's your playlist!")
chart = alt.Chart(playlistDF).mark_line(
point=True, color="#D73F09"
).encode(
x='valence', y='arousal',
tooltip=['id-deezer', 'title', 'artist', 'valence', 'arousal']
)
st.altair_chart(chart, use_container_width=True)
st.dataframe(playlistDF[["artist", "title", "valence", "arousal"]], use_container_width=True)
if spotyn:
title = "Stream Playlist {}".format(str(time.strftime("%Y-%m-%d-%H:%M")))
spid = spotify.makePlaylist(sp, spo, title, playlistDF["id-spotify"], True)
splink = "https://open.spotify.com/playlist/{}".format(spid)
st.markdown("[Playlist on Spotify]({})".format(splink))