jsd219 commited on
Commit
33178c8
·
verified ·
1 Parent(s): 87cd387

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import gradio as gr
4
+ import librosa
5
+ import numpy as np
6
+
7
+ def estimate_genre(filepath):
8
+ try:
9
+ y, sr = librosa.load(filepath, duration=30)
10
+ except Exception as e:
11
+ return {"Error": f"Failed to load audio: {str(e)}"}
12
+
13
+ try:
14
+ tempo, _ = librosa.beat.beat_track(y, sr=sr)
15
+ centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
16
+ chroma = librosa.feature.chroma_stft(y=y, sr=sr)
17
+ chroma_strength = np.mean(np.max(chroma, axis=0))
18
+ except Exception as e:
19
+ return {"Error": f"Feature extraction failed: {str(e)}"}
20
+
21
+ # Heuristic genre rules
22
+ if tempo > 140 and centroid > 3000:
23
+ genre = "Electronic / Dance"
24
+ elif tempo < 80 and chroma_strength < 0.5:
25
+ genre = "Classical"
26
+ elif 90 < tempo < 110:
27
+ genre = "Hip Hop / R&B"
28
+ elif 110 <= tempo <= 130:
29
+ genre = "Pop / Rock"
30
+ elif tempo > 130 and chroma_strength > 0.6:
31
+ genre = "Funk / Disco"
32
+ else:
33
+ genre = "Other"
34
+
35
+ return {
36
+ "Predicted Genre": genre,
37
+ "Tempo (BPM)": round(tempo, 1),
38
+ "Brightness (Centroid)": round(centroid, 1),
39
+ "Chroma Strength": round(chroma_strength, 3)
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="🎶 Simple Genre Estimator",
47
+ description="Estimates genre based on tempo, brightness, and harmonic content using librosa."
48
+ ).launch()