handex commited on
Commit
a5ca18c
·
verified ·
1 Parent(s): 016dd4e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
 
 
 
 
 
 
 
 
22
 
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
 
25
 
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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))