Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +77 -33
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,84 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from catboost import CatBoostRegressor
|
| 5 |
|
| 6 |
+
# Page
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="BPM Predictor",
|
| 9 |
+
page_icon="🎵",
|
| 10 |
+
layout="centered"
|
| 11 |
+
)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Feature Engineering
|
| 14 |
+
def create_features(df):
|
| 15 |
+
df = df.copy()
|
| 16 |
+
# Energy - Rhythm
|
| 17 |
+
df['Energy_x_Rhythm'] = df['Energy'] * df['RhythmScore']
|
| 18 |
+
df['Energy_x_Loudness'] = df['Energy'] * df['AudioLoudness']
|
| 19 |
+
df['Mood_x_Rhythm'] = df['MoodScore'] * df['RhythmScore']
|
| 20 |
+
# Ratios
|
| 21 |
+
df['Inst_Vocal_Ratio'] = df['InstrumentalScore'] / (df['VocalContent'] + 1)
|
| 22 |
+
df['Loudness_Energy_Ratio'] = df['AudioLoudness'] / (df['Energy'] + 1)
|
| 23 |
+
# milliseconds to minutes
|
| 24 |
+
df['Duration_Min'] = df['TrackDurationMs'] / 60000
|
| 25 |
+
# Squared for importance
|
| 26 |
+
df['Energy_Squared'] = df['Energy'] ** 2
|
| 27 |
+
df['Rhythm_Squared'] = df['RhythmScore'] ** 2
|
| 28 |
+
|
| 29 |
+
return df
|
| 30 |
|
| 31 |
+
# Load Model
|
| 32 |
+
@st.cache_resource
|
| 33 |
+
def load_model():
|
| 34 |
+
model = CatBoostRegressor()
|
| 35 |
+
model.load_model("src/catboost_model.cbm")
|
| 36 |
+
return model
|
| 37 |
+
try:
|
| 38 |
+
model = load_model()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"Error loading model. Make sure 'catboost_model.cbm' is uploaded. Error: {e}")
|
| 41 |
+
st.stop()
|
| 42 |
|
| 43 |
+
# UI
|
| 44 |
+
st.title("🎵 Song BPM Predictor")
|
| 45 |
+
st.write("Enter the audio features of a song to predict its Beats Per Minute (BPM).")
|
| 46 |
|
| 47 |
+
# User Inputs
|
| 48 |
+
with st.form("prediction_form"):
|
| 49 |
+
st.subheader("Audio Features")
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
with col1:
|
| 53 |
+
track_duration = st.number_input("Track Duration (ms)", min_value=10000, max_value=1000000, value=200000, step=1000, help="Length of the song in milliseconds")
|
| 54 |
+
energy = st.slider("Energy", 0.0, 1.0, 0.5)
|
| 55 |
+
rhythm = st.slider("Rhythm Score", 0.0, 1.0, 0.5)
|
| 56 |
+
loudness = st.slider("Audio Loudness (dB)", -60.0, 0.0, -10.0)
|
| 57 |
+
vocal = st.slider("Vocal Content", 0.0, 1.0, 0.5)
|
| 58 |
+
with col2:
|
| 59 |
+
acoustic = st.slider("Acoustic Quality", 0.0, 1.0, 0.5)
|
| 60 |
+
instrumental = st.slider("Instrumental Score", 0.0, 1.0, 0.5)
|
| 61 |
+
live = st.slider("Live Performance Likelihood", 0.0, 1.0, 0.5)
|
| 62 |
+
mood = st.slider("Mood Score", 0.0, 1.0, 0.5)
|
| 63 |
+
submitted = st.form_submit_button("Predict BPM 🚀")
|
| 64 |
|
| 65 |
+
# Prediction
|
| 66 |
+
if submitted:
|
| 67 |
+
input_data = pd.DataFrame({
|
| 68 |
+
'RhythmScore': [rhythm],
|
| 69 |
+
'AudioLoudness': [loudness],
|
| 70 |
+
'VocalContent': [vocal],
|
| 71 |
+
'AcousticQuality': [acoustic],
|
| 72 |
+
'InstrumentalScore': [instrumental],
|
| 73 |
+
'LivePerformanceLikelihood': [live],
|
| 74 |
+
'MoodScore': [mood],
|
| 75 |
+
'TrackDurationMs': [track_duration],
|
| 76 |
+
'Energy': [energy]})
|
| 77 |
+
|
| 78 |
+
processed_data = create_features(input_data)
|
| 79 |
+
|
| 80 |
+
prediction = model.predict(processed_data)[0]
|
| 81 |
+
|
| 82 |
+
st.success(f"Predicted Tempo: **{prediction:.2f} BPM**")
|
| 83 |
+
|
| 84 |
+
st.progress(min(prediction / 200, 1.0))
|