Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
-
import
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
-
# 1️⃣
|
| 8 |
# -------------------------------
|
| 9 |
DATA_PATH = "spotify_clustered.csv"
|
| 10 |
MODEL_PATH = "kmeans_music_model.pkl"
|
|
@@ -12,11 +12,7 @@ SCALER_PATH = "scaler_music.pkl"
|
|
| 12 |
|
| 13 |
@st.cache_data
|
| 14 |
def load_data():
|
| 15 |
-
|
| 16 |
-
df = pd.read_csv(DATA_PATH)
|
| 17 |
-
except FileNotFoundError:
|
| 18 |
-
st.error("spotify_clustered.csv bulunamadı! Lütfen root klasöre yükleyin.")
|
| 19 |
-
st.stop()
|
| 20 |
unnecessary_cols = ['Unnamed: 0', 'track_id', 'album_name', 'explicit']
|
| 21 |
df_display = df.drop(columns=[c for c in unnecessary_cols if c in df.columns])
|
| 22 |
return df, df_display
|
|
@@ -44,72 +40,50 @@ st.subheader("📄 Dataset Preview / Veri Önizleme")
|
|
| 44 |
st.dataframe(df_display.head(10), use_container_width=True)
|
| 45 |
|
| 46 |
# -------------------------------
|
| 47 |
-
# 4️⃣
|
| 48 |
# -------------------------------
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def create_scatter(df):
|
| 62 |
-
fig, ax = plt.subplots(figsize=(8,5))
|
| 63 |
-
scatter = ax.scatter(df['danceability'], df['energy'], c=df['cluster'], cmap='viridis', alpha=0.6)
|
| 64 |
-
ax.set_xlabel("Danceability / Dans Edilebilirlik")
|
| 65 |
-
ax.set_ylabel("Energy / Enerji")
|
| 66 |
-
plt.colorbar(scatter, label="Cluster / Küme")
|
| 67 |
-
return fig
|
| 68 |
-
|
| 69 |
-
fig = create_scatter(df)
|
| 70 |
-
st.pyplot(fig, clear_figure=False)
|
| 71 |
-
plt.close(fig)
|
| 72 |
|
| 73 |
# -------------------------------
|
| 74 |
-
# 5️⃣
|
| 75 |
# -------------------------------
|
| 76 |
st.divider()
|
| 77 |
st.subheader("🤖 Predict New Song Cluster / Yeni Şarkı Tahmini")
|
| 78 |
-
st.info("Adjust sliders to see which cluster a song belongs to / Sürgüleri ayarlayın.")
|
| 79 |
-
|
| 80 |
c1, c2, c3 = st.columns(3)
|
| 81 |
-
|
| 82 |
with c1:
|
| 83 |
-
pop = st.slider("Popularity
|
| 84 |
-
dur = st.slider("Duration (ms)
|
| 85 |
-
dance = st.slider("Danceability
|
| 86 |
-
|
| 87 |
with c2:
|
| 88 |
-
energy = st.slider("Energy
|
| 89 |
-
loud = st.slider("Loudness
|
| 90 |
-
tempo = st.slider("Tempo
|
| 91 |
-
|
| 92 |
with c3:
|
| 93 |
-
speech = st.slider("Speechiness
|
| 94 |
|
| 95 |
-
if st.button("Predict Cluster
|
| 96 |
new_data = [[pop, dur, dance, energy, loud, tempo, speech]]
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.write("**Similar songs / Bu gruptaki benzer şarkılar:**")
|
| 102 |
-
samples = df[df['cluster'] == res][['track_name', 'artists']].head(5)
|
| 103 |
-
st.table(samples)
|
| 104 |
-
except Exception as e:
|
| 105 |
-
st.error(f"Prediction Error / Tahmin Hatası: {e}")
|
| 106 |
|
| 107 |
# -------------------------------
|
| 108 |
-
# 6️⃣
|
| 109 |
# -------------------------------
|
| 110 |
st.divider()
|
| 111 |
-
st.subheader("
|
| 112 |
numeric_only = df.select_dtypes(include=['float64','int64'])
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
st.dataframe(means, use_container_width=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
+
import plotly.express as px
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
+
# 1️⃣ Dosyaları yükle
|
| 8 |
# -------------------------------
|
| 9 |
DATA_PATH = "spotify_clustered.csv"
|
| 10 |
MODEL_PATH = "kmeans_music_model.pkl"
|
|
|
|
| 12 |
|
| 13 |
@st.cache_data
|
| 14 |
def load_data():
|
| 15 |
+
df = pd.read_csv(DATA_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
unnecessary_cols = ['Unnamed: 0', 'track_id', 'album_name', 'explicit']
|
| 17 |
df_display = df.drop(columns=[c for c in unnecessary_cols if c in df.columns])
|
| 18 |
return df, df_display
|
|
|
|
| 40 |
st.dataframe(df_display.head(10), use_container_width=True)
|
| 41 |
|
| 42 |
# -------------------------------
|
| 43 |
+
# 4️⃣ Titremesiz grafik – Plotly
|
| 44 |
# -------------------------------
|
| 45 |
+
st.subheader("🎯 Feature Analysis / Özellik Analizi")
|
| 46 |
+
fig = px.scatter(
|
| 47 |
+
df,
|
| 48 |
+
x='danceability',
|
| 49 |
+
y='energy',
|
| 50 |
+
color='cluster',
|
| 51 |
+
labels={'danceability': 'Danceability / Dans Edilebilirlik',
|
| 52 |
+
'energy': 'Energy / Enerji',
|
| 53 |
+
'cluster': 'Cluster / Küme'},
|
| 54 |
+
opacity=0.6
|
| 55 |
+
)
|
| 56 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# -------------------------------
|
| 59 |
+
# 5️⃣ Prediction Section / Tahmin
|
| 60 |
# -------------------------------
|
| 61 |
st.divider()
|
| 62 |
st.subheader("🤖 Predict New Song Cluster / Yeni Şarkı Tahmini")
|
|
|
|
|
|
|
| 63 |
c1, c2, c3 = st.columns(3)
|
|
|
|
| 64 |
with c1:
|
| 65 |
+
pop = st.slider("Popularity", 0, 100, 50)
|
| 66 |
+
dur = st.slider("Duration (ms)", 0, 600000, 200000)
|
| 67 |
+
dance = st.slider("Danceability", 0.0, 1.0, 0.5)
|
|
|
|
| 68 |
with c2:
|
| 69 |
+
energy = st.slider("Energy", 0.0, 1.0, 0.5)
|
| 70 |
+
loud = st.slider("Loudness", -60.0, 0.0, -10.0)
|
| 71 |
+
tempo = st.slider("Tempo", 0.0, 250.0, 120.0)
|
|
|
|
| 72 |
with c3:
|
| 73 |
+
speech = st.slider("Speechiness", 0.0, 1.0, 0.1)
|
| 74 |
|
| 75 |
+
if st.button("Predict Cluster"):
|
| 76 |
new_data = [[pop, dur, dance, energy, loud, tempo, speech]]
|
| 77 |
+
new_data_scaled = scaler.transform(new_data)
|
| 78 |
+
res = model.predict(new_data_scaled)[0]
|
| 79 |
+
st.success(f"Predicted Cluster: {res}")
|
| 80 |
+
st.write(df[df['cluster']==res][['track_name','artists']].head(5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# -------------------------------
|
| 83 |
+
# 6️⃣ Cluster means
|
| 84 |
# -------------------------------
|
| 85 |
st.divider()
|
| 86 |
+
st.subheader("Cluster Characteristics / Küme Ortalamaları")
|
| 87 |
numeric_only = df.select_dtypes(include=['float64','int64'])
|
| 88 |
+
means = numeric_only.groupby(df['cluster']).mean()
|
| 89 |
+
st.dataframe(means, use_container_width=True)
|
|
|