Update app.py
Browse files
app.py
CHANGED
|
@@ -1,382 +1,611 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import logging
|
|
|
|
| 4 |
import traceback
|
| 5 |
import tempfile
|
| 6 |
-
import time
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
from audio_separator.separator import Separator
|
| 11 |
-
|
| 12 |
-
# Configuración
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
-
title = "<center><strong><font size='7'>🎵 AI
|
| 17 |
description = """
|
| 18 |
-
### 🤖 Separador
|
| 19 |
-
**
|
| 20 |
-
- 🎤 **Voces
|
| 21 |
-
- 🥁 **
|
| 22 |
-
- 🎸 **
|
| 23 |
-
-
|
| 24 |
-
-
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
# Configuración
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
},
|
| 35 |
-
"demucs_4stems": {
|
| 36 |
-
"model_name": "htdemucs_ft.yaml",
|
| 37 |
-
"description": "🎯 Separación completa (Demucs 4-stems)",
|
| 38 |
-
"stems": ["vocals", "drums", "bass", "other"]
|
| 39 |
-
},
|
| 40 |
-
"instrumental_ht": {
|
| 41 |
-
"model_name": "UVR-MDX-NET-Inst_HQ_4.onnx",
|
| 42 |
-
"description": "🎵 Instrumental de alta calidad",
|
| 43 |
-
"stems": ["Other", "Instrumental"]
|
| 44 |
-
},
|
| 45 |
-
|
| 46 |
-
# Modelos especializados
|
| 47 |
-
"piano": {
|
| 48 |
-
"model_name": "Kim_Piano_1.onnx",
|
| 49 |
-
"description": "🎹 Piano especializado",
|
| 50 |
-
"stems": ["Piano", "No Piano"]
|
| 51 |
-
},
|
| 52 |
-
"drums": {
|
| 53 |
-
"model_name": "UVR-MDX-NET-Kag_2.onnx",
|
| 54 |
-
"description": "🥁 Batería especializada",
|
| 55 |
-
"stems": ["Drums", "No Drums"]
|
| 56 |
-
},
|
| 57 |
-
"bass": {
|
| 58 |
-
"model_name": "Kim_Bass_1.onnx",
|
| 59 |
-
"description": "🎸 Bajo especializado",
|
| 60 |
-
"stems": ["Bass", "No Bass"]
|
| 61 |
-
},
|
| 62 |
-
"guitar": {
|
| 63 |
-
"model_name": "UVR-MDX-NET-Kag_3.onnx",
|
| 64 |
-
"description": "🎸 Guitarra especializada",
|
| 65 |
-
"stems": ["Guitar", "No Guitar"]
|
| 66 |
-
},
|
| 67 |
-
"karaoke": {
|
| 68 |
-
"model_name": "UVR_MDXNET_KARA_2.onnx",
|
| 69 |
-
"description": "🎤 Karaoke/Voces principales",
|
| 70 |
-
"stems": ["Main Vocals", "Backup Vocals"]
|
| 71 |
-
},
|
| 72 |
-
"dereverb": {
|
| 73 |
-
"model_name": "Reverb_HQ_By_FoxJoy.onnx",
|
| 74 |
-
"description": "🔄 Eliminar reverb",
|
| 75 |
-
"stems": ["Dry", "Reverb"]
|
| 76 |
-
}
|
| 77 |
}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
else:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
valid_files = [f for f in output_files if os.path.exists(f) and os.path.getsize(f) > 1024]
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
logger.info("🔄 Intentando con modelo básico de voces...")
|
| 163 |
-
separator.load_model(model_filename="UVR-MDX-NET-Voc_FT.onnx")
|
| 164 |
-
result = separator.separate(audio_file)
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
try:
|
| 186 |
-
all_outputs = []
|
| 187 |
-
total_models = len(models_list)
|
| 188 |
-
|
| 189 |
-
for i, model_key in enumerate(models_list):
|
| 190 |
-
if progress_callback:
|
| 191 |
-
progress = 0.1 + (i / total_models) * 0.8
|
| 192 |
-
model_name = AVAILABLE_MODELS[model_key]["description"]
|
| 193 |
-
progress_callback(progress, f"Modelo {i+1}/{total_models}: {model_name}")
|
| 194 |
-
|
| 195 |
-
try:
|
| 196 |
-
outputs = self.separate_audio(audio_file, model_key)
|
| 197 |
-
all_outputs.extend(outputs)
|
| 198 |
-
logger.info(f"✅ Completado: {AVAILABLE_MODELS[model_key]['description']}")
|
| 199 |
-
except Exception as e:
|
| 200 |
-
logger.warning(f"⚠️ Error con {model_key}: {e}")
|
| 201 |
-
continue
|
| 202 |
-
|
| 203 |
-
if not all_outputs:
|
| 204 |
-
raise Exception("❌ Ningún modelo produjo resultados válidos")
|
| 205 |
-
|
| 206 |
-
# Eliminar duplicados
|
| 207 |
-
unique_outputs = list(set(all_outputs))
|
| 208 |
-
return unique_outputs
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
raise
|
| 213 |
|
| 214 |
-
def
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
|
| 245 |
-
|
| 246 |
-
# Combinación profesional: voces + karaoke + dereverb
|
| 247 |
-
models = ["vocals_ht", "karaoke", "dereverb"]
|
| 248 |
-
result_files = separator.separate_multi_model(audio_file, models, progress_callback)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
else:
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
|
|
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
gr.Markdown(title)
|
| 269 |
-
gr.Markdown(description)
|
| 270 |
-
|
| 271 |
-
with gr.Row():
|
| 272 |
-
with gr.Column():
|
| 273 |
-
audio_input = gr.Audio(
|
| 274 |
-
label="🎵 Subir archivo de audio (máx 100MB)",
|
| 275 |
-
type="filepath"
|
| 276 |
-
)
|
| 277 |
-
|
| 278 |
-
separation_mode = gr.Radio(
|
| 279 |
-
choices=[
|
| 280 |
-
("🎤 Voces Ultra HD (MDX-Net)", "vocals_ultra"),
|
| 281 |
-
("🎯 4 Stems Completo (Demucs AI)", "demucs_4stems"),
|
| 282 |
-
("🚀 Multi-Instrumento (4 modelos)", "multi_instrument"),
|
| 283 |
-
("🎹 Solo Piano (Kim Model)", "piano_only"),
|
| 284 |
-
("🥁 Solo Batería (MDX-Net)", "drums_only"),
|
| 285 |
-
("🎸 Solo Bajo (Kim Model)", "bass_only"),
|
| 286 |
-
("🎸 Solo Guitarra (MDX-Net)", "guitar_only"),
|
| 287 |
-
("🎤 Karaoke/Voces Principales", "karaoke_only"),
|
| 288 |
-
("🔄 Eliminar Reverb", "dereverb_only"),
|
| 289 |
-
("👑 Profesional (Multi-modelo)", "professional")
|
| 290 |
-
],
|
| 291 |
-
value="demucs_4stems",
|
| 292 |
-
label="🤖 Modelo de IA",
|
| 293 |
-
info="Cada modelo usa redes neuronales especializadas"
|
| 294 |
-
)
|
| 295 |
-
|
| 296 |
-
process_btn = gr.Button(
|
| 297 |
-
"🚀 Separar con IA",
|
| 298 |
-
variant="primary",
|
| 299 |
-
size="lg"
|
| 300 |
-
)
|
| 301 |
-
|
| 302 |
-
with gr.Column():
|
| 303 |
-
status_output = gr.Textbox(
|
| 304 |
-
label="🤖 Estado de la IA",
|
| 305 |
-
lines=8,
|
| 306 |
-
interactive=False
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
output_files = gr.File(
|
| 310 |
-
label="📥 Archivos separados por IA",
|
| 311 |
-
file_count="multiple",
|
| 312 |
-
interactive=False
|
| 313 |
-
)
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
)
|
| 321 |
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
""
|
| 358 |
-
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
|
| 361 |
-
def
|
| 362 |
-
"""
|
| 363 |
try:
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
except Exception as e:
|
| 378 |
-
logger.error(f"❌ Error: {e}")
|
| 379 |
-
|
| 380 |
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
# Instalar ONNX Runtime exactamente como r3gm
|
| 3 |
+
os.system("pip install ort-nightly-gpu --index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ort-cuda-12-nightly/pypi/simple/")
|
| 4 |
+
|
| 5 |
+
import gc
|
| 6 |
+
import hashlib
|
| 7 |
+
import queue
|
| 8 |
+
import threading
|
| 9 |
+
import json
|
| 10 |
import sys
|
| 11 |
+
import subprocess
|
| 12 |
+
import librosa
|
| 13 |
+
import numpy as np
|
| 14 |
+
import soundfile as sf
|
| 15 |
+
import torch
|
| 16 |
+
from tqdm import tqdm
|
| 17 |
+
import random
|
| 18 |
+
import onnxruntime as ort
|
| 19 |
+
import warnings
|
| 20 |
+
import gradio as gr
|
| 21 |
import logging
|
| 22 |
+
import time
|
| 23 |
import traceback
|
| 24 |
import tempfile
|
|
|
|
| 25 |
from pathlib import Path
|
| 26 |
+
from urllib.parse import urlparse
|
| 27 |
|
| 28 |
+
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
|
|
| 29 |
logging.basicConfig(level=logging.INFO)
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
| 32 |
+
title = "<center><strong><font size='7'>🎵 Multi-Instrument AI Separator</font></strong></center>"
|
| 33 |
description = """
|
| 34 |
+
### 🤖 Separador profesional con IA - Tecnología probada de r3gm
|
| 35 |
+
**Separación multi-instrumento usando modelos MDX-Net especializados**
|
| 36 |
+
- 🎤 **Voces** - Ultra alta calidad con múltiples modelos
|
| 37 |
+
- 🥁 **Batería** - Separación percusiva especializada
|
| 38 |
+
- 🎸 **Bajo** - Frecuencias graves optimizadas
|
| 39 |
+
- 🎹 **Piano** - Detección de teclas avanzada
|
| 40 |
+
- 🎸 **Guitarra** - Componentes armónicos
|
| 41 |
+
- 🎛️ **Otros** - Sintetizadores y instrumentos restantes
|
| 42 |
"""
|
| 43 |
|
| 44 |
+
# Configuración basada en r3gm
|
| 45 |
+
stem_naming = {
|
| 46 |
+
"Vocals": "Instrumental",
|
| 47 |
+
"Other": "Instruments",
|
| 48 |
+
"Instrumental": "Vocals",
|
| 49 |
+
"Drums": "Drumless",
|
| 50 |
+
"Bass": "Bassless",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# URLs exactas de r3gm
|
| 54 |
+
MDX_DOWNLOAD_LINK = "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/"
|
| 55 |
+
UVR_MODELS = [
|
| 56 |
+
"UVR-MDX-NET-Voc_FT.onnx", # Voces principales
|
| 57 |
+
"UVR_MDXNET_KARA_2.onnx", # Karaoke/Voces principales vs coros
|
| 58 |
+
"Reverb_HQ_By_FoxJoy.onnx", # Eliminar reverb
|
| 59 |
+
"UVR-MDX-NET-Inst_HQ_4.onnx", # Instrumental de alta calidad
|
| 60 |
+
"Kim_Piano_1.onnx", # Piano especializado
|
| 61 |
+
"Kim_Bass_1.onnx", # Bajo especializado
|
| 62 |
+
"UVR-MDX-NET-Kag_2.onnx", # Batería
|
| 63 |
+
"UVR-MDX-NET-Kag_3.onnx", # Guitarra
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# Directorios
|
| 67 |
+
BASE_DIR = "."
|
| 68 |
+
mdxnet_models_dir = os.path.join(BASE_DIR, "mdx_models")
|
| 69 |
+
output_dir = os.path.join(BASE_DIR, "separated_audio")
|
| 70 |
+
|
| 71 |
+
# Funciones de utilidad (copiadas de r3gm utils.py)
|
| 72 |
+
def load_file_from_url(url: str, model_dir: str, file_name: str = None, overwrite: bool = False, progress: bool = True) -> str:
|
| 73 |
+
"""Descargar archivo desde URL - Exacto de r3gm utils.py"""
|
| 74 |
+
os.makedirs(model_dir, exist_ok=True)
|
| 75 |
+
if not file_name:
|
| 76 |
+
parts = urlparse(url)
|
| 77 |
+
file_name = os.path.basename(parts.path)
|
| 78 |
+
cached_file = os.path.abspath(os.path.join(model_dir, file_name))
|
| 79 |
+
|
| 80 |
+
if os.path.exists(cached_file):
|
| 81 |
+
if overwrite or os.path.getsize(cached_file) == 0:
|
| 82 |
+
if os.path.exists(cached_file):
|
| 83 |
+
os.remove(cached_file)
|
| 84 |
+
|
| 85 |
+
if not os.path.exists(cached_file):
|
| 86 |
+
logger.info(f'Descargando: "{url}" to {cached_file}')
|
| 87 |
+
from torch.hub import download_url_to_file
|
| 88 |
+
download_url_to_file(url, cached_file, progress=progress)
|
| 89 |
+
else:
|
| 90 |
+
logger.debug(cached_file)
|
| 91 |
+
|
| 92 |
+
return cached_file
|
| 93 |
+
|
| 94 |
+
def download_manager(url: str, path: str, extension: str = "", overwrite: bool = False, progress: bool = True):
|
| 95 |
+
"""Gestor de descarga - Exacto de r3gm"""
|
| 96 |
+
url = url.strip()
|
| 97 |
|
| 98 |
+
parts = urlparse(url)
|
| 99 |
+
file_name = os.path.basename(parts.path)
|
| 100 |
+
model_name, ext = os.path.splitext(file_name)
|
| 101 |
+
name = model_name + (ext if not extension else f".{extension}")
|
| 102 |
+
|
| 103 |
+
if url.startswith("http"):
|
| 104 |
+
filename = load_file_from_url(
|
| 105 |
+
url=url,
|
| 106 |
+
model_dir=path,
|
| 107 |
+
file_name=name,
|
| 108 |
+
overwrite=overwrite,
|
| 109 |
+
progress=progress,
|
| 110 |
+
)
|
| 111 |
+
else:
|
| 112 |
+
filename = path
|
| 113 |
+
|
| 114 |
+
return filename
|
| 115 |
+
|
| 116 |
+
def create_directories():
|
| 117 |
+
"""Crear directorios necesarios"""
|
| 118 |
+
os.makedirs(mdxnet_models_dir, exist_ok=True)
|
| 119 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 120 |
+
|
| 121 |
+
def get_hash(model_path):
|
| 122 |
+
"""Calcular hash MD5 del modelo - Exacto de r3gm"""
|
| 123 |
+
try:
|
| 124 |
+
with open(model_path, "rb") as f:
|
| 125 |
+
f.seek(-10000 * 1024, 2)
|
| 126 |
+
model_hash = hashlib.md5(f.read()).hexdigest()
|
| 127 |
+
except:
|
| 128 |
+
model_hash = hashlib.md5(open(model_path, "rb").read()).hexdigest()
|
| 129 |
+
return model_hash
|
| 130 |
+
|
| 131 |
+
def create_data_json():
|
| 132 |
+
"""Crear data.json con configuraciones por hash - Basado en r3gm"""
|
| 133 |
+
data_json_path = os.path.join(mdxnet_models_dir, "data.json")
|
| 134 |
+
|
| 135 |
+
# Data.json con configuraciones exactas de r3gm (muestras principales)
|
| 136 |
+
model_data = {
|
| 137 |
+
# UVR-MDX-NET-Voc_FT.onnx (hash típico)
|
| 138 |
+
"0ddfc0eb5792638ad5dc27850236c246": {
|
| 139 |
+
"compensate": 1.035,
|
| 140 |
+
"mdx_dim_f_set": 2048,
|
| 141 |
+
"mdx_dim_t_set": 8,
|
| 142 |
+
"mdx_n_fft_scale_set": 6144,
|
| 143 |
+
"primary_stem": "Vocals"
|
| 144 |
+
},
|
| 145 |
+
# UVR_MDXNET_KARA_2.onnx (hash típico)
|
| 146 |
+
"2f5501189a2f6db6349916fabe8c90de": {
|
| 147 |
+
"compensate": 1.035,
|
| 148 |
+
"mdx_dim_f_set": 2048,
|
| 149 |
+
"mdx_dim_t_set": 8,
|
| 150 |
+
"mdx_n_fft_scale_set": 6144,
|
| 151 |
+
"primary_stem": "Vocals"
|
| 152 |
+
},
|
| 153 |
+
# Reverb_HQ_By_FoxJoy.onnx
|
| 154 |
+
"d7bff498db9324db933d913388cba6be": {
|
| 155 |
+
"compensate": 1.035,
|
| 156 |
+
"mdx_dim_f_set": 2048,
|
| 157 |
+
"mdx_dim_t_set": 8,
|
| 158 |
+
"mdx_n_fft_scale_set": 6144,
|
| 159 |
+
"primary_stem": "Vocals"
|
| 160 |
+
},
|
| 161 |
+
# UVR-MDX-NET-Inst_HQ_4.onnx
|
| 162 |
+
"26d308f91f3423a67dc69a6d12a8793d": {
|
| 163 |
+
"compensate": 1.035,
|
| 164 |
+
"mdx_dim_f_set": 2048,
|
| 165 |
+
"mdx_dim_t_set": 9,
|
| 166 |
+
"mdx_n_fft_scale_set": 8192,
|
| 167 |
+
"primary_stem": "Other"
|
| 168 |
+
},
|
| 169 |
+
# Kim_Piano_1.onnx (configuración estimada)
|
| 170 |
+
"piano_hash_placeholder": {
|
| 171 |
+
"compensate": 1.040,
|
| 172 |
+
"mdx_dim_f_set": 3072,
|
| 173 |
+
"mdx_dim_t_set": 8,
|
| 174 |
+
"mdx_n_fft_scale_set": 7680,
|
| 175 |
+
"primary_stem": "Piano"
|
| 176 |
+
},
|
| 177 |
+
# Kim_Bass_1.onnx
|
| 178 |
+
"6703e39f36f18aa7855ee1047765621d": {
|
| 179 |
+
"compensate": 1.035,
|
| 180 |
+
"mdx_dim_f_set": 2048,
|
| 181 |
+
"mdx_dim_t_set": 9,
|
| 182 |
+
"mdx_n_fft_scale_set": 16384,
|
| 183 |
+
"primary_stem": "Bass"
|
| 184 |
+
},
|
| 185 |
+
# UVR-MDX-NET-Kag_2.onnx (Drums)
|
| 186 |
+
"4910e7827f335048bdac11fa967772f9": {
|
| 187 |
+
"compensate": 1.035,
|
| 188 |
+
"mdx_dim_f_set": 2048,
|
| 189 |
+
"mdx_dim_t_set": 7,
|
| 190 |
+
"mdx_n_fft_scale_set": 4096,
|
| 191 |
+
"primary_stem": "Drums"
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
# Actualizar con hashes reales de modelos descargados
|
| 196 |
+
for model in UVR_MODELS:
|
| 197 |
+
model_path = os.path.join(mdxnet_models_dir, model)
|
| 198 |
+
if os.path.exists(model_path):
|
| 199 |
+
model_hash = get_hash(model_path)
|
| 200 |
|
| 201 |
+
# Configuraciones específicas por modelo
|
| 202 |
+
if "Voc_FT" in model:
|
| 203 |
+
config = {
|
| 204 |
+
"compensate": 1.035,
|
| 205 |
+
"mdx_dim_f_set": 2048,
|
| 206 |
+
"mdx_dim_t_set": 8,
|
| 207 |
+
"mdx_n_fft_scale_set": 6144,
|
| 208 |
+
"primary_stem": "Vocals"
|
| 209 |
+
}
|
| 210 |
+
elif "KARA" in model:
|
| 211 |
+
config = {
|
| 212 |
+
"compensate": 1.035,
|
| 213 |
+
"mdx_dim_f_set": 2048,
|
| 214 |
+
"mdx_dim_t_set": 8,
|
| 215 |
+
"mdx_n_fft_scale_set": 6144,
|
| 216 |
+
"primary_stem": "Vocals"
|
| 217 |
+
}
|
| 218 |
+
elif "Reverb" in model:
|
| 219 |
+
config = {
|
| 220 |
+
"compensate": 1.035,
|
| 221 |
+
"mdx_dim_f_set": 2048,
|
| 222 |
+
"mdx_dim_t_set": 8,
|
| 223 |
+
"mdx_n_fft_scale_set": 6144,
|
| 224 |
+
"primary_stem": "Vocals"
|
| 225 |
+
}
|
| 226 |
+
elif "Inst_HQ" in model:
|
| 227 |
+
config = {
|
| 228 |
+
"compensate": 1.035,
|
| 229 |
+
"mdx_dim_f_set": 2048,
|
| 230 |
+
"mdx_dim_t_set": 9,
|
| 231 |
+
"mdx_n_fft_scale_set": 8192,
|
| 232 |
+
"primary_stem": "Other"
|
| 233 |
+
}
|
| 234 |
+
elif "Piano" in model:
|
| 235 |
+
config = {
|
| 236 |
+
"compensate": 1.040,
|
| 237 |
+
"mdx_dim_f_set": 3072,
|
| 238 |
+
"mdx_dim_t_set": 8,
|
| 239 |
+
"mdx_n_fft_scale_set": 7680,
|
| 240 |
+
"primary_stem": "Piano"
|
| 241 |
+
}
|
| 242 |
+
elif "Bass" in model:
|
| 243 |
+
config = {
|
| 244 |
+
"compensate": 1.035,
|
| 245 |
+
"mdx_dim_f_set": 2048,
|
| 246 |
+
"mdx_dim_t_set": 9,
|
| 247 |
+
"mdx_n_fft_scale_set": 16384,
|
| 248 |
+
"primary_stem": "Bass"
|
| 249 |
+
}
|
| 250 |
+
elif "Kag_2" in model: # Drums
|
| 251 |
+
config = {
|
| 252 |
+
"compensate": 1.035,
|
| 253 |
+
"mdx_dim_f_set": 2048,
|
| 254 |
+
"mdx_dim_t_set": 7,
|
| 255 |
+
"mdx_n_fft_scale_set": 4096,
|
| 256 |
+
"primary_stem": "Drums"
|
| 257 |
+
}
|
| 258 |
+
elif "Kag_3" in model: # Guitar
|
| 259 |
+
config = {
|
| 260 |
+
"compensate": 1.040,
|
| 261 |
+
"mdx_dim_f_set": 3072,
|
| 262 |
+
"mdx_dim_t_set": 8,
|
| 263 |
+
"mdx_n_fft_scale_set": 7680,
|
| 264 |
+
"primary_stem": "Guitar"
|
| 265 |
+
}
|
| 266 |
+
else:
|
| 267 |
+
continue
|
| 268 |
|
| 269 |
+
model_data[model_hash] = config
|
| 270 |
+
|
| 271 |
+
with open(data_json_path, 'w') as f:
|
| 272 |
+
json.dump(model_data, f, indent=2)
|
| 273 |
+
|
| 274 |
+
logger.info(f"✅ data.json creado con {len(model_data)} configuraciones")
|
| 275 |
+
|
| 276 |
+
# Clases MDX exactas de r3gm (copiadas del app.py original)
|
| 277 |
+
class MDXModel:
|
| 278 |
+
def __init__(self, device, dim_f, dim_t, n_fft, hop=1024, stem_name=None, compensation=1.000):
|
| 279 |
+
self.dim_f = dim_f
|
| 280 |
+
self.dim_t = dim_t
|
| 281 |
+
self.dim_c = 4
|
| 282 |
+
self.n_fft = n_fft
|
| 283 |
+
self.hop = hop
|
| 284 |
+
self.stem_name = stem_name
|
| 285 |
+
self.compensation = compensation
|
| 286 |
+
|
| 287 |
+
self.n_bins = self.n_fft // 2 + 1
|
| 288 |
+
self.chunk_size = hop * (self.dim_t - 1)
|
| 289 |
+
self.window = torch.hann_window(window_length=self.n_fft, periodic=True).to(device)
|
| 290 |
+
|
| 291 |
+
out_c = self.dim_c
|
| 292 |
+
self.freq_pad = torch.zeros([1, out_c, self.n_bins - self.dim_f, self.dim_t]).to(device)
|
| 293 |
+
|
| 294 |
+
def stft(self, x):
|
| 295 |
+
x = x.reshape([-1, self.chunk_size])
|
| 296 |
+
x = torch.stft(x, n_fft=self.n_fft, hop_length=self.hop, window=self.window, center=True, return_complex=True)
|
| 297 |
+
x = torch.view_as_real(x)
|
| 298 |
+
x = x.permute([0, 3, 1, 2])
|
| 299 |
+
x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape([-1, 4, self.n_bins, self.dim_t])
|
| 300 |
+
return x[:, :, : self.dim_f]
|
| 301 |
+
|
| 302 |
+
def istft(self, x, freq_pad=None):
|
| 303 |
+
freq_pad = self.freq_pad.repeat([x.shape[0], 1, 1, 1]) if freq_pad is None else freq_pad
|
| 304 |
+
x = torch.cat([x, freq_pad], -2)
|
| 305 |
+
x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape([-1, 2, self.n_bins, self.dim_t])
|
| 306 |
+
x = x.permute([0, 2, 3, 1])
|
| 307 |
+
x = x.contiguous()
|
| 308 |
+
x = torch.view_as_complex(x)
|
| 309 |
+
x = torch.istft(x, n_fft=self.n_fft, hop_length=self.hop, window=self.window, center=True)
|
| 310 |
+
return x.reshape([-1, 2, self.chunk_size])
|
| 311 |
+
|
| 312 |
+
class MDX:
|
| 313 |
+
DEFAULT_SR = 44100
|
| 314 |
+
DEFAULT_CHUNK_SIZE = 0 * DEFAULT_SR
|
| 315 |
+
DEFAULT_MARGIN_SIZE = 1 * DEFAULT_SR
|
| 316 |
+
|
| 317 |
+
def __init__(self, model_path: str, params: MDXModel, processor=0):
|
| 318 |
+
self.device = torch.device(f"cuda:{processor}") if processor >= 0 and torch.cuda.is_available() else torch.device("cpu")
|
| 319 |
+
self.provider = ["CUDAExecutionProvider"] if processor >= 0 and torch.cuda.is_available() else ["CPUExecutionProvider"]
|
| 320 |
+
|
| 321 |
+
self.model = params
|
| 322 |
+
|
| 323 |
+
try:
|
| 324 |
+
self.ort = ort.InferenceSession(model_path, providers=self.provider)
|
| 325 |
+
dummy_input = torch.rand(1, 4, params.dim_f, params.dim_t).numpy()
|
| 326 |
+
self.ort.run(None, {"input": dummy_input})
|
| 327 |
+
self.process = lambda spec: self.ort.run(None, {"input": spec.cpu().numpy()})[0]
|
| 328 |
+
logger.info(f"✅ Modelo ONNX cargado: {os.path.basename(model_path)}")
|
| 329 |
+
except Exception as e:
|
| 330 |
+
logger.error(f"❌ Error cargando modelo ONNX: {e}")
|
| 331 |
+
raise
|
| 332 |
+
|
| 333 |
+
self.prog = None
|
| 334 |
+
|
| 335 |
+
@staticmethod
|
| 336 |
+
def segment(wave, combine=True, chunk_size=DEFAULT_CHUNK_SIZE, margin_size=DEFAULT_MARGIN_SIZE):
|
| 337 |
+
if combine:
|
| 338 |
+
processed_wave = None
|
| 339 |
+
for segment_count, segment in enumerate(wave):
|
| 340 |
+
start = 0 if segment_count == 0 else margin_size
|
| 341 |
+
end = None if segment_count == len(wave) - 1 else -margin_size
|
| 342 |
+
if margin_size == 0:
|
| 343 |
+
end = None
|
| 344 |
+
if processed_wave is None:
|
| 345 |
+
processed_wave = segment[:, start:end]
|
| 346 |
else:
|
| 347 |
+
processed_wave = np.concatenate((processed_wave, segment[:, start:end]), axis=-1)
|
| 348 |
+
else:
|
| 349 |
+
processed_wave = []
|
| 350 |
+
sample_count = wave.shape[-1]
|
| 351 |
+
|
| 352 |
+
if chunk_size <= 0 or chunk_size > sample_count:
|
| 353 |
+
chunk_size = sample_count
|
|
|
|
| 354 |
|
| 355 |
+
if margin_size > chunk_size:
|
| 356 |
+
margin_size = chunk_size
|
| 357 |
|
| 358 |
+
for segment_count, skip in enumerate(range(0, sample_count, chunk_size)):
|
| 359 |
+
margin = 0 if segment_count == 0 else margin_size
|
| 360 |
+
end = min(skip + chunk_size + margin_size, sample_count)
|
| 361 |
+
start = skip - margin
|
| 362 |
|
| 363 |
+
cut = wave[:, start:end].copy()
|
| 364 |
+
processed_wave.append(cut)
|
| 365 |
|
| 366 |
+
if end == sample_count:
|
| 367 |
+
break
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
+
return processed_wave
|
| 370 |
+
|
| 371 |
+
def pad_wave(self, wave):
|
| 372 |
+
n_sample = wave.shape[1]
|
| 373 |
+
trim = self.model.n_fft // 2
|
| 374 |
+
gen_size = self.model.chunk_size - 2 * trim
|
| 375 |
+
pad = gen_size - n_sample % gen_size
|
| 376 |
+
|
| 377 |
+
wave_p = np.concatenate((
|
| 378 |
+
np.zeros((2, trim)),
|
| 379 |
+
wave,
|
| 380 |
+
np.zeros((2, pad)),
|
| 381 |
+
np.zeros((2, trim)),
|
| 382 |
+
), 1)
|
| 383 |
+
|
| 384 |
+
mix_waves = []
|
| 385 |
+
for i in range(0, n_sample + pad, gen_size):
|
| 386 |
+
waves = np.array(wave_p[:, i:i + self.model.chunk_size])
|
| 387 |
+
mix_waves.append(waves)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
+
mix_waves = torch.tensor(mix_waves, dtype=torch.float32).to(self.device)
|
| 390 |
+
return mix_waves, pad, trim
|
|
|
|
| 391 |
|
| 392 |
+
def _process_wave(self, mix_waves, trim, pad, q: queue.Queue, _id: int):
|
| 393 |
+
mix_waves = mix_waves.split(1)
|
| 394 |
+
with torch.no_grad():
|
| 395 |
+
pw = []
|
| 396 |
+
for mix_wave in mix_waves:
|
| 397 |
+
if self.prog:
|
| 398 |
+
self.prog.update()
|
| 399 |
+
spec = self.model.stft(mix_wave)
|
| 400 |
+
processed_spec = torch.tensor(self.process(spec))
|
| 401 |
+
processed_wav = self.model.istft(processed_spec.to(self.device))
|
| 402 |
+
processed_wav = processed_wav[:, :, trim:-trim].transpose(0, 1).reshape(2, -1).cpu().numpy()
|
| 403 |
+
pw.append(processed_wav)
|
| 404 |
|
| 405 |
+
processed_signal = np.concatenate(pw, axis=-1)[:, :-pad]
|
| 406 |
+
q.put({_id: processed_signal})
|
| 407 |
+
return processed_signal
|
| 408 |
+
|
| 409 |
+
def process_wave(self, wave: np.array, mt_threads=1):
|
| 410 |
+
self.prog = tqdm(total=0, desc="Procesando con IA")
|
| 411 |
+
chunk = wave.shape[-1] // mt_threads if mt_threads > 1 else wave.shape[-1]
|
| 412 |
+
waves = self.segment(wave, False, chunk) if mt_threads > 1 else [wave]
|
| 413 |
|
| 414 |
+
q = queue.Queue()
|
| 415 |
+
threads = []
|
| 416 |
|
| 417 |
+
for c, batch in enumerate(waves):
|
| 418 |
+
mix_waves, pad, trim = self.pad_wave(batch)
|
| 419 |
+
self.prog.total = len(mix_waves) * len(waves)
|
| 420 |
+
thread = threading.Thread(target=self._process_wave, args=(mix_waves, trim, pad, q, c))
|
| 421 |
+
thread.start()
|
| 422 |
+
threads.append(thread)
|
| 423 |
+
|
| 424 |
+
for thread in threads:
|
| 425 |
+
thread.join()
|
| 426 |
+
|
| 427 |
+
if self.prog:
|
| 428 |
+
self.prog.close()
|
| 429 |
+
|
| 430 |
+
processed_batches = []
|
| 431 |
+
while not q.empty():
|
| 432 |
+
processed_batches.append(q.get())
|
| 433 |
+
|
| 434 |
+
processed_batches = [list(wave.values())[0] for wave in sorted(processed_batches, key=lambda d: list(d.keys())[0])]
|
| 435 |
+
|
| 436 |
+
if len(processed_batches) != len(waves):
|
| 437 |
+
logger.warning("Procesamiento incompleto")
|
| 438 |
+
return processed_batches[0] if processed_batches else wave
|
| 439 |
+
|
| 440 |
+
return self.segment(processed_batches, True, chunk) if mt_threads > 1 else processed_batches[0]
|
| 441 |
+
|
| 442 |
+
def convert_to_stereo_and_wav(audio_path):
|
| 443 |
+
"""Convertir audio a estéreo WAV usando FFmpeg como r3gm"""
|
| 444 |
+
try:
|
| 445 |
+
wave, sr = librosa.load(audio_path, mono=False, sr=44100)
|
| 446 |
+
|
| 447 |
+
if len(wave.shape) == 1 or not audio_path.lower().endswith('.wav'):
|
| 448 |
+
stereo_path = os.path.join(output_dir, f"{Path(audio_path).stem}_stereo.wav")
|
| 449 |
|
| 450 |
+
# Usar FFmpeg como r3gm
|
| 451 |
+
command = [
|
| 452 |
+
'ffmpeg', '-y', '-loglevel', 'error',
|
| 453 |
+
'-i', audio_path,
|
| 454 |
+
'-ac', '2', '-f', 'wav', stereo_path
|
| 455 |
+
]
|
| 456 |
|
| 457 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
+
if result.returncode == 0 and os.path.exists(stereo_path):
|
| 460 |
+
return stereo_path
|
| 461 |
+
else:
|
| 462 |
+
# Fallback con soundfile
|
| 463 |
+
if len(wave.shape) == 1:
|
| 464 |
+
wave = np.stack([wave, wave])
|
| 465 |
+
sf.write(stereo_path, wave.T, 44100)
|
| 466 |
+
return stereo_path
|
| 467 |
else:
|
| 468 |
+
return audio_path
|
| 469 |
+
except Exception as e:
|
| 470 |
+
logger.error(f"Error convirtiendo audio: {e}")
|
| 471 |
+
return audio_path
|
| 472 |
+
|
| 473 |
+
def run_mdx_separation(model_path, filename, model_params, denoise=False):
|
| 474 |
+
"""Ejecutar separación MDX - Simplificado de r3gm"""
|
| 475 |
+
try:
|
| 476 |
+
device_base = "cuda" if torch.cuda.is_available() else "cpu"
|
| 477 |
|
| 478 |
+
if device_base == "cuda":
|
| 479 |
+
device = torch.device("cuda:0")
|
| 480 |
+
processor_num = 0
|
| 481 |
+
m_threads = 1
|
| 482 |
+
logger.info("🔧 Usando GPU")
|
| 483 |
+
else:
|
| 484 |
+
device = torch.device("cpu")
|
| 485 |
+
processor_num = -1
|
| 486 |
+
m_threads = 1
|
| 487 |
+
logger.info("🔧 Usando CPU")
|
| 488 |
|
| 489 |
+
# Obtener configuración por hash
|
| 490 |
+
model_hash = get_hash(model_path)
|
| 491 |
+
mp = model_params.get(model_hash)
|
| 492 |
|
| 493 |
+
if not mp:
|
| 494 |
+
logger.warning(f"Hash no encontrado: {model_hash}, usando configuración por defecto")
|
| 495 |
+
mp = {
|
| 496 |
+
"compensate": 1.035,
|
| 497 |
+
"mdx_dim_f_set": 2048,
|
| 498 |
+
"mdx_dim_t_set": 8,
|
| 499 |
+
"mdx_n_fft_scale_set": 6144,
|
| 500 |
+
"primary_stem": "Vocals"
|
| 501 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
|
| 503 |
+
# Crear modelo MDX
|
| 504 |
+
model = MDXModel(
|
| 505 |
+
device,
|
| 506 |
+
dim_f=mp["mdx_dim_f_set"],
|
| 507 |
+
dim_t=2 ** mp["mdx_dim_t_set"],
|
| 508 |
+
n_fft=mp["mdx_n_fft_scale_set"],
|
| 509 |
+
stem_name=mp["primary_stem"],
|
| 510 |
+
compensation=mp["compensate"],
|
| 511 |
)
|
| 512 |
|
| 513 |
+
# Crear sesión MDX
|
| 514 |
+
mdx_sess = MDX(model_path, model, processor=processor_num)
|
| 515 |
+
|
| 516 |
+
# Cargar y procesar audio
|
| 517 |
+
wave, sr = librosa.load(filename, mono=False, sr=44100)
|
| 518 |
+
|
| 519 |
+
# Normalizar
|
| 520 |
+
peak = max(np.max(wave), abs(np.min(wave)))
|
| 521 |
+
if peak > 0:
|
| 522 |
+
wave /= peak
|
| 523 |
+
|
| 524 |
+
# Procesar
|
| 525 |
+
if denoise:
|
| 526 |
+
wave_processed = -(mdx_sess.process_wave(-wave, m_threads)) + (mdx_sess.process_wave(wave, m_threads))
|
| 527 |
+
wave_processed *= 0.5
|
| 528 |
+
else:
|
| 529 |
+
wave_processed = mdx_sess.process_wave(wave, m_threads)
|
| 530 |
+
|
| 531 |
+
# Restaurar peak
|
| 532 |
+
wave_processed *= peak
|
| 533 |
+
|
| 534 |
+
# Crear archivos de salida
|
| 535 |
+
timestamp = int(time.time())
|
| 536 |
+
song_output_dir = os.path.join(output_dir, f"separated_{timestamp}")
|
| 537 |
+
os.makedirs(song_output_dir, exist_ok=True)
|
| 538 |
+
|
| 539 |
+
base_name = Path(filename).stem
|
| 540 |
+
stem_name = model.stem_name
|
| 541 |
+
|
| 542 |
+
# Guardar stem principal
|
| 543 |
+
main_filepath = os.path.join(song_output_dir, f"{base_name}_{stem_name}.wav")
|
| 544 |
+
sf.write(main_filepath, wave_processed.T, sr)
|
| 545 |
+
|
| 546 |
+
# Guardar stem invertido
|
| 547 |
+
invert_name = stem_naming.get(stem_name, "Other")
|
| 548 |
+
invert_filepath = os.path.join(song_output_dir, f"{base_name}_{invert_name}.wav")
|
| 549 |
+
inverted_audio = (-wave_processed.T * model.compensation) + wave.T
|
| 550 |
+
sf.write(invert_filepath, inverted_audio, sr)
|
| 551 |
+
|
| 552 |
+
# Limpieza
|
| 553 |
+
del mdx_sess, wave_processed, wave
|
| 554 |
+
gc.collect()
|
| 555 |
+
if torch.cuda.is_available():
|
| 556 |
+
torch.cuda.empty_cache()
|
| 557 |
+
|
| 558 |
+
logger.info(f"✅ Separación completada: {stem_name} + {invert_name}")
|
| 559 |
+
return [main_filepath, invert_filepath]
|
| 560 |
+
|
| 561 |
+
except Exception as e:
|
| 562 |
+
logger.error(f"❌ Error en separación MDX: {e}")
|
| 563 |
+
traceback.print_exc()
|
| 564 |
+
raise
|
| 565 |
|
| 566 |
+
def separate_multi_instrument(audio_file, models_to_use):
|
| 567 |
+
"""Separar usando múltiples modelos secuencialmente"""
|
| 568 |
try:
|
| 569 |
+
# Cargar configuraciones
|
| 570 |
+
data_json_path = os.path.join(mdxnet_models_dir, "data.json")
|
| 571 |
+
with open(data_json_path) as f:
|
| 572 |
+
model_params = json.load(f)
|
| 573 |
+
|
| 574 |
+
# Convertir audio
|
| 575 |
+
converted_file = convert_to_stereo_and_wav(audio_file)
|
| 576 |
+
|
| 577 |
+
all_outputs = []
|
| 578 |
+
|
| 579 |
+
for model_name in models_to_use:
|
| 580 |
+
model_path = os.path.join(mdxnet_models_dir, model_name)
|
| 581 |
+
|
| 582 |
+
if os.path.exists(model_path):
|
| 583 |
+
logger.info(f"🎵 Procesando con {model_name}")
|
| 584 |
+
try:
|
| 585 |
+
outputs = run_mdx_separation(model_path, converted_file, model_params, denoise=True)
|
| 586 |
+
all_outputs.extend(outputs)
|
| 587 |
+
except Exception as e:
|
| 588 |
+
logger.warning(f"⚠️ Error con {model_name}: {e}")
|
| 589 |
+
continue
|
| 590 |
+
else:
|
| 591 |
+
logger.warning(f"⚠️ Modelo no encontrado: {model_name}")
|
| 592 |
+
|
| 593 |
+
return all_outputs
|
| 594 |
|
| 595 |
except Exception as e:
|
| 596 |
+
logger.error(f"❌ Error en separación multi-instrumento: {e}")
|
| 597 |
+
raise
|
| 598 |
|
| 599 |
+
def setup_models():
|
| 600 |
+
"""Configurar modelos - Descarga automática como r3gm"""
|
| 601 |
+
try:
|
| 602 |
+
logger.info("📥 Configurando modelos...")
|
| 603 |
+
|
| 604 |
+
for model in UVR_MODELS:
|
| 605 |
+
model_url = MDX_DOWNLOAD_LINK + model
|
| 606 |
+
download_manager(model_url, mdxnet_models_dir)
|
| 607 |
+
|
| 608 |
+
# Crear data.json con configuraciones
|
| 609 |
+
create_data_json()
|
| 610 |
+
|
| 611 |
+
logger.info("✅
|