Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,36 +13,57 @@ def estimate_genre(filepath):
|
|
13 |
return {"Error": f"Failed to load audio: {str(e)}"}
|
14 |
|
15 |
try:
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
except Exception as e:
|
21 |
return {"Error": f"Feature extraction failed: {str(e)}"}
|
22 |
|
23 |
-
# Heuristic genre rules
|
24 |
-
if tempo > 140 and
|
25 |
genre = "Electronic / Dance"
|
26 |
-
elif tempo < 90 and
|
27 |
genre = "Hip Hop / Trap"
|
28 |
-
elif 100 < tempo < 130 and
|
29 |
genre = "Pop / Rock"
|
30 |
-
elif
|
31 |
genre = "Jazz / Soul"
|
32 |
else:
|
33 |
genre = "Unknown"
|
34 |
|
35 |
return {
|
36 |
"Predicted Genre": genre,
|
37 |
-
"Tempo (BPM)": round(
|
38 |
-
"Brightness (Centroid)": round(
|
39 |
-
"
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
|
42 |
gr.Interface(
|
43 |
fn=estimate_genre,
|
44 |
inputs=gr.Audio(type="filepath", label="Upload audio file"),
|
45 |
outputs=gr.JSON(),
|
46 |
-
title="🎶
|
47 |
-
description="Estimates genre based on tempo, brightness, and harmonic content using librosa."
|
48 |
).launch()
|
|
|
13 |
return {"Error": f"Failed to load audio: {str(e)}"}
|
14 |
|
15 |
try:
|
16 |
+
# Tempo
|
17 |
+
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
18 |
+
|
19 |
+
# Spectral centroid (brightness)
|
20 |
+
centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
|
21 |
+
centroid_mean = float(np.mean(centroid))
|
22 |
+
centroid_std = float(np.std(centroid))
|
23 |
+
|
24 |
+
# Chroma
|
25 |
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
26 |
+
chroma_mean = float(np.mean(chroma))
|
27 |
+
chroma_std = float(np.std(chroma))
|
28 |
+
|
29 |
+
# Zero crossing rate
|
30 |
+
zcr = librosa.feature.zero_crossing_rate(y)
|
31 |
+
zcr_mean = float(np.mean(zcr))
|
32 |
+
|
33 |
+
# RMS (energy)
|
34 |
+
rms = librosa.feature.rms(y=y)
|
35 |
+
rms_mean = float(np.mean(rms))
|
36 |
+
|
37 |
except Exception as e:
|
38 |
return {"Error": f"Feature extraction failed: {str(e)}"}
|
39 |
|
40 |
+
# Heuristic genre rules (updated)
|
41 |
+
if tempo > 140 and centroid_mean > 2500 and rms_mean > 0.05:
|
42 |
genre = "Electronic / Dance"
|
43 |
+
elif tempo < 90 and chroma_mean < 0.3 and zcr_mean > 0.1:
|
44 |
genre = "Hip Hop / Trap"
|
45 |
+
elif 100 < tempo < 130 and centroid_mean > 2200 and chroma_std > 0.5:
|
46 |
genre = "Pop / Rock"
|
47 |
+
elif centroid_mean < 1800 and chroma_mean > 0.9 and rms_mean < 0.05:
|
48 |
genre = "Jazz / Soul"
|
49 |
else:
|
50 |
genre = "Unknown"
|
51 |
|
52 |
return {
|
53 |
"Predicted Genre": genre,
|
54 |
+
"Tempo (BPM)": round(tempo, 1),
|
55 |
+
"Brightness (Centroid Mean)": round(centroid_mean, 1),
|
56 |
+
"Brightness (Centroid Std)": round(centroid_std, 1),
|
57 |
+
"Chroma Mean": round(chroma_mean, 3),
|
58 |
+
"Chroma Std": round(chroma_std, 3),
|
59 |
+
"Zero Crossing Rate": round(zcr_mean, 4),
|
60 |
+
"RMS Energy": round(rms_mean, 4),
|
61 |
}
|
62 |
|
63 |
gr.Interface(
|
64 |
fn=estimate_genre,
|
65 |
inputs=gr.Audio(type="filepath", label="Upload audio file"),
|
66 |
outputs=gr.JSON(),
|
67 |
+
title="🎶 Smarter Genre Estimator",
|
68 |
+
description="Estimates genre based on tempo, brightness, energy, and harmonic content using librosa features."
|
69 |
).launch()
|