Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,30 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import streamlit.components.v1 as components
|
| 3 |
from PIL import Image
|
| 4 |
-
import spotipy.util as util
|
| 5 |
-
import pickle
|
| 6 |
-
import spotipy
|
| 7 |
from utils import *
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="EmotionPlaylist",
|
| 11 |
page_icon="🎧",
|
| 12 |
)
|
| 13 |
-
debug = False
|
| 14 |
-
dir_path = os.path.dirname(os.path.realpath(__file__))
|
| 15 |
-
|
| 16 |
st.title('Customize Emotional Playlists')
|
| 17 |
|
| 18 |
-
def centered_button(func, text, n_columns=7, args=None):
|
| 19 |
-
columns = st.columns(np.ones(n_columns))
|
| 20 |
-
with columns[n_columns//2]:
|
| 21 |
-
return func(text)
|
| 22 |
-
|
| 23 |
-
# get credentials
|
| 24 |
-
def setup_credentials():
|
| 25 |
-
if 'client_id' in os.environ.keys() and 'client_secret' in os.environ.keys():
|
| 26 |
-
client_info = dict(client_id=os.environ['client_id'],
|
| 27 |
-
client_secret=os.environ['client_secret'])
|
| 28 |
-
else:
|
| 29 |
-
with open(dir_path + "/ids.pk", 'rb') as f:
|
| 30 |
-
client_info = pickle.load(f)
|
| 31 |
-
|
| 32 |
-
os.environ['SPOTIPY_CLIENT_ID'] = client_info['client_id']
|
| 33 |
-
os.environ['SPOTIPY_CLIENT_SECRET'] = client_info['client_secret']
|
| 34 |
-
os.environ['SPOTIPY_REDIRECT_URI'] = 'https://huggingface.co/spaces/ccolas/EmotionPlaylist/'
|
| 35 |
-
|
| 36 |
-
relevant_audio_features = ["danceability", "energy", "loudness", "mode", "valence", "tempo"]
|
| 37 |
-
|
| 38 |
|
| 39 |
-
def get_client():
|
| 40 |
-
scope = "playlist-modify-public"
|
| 41 |
-
token = util.prompt_for_user_token(scope=scope)
|
| 42 |
-
sp = spotipy.Spotify(auth=token)
|
| 43 |
-
user_id = sp.me()['id']
|
| 44 |
-
return sp, user_id
|
| 45 |
-
|
| 46 |
-
def extract_uris_from_links(links, url_type):
|
| 47 |
-
assert url_type in ['playlist', 'artist', 'user']
|
| 48 |
-
urls = links.split('\n')
|
| 49 |
-
uris = []
|
| 50 |
-
for url in urls:
|
| 51 |
-
if 'playlist' in url:
|
| 52 |
-
uri = url.split(f'{url_type}/')[-1].split('?')[0]
|
| 53 |
-
else:
|
| 54 |
-
uri = url.split('?')[0]
|
| 55 |
-
uris.append(uri)
|
| 56 |
-
return uris
|
| 57 |
-
|
| 58 |
-
def wall_of_checkboxes(labels, max_width=10):
|
| 59 |
-
n_labels = len(labels)
|
| 60 |
-
n_rows = int(np.ceil(n_labels/max_width))
|
| 61 |
-
checkboxes = []
|
| 62 |
-
for i in range(n_rows):
|
| 63 |
-
columns = st.columns(np.ones(max_width))
|
| 64 |
-
row_length = n_labels % max_width if i == n_rows - 1 else max_width
|
| 65 |
-
for j in range(row_length):
|
| 66 |
-
with columns[j]:
|
| 67 |
-
checkboxes.append(st.empty())
|
| 68 |
-
return checkboxes
|
| 69 |
-
|
| 70 |
-
def aggregate_genres(genres, legit_genres, verbose=False):
|
| 71 |
-
genres_output = dict()
|
| 72 |
-
legit_genres_formatted = [lg.replace('-', '').replace(' ', '') for lg in legit_genres]
|
| 73 |
-
for glabel in genres.keys():
|
| 74 |
-
if verbose: print('\n', glabel)
|
| 75 |
-
glabel_formatted = glabel.replace(' ', '').replace('-', '')
|
| 76 |
-
best_match = None
|
| 77 |
-
best_match_score = 0
|
| 78 |
-
for legit_glabel, legit_glabel_formatted in zip(legit_genres, legit_genres_formatted):
|
| 79 |
-
if 'jazz' in glabel_formatted:
|
| 80 |
-
best_match = 'jazz'
|
| 81 |
-
if verbose: print('\t', 'pop')
|
| 82 |
-
break
|
| 83 |
-
if 'ukpop' in glabel_formatted:
|
| 84 |
-
best_match = 'pop'
|
| 85 |
-
if verbose: print('\t', 'pop')
|
| 86 |
-
break
|
| 87 |
-
if legit_glabel_formatted == glabel_formatted:
|
| 88 |
-
if verbose: print('\t', legit_glabel_formatted)
|
| 89 |
-
best_match = legit_glabel
|
| 90 |
-
break
|
| 91 |
-
elif glabel_formatted in legit_glabel_formatted:
|
| 92 |
-
if verbose: print('\t', legit_glabel_formatted)
|
| 93 |
-
if len(glabel_formatted) > best_match_score:
|
| 94 |
-
best_match = legit_glabel
|
| 95 |
-
best_match_score = len(glabel_formatted)
|
| 96 |
-
elif legit_glabel_formatted in glabel_formatted:
|
| 97 |
-
if verbose: print('\t', legit_glabel_formatted)
|
| 98 |
-
if len(legit_glabel_formatted) > best_match_score:
|
| 99 |
-
best_match = legit_glabel
|
| 100 |
-
best_match_score = len(legit_glabel_formatted)
|
| 101 |
-
|
| 102 |
-
if best_match is not None:
|
| 103 |
-
if verbose: print('\t', '-->', best_match)
|
| 104 |
-
if best_match in genres_output.keys():
|
| 105 |
-
genres_output[best_match] += genres[glabel]
|
| 106 |
-
else:
|
| 107 |
-
genres_output[best_match] = genres[glabel]
|
| 108 |
-
return genres_output
|
| 109 |
|
| 110 |
def setup_streamlite():
|
| 111 |
setup_credentials()
|
|
|
|
|
|
|
| 112 |
image = Image.open(dir_path + '/image.png')
|
| 113 |
st.image(image)
|
| 114 |
st.markdown("This app let's you quickly build playlists in a customized way: ")
|
|
@@ -117,14 +33,24 @@ def setup_streamlite():
|
|
| 117 |
|
| 118 |
st.subheader("Step 1: Connect to your Spotify app")
|
| 119 |
st.markdown("Log into your Spotify account to let the app create the custom playlist.")
|
|
|
|
| 120 |
if 'login' not in st.session_state:
|
| 121 |
-
|
| 122 |
-
if
|
| 123 |
-
|
| 124 |
legit_genres = sp.recommendation_genre_seeds()['genres']
|
| 125 |
st.session_state['login'] = (sp, user_id, legit_genres)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if 'login' in st.session_state or debug:
|
|
|
|
| 128 |
if not debug: sp, user_id, legit_genres = st.session_state['login']
|
| 129 |
st.success('You are logged in.')
|
| 130 |
|
|
@@ -260,6 +186,5 @@ def setup_streamlite():
|
|
| 260 |
|
| 261 |
stop = 1
|
| 262 |
|
| 263 |
-
|
| 264 |
if __name__ == '__main__':
|
| 265 |
setup_streamlite()
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 2 |
from utils import *
|
| 3 |
+
from app_utils import *
|
| 4 |
|
| 5 |
+
debug = False
|
| 6 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
| 7 |
+
# os.environ['FLASK_APP'] = dir_path + 'app2.py'
|
| 8 |
+
# if debug: os.environ['FLASK_ENV'] = 'development'
|
| 9 |
+
#
|
| 10 |
+
#
|
| 11 |
+
# app = Flask(__name__)
|
| 12 |
+
# app.config['SECRET_KEY'] = os.urandom(64)
|
| 13 |
+
# app.config['SESSION_TYPE'] = 'filesystem'
|
| 14 |
+
# app.config['SESSION_FILE_DIR'] = './.flask_session/'
|
| 15 |
+
# Session(app)
|
| 16 |
st.set_page_config(
|
| 17 |
page_title="EmotionPlaylist",
|
| 18 |
page_icon="🎧",
|
| 19 |
)
|
|
|
|
|
|
|
|
|
|
| 20 |
st.title('Customize Emotional Playlists')
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def setup_streamlite():
|
| 25 |
setup_credentials()
|
| 26 |
+
|
| 27 |
+
print('Here1')
|
| 28 |
image = Image.open(dir_path + '/image.png')
|
| 29 |
st.image(image)
|
| 30 |
st.markdown("This app let's you quickly build playlists in a customized way: ")
|
|
|
|
| 33 |
|
| 34 |
st.subheader("Step 1: Connect to your Spotify app")
|
| 35 |
st.markdown("Log into your Spotify account to let the app create the custom playlist.")
|
| 36 |
+
print('Here2')
|
| 37 |
if 'login' not in st.session_state:
|
| 38 |
+
sp, user_id = new_get_client(session=st.session_state)
|
| 39 |
+
if sp != None:
|
| 40 |
+
print("USER", user_id)
|
| 41 |
legit_genres = sp.recommendation_genre_seeds()['genres']
|
| 42 |
st.session_state['login'] = (sp, user_id, legit_genres)
|
| 43 |
|
| 44 |
+
# if 'login' not in st.session_state:
|
| 45 |
+
# login = centered_button(st.button, 'Log in', n_columns=7)
|
| 46 |
+
# if login or debug:
|
| 47 |
+
# sp, user_id = get_client(session=st.session_state)
|
| 48 |
+
# user_id = sp.me()['id']
|
| 49 |
+
# legit_genres = sp.recommendation_genre_seeds()['genres']
|
| 50 |
+
# st.session_state['login'] = (sp, user_id, legit_genres)
|
| 51 |
+
|
| 52 |
if 'login' in st.session_state or debug:
|
| 53 |
+
print('Here8')
|
| 54 |
if not debug: sp, user_id, legit_genres = st.session_state['login']
|
| 55 |
st.success('You are logged in.')
|
| 56 |
|
|
|
|
| 186 |
|
| 187 |
stop = 1
|
| 188 |
|
|
|
|
| 189 |
if __name__ == '__main__':
|
| 190 |
setup_streamlite()
|