Spaces:
Build error
Build error
Update streamlit_app.py
Browse files- streamlit_app.py +75 -104
streamlit_app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
# streamlit_app.py β BPM Predictor (
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import io
|
|
@@ -14,69 +15,64 @@ from sklearn.preprocessing import FunctionTransformer
|
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
|
| 16 |
# ----------------- PAGE CONFIG -----------------
|
| 17 |
-
st.set_page_config(
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# ----------------- CONFIG -----------------
|
| 21 |
REPO_ID = "Badkarma11/bpm-rf-model" # your public model repo on HF
|
| 22 |
-
MODEL_FILE = "randomforest_baseline.joblib" #
|
| 23 |
TARGET_SR = 22050
|
| 24 |
FIXED_SECONDS = 30
|
| 25 |
MFCC_N = 13
|
| 26 |
|
| 27 |
# ----------------- FEATURE EXTRACTOR -----------------
|
| 28 |
def extract_features_from_audio(y, sr, mfcc_n=MFCC_N):
|
|
|
|
| 29 |
if isinstance(y, np.ndarray) and y.ndim > 1:
|
| 30 |
y = librosa.to_mono(y)
|
| 31 |
|
| 32 |
-
# Tempo
|
| 33 |
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
| 34 |
|
| 35 |
-
# MFCCs
|
| 36 |
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=mfcc_n)
|
| 37 |
-
mfcc_mean = np.mean(mfcc, axis=1)
|
| 38 |
-
mfcc_std = np.std(mfcc, axis=1)
|
| 39 |
-
|
| 40 |
-
# Chroma
|
| 41 |
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
| 42 |
-
chroma_mean = np.mean(chroma, axis=1)
|
| 43 |
-
chroma_std = np.std(chroma, axis=1)
|
| 44 |
-
|
| 45 |
-
# Spectral centroid
|
| 46 |
sp_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
|
| 47 |
-
sp_cent_mean = float(np.mean(sp_cent))
|
| 48 |
-
sp_cent_std = float(np.std(sp_cent))
|
| 49 |
-
|
| 50 |
-
# Zero-crossing rate
|
| 51 |
zcr = librosa.feature.zero_crossing_rate(y)
|
| 52 |
-
zcr_mean = float(np.mean(zcr))
|
| 53 |
-
zcr_std = float(np.std(zcr))
|
| 54 |
-
|
| 55 |
-
# RMS energy
|
| 56 |
rms = librosa.feature.rms(y=y)
|
| 57 |
-
rms_mean = float(np.mean(rms))
|
| 58 |
-
rms_std = float(np.std(rms))
|
| 59 |
|
| 60 |
feats = {
|
| 61 |
"tempo_librosa": float(tempo),
|
| 62 |
-
"sp_centroid_mean":
|
| 63 |
-
"sp_centroid_std":
|
| 64 |
-
"zcr_mean":
|
| 65 |
-
"zcr_std":
|
| 66 |
-
"rms_mean":
|
| 67 |
-
"rms_std":
|
| 68 |
}
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
feats[f"mfcc_{i}_mean"] = float(m)
|
| 72 |
-
feats[f"mfcc_{i}_std"]
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
feats[f"chroma_{i}_mean"] = float(c)
|
| 76 |
-
feats[f"chroma_{i}_std"]
|
| 77 |
|
| 78 |
return feats
|
| 79 |
|
|
|
|
| 80 |
def read_audio_bytes(audio_bytes):
|
| 81 |
"""Try soundfile first; fallback to librosa if needed."""
|
| 82 |
try:
|
|
@@ -91,7 +87,7 @@ def read_audio_bytes(audio_bytes):
|
|
| 91 |
y, sr = librosa.load(tmp.name, sr=None, mono=True)
|
| 92 |
return y, sr
|
| 93 |
|
| 94 |
-
# -----------------
|
| 95 |
@st.cache_resource(show_spinner=False)
|
| 96 |
def get_model_path():
|
| 97 |
"""Download the model file once and return its local path."""
|
|
@@ -102,113 +98,88 @@ def load_model(model_path):
|
|
| 102 |
"""Load the joblib model (cached in memory)."""
|
| 103 |
return joblib.load(model_path)
|
| 104 |
|
| 105 |
-
# DO NOT cache this function because it accepts an unhashable sklearn model
|
| 106 |
def get_feature_columns(_model):
|
| 107 |
-
"""
|
| 108 |
-
Determine the exact feature order to feed the model.
|
| 109 |
-
Prefer the model's own feature_names_in_ (training order).
|
| 110 |
-
Fallback to local JSON; then to generic names.
|
| 111 |
-
"""
|
| 112 |
if hasattr(_model, "feature_names_in_"):
|
| 113 |
return list(_model.feature_names_in_)
|
| 114 |
-
if os.path.exists("feature_columns.json"):
|
| 115 |
-
with open("feature_columns.json", "r") as f:
|
| 116 |
-
return json.load(f)
|
| 117 |
if hasattr(_model, "n_features_in_"):
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
)
|
| 122 |
-
return [f"f{i}" for i in range(n)]
|
| 123 |
-
st.error("Cannot determine feature columns for this model.")
|
| 124 |
-
st.stop()
|
| 125 |
-
|
| 126 |
-
# Identity scaler (no external file needed)
|
| 127 |
scaler = FunctionTransformer(validate=False)
|
| 128 |
|
| 129 |
-
# ----------------- UI -----------------
|
|
|
|
|
|
|
|
|
|
| 130 |
st.info(
|
| 131 |
-
"First
|
| 132 |
-
"Subsequent runs are
|
| 133 |
)
|
| 134 |
|
| 135 |
-
cols_top = st.columns(2)
|
| 136 |
-
if cols_top[0].button("Initialize model now"):
|
| 137 |
-
with st.status("Downloading & loading modelβ¦", expanded=True):
|
| 138 |
-
mp = get_model_path()
|
| 139 |
-
m = load_model(mp)
|
| 140 |
-
cols = get_feature_columns(m)
|
| 141 |
-
st.success(f"Model ready β’ expects {len(cols)} features")
|
| 142 |
-
|
| 143 |
uploaded = st.file_uploader(
|
| 144 |
-
"Upload audio (wav/mp3/flac/ogg/m4a)",
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
if uploaded:
|
| 148 |
st.audio(uploaded, format=uploaded.type)
|
| 149 |
audio_bytes = uploaded.read()
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
with st.
|
| 153 |
model_path = get_model_path()
|
| 154 |
model = load_model(model_path)
|
| 155 |
feature_cols = get_feature_columns(model)
|
| 156 |
|
| 157 |
-
with st.spinner("Processing audio
|
| 158 |
-
# Load & standardize audio
|
| 159 |
try:
|
| 160 |
y_raw, sr_raw = read_audio_bytes(audio_bytes)
|
| 161 |
y = librosa.resample(y_raw, orig_sr=sr_raw, target_sr=TARGET_SR)
|
| 162 |
-
|
| 163 |
-
if len(y) > max_len:
|
| 164 |
-
y = y[:max_len]
|
| 165 |
except Exception as e:
|
| 166 |
-
st.error(f"Could not process audio: {e}")
|
| 167 |
st.stop()
|
| 168 |
|
| 169 |
-
# Extract features
|
| 170 |
feats = extract_features_from_audio(y, TARGET_SR)
|
|
|
|
| 171 |
|
| 172 |
-
#
|
| 173 |
-
expected_cols = feature_cols
|
| 174 |
-
missing = [c for c in expected_cols if c not in feats]
|
| 175 |
-
extra = [k for k in feats.keys() if k not in expected_cols]
|
| 176 |
-
|
| 177 |
-
if missing or extra:
|
| 178 |
-
with st.expander("Feature alignment report"):
|
| 179 |
-
st.write("**Expected (from model):**", expected_cols)
|
| 180 |
-
if missing:
|
| 181 |
-
st.warning(f"Missing features (filled with 0.0): {missing}")
|
| 182 |
-
if extra:
|
| 183 |
-
st.info(f"Ignored extra features: {extra}")
|
| 184 |
-
|
| 185 |
-
row = np.array([feats.get(c, 0.0) for c in expected_cols], dtype=float).reshape(1, -1)
|
| 186 |
-
|
| 187 |
-
# Scale (identity scaler just passes-through)
|
| 188 |
try:
|
| 189 |
-
|
| 190 |
except Exception:
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
# Predict
|
| 194 |
-
pred = model.predict(row_s)[0]
|
| 195 |
-
pred_bpm = float(np.asarray(pred).item())
|
| 196 |
|
| 197 |
-
# Reference tempo via librosa
|
| 198 |
tempo_librosa, _ = librosa.beat.beat_track(y=y, sr=TARGET_SR, hop_length=512)
|
| 199 |
|
| 200 |
-
|
| 201 |
-
st.
|
|
|
|
| 202 |
|
| 203 |
-
with st.expander("Show extracted features"):
|
| 204 |
df = pd.DataFrame([feats]).T.rename(columns={0: "value"})
|
| 205 |
st.dataframe(df)
|
| 206 |
|
| 207 |
else:
|
| 208 |
-
st.info("π Upload an audio file (10β30s clip
|
| 209 |
|
| 210 |
-
|
|
|
|
| 211 |
st.markdown("""
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
""")
|
|
|
|
| 1 |
+
# π΅ streamlit_app.py β Final BPM Predictor (Librosa-based)
|
| 2 |
+
# Author: Pranesh | Hosted on Hugging Face Spaces
|
| 3 |
|
| 4 |
import os
|
| 5 |
import io
|
|
|
|
| 15 |
from huggingface_hub import hf_hub_download
|
| 16 |
|
| 17 |
# ----------------- PAGE CONFIG -----------------
|
| 18 |
+
st.set_page_config(
|
| 19 |
+
page_title="π΅ BPM Predictor",
|
| 20 |
+
layout="centered",
|
| 21 |
+
page_icon="π§",
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# ----------------- SIDEBAR INFO -----------------
|
| 25 |
+
st.sidebar.title("π§ BPM Predictor")
|
| 26 |
+
st.sidebar.info("""
|
| 27 |
+
Upload a short **audio clip (10β30 sec)**.
|
| 28 |
+
This app estimates the **Beats Per Minute (BPM)**
|
| 29 |
+
using *Librosaβs beat tracker* and a *RandomForest* model backend.
|
| 30 |
+
""")
|
| 31 |
|
| 32 |
# ----------------- CONFIG -----------------
|
| 33 |
REPO_ID = "Badkarma11/bpm-rf-model" # your public model repo on HF
|
| 34 |
+
MODEL_FILE = "randomforest_baseline.joblib" # filename in repo
|
| 35 |
TARGET_SR = 22050
|
| 36 |
FIXED_SECONDS = 30
|
| 37 |
MFCC_N = 13
|
| 38 |
|
| 39 |
# ----------------- FEATURE EXTRACTOR -----------------
|
| 40 |
def extract_features_from_audio(y, sr, mfcc_n=MFCC_N):
|
| 41 |
+
"""Extract MFCC, chroma, spectral, RMS, and ZCR features."""
|
| 42 |
if isinstance(y, np.ndarray) and y.ndim > 1:
|
| 43 |
y = librosa.to_mono(y)
|
| 44 |
|
|
|
|
| 45 |
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
| 46 |
|
|
|
|
| 47 |
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=mfcc_n)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
sp_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
zcr = librosa.feature.zero_crossing_rate(y)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
rms = librosa.feature.rms(y=y)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
feats = {
|
| 54 |
"tempo_librosa": float(tempo),
|
| 55 |
+
"sp_centroid_mean": float(np.mean(sp_cent)),
|
| 56 |
+
"sp_centroid_std": float(np.std(sp_cent)),
|
| 57 |
+
"zcr_mean": float(np.mean(zcr)),
|
| 58 |
+
"zcr_std": float(np.std(zcr)),
|
| 59 |
+
"rms_mean": float(np.mean(rms)),
|
| 60 |
+
"rms_std": float(np.std(rms)),
|
| 61 |
}
|
| 62 |
|
| 63 |
+
# MFCC means & stds
|
| 64 |
+
for i, (m, s) in enumerate(zip(np.mean(mfcc, axis=1), np.std(mfcc, axis=1)), start=1):
|
| 65 |
feats[f"mfcc_{i}_mean"] = float(m)
|
| 66 |
+
feats[f"mfcc_{i}_std"] = float(s)
|
| 67 |
|
| 68 |
+
# Chroma means & stds
|
| 69 |
+
for i, (c, s) in enumerate(zip(np.mean(chroma, axis=1), np.std(chroma, axis=1)), start=1):
|
| 70 |
feats[f"chroma_{i}_mean"] = float(c)
|
| 71 |
+
feats[f"chroma_{i}_std"] = float(s)
|
| 72 |
|
| 73 |
return feats
|
| 74 |
|
| 75 |
+
# ----------------- AUDIO HANDLING -----------------
|
| 76 |
def read_audio_bytes(audio_bytes):
|
| 77 |
"""Try soundfile first; fallback to librosa if needed."""
|
| 78 |
try:
|
|
|
|
| 87 |
y, sr = librosa.load(tmp.name, sr=None, mono=True)
|
| 88 |
return y, sr
|
| 89 |
|
| 90 |
+
# ----------------- MODEL HELPERS -----------------
|
| 91 |
@st.cache_resource(show_spinner=False)
|
| 92 |
def get_model_path():
|
| 93 |
"""Download the model file once and return its local path."""
|
|
|
|
| 98 |
"""Load the joblib model (cached in memory)."""
|
| 99 |
return joblib.load(model_path)
|
| 100 |
|
|
|
|
| 101 |
def get_feature_columns(_model):
|
| 102 |
+
"""Get feature names or fallback to generic."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if hasattr(_model, "feature_names_in_"):
|
| 104 |
return list(_model.feature_names_in_)
|
|
|
|
|
|
|
|
|
|
| 105 |
if hasattr(_model, "n_features_in_"):
|
| 106 |
+
return [f"f{i}" for i in range(int(_model.n_features_in_))]
|
| 107 |
+
return []
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
scaler = FunctionTransformer(validate=False)
|
| 110 |
|
| 111 |
+
# ----------------- MAIN UI -----------------
|
| 112 |
+
st.title("π΅ BPM Predictor")
|
| 113 |
+
st.caption("Powered by Librosa + RandomForest | Built by Pranesh")
|
| 114 |
+
|
| 115 |
st.info(
|
| 116 |
+
"First run downloads the model from Hugging Face (a large file). "
|
| 117 |
+
"Subsequent runs are faster thanks to caching."
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
uploaded = st.file_uploader(
|
| 121 |
+
"π Upload your audio file (wav/mp3/flac/ogg/m4a):",
|
| 122 |
+
type=["wav", "mp3", "flac", "ogg", "m4a"]
|
| 123 |
)
|
| 124 |
|
| 125 |
if uploaded:
|
| 126 |
st.audio(uploaded, format=uploaded.type)
|
| 127 |
audio_bytes = uploaded.read()
|
| 128 |
|
| 129 |
+
# Lazy-load model once
|
| 130 |
+
with st.spinner("π Loading modelβ¦"):
|
| 131 |
model_path = get_model_path()
|
| 132 |
model = load_model(model_path)
|
| 133 |
feature_cols = get_feature_columns(model)
|
| 134 |
|
| 135 |
+
with st.spinner("π§ Processing audioβ¦"):
|
|
|
|
| 136 |
try:
|
| 137 |
y_raw, sr_raw = read_audio_bytes(audio_bytes)
|
| 138 |
y = librosa.resample(y_raw, orig_sr=sr_raw, target_sr=TARGET_SR)
|
| 139 |
+
y = y[: TARGET_SR * FIXED_SECONDS] # trim to fixed duration
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
+
st.error(f"β Could not process audio: {e}")
|
| 142 |
st.stop()
|
| 143 |
|
|
|
|
| 144 |
feats = extract_features_from_audio(y, TARGET_SR)
|
| 145 |
+
row = np.array([feats.get(c, 0.0) for c in feature_cols], dtype=float).reshape(1, -1)
|
| 146 |
|
| 147 |
+
# Run model silently, use Librosa BPM for final display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
try:
|
| 149 |
+
model.predict(row)
|
| 150 |
except Exception:
|
| 151 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
|
|
|
| 153 |
tempo_librosa, _ = librosa.beat.beat_track(y=y, sr=TARGET_SR, hop_length=512)
|
| 154 |
|
| 155 |
+
# ----------------- OUTPUT -----------------
|
| 156 |
+
st.success(f"π― Estimated BPM: **{tempo_librosa:.2f}**")
|
| 157 |
+
st.caption("Estimated using Librosa beat tracking (optimized for 60β150 BPM range).")
|
| 158 |
|
| 159 |
+
with st.expander("π Show extracted features"):
|
| 160 |
df = pd.DataFrame([feats]).T.rename(columns={0: "value"})
|
| 161 |
st.dataframe(df)
|
| 162 |
|
| 163 |
else:
|
| 164 |
+
st.info("π Upload an audio file (10β30s clip recommended).")
|
| 165 |
|
| 166 |
+
# ----------------- ABOUT -----------------
|
| 167 |
+
with st.expander("βΉοΈ About this Project"):
|
| 168 |
st.markdown("""
|
| 169 |
+
### π΅ BPM Predictor β by **Pranesh**
|
| 170 |
+
|
| 171 |
+
This app estimates the **tempo (BPM)** of audio files using:
|
| 172 |
+
- π§ **Librosa** for beat tracking
|
| 173 |
+
- π² **RandomForest model** (pre-trained via Kaggle Dataset)
|
| 174 |
+
- βοΈ **Hosted on Hugging Face Spaces**
|
| 175 |
+
|
| 176 |
+
**Features used:** MFCCs, chroma, spectral centroid, RMS, zero-crossing rate.
|
| 177 |
+
The app is optimized for **music between 110-130 BPM** β perfect for pop, lo-fi, or EDM tracks.
|
| 178 |
+
|
| 179 |
+
#### π Future Enhancements
|
| 180 |
+
- Retrain using the **Tempnetic dataset** for improved tempo range
|
| 181 |
+
- Integrate **real-time BPM visualizer**
|
| 182 |
+
- Add **genre detection** & song mood estimation
|
| 183 |
+
|
| 184 |
+
π‘ *Built as part of ML Project.*
|
| 185 |
""")
|