import streamlit as st import random
PLAYLISTS = { "Fantasy/Sci-Fi": { "theme": "Theme: The Call to Adventure — For those long, intricate journeys.", "songs": [ "Overture (The Composer) - Epic Orchestral", "Stars Aligned (SynthWave) - Cosmic Dreams", "Dragon's Breath (Orchestral) - Battle Theme", "Floating City (Ambient) - World Building Background", "Ancient Scrolls (Lo-Fi) - Study & Lore" ] }, "Romance/Literary": { "theme": "Theme: Quiet Contemplation — Perfect for character introspection and tender moments.", "songs": [ "Soft Light (Indie Folk) - Intimate Acoustic", "Rainy Day Piano (Acoustic) - Melancholy Instrumental", "First Embrace (Jazz) - Smooth and Warm", "Letter to June (Singer-Songwriter) - Nostalgic Ballad" ] }, "Thriller/Mystery": { "theme": "Theme: Suspense & Intrigue — Keep the pages turning with a sense of dread.", "songs": [ "The Clock Ticks (Cinematic) - Building Tension", "Foggy Streets (Trip Hop) - Dark and Moody", "Red Herring (Instrumental) - Deceptive Clues", "Midnight Chase (Electronic) - High-Energy Escape" ] }, "Historical Fiction": { "theme": "Theme: Echoes of the Past — Grand, sweeping soundscapes for another era.", "songs": [ "Waltz of the Ages (Classical) - Period Piece Score", "Old Photographs (Folk) - Nostalgic & Rustic", "Hearthside Tale (Instrumental) - Cozy Storytelling", "Harbor Lights (Acoustic) - Longing & Distance" ] }, "Self-Help/Inspirational": { "theme": "Theme: Focused Flow — Gentle beats to boost concentration and motivation.", "songs": [ "New Horizons (Lo-Fi Beats) - Gentle Focus Track", "Golden Hour (Chill Pop) - Uplifting Vibes", "Morning Coffee (Instrumental) - Positive Start", "Gentle Persistence (Ambient) - Steady Background" ] } }
st.markdown("""
<style> /* Global Background and Font */ body { background-color: #f7f3e8; /* Light linen background */ } .main { background-color: #fefcf3; /* Creamy White "Paper" */ padding: 40px; border-radius: 15px; box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1); font-family: 'Inter', sans-serif; } h1, h2 { font-family: 'Playfair Display', serif; color: #c07e5c; /* Terracotta Accent */ } .stButton>button { border-radius: 12px; font-weight: bold; transition: all 0.2s ease-in-out; } /* Style for the Generate button (Warm Wood Color) */ .stButton:nth-child(1) button { background-color: #a67c52; color: white; border: none; } .stButton:nth-child(1) button:hover { background-color: #8f6a46; } /* Style for the Surprise button (Terracotta Accent Color) */ .stButton:nth-child(2) button { background-color: #c07e5c; color: white; border: none; } .stButton:nth-child(2) button:hover { background-color: #a3694f; } /* Style for the playlist output cards */ .song-list-item { background-color: #a67c521a; /* wood/10 opacity */ padding: 10px; margin-bottom: 5px; border-radius: 8px; border-left: 5px solid #c07e5c; /* Accent border */ } </style>""", unsafe_allow_html=True)
st.title("📚 The Book Nook Jukebox") st.subheader("Find the perfect soundtrack for your next chapter.")
if 'playlist_data' not in st.session_state: st.session_state.playlist_data = None if 'title_input' not in st.session_state: st.session_state.title_input = "" if 'genre_select' not in st.session_state: st.session_state.genre_select = ""
st.markdown("---") st.markdown("## Tell Us About Your Read")
st.session_state.title_input = st.text_input( "1. Reading a specific book? (Optional)", placeholder="e.g., Dune, The Secret History, Pride and Prejudice", value=st.session_state.title_input, key="book_title_key" )
genres = ["-- Choose a Genre Vibe --", "Fantasy/Sci-Fi", "Romance/Literary", "Thriller/Mystery", "Historical Fiction", "Self-Help/Inspirational"]
st.session_state.genre_select = st.selectbox( "2. Or, select the genre:", options=genres, index=genres.index(st.session_state.genre_select) if st.session_state.genre_select in genres else 0, key="genre_select_key" )
def generate_playlist(genre_key, is_surprise=False): """Fetches and stores the selected playlist.""" if genre_key in PLAYLISTS: data = PLAYLISTS[genre_key] theme = data["theme"] if is_surprise: theme = f"Theme: Surprise Me! We picked the '{genre_key}' vibe just for you."
st.session_state.playlist_data = {
"theme": theme,
"songs": data["songs"]
}
else:
st.error("Invalid genre selection.")
st.session_state.playlist_data = None
def handle_generate_click(): """Determines the genre based on user input and generates the playlist.""" selected_genre = st.session_state.genre_select book_title = st.session_state.title_input.strip()
if selected_genre != "-- Choose a Genre Vibe --":
generate_playlist(selected_genre)
elif book_title:
# Default to Literary/Romance vibe if only a title is provided
generate_playlist("Romance/Literary")
else:
st.error("Please select a genre or enter a book title to get started!")
st.session_state.playlist_data = None
def handle_surprise_click(): """Selects a random playlist.""" random_key = random.choice(list(PLAYLISTS.keys())) generate_playlist(random_key, is_surprise=True)
col1, col2 = st.columns(2)
with col1: st.button( "📚 Generate Playlist", on_click=handle_generate_click, use_container_width=True )
with col2: st.button( "✨ Surprise Me!", on_click=handle_surprise_click, use_container_width=True )
if st.session_state.playlist_data: st.markdown("---") st.markdown("## Your Reading Soundtrack")
# Use a Streamlit 'container' or 'expander' to create a visual card
with st.container(border=True):
data = st.session_state.playlist_data
st.markdown(f"**{data['theme']}**")
# Display songs using a markdown list with custom styling via HTML
song_list_html = ""
for i, song in enumerate(data['songs']):
# Use custom CSS class for visual appeal
song_list_html += f"<div class='song-list-item'>**{i+1}.** {song}</div>"
st.markdown(song_list_html, unsafe_allow_html=True)
st.caption("🎧 *These hardcoded songs are placeholders to match your book's mood!")