| import streamlit as st
|
| import pandas as pd
|
| import joblib
|
| import os
|
|
|
|
|
| st.set_page_config(page_title="BgemBox Music Engine", layout="wide", page_icon="🎵")
|
|
|
|
|
| st.markdown("""
|
| <style>
|
| .main .block-container {padding-top: 2rem;}
|
| th {background-color: #f0f2f6 !important; font-size: 16px !important;}
|
| td {font-size: 15px !important;}
|
| </style>
|
| """, unsafe_allow_html=True)
|
|
|
|
|
| @st.cache_resource
|
| def load_assets():
|
|
|
| data = pd.read_pickle("final_music_data.pkl")
|
|
|
|
|
| scaler = joblib.load("scaler.pkl")
|
|
|
|
|
| if 'sub_cluster' not in data.columns:
|
| if 'cluster' in data.columns:
|
| data['sub_cluster'] = data['cluster']
|
| else:
|
|
|
| print("Critical Error: No clustering columns found!")
|
|
|
|
|
| return data, scaler
|
|
|
| try:
|
|
|
| df, music_scaler = load_assets()
|
| except Exception as e:
|
| st.error(f"Asset Loading Error: {e}")
|
| st.stop()
|
|
|
|
|
| st.sidebar.title("Music Engine Settings")
|
| st.sidebar.markdown("---")
|
| st.sidebar.write("This AI-powered engine clusters music based on technical audio features like BPM, Energy, and Acousticness.")
|
| st.sidebar.info("Developed by Elif | 2026")
|
|
|
|
|
| cluster_names = {
|
| 8.0: "🌟 Mainstream Pop Hits",
|
| 1.0: "🎸 Classic Rock & Dynamic Rhythms",
|
| 2.0: "🎹 Alternative & Indie Vibes",
|
| 7.0: "📜 Nostalgic Oldies",
|
| 3.0: "🌊 Chill & Low-Fi Moods",
|
| 4.0: "⚡ High-Energy / Gym Motivation",
|
| 0.0: "💎 Unique Rare Finds",
|
| 5.0: "💎 Unique Rare Finds",
|
| 6.0: "💎 Unique Rare Finds"
|
| }
|
|
|
|
|
| st.title("🎵 BgemBox Music Recommendation System")
|
| st.subheader("Discover music through Data Science")
|
| st.markdown("---")
|
|
|
| tab1, tab2 = st.tabs(["Search by Artist", "Discover by Mood"])
|
|
|
|
|
| with tab1:
|
| st.write("### Find Similar Artists")
|
| artist_list = sorted(df['Artist'].unique())
|
| selected_artist = st.selectbox("Select an Artist you like:", artist_list)
|
|
|
| if st.button("Recommend Similar Music"):
|
| artist_data = df[df['Artist'] == selected_artist].iloc[0]
|
| cluster_id = artist_data['sub_cluster']
|
|
|
| friendly_name = cluster_names.get(cluster_id, "Similar Style Tracks")
|
| st.success(f"Artist **{selected_artist}** belongs to the **{friendly_name}** segment.")
|
|
|
| recommendations = df[(df['sub_cluster'] == cluster_id) & (df['Artist'] != selected_artist)]
|
| recommendations = recommendations.sort_values(by='Popularity', ascending=False).head(5)
|
|
|
| st.write("#### Recommended for you:")
|
|
|
| st.table(recommendations[['Artist', 'Top Genre', 'Popularity']])
|
|
|
|
|
| with tab2:
|
| st.write("### How are you feeling today?")
|
| selected_mood_name = st.selectbox("Select a Mood:", list(cluster_names.values()))
|
|
|
| mood_id = [k for k, v in cluster_names.items() if v == selected_mood_name][0]
|
|
|
| if st.button("Generate Playlist"):
|
| mood_list = df[df['sub_cluster'] == mood_id]
|
|
|
| if not mood_list.empty:
|
|
|
| playlist = mood_list.sample(min(len(mood_list), 10))
|
| st.write(f"#### Your {selected_mood_name} Playlist:")
|
|
|
|
|
| st.table(playlist[['Artist', 'Top Genre', 'Popularity', 'Energy', 'Beats Per Minute (BPM)']])
|
| else:
|
| st.warning("This specific cluster is currently empty or has only one track.")
|
|
|
|
|
| st.markdown("---")
|
| st.caption("© 2026 Data Science Specialization Project")
|
|
|