Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py para tu Hugging Face Space (Gradio)
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Carga tu modelo desde Hugging Face Hub
|
| 7 |
+
# 隆IMPORTANTE! Reemplaza "TU_NOMBRE_DE_USUARIO/tu_modelo_de_instrumentos" con el ID de tu modelo
|
| 8 |
+
model_id = "Janiopi/detector_de_instrumentos_v1"
|
| 9 |
+
classifier = pipeline("audio-classification", model=model_id)
|
| 10 |
+
|
| 11 |
+
def classify_audio(audio_path):
|
| 12 |
+
"""
|
| 13 |
+
Clasifica un archivo de audio para identificar instrumentos musicales.
|
| 14 |
+
"""
|
| 15 |
+
if audio_path is None:
|
| 16 |
+
return "Por favor, sube un archivo de audio."
|
| 17 |
+
|
| 18 |
+
# El pipeline de Hugging Face maneja la carga y pre-procesamiento del audio
|
| 19 |
+
# directamente desde la ruta del archivo.
|
| 20 |
+
predictions = classifier(audio_path)
|
| 21 |
+
|
| 22 |
+
# Formatea las predicciones para una mejor visualizaci贸n
|
| 23 |
+
results = {}
|
| 24 |
+
for p in predictions:
|
| 25 |
+
results[p['label']] = p['score']
|
| 26 |
+
|
| 27 |
+
# Ordena los resultados de mayor a menor puntuaci贸n
|
| 28 |
+
sorted_results = sorted(results.items(), key=lambda item: item[1], reverse=True)
|
| 29 |
+
|
| 30 |
+
output_text = "Instrumentos detectados:\n"
|
| 31 |
+
for label, score in sorted_results:
|
| 32 |
+
output_text += f"- {label}: {score:.4f}\n"
|
| 33 |
+
|
| 34 |
+
return output_text
|
| 35 |
+
|
| 36 |
+
# Crea la interfaz de Gradio
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=classify_audio,
|
| 39 |
+
inputs=gr.Audio(type="filepath", label="Sube o graba un audio"),
|
| 40 |
+
outputs="text",
|
| 41 |
+
title="Detector de Instrumentos Musicales",
|
| 42 |
+
description="Sube un archivo de audio o graba uno para detectar instrumentos musicales como guitarra, bater铆a y piano."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Lanza la interfaz
|
| 46 |
+
iface.launch()
|