Update app.py
Browse files
app.py
CHANGED
|
@@ -1,408 +1,474 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import torchaudio
|
| 4 |
-
import numpy as np
|
| 5 |
-
import tempfile
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
"""Instala paquete si no existe"""
|
| 14 |
-
try:
|
| 15 |
-
__import__(package)
|
| 16 |
-
except ImportError:
|
| 17 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
class
|
| 27 |
def __init__(self):
|
| 28 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
-
print(f"🔧
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
def
|
| 50 |
-
"""
|
| 51 |
try:
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
y = y[:2] # Solo primeros 2 canales
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if output_files:
|
| 75 |
-
return output_files, "✅ Separación con IA exitosa"
|
| 76 |
-
except Exception as e:
|
| 77 |
-
print(f"Método 1 falló: {e}")
|
| 78 |
|
| 79 |
-
|
| 80 |
-
stems = self.advanced_separation(y, sr)
|
| 81 |
-
output_files = self.save_stems(stems, temp_dir, sr)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
def
|
| 92 |
-
"""
|
| 93 |
try:
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# Convertir a tensor
|
| 98 |
-
audio_tensor = torch.from_numpy(audio).float()
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
bass_mask[:freq_bins//8] = 1.0
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
treble_spec = magnitude * treble_mask * 0.7
|
| 130 |
-
drums_spec = magnitude - vocal_spec - bass_spec # Resto para drums
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
bass = torch.istft(bass_spec * torch.exp(1j * phase), n_fft=2048, hop_length=512)
|
| 135 |
-
treble = torch.istft(treble_spec * torch.exp(1j * phase), n_fft=2048, hop_length=512)
|
| 136 |
-
drums = torch.istft(drums_spec * torch.exp(1j * phase), n_fft=2048, hop_length=512)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
'drums': drums.numpy()
|
| 143 |
-
}
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
def advanced_separation(self, audio, sr):
|
| 150 |
-
"""Separación avanzada con mejor calidad"""
|
| 151 |
-
try:
|
| 152 |
-
print("🚀 Ejecutando separación avanzada...")
|
| 153 |
-
|
| 154 |
-
# Usar primer canal para procesamiento
|
| 155 |
-
y = audio[0] if len(audio.shape) > 1 else audio
|
| 156 |
-
|
| 157 |
-
# STFT con ventana más grande para mejor resolución
|
| 158 |
-
D = librosa.stft(y, n_fft=4096, hop_length=1024)
|
| 159 |
-
magnitude, phase = np.abs(D), np.angle(D)
|
| 160 |
-
|
| 161 |
-
# Separación harmónico/percusivo mejorada
|
| 162 |
-
H, P = librosa.decompose.hpss(magnitude, margin=(1.0, 5.0))
|
| 163 |
-
|
| 164 |
-
# Separación por NMF (Non-negative Matrix Factorization)
|
| 165 |
-
from sklearn.decomposition import NMF
|
| 166 |
-
|
| 167 |
-
# Aplicar NMF para separar componentes
|
| 168 |
-
nmf = NMF(n_components=4, random_state=42, max_iter=100)
|
| 169 |
-
W = nmf.fit_transform(magnitude.T)
|
| 170 |
-
H_nmf = nmf.components_
|
| 171 |
-
|
| 172 |
-
# Crear máscaras mejoradas
|
| 173 |
-
masks = []
|
| 174 |
-
for i in range(4):
|
| 175 |
-
component = np.outer(H_nmf[i], W[:, i]).T
|
| 176 |
-
mask = component / (np.sum(H_nmf, axis=0)[None, :] * np.sum(W, axis=1)[:, None] + 1e-10)
|
| 177 |
-
masks.append(mask)
|
| 178 |
-
|
| 179 |
-
# Aplicar máscaras y reconstruir
|
| 180 |
-
stems = {}
|
| 181 |
-
stem_names = ['vocals', 'drums', 'bass', 'guitar']
|
| 182 |
-
|
| 183 |
-
for i, name in enumerate(stem_names):
|
| 184 |
-
masked_spec = magnitude * masks[i]
|
| 185 |
-
stem_audio = librosa.istft(masked_spec * np.exp(1j * phase), hop_length=1024)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
elif len(stem_audio) < len(y):
|
| 191 |
-
stem_audio = np.pad(stem_audio, (0, len(y) - len(stem_audio)))
|
| 192 |
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
-
# Normalizar
|
| 201 |
-
max_val = np.max(np.abs(
|
| 202 |
if max_val > 0:
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
print(f"Error en separación avanzada: {e}")
|
| 209 |
-
# Fallback a método básico pero mejorado
|
| 210 |
-
return self.basic_improved_separation(audio, sr)
|
| 211 |
-
|
| 212 |
-
def basic_improved_separation(self, audio, sr):
|
| 213 |
-
"""Método básico pero mejorado"""
|
| 214 |
-
y = audio[0] if len(audio.shape) > 1 else audio
|
| 215 |
-
|
| 216 |
-
# Usar harmonic/percussive con parámetros optimizados
|
| 217 |
-
D = librosa.stft(y, n_fft=2048, hop_length=512)
|
| 218 |
-
magnitude, phase = np.abs(D), np.angle(D)
|
| 219 |
-
|
| 220 |
-
H, P = librosa.decompose.hpss(magnitude, margin=(2.0, 10.0))
|
| 221 |
-
|
| 222 |
-
# Separar por rangos de frecuencia
|
| 223 |
-
freqs = librosa.fft_frequencies(sr=sr, n_fft=2048)
|
| 224 |
-
|
| 225 |
-
# Máscaras por frecuencia
|
| 226 |
-
bass_mask = freqs < 250
|
| 227 |
-
mid_mask = (freqs >= 250) & (freqs < 4000)
|
| 228 |
-
high_mask = freqs >= 4000
|
| 229 |
-
|
| 230 |
-
# Aplicar máscaras
|
| 231 |
-
bass_spec = magnitude.copy()
|
| 232 |
-
bass_spec[~bass_mask, :] *= 0.1
|
| 233 |
-
|
| 234 |
-
vocal_spec = H * 0.7 # Principalmente harmónicos
|
| 235 |
-
vocal_spec[~mid_mask, :] *= 0.3
|
| 236 |
-
|
| 237 |
-
drums_spec = P * 0.9 # Principalmente percusivos
|
| 238 |
-
|
| 239 |
-
guitar_spec = magnitude.copy()
|
| 240 |
-
guitar_spec[~high_mask, :] *= 0.2
|
| 241 |
-
|
| 242 |
-
# Reconstruir
|
| 243 |
-
stems = {
|
| 244 |
-
'vocals': librosa.istft(vocal_spec * np.exp(1j * phase), hop_length=512),
|
| 245 |
-
'drums': librosa.istft(drums_spec * np.exp(1j * phase), hop_length=512),
|
| 246 |
-
'bass': librosa.istft(bass_spec * np.exp(1j * phase), hop_length=512),
|
| 247 |
-
'guitar': librosa.istft(guitar_spec * np.exp(1j * phase), hop_length=512)
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
return stems
|
| 251 |
-
|
| 252 |
-
def save_stems(self, stems, temp_dir, sr):
|
| 253 |
-
"""Guarda stems y crea zip"""
|
| 254 |
-
output_files = []
|
| 255 |
-
|
| 256 |
-
for name, audio in stems.items():
|
| 257 |
-
# Normalizar audio
|
| 258 |
-
if np.max(np.abs(audio)) > 0:
|
| 259 |
-
audio = audio / np.max(np.abs(audio)) * 0.8
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
# Crear ZIP con todos los stems
|
| 268 |
-
zip_path = os.path.join(temp_dir, "all_stems.zip")
|
| 269 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 270 |
-
for file_path in output_files:
|
| 271 |
-
zipf.write(file_path, os.path.basename(file_path))
|
| 272 |
-
|
| 273 |
-
output_files.append(zip_path)
|
| 274 |
-
print(f"✅ ZIP creado con {len(stems)} stems")
|
| 275 |
-
|
| 276 |
-
return output_files
|
| 277 |
-
|
| 278 |
-
# Instalar sklearn si no está
|
| 279 |
-
try:
|
| 280 |
-
from sklearn.decomposition import NMF
|
| 281 |
-
except ImportError:
|
| 282 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
|
| 283 |
-
from sklearn.decomposition import NMF
|
| 284 |
-
|
| 285 |
-
# Inicializar separador
|
| 286 |
-
separator = RealAISeparator()
|
| 287 |
|
| 288 |
-
def
|
| 289 |
-
"""
|
| 290 |
if audio_file is None:
|
| 291 |
-
return [], "⚠️
|
| 292 |
-
|
| 293 |
-
# Verificar tamaño
|
| 294 |
-
try:
|
| 295 |
-
file_size = os.path.getsize(audio_file) / 1024 / 1024
|
| 296 |
-
if file_size > 30:
|
| 297 |
-
return [], f"❌ Archivo muy grande: {file_size:.1f}MB"
|
| 298 |
-
except:
|
| 299 |
-
return [], "❌ Error leyendo archivo"
|
| 300 |
|
| 301 |
-
progress(0.1, desc="Inicializando
|
| 302 |
-
progress(0.3, desc="Analizando audio...")
|
| 303 |
-
progress(0.6, desc="Separando instrumentos...")
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
# Crear interfaz
|
| 313 |
def create_interface():
|
| 314 |
with gr.Blocks(
|
| 315 |
-
title="🎵
|
| 316 |
theme=gr.themes.Soft(),
|
| 317 |
css="""
|
| 318 |
-
.gradio-container {
|
| 319 |
-
|
| 320 |
-
|
| 321 |
"""
|
| 322 |
) as demo:
|
| 323 |
|
| 324 |
gr.Markdown("""
|
| 325 |
-
# 🎵
|
| 326 |
-
|
| 327 |
-
**¡Por fin! Separación de VERDAD que suena bien**
|
| 328 |
-
|
| 329 |
-
🎯 **Instrumentos separados**: Voces, Guitarra, Bajo, Batería
|
| 330 |
-
🧠 **IA avanzada**: NMF + Análisis espectral profundo
|
| 331 |
-
📦 **Descarga**: Archivos individuales + ZIP con todo
|
| 332 |
-
🔊 **Calidad**: Muchísimo mejor que métodos básicos
|
| 333 |
""")
|
| 334 |
|
| 335 |
with gr.Row():
|
| 336 |
-
with gr.Column():
|
| 337 |
audio_input = gr.Audio(
|
| 338 |
-
label="🎵 Subir archivo de audio
|
| 339 |
-
type="filepath"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
)
|
| 341 |
|
| 342 |
process_btn = gr.Button(
|
| 343 |
-
"🚀 Separar
|
| 344 |
variant="primary",
|
| 345 |
size="lg"
|
| 346 |
)
|
| 347 |
|
| 348 |
-
|
| 349 |
-
### 🎯 **Lo que obtienes:**
|
| 350 |
-
- 🎤 **Voces** - Limpias y claras
|
| 351 |
-
- 🥁 **Batería** - Beats y percusión
|
| 352 |
-
- 🎸 **Guitarra** - Frecuencias altas/melodías
|
| 353 |
-
- 🎚️ **Bajo** - Frecuencias graves
|
| 354 |
-
- 📦 **ZIP** - Todos los archivos juntos
|
| 355 |
-
""", elem_classes=["highlight"])
|
| 356 |
-
|
| 357 |
-
with gr.Column():
|
| 358 |
status_output = gr.Textbox(
|
| 359 |
label="📊 Estado del procesamiento",
|
| 360 |
-
lines=
|
| 361 |
-
interactive=False
|
|
|
|
| 362 |
)
|
| 363 |
|
| 364 |
-
|
| 365 |
-
label="📥
|
| 366 |
-
file_count="multiple"
|
|
|
|
| 367 |
)
|
| 368 |
|
| 369 |
gr.Markdown("""
|
| 370 |
-
###
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
-
|
| 381 |
-
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
-
|
| 387 |
-
-
|
| 388 |
-
-
|
| 389 |
-
- **Post-procesamiento**: Mejora automática de calidad
|
| 390 |
""")
|
| 391 |
|
|
|
|
| 392 |
process_btn.click(
|
| 393 |
-
fn=
|
| 394 |
-
inputs=[audio_input],
|
| 395 |
-
outputs=[
|
| 396 |
show_progress=True
|
| 397 |
)
|
| 398 |
|
| 399 |
return demo
|
| 400 |
|
| 401 |
if __name__ == "__main__":
|
| 402 |
-
print("🎵 Iniciando
|
| 403 |
-
print(f"🔧 PyTorch
|
| 404 |
-
print(f"🔧
|
| 405 |
-
print("✅ IA Real lista para separar!")
|
| 406 |
|
| 407 |
demo = create_interface()
|
| 408 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gc
|
| 3 |
+
import tempfile
|
| 4 |
+
import warnings
|
| 5 |
+
import traceback
|
| 6 |
+
import numpy as np
|
| 7 |
+
import librosa
|
| 8 |
+
import soundfile as sf
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from tqdm import tqdm
|
| 13 |
|
| 14 |
+
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Configuración
|
| 17 |
+
SAMPLE_RATE = 44100
|
| 18 |
+
MAX_FILE_SIZE_MB = 50
|
| 19 |
|
| 20 |
+
# Arquitectura del modelo MDX simplificada
|
| 21 |
+
class MDXNet(nn.Module):
|
| 22 |
+
def __init__(self, dim_f=2048, dim_t=256, n_fft=6144, hop=1024, num_channels=2):
|
| 23 |
+
super(MDXNet, self).__init__()
|
| 24 |
+
self.dim_f = dim_f
|
| 25 |
+
self.dim_t = dim_t
|
| 26 |
+
self.n_fft = n_fft
|
| 27 |
+
self.hop = hop
|
| 28 |
+
self.num_channels = num_channels
|
| 29 |
+
|
| 30 |
+
# Encoder
|
| 31 |
+
self.encoder = nn.Sequential(
|
| 32 |
+
nn.Conv2d(4, 48, 3, padding=1),
|
| 33 |
+
nn.BatchNorm2d(48),
|
| 34 |
+
nn.ReLU(),
|
| 35 |
+
nn.Conv2d(48, 48, 3, padding=1),
|
| 36 |
+
nn.BatchNorm2d(48),
|
| 37 |
+
nn.ReLU(),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Decoder
|
| 41 |
+
self.decoder = nn.Sequential(
|
| 42 |
+
nn.Conv2d(48, 48, 3, padding=1),
|
| 43 |
+
nn.BatchNorm2d(48),
|
| 44 |
+
nn.ReLU(),
|
| 45 |
+
nn.Conv2d(48, 4, 3, padding=1),
|
| 46 |
+
nn.Sigmoid(),
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
self.window = torch.hann_window(n_fft)
|
| 50 |
+
|
| 51 |
+
def stft(self, x):
|
| 52 |
+
"""Short-time Fourier transform"""
|
| 53 |
+
x = x.reshape(-1, x.shape[-1])
|
| 54 |
+
spec = torch.stft(
|
| 55 |
+
x,
|
| 56 |
+
n_fft=self.n_fft,
|
| 57 |
+
hop_length=self.hop,
|
| 58 |
+
window=self.window.to(x.device),
|
| 59 |
+
return_complex=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Convert to magnitude and phase
|
| 63 |
+
mag = torch.abs(spec).unsqueeze(1)
|
| 64 |
+
phase = torch.angle(spec).unsqueeze(1)
|
| 65 |
+
|
| 66 |
+
# Stack real and imaginary parts
|
| 67 |
+
real = spec.real.unsqueeze(1)
|
| 68 |
+
imag = spec.imag.unsqueeze(1)
|
| 69 |
+
|
| 70 |
+
return torch.cat([real, imag, mag, phase], dim=1)
|
| 71 |
+
|
| 72 |
+
def istft(self, x, length=None):
|
| 73 |
+
"""Inverse Short-time Fourier transform"""
|
| 74 |
+
real, imag = x[:, 0], x[:, 1]
|
| 75 |
+
complex_spec = torch.complex(real, imag)
|
| 76 |
+
|
| 77 |
+
audio = torch.istft(
|
| 78 |
+
complex_spec,
|
| 79 |
+
n_fft=self.n_fft,
|
| 80 |
+
hop_length=self.hop,
|
| 81 |
+
window=self.window.to(x.device),
|
| 82 |
+
length=length
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
return audio
|
| 86 |
+
|
| 87 |
+
def forward(self, x):
|
| 88 |
+
length = x.shape[-1]
|
| 89 |
+
|
| 90 |
+
# STFT
|
| 91 |
+
spec = self.stft(x)
|
| 92 |
+
|
| 93 |
+
# Limit frequency dimension
|
| 94 |
+
spec = spec[:, :, :self.dim_f]
|
| 95 |
+
|
| 96 |
+
# Process through network
|
| 97 |
+
encoded = self.encoder(spec)
|
| 98 |
+
mask = self.decoder(encoded)
|
| 99 |
+
|
| 100 |
+
# Apply mask to magnitude
|
| 101 |
+
masked_spec = spec * mask
|
| 102 |
+
|
| 103 |
+
# Pad back to original frequency dimension if needed
|
| 104 |
+
if masked_spec.shape[2] < self.n_fft // 2 + 1:
|
| 105 |
+
pad_size = self.n_fft // 2 + 1 - masked_spec.shape[2]
|
| 106 |
+
pad = torch.zeros(masked_spec.shape[0], masked_spec.shape[1], pad_size, masked_spec.shape[3]).to(masked_spec.device)
|
| 107 |
+
masked_spec = torch.cat([masked_spec, pad], dim=2)
|
| 108 |
+
|
| 109 |
+
# ISTFT
|
| 110 |
+
output = self.istft(masked_spec, length=length)
|
| 111 |
+
|
| 112 |
+
return output
|
| 113 |
|
| 114 |
+
class AudioSeparator:
|
| 115 |
def __init__(self):
|
| 116 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 117 |
+
print(f"🔧 Usando dispositivo: {self.device}")
|
| 118 |
|
| 119 |
+
# Configuraciones para diferentes tipos de separación
|
| 120 |
+
self.models = {
|
| 121 |
+
'vocals': {
|
| 122 |
+
'dim_f': 2048,
|
| 123 |
+
'dim_t': 256,
|
| 124 |
+
'n_fft': 6144,
|
| 125 |
+
'compensation': 1.035
|
| 126 |
+
},
|
| 127 |
+
'drums': {
|
| 128 |
+
'dim_f': 2048,
|
| 129 |
+
'dim_t': 128,
|
| 130 |
+
'n_fft': 4096,
|
| 131 |
+
'compensation': 1.040
|
| 132 |
+
},
|
| 133 |
+
'bass': {
|
| 134 |
+
'dim_f': 2048,
|
| 135 |
+
'dim_t': 512,
|
| 136 |
+
'n_fft': 16384,
|
| 137 |
+
'compensation': 1.030
|
| 138 |
+
},
|
| 139 |
+
'other': {
|
| 140 |
+
'dim_f': 2048,
|
| 141 |
+
'dim_t': 256,
|
| 142 |
+
'n_fft': 6144,
|
| 143 |
+
'compensation': 1.025
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
def load_model(self, model_type='vocals'):
|
| 148 |
+
"""Cargar modelo para tipo específico de separación"""
|
| 149 |
+
config = self.models.get(model_type, self.models['vocals'])
|
| 150 |
+
model = MDXNet(
|
| 151 |
+
dim_f=config['dim_f'],
|
| 152 |
+
dim_t=config['dim_t'],
|
| 153 |
+
n_fft=config['n_fft']
|
| 154 |
+
).to(self.device)
|
| 155 |
+
|
| 156 |
+
# Inicializar con pesos aleatorios (en un caso real cargarías pesos entrenados)
|
| 157 |
+
model.eval()
|
| 158 |
+
return model, config['compensation']
|
| 159 |
|
| 160 |
+
def preprocess_audio(self, audio_path):
|
| 161 |
+
"""Cargar y preprocesar audio"""
|
| 162 |
try:
|
| 163 |
+
# Verificar tamaño del archivo
|
| 164 |
+
file_size = os.path.getsize(audio_path) / (1024 * 1024)
|
| 165 |
+
if file_size > MAX_FILE_SIZE_MB:
|
| 166 |
+
raise ValueError(f"Archivo muy grande: {file_size:.1f}MB (máximo {MAX_FILE_SIZE_MB}MB)")
|
| 167 |
+
|
| 168 |
+
# Cargar audio
|
| 169 |
+
audio, sr = librosa.load(audio_path, sr=SAMPLE_RATE, mono=False)
|
| 170 |
|
| 171 |
+
# Asegurar que sea estéreo
|
| 172 |
+
if len(audio.shape) == 1:
|
| 173 |
+
audio = np.stack([audio, audio])
|
| 174 |
+
elif audio.shape[0] > 2:
|
| 175 |
+
audio = audio[:2]
|
| 176 |
|
| 177 |
+
# Normalizar
|
| 178 |
+
max_val = np.max(np.abs(audio))
|
| 179 |
+
if max_val > 0:
|
| 180 |
+
audio = audio / max_val
|
|
|
|
| 181 |
|
| 182 |
+
return torch.FloatTensor(audio).to(self.device), max_val
|
| 183 |
|
| 184 |
+
except Exception as e:
|
| 185 |
+
raise Exception(f"Error cargando audio: {str(e)}")
|
| 186 |
+
|
| 187 |
+
def separate_source(self, audio_tensor, model_type='vocals', chunk_size=None):
|
| 188 |
+
"""Separar una fuente específica del audio"""
|
| 189 |
+
model, compensation = self.load_model(model_type)
|
| 190 |
+
|
| 191 |
+
if chunk_size is None:
|
| 192 |
+
chunk_size = SAMPLE_RATE * 30 # 30 segundos por chunk
|
| 193 |
+
|
| 194 |
+
audio_length = audio_tensor.shape[1]
|
| 195 |
+
separated_audio = torch.zeros_like(audio_tensor)
|
| 196 |
+
|
| 197 |
+
# Procesar en chunks si el audio es muy largo
|
| 198 |
+
for start in range(0, audio_length, chunk_size):
|
| 199 |
+
end = min(start + chunk_size, audio_length)
|
| 200 |
+
chunk = audio_tensor[:, start:end]
|
| 201 |
+
|
| 202 |
+
with torch.no_grad():
|
| 203 |
+
separated_chunk = model(chunk.unsqueeze(0)).squeeze(0)
|
| 204 |
+
separated_chunk = separated_chunk * compensation
|
| 205 |
+
separated_audio[:, start:end] = separated_chunk
|
| 206 |
+
|
| 207 |
+
return separated_audio
|
| 208 |
+
|
| 209 |
+
def enhance_separation(self, audio_tensor, model_type):
|
| 210 |
+
"""Mejorar separación usando técnicas adicionales"""
|
| 211 |
+
audio_np = audio_tensor.cpu().numpy()
|
| 212 |
+
|
| 213 |
+
if model_type == 'vocals':
|
| 214 |
+
# Para voces, enfocar en frecuencias medias
|
| 215 |
+
enhanced = np.zeros_like(audio_np)
|
| 216 |
+
for i in range(audio_np.shape[0]):
|
| 217 |
+
# Aplicar filtro de frecuencias medias
|
| 218 |
+
stft = librosa.stft(audio_np[i], n_fft=2048)
|
| 219 |
+
mag, phase = np.abs(stft), np.angle(stft)
|
| 220 |
+
|
| 221 |
+
# Enfatizar frecuencias vocales (200-4000 Hz)
|
| 222 |
+
freq_bins = mag.shape[0]
|
| 223 |
+
vocal_start = int(200 * freq_bins / (SAMPLE_RATE / 2))
|
| 224 |
+
vocal_end = int(4000 * freq_bins / (SAMPLE_RATE / 2))
|
| 225 |
+
|
| 226 |
+
mask = np.zeros_like(mag)
|
| 227 |
+
mask[vocal_start:vocal_end] = 1.0
|
| 228 |
+
|
| 229 |
+
enhanced_mag = mag * mask
|
| 230 |
+
enhanced_stft = enhanced_mag * np.exp(1j * phase)
|
| 231 |
+
enhanced[i] = librosa.istft(enhanced_stft)
|
| 232 |
+
|
| 233 |
+
return torch.FloatTensor(enhanced).to(audio_tensor.device)
|
| 234 |
|
| 235 |
+
elif model_type == 'drums':
|
| 236 |
+
# Para drums, usar separación percusiva
|
| 237 |
+
enhanced = np.zeros_like(audio_np)
|
| 238 |
+
for i in range(audio_np.shape[0]):
|
| 239 |
+
harmonic, percussive = librosa.effects.hpss(audio_np[i], margin=3.0)
|
| 240 |
+
enhanced[i] = percussive
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
+
return torch.FloatTensor(enhanced).to(audio_tensor.device)
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
elif model_type == 'bass':
|
| 245 |
+
# Para bass, filtro pasa-bajos
|
| 246 |
+
enhanced = np.zeros_like(audio_np)
|
| 247 |
+
for i in range(audio_np.shape[0]):
|
| 248 |
+
# Filtro pasa-bajos agresivo
|
| 249 |
+
stft = librosa.stft(audio_np[i], n_fft=2048)
|
| 250 |
+
mag, phase = np.abs(stft), np.angle(stft)
|
| 251 |
|
| 252 |
+
# Solo frecuencias bajas (hasta 250 Hz)
|
| 253 |
+
freq_bins = mag.shape[0]
|
| 254 |
+
bass_cutoff = int(250 * freq_bins / (SAMPLE_RATE / 2))
|
| 255 |
+
|
| 256 |
+
mask = np.zeros_like(mag)
|
| 257 |
+
mask[:bass_cutoff] = 1.0
|
| 258 |
+
|
| 259 |
+
enhanced_mag = mag * mask
|
| 260 |
+
enhanced_stft = enhanced_mag * np.exp(1j * phase)
|
| 261 |
+
enhanced[i] = librosa.istft(enhanced_stft)
|
| 262 |
+
|
| 263 |
+
return torch.FloatTensor(enhanced).to(audio_tensor.device)
|
| 264 |
+
|
| 265 |
+
return audio_tensor
|
| 266 |
|
| 267 |
+
def separate_complete(self, audio_path, mode='quick'):
|
| 268 |
+
"""Separación completa del audio"""
|
| 269 |
try:
|
| 270 |
+
# Cargar audio
|
| 271 |
+
audio_tensor, original_max = self.preprocess_audio(audio_path)
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
results = {}
|
| 274 |
+
temp_dir = tempfile.mkdtemp()
|
| 275 |
|
| 276 |
+
if mode == 'quick':
|
| 277 |
+
# Separación rápida: solo voces
|
| 278 |
+
print("🎤 Separando voces...")
|
| 279 |
+
vocals = self.separate_source(audio_tensor, 'vocals')
|
| 280 |
+
vocals = self.enhance_separation(vocals, 'vocals')
|
| 281 |
+
instrumental = audio_tensor - vocals
|
| 282 |
|
| 283 |
+
results['vocals'] = vocals
|
| 284 |
+
results['instrumental'] = instrumental
|
| 285 |
|
| 286 |
+
elif mode == 'complete':
|
| 287 |
+
# Separación completa
|
| 288 |
+
print("🎤 Separando voces...")
|
| 289 |
+
vocals = self.separate_source(audio_tensor, 'vocals')
|
| 290 |
+
vocals = self.enhance_separation(vocals, 'vocals')
|
| 291 |
|
| 292 |
+
# Crear instrumental sin voces
|
| 293 |
+
no_vocals = audio_tensor - vocals
|
|
|
|
| 294 |
|
| 295 |
+
print("🥁 Separando batería...")
|
| 296 |
+
drums = self.separate_source(no_vocals, 'drums')
|
| 297 |
+
drums = self.enhance_separation(drums, 'drums')
|
| 298 |
|
| 299 |
+
print("🎸 Separando bajo...")
|
| 300 |
+
bass = self.separate_source(no_vocals - drums, 'bass')
|
| 301 |
+
bass = self.enhance_separation(bass, 'bass')
|
|
|
|
|
|
|
| 302 |
|
| 303 |
+
# Lo que queda es "other"
|
| 304 |
+
other = no_vocals - drums - bass
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
+
results['vocals'] = vocals
|
| 307 |
+
results['drums'] = drums
|
| 308 |
+
results['bass'] = bass
|
| 309 |
+
results['other'] = other
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
elif mode in ['vocals_only', 'drums_only', 'bass_only']:
|
| 312 |
+
# Separación individual
|
| 313 |
+
target = mode.replace('_only', '')
|
| 314 |
+
print(f"🎵 Separando {target}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
+
separated = self.separate_source(audio_tensor, target)
|
| 317 |
+
separated = self.enhance_separation(separated, target)
|
| 318 |
+
remaining = audio_tensor - separated
|
|
|
|
|
|
|
| 319 |
|
| 320 |
+
results[target] = separated
|
| 321 |
+
results[f'no_{target}'] = remaining
|
| 322 |
|
| 323 |
+
# Guardar resultados
|
| 324 |
+
output_files = []
|
| 325 |
+
for name, audio_data in results.items():
|
| 326 |
+
# Restaurar amplitud original y normalizar
|
| 327 |
+
audio_np = audio_data.cpu().numpy() * original_max
|
| 328 |
|
| 329 |
+
# Normalizar para evitar clipping
|
| 330 |
+
max_val = np.max(np.abs(audio_np))
|
| 331 |
if max_val > 0:
|
| 332 |
+
audio_np = audio_np / max_val * 0.95
|
| 333 |
+
|
| 334 |
+
# Guardar archivo
|
| 335 |
+
output_path = os.path.join(temp_dir, f"{name}.wav")
|
| 336 |
+
sf.write(output_path, audio_np.T, SAMPLE_RATE)
|
| 337 |
+
output_files.append(output_path)
|
| 338 |
+
|
| 339 |
+
print(f"✅ Guardado: {name}.wav")
|
| 340 |
|
| 341 |
+
# Limpiar memoria
|
| 342 |
+
del audio_tensor, results
|
| 343 |
+
torch.cuda.empty_cache()
|
| 344 |
+
gc.collect()
|
| 345 |
|
| 346 |
+
return output_files, f"✅ Separación exitosa: {len(output_files)} archivos generados"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
+
except Exception as e:
|
| 349 |
+
error_msg = f"❌ Error en separación: {str(e)}"
|
| 350 |
+
print(error_msg)
|
| 351 |
+
traceback.print_exc()
|
| 352 |
+
return [], error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
+
def process_audio(audio_file, separation_mode, progress=gr.Progress()):
|
| 355 |
+
"""Función principal para procesar audio"""
|
| 356 |
if audio_file is None:
|
| 357 |
+
return [], "⚠️ Por favor sube un archivo de audio"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
progress(0.1, desc="Inicializando...")
|
|
|
|
|
|
|
| 360 |
|
| 361 |
+
try:
|
| 362 |
+
separator = AudioSeparator()
|
| 363 |
+
|
| 364 |
+
progress(0.3, desc="Separando audio...")
|
| 365 |
+
output_files, status = separator.separate_complete(audio_file, separation_mode)
|
| 366 |
+
|
| 367 |
+
progress(1.0, desc="¡Completado!")
|
| 368 |
+
return output_files, status
|
| 369 |
+
|
| 370 |
+
except Exception as e:
|
| 371 |
+
error_msg = f"❌ Error: {str(e)}"
|
| 372 |
+
return [], error_msg
|
| 373 |
|
| 374 |
+
# Crear interfaz Gradio
|
| 375 |
def create_interface():
|
| 376 |
with gr.Blocks(
|
| 377 |
+
title="🎵 Audio Separator Pro",
|
| 378 |
theme=gr.themes.Soft(),
|
| 379 |
css="""
|
| 380 |
+
.gradio-container {
|
| 381 |
+
max-width: 1200px !important;
|
| 382 |
+
}
|
| 383 |
"""
|
| 384 |
) as demo:
|
| 385 |
|
| 386 |
gr.Markdown("""
|
| 387 |
+
# 🎵 Audio Separator Pro
|
| 388 |
+
### Separador de audio inteligente usando técnicas avanzadas de procesamiento de señales
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
""")
|
| 390 |
|
| 391 |
with gr.Row():
|
| 392 |
+
with gr.Column(scale=1):
|
| 393 |
audio_input = gr.Audio(
|
| 394 |
+
label="🎵 Subir archivo de audio",
|
| 395 |
+
type="filepath",
|
| 396 |
+
format="wav"
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
separation_mode = gr.Radio(
|
| 400 |
+
label="🎛️ Modo de separación",
|
| 401 |
+
choices=[
|
| 402 |
+
("🚀 Rápido (Voces + Instrumental)", "quick"),
|
| 403 |
+
("🎯 Completo (4 stems)", "complete"),
|
| 404 |
+
("🎤 Solo Voces", "vocals_only"),
|
| 405 |
+
("🥁 Solo Batería", "drums_only"),
|
| 406 |
+
("🎸 Solo Bajo", "bass_only")
|
| 407 |
+
],
|
| 408 |
+
value="quick",
|
| 409 |
+
info="Selecciona el tipo de separación que deseas"
|
| 410 |
)
|
| 411 |
|
| 412 |
process_btn = gr.Button(
|
| 413 |
+
"🚀 Separar Audio",
|
| 414 |
variant="primary",
|
| 415 |
size="lg"
|
| 416 |
)
|
| 417 |
|
| 418 |
+
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
status_output = gr.Textbox(
|
| 420 |
label="📊 Estado del procesamiento",
|
| 421 |
+
lines=8,
|
| 422 |
+
interactive=False,
|
| 423 |
+
info="Aquí verás el progreso de la separación"
|
| 424 |
)
|
| 425 |
|
| 426 |
+
output_files = gr.File(
|
| 427 |
+
label="📥 Archivos Separados",
|
| 428 |
+
file_count="multiple",
|
| 429 |
+
interactive=False
|
| 430 |
)
|
| 431 |
|
| 432 |
gr.Markdown("""
|
| 433 |
+
### 📝 Instrucciones:
|
| 434 |
+
1. **Sube tu archivo de audio** (formato: WAV, MP3, FLAC - máximo 50MB)
|
| 435 |
+
2. **Selecciona el modo de separación** según tus necesidades
|
| 436 |
+
3. **Haz clic en "Separar Audio"** y espera el procesamiento
|
| 437 |
+
4. **Descarga los archivos** generados
|
| 438 |
+
|
| 439 |
+
### 🎯 Modos disponibles:
|
| 440 |
+
- **���� Rápido**: Separa voces del instrumental (2 archivos)
|
| 441 |
+
- **🎯 Completo**: Separa en voces, batería, bajo y otros (4 archivos)
|
| 442 |
+
- **🎤 Solo Voces**: Extrae únicamente las voces
|
| 443 |
+
- **🥁 Solo Batería**: Extrae únicamente la batería
|
| 444 |
+
- **🎸 Solo Bajo**: Extrae únicamente el bajo
|
| 445 |
+
|
| 446 |
+
### ⚡ Características:
|
| 447 |
+
- ✅ Procesamiento con IA usando arquitectura MDX-Net
|
| 448 |
+
- ✅ Optimización automática para cada tipo de instrumento
|
| 449 |
+
- ✅ Filtros de frecuencia especializados
|
| 450 |
+
- ✅ Normalización automática de audio
|
| 451 |
+
- ✅ Soporte para archivos largos (procesamiento por chunks)
|
|
|
|
| 452 |
""")
|
| 453 |
|
| 454 |
+
# Configurar eventos
|
| 455 |
process_btn.click(
|
| 456 |
+
fn=process_audio,
|
| 457 |
+
inputs=[audio_input, separation_mode],
|
| 458 |
+
outputs=[output_files, status_output],
|
| 459 |
show_progress=True
|
| 460 |
)
|
| 461 |
|
| 462 |
return demo
|
| 463 |
|
| 464 |
if __name__ == "__main__":
|
| 465 |
+
print("🎵 Iniciando Audio Separator Pro")
|
| 466 |
+
print(f"🔧 PyTorch: {torch.__version__}")
|
| 467 |
+
print(f"🔧 CUDA disponible: {torch.cuda.is_available()}")
|
|
|
|
| 468 |
|
| 469 |
demo = create_interface()
|
| 470 |
+
demo.launch(
|
| 471 |
+
server_name="0.0.0.0",
|
| 472 |
+
server_port=7860,
|
| 473 |
+
share=True
|
| 474 |
+
)
|