Music Genre Classifier of Peruvian Music
The objective of this model is to classify the genre of a 10-15s audio fragment of a song. Its trained from the base model: MIT/ast-finetuned-audioset-10-10-0.4593 and it uses the songs from this dataset.
The genres it classifies are:
- Chicha
- Criolla
- Cumbia Amazónica
- Huayno
- Rock
Metrics
The final results of the model are the following:
precision recall f1-score support
chicha 0.90 0.85 0.87 407
criolla 0.88 0.92 0.90 346
cumbia_amazonica 0.89 0.77 0.82 350
huayno 0.84 0.92 0.88 521
rock 0.94 0.95 0.95 414
accuracy 0.89 2038
macro avg 0.89 0.88 0.88 2038
weighted avg 0.89 0.89 0.89 2038
The confusion matrix is the one below:

How to use
import librosa
import numpy as np
import torch
import joblib
from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
from huggingface_hub import hf_hub_download
repo_id = "pollitoconpapass/genre-classifier-peruvian-music-ast-v1"
SR = 16000
CLIP_SEC = 10.24
CLIP_SAMPLES = int(SR * CLIP_SEC)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Descargando componentes desde {repo_id}...")
le_path = hf_hub_download(repo_id=repo_id, filename="label_encoder.joblib")
le_loaded = joblib.load(le_path)
ast_loaded = AutoModelForAudioClassification.from_pretrained(repo_id).to(device)
fx_loaded = AutoFeatureExtractor.from_pretrained(repo_id)
print("Modelo AST descargado y cargado correctamente.")
def ventanas_inferencia(y, clip=CLIP_SAMPLES, min_sobra=0.5):
if len(y) < clip:
return [np.pad(y, (0, clip - len(y)))]
inis = list(range(0, len(y) - clip + 1, clip))
if len(y) - (inis[-1] + clip) >= int(min_sobra * SR):
inis.append(len(y) - clip)
return [y[s:s + clip] for s in inis]
def predecir_con_modelo_hf(ruta_audio, max_seg=15):
y, _ = librosa.load(ruta_audio, sr=SR, mono=True, duration=max_seg)
if len(y) < CLIP_SAMPLES:
y = np.pad(y, (0, CLIP_SAMPLES - len(y)))
clips = ventanas_inferencia(y)
X_inf = np.stack([fx_loaded(c, sampling_rate=SR)['input_values'][0] for c in clips]).astype(np.float32)
ast_loaded.eval()
with torch.no_grad():
logits = ast_loaded(input_values=torch.from_numpy(X_inf).to(device)).logits
proba = torch.softmax(logits, dim=1).mean(0).cpu().numpy()
orden = np.argsort(proba)[::-1]
return str(le_loaded.classes_[orden[0]]), {str(le_loaded.classes_[i]): round(float(proba[i]), 3) for i in orden}
# Main
archivo_audio = "/content/grabacion.wav" # <- replace with your 10-15s audio
genero_predicho, probabilidades = predecir_con_modelo_hf(archivo_audio)
print("Género predicho:", genero_predicho)
print("Probabilidades:", probabilidades)