File size: 1,557 Bytes
33178c8
 
 
 
 
 
 
9e17858
 
33178c8
 
 
 
 
 
386b508
33178c8
 
 
 
 
 
 
c869916
33178c8
3195410
 
c869916
33178c8
c869916
3195410
33178c8
3195410
33178c8
 
 
c8a8e04
 
33178c8
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
# app.py

import gradio as gr
import librosa
import numpy as np

def estimate_genre(filepath):
    print(f"[DEBUG] Filepath received: {filepath}")

    try:
        y, sr = librosa.load(filepath, duration=30)
    except Exception as e:
        return {"Error": f"Failed to load audio: {str(e)}"}

    try:
        tempo, _ = librosa.beat.beat_track(y=y, sr=sr)  # โœ… FIXED here
        centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
        chroma = librosa.feature.chroma_stft(y=y, sr=sr)
        chroma_strength = np.mean(np.max(chroma, axis=0))
    except Exception as e:
        return {"Error": f"Feature extraction failed: {str(e)}"}

    # Heuristic genre rules
    if tempo > 140 and centroid > 2500:
        genre = "Electronic / Dance"
    elif tempo < 90 and chroma_strength < 0.3:
        genre = "Hip Hop / Trap"
    elif 100 < tempo < 130 and centroid > 2300 and chroma_strength > 0.7:
        genre = "Pop / Rock"
    elif centroid < 1800 and chroma_strength > 0.9:
        genre = "Jazz / Soul"
    else:
        genre = "Unknown"

    return {
        "Predicted Genre": genre,
        "Tempo (BPM)": round(float(tempo), 1),
        "Brightness (Centroid)": round(float(centroid), 1),
        "Chroma Strength": round(chroma_strength, 3)
    }

gr.Interface(
    fn=estimate_genre,
    inputs=gr.Audio(type="filepath", label="Upload audio file"),
    outputs=gr.JSON(),
    title="๐ŸŽถ Simple Genre Estimator",
    description="Estimates genre based on tempo, brightness, and harmonic content using librosa."
).launch()