Spaces:
Sleeping
Sleeping
# 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() | |