Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
407 lines (351 loc) · 10.9 KB
/
Copy pathapp.py
File metadata and controls
407 lines (351 loc) · 10.9 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import streamlit as st
from playlist_logic import (
DEFAULT_PROFILE,
Song,
build_playlists,
compute_playlist_stats,
history_summary,
lucky_pick,
merge_playlists,
normalize_song,
search_songs,
)
def init_state():
"""Initialize Streamlit session state."""
if "songs" not in st.session_state:
st.session_state.songs = default_songs()
if "profile" not in st.session_state:
st.session_state.profile = dict(DEFAULT_PROFILE)
if "history" not in st.session_state:
st.session_state.history = []
def default_songs():
"""Return a default list of songs."""
return [
{
"title": "Thunderstruck",
"artist": "AC/DC",
"genre": "rock",
"energy": 9,
"tags": ["classic", "guitar"],
},
{
"title": "Lo-fi Rain",
"artist": "DJ Calm",
"genre": "lofi",
"energy": 2,
"tags": ["study"],
},
{
"title": "Night Drive",
"artist": "Neon Echo",
"genre": "electronic",
"energy": 6,
"tags": ["synth"],
},
{
"title": "Soft Piano",
"artist": "Sleep Sound",
"genre": "ambient",
"energy": 1,
"tags": ["sleep"],
},
{
"title": "Bohemian Rhapsody",
"artist": "Queen",
"genre": "rock",
"energy": 8,
"tags": ["classic", "opera"],
},
{
"title": "Blinding Lights",
"artist": "The Weeknd",
"genre": "pop",
"energy": 8,
"tags": ["synth", "dance"],
},
{
"title": "Take Five",
"artist": "Dave Brubeck",
"genre": "jazz",
"energy": 4,
"tags": ["classic", "instrumental"],
},
{
"title": "Strobe",
"artist": "Deadmau5",
"genre": "electronic",
"energy": 7,
"tags": ["progressive", "long"],
},
{
"title": "Weightless",
"artist": "Marconi Union",
"genre": "ambient",
"energy": 1,
"tags": ["relax", "sleep"],
},
{
"title": "Smells Like Teen Spirit",
"artist": "Nirvana",
"genre": "rock",
"energy": 9,
"tags": ["grunge", "90s"],
},
{
"title": "Levitating",
"artist": "Dua Lipa",
"genre": "pop",
"energy": 8,
"tags": ["dance", "party"],
},
{
"title": "So What",
"artist": "Miles Davis",
"genre": "jazz",
"energy": 3,
"tags": ["trumpet", "cool"],
},
{
"title": "Midnight City",
"artist": "M83",
"genre": "electronic",
"energy": 7,
"tags": ["indie", "dream"],
},
{
"title": "Gymnopedie No.1",
"artist": "Erik Satie",
"genre": "ambient",
"energy": 1,
"tags": ["piano", "calm"],
},
{
"title": "Sweet Child O' Mine",
"artist": "Guns N' Roses",
"genre": "rock",
"energy": 8,
"tags": ["guitar", "80s"],
},
{
"title": "Bad Guy",
"artist": "Billie Eilish",
"genre": "pop",
"energy": 6,
"tags": ["bass", "dark"],
},
{
"title": "Fly Me to the Moon",
"artist": "Frank Sinatra",
"genre": "jazz",
"energy": 5,
"tags": ["vocal", "swing"],
},
{
"title": "Sandstorm",
"artist": "Darude",
"genre": "electronic",
"energy": 10,
"tags": ["trance", "meme"],
},
{
"title": "Clair de Lune",
"artist": "Claude Debussy",
"genre": "ambient",
"energy": 2,
"tags": ["piano", "classical"],
},
{
"title": "Hotel California",
"artist": "Eagles",
"genre": "rock",
"energy": 6,
"tags": ["classic", "guitar"],
},
{
"title": "Uptown Funk",
"artist": "Mark Ronson ft. Bruno Mars",
"genre": "pop",
"energy": 9,
"tags": ["funk", "dance"],
},
{
"title": "Feeling Good",
"artist": "Nina Simone",
"genre": "jazz",
"energy": 6,
"tags": ["soul", "vocal"],
},
]
def profile_sidebar():
"""Render and update the user profile."""
st.sidebar.header("Mood profile")
profile = st.session_state.profile
profile["name"] = st.sidebar.text_input(
"Profile name",
value=str(profile.get("name", "")),
)
col1, col2 = st.sidebar.columns(2)
with col1:
profile["hype_min_energy"] = st.sidebar.slider(
"Hype min energy",
min_value=1,
max_value=10,
value=int(profile.get("hype_min_energy", 7)),
)
with col2:
profile["chill_max_energy"] = st.sidebar.slider(
"Chill max energy",
min_value=1,
max_value=10,
value=int(profile.get("chill_max_energy", 3)),
)
profile["favorite_genre"] = st.sidebar.selectbox(
"Favorite genre",
options=["rock", "lofi", "pop", "jazz", "electronic", "ambient", "other"],
index=0,
)
profile["include_mixed"] = st.sidebar.checkbox(
"Include Mixed playlist in views",
value=bool(profile.get("include_mixed", True)),
)
st.sidebar.write("Current profile:", profile["name"])
def add_song_sidebar():
"""Render the Add Song controls in the sidebar."""
st.sidebar.header("Add a song")
title = st.sidebar.text_input("Title")
artist = st.sidebar.text_input("Artist")
genre = st.sidebar.selectbox(
"Genre",
options=["rock", "lofi", "pop", "jazz", "electronic", "ambient", "other"],
)
energy = st.sidebar.slider("Energy", min_value=1, max_value=10, value=5)
tags_text = st.sidebar.text_input("Tags (comma separated)")
if st.sidebar.button("Add to playlist"):
raw_tags = [t.strip() for t in tags_text.split(",")]
tags = [t for t in raw_tags if t]
song: Song = {
"title": title,
"artist": artist,
"genre": genre,
"energy": energy,
"tags": tags,
}
if title and artist:
normalized = normalize_song(song)
all_songs = st.session_state.songs[:]
all_songs.append(normalized)
st.session_state.songs = all_songs
def playlist_tabs(playlists):
"""Render playlists in tabs."""
include_mixed = st.session_state.profile.get("include_mixed", True)
tab_labels = ["Hype", "Chill"]
if include_mixed:
tab_labels.append("Mixed")
tabs = st.tabs(tab_labels)
for label, tab in zip(tab_labels, tabs):
with tab:
render_playlist(label, playlists.get(label, []))
def render_playlist(label, songs):
st.subheader(f"{label} playlist")
if not songs:
st.write("No songs in this playlist.")
return
query = st.text_input(f"Search {label} playlist by artist", key=f"search_{label}")
filtered = search_songs(songs, query, field="artist")
if not filtered:
st.write("No matching songs.")
return
for song in filtered:
mood = song.get("mood", "?")
tags = ", ".join(song.get("tags", []))
st.write(
f"- **{song['title']}** by {song['artist']} "
f"(genre {song['genre']}, energy {song['energy']}, mood {mood}) "
f"[{tags}]"
)
def lucky_section(playlists):
"""Render the lucky pick controls and result."""
st.header("Lucky pick")
mode = st.selectbox(
"Pick from",
options=["any", "hype", "chill"],
index=0,
)
if st.button("Feeling lucky"):
pick = lucky_pick(playlists, mode=mode)
if pick is None:
st.warning("No songs available for this mode.")
return
st.success(
f"Lucky song: {pick['title']} by {pick['artist']} "
f"(mood {pick.get('mood', '?')})"
)
history = st.session_state.history
history.append(pick)
st.session_state.history = history
def stats_section(playlists):
"""Render statistics based on the playlists."""
st.header("Playlist stats")
stats = compute_playlist_stats(playlists)
col1, col2, col3 = st.columns(3)
col1.metric("Total songs", stats["total_songs"])
col2.metric("Hype songs", stats["hype_count"])
col3.metric("Chill songs", stats["chill_count"])
col4, col5, col6 = st.columns(3)
col4.metric("Mixed songs", stats["mixed_count"])
col5.metric("Hype ratio", f"{stats['hype_ratio']:.2f}")
col6.metric("Average energy", f"{stats['avg_energy']:.2f}")
top_artist = stats["top_artist"]
if top_artist:
st.write(
f"Most common artist: {top_artist} "
f"({stats['top_artist_count']} songs)"
)
else:
st.write("No top artist yet.")
def history_section():
"""Render the pick history overview."""
st.header("History")
history = st.session_state.history
if not history:
st.write("No history yet.")
return
summary = history_summary(history)
st.write("Recent picks by mood:", summary)
show_details = st.checkbox("Show full history")
if show_details:
for song in history:
st.write(
f"{song.get('mood', '?')}: {song['title']} by {song['artist']}"
)
def clear_controls():
"""Render a small section for clearing data."""
st.sidebar.header("Manage data")
if st.sidebar.button("Reset songs to default"):
st.session_state.songs = default_songs()
if st.sidebar.button("Clear history"):
st.session_state.history = []
def main():
st.set_page_config(page_title="Playlist Chaos", layout="wide")
st.title("Playlist Chaos")
st.write(
"An AI assistant tried to build a smart playlist engine. "
"The code runs, but the behavior is a bit unpredictable."
)
init_state()
profile_sidebar()
add_song_sidebar()
clear_controls()
profile = st.session_state.profile
songs = st.session_state.songs
base_playlists = build_playlists(songs, profile)
merged_playlists = merge_playlists(base_playlists, {})
playlist_tabs(merged_playlists)
st.divider()
lucky_section(merged_playlists)
st.divider()
stats_section(merged_playlists)
st.divider()
history_section()
if __name__ == "__main__":
main()