Update processing.py
Browse files- processing.py +72 -10
processing.py
CHANGED
|
@@ -53,7 +53,7 @@ def extract_model_name(full_model_string):
|
|
| 53 |
cleaned = cleaned[len(prefix):]
|
| 54 |
return cleaned.strip()
|
| 55 |
|
| 56 |
-
def run_command_and_process_files(model_type, config_path, start_check_point, INPUT_DIR, OUTPUT_DIR, extract_instrumental, use_tta, demud_phaseremix_inst, clean_model):
|
| 57 |
try:
|
| 58 |
# inference.py'nin tam yolunu kullan
|
| 59 |
cmd_parts = [
|
|
@@ -81,12 +81,30 @@ def run_command_and_process_files(model_type, config_path, start_check_point, IN
|
|
| 81 |
universal_newlines=True
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
for line in process.stdout:
|
| 85 |
print(line.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
for line in process.stderr:
|
| 87 |
print(line.strip())
|
| 88 |
|
| 89 |
process.wait()
|
|
|
|
|
|
|
| 90 |
|
| 91 |
filename_model = clean_model_name(clean_model)
|
| 92 |
|
|
@@ -148,10 +166,11 @@ def run_command_and_process_files(model_type, config_path, start_check_point, IN
|
|
| 148 |
print(f"An error occurred: {e}")
|
| 149 |
return (None,) * 14
|
| 150 |
|
|
|
|
| 151 |
clear_directory(INPUT_DIR)
|
| 152 |
|
| 153 |
-
def process_audio(input_audio_file, model, chunk_size, overlap, export_format, use_tta, demud_phaseremix_inst, extract_instrumental, clean_model, *args, **kwargs):
|
| 154 |
-
"""Processes audio using the specified model and returns separated stems."""
|
| 155 |
if input_audio_file is not None:
|
| 156 |
audio_path = input_audio_file.name
|
| 157 |
else:
|
|
@@ -169,6 +188,7 @@ def process_audio(input_audio_file, model, chunk_size, overlap, export_format, u
|
|
| 169 |
clean_model_name_full = extract_model_name(model)
|
| 170 |
print(f"Processing audio from: {audio_path} using model: {clean_model_name_full}")
|
| 171 |
|
|
|
|
| 172 |
model_type, config_path, start_check_point = get_model_config(clean_model_name_full, chunk_size, overlap)
|
| 173 |
|
| 174 |
outputs = run_command_and_process_files(
|
|
@@ -180,12 +200,14 @@ def process_audio(input_audio_file, model, chunk_size, overlap, export_format, u
|
|
| 180 |
extract_instrumental=extract_instrumental,
|
| 181 |
use_tta=use_tta,
|
| 182 |
demud_phaseremix_inst=demud_phaseremix_inst,
|
| 183 |
-
clean_model=clean_model_name_full
|
|
|
|
| 184 |
)
|
| 185 |
|
|
|
|
| 186 |
return outputs
|
| 187 |
|
| 188 |
-
def ensemble_audio_fn(files, method, weights):
|
| 189 |
try:
|
| 190 |
if len(files) < 2:
|
| 191 |
return None, "⚠️ Minimum 2 files required"
|
|
@@ -210,20 +232,33 @@ def ensemble_audio_fn(files, method, weights):
|
|
| 210 |
weights_list = [str(w) for w in map(float, weights.split(','))]
|
| 211 |
ensemble_args += ["--weights", *weights_list]
|
| 212 |
|
|
|
|
| 213 |
result = subprocess.run(
|
| 214 |
["python", "ensemble.py"] + ensemble_args,
|
| 215 |
capture_output=True,
|
| 216 |
text=True
|
| 217 |
)
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
log = f"✅ Success!\n{result.stdout}" if not result.stderr else f"❌ Error!\n{result.stderr}"
|
| 220 |
return output_path, log
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
return None, f"⛔ Critical Error: {str(e)}"
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap, export_format, use_tta, extract_instrumental, ensemble_type, _state, *args, **kwargs):
|
| 226 |
-
"""Processes audio with multiple models and performs ensemble."""
|
| 227 |
try:
|
| 228 |
if not selected_models or len(selected_models) < 1:
|
| 229 |
return None, "❌ No models selected"
|
|
@@ -236,18 +271,23 @@ def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap
|
|
| 236 |
else:
|
| 237 |
audio_path = input_audio_file.name
|
| 238 |
|
| 239 |
-
# AUTO_ENSEMBLE_TEMP'i de BASE_DIR üzerinden tanımla
|
| 240 |
auto_ensemble_temp = os.path.join(BASE_DIR, "auto_ensemble_temp")
|
| 241 |
os.makedirs(auto_ensemble_temp, exist_ok=True)
|
| 242 |
os.makedirs(AUTO_ENSEMBLE_OUTPUT, exist_ok=True)
|
| 243 |
clear_directory(auto_ensemble_temp)
|
| 244 |
|
| 245 |
all_outputs = []
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
clean_model = extract_model_name(model)
|
| 248 |
model_output_dir = os.path.join(auto_ensemble_temp, clean_model)
|
| 249 |
os.makedirs(model_output_dir, exist_ok=True)
|
| 250 |
|
|
|
|
| 251 |
model_type, config_path, start_check_point = get_model_config(clean_model, chunk_size, overlap)
|
| 252 |
|
| 253 |
cmd = [
|
|
@@ -273,11 +313,22 @@ def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap
|
|
| 273 |
except Exception as e:
|
| 274 |
return None, f"Critical error with {model}: {str(e)}"
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
model_outputs = glob.glob(os.path.join(model_output_dir, "*.wav"))
|
| 277 |
if not model_outputs:
|
| 278 |
raise FileNotFoundError(f"{model} failed to produce output")
|
| 279 |
all_outputs.extend(model_outputs)
|
| 280 |
|
|
|
|
| 281 |
def wait_for_files(files, timeout=300):
|
| 282 |
start = time.time()
|
| 283 |
while time.time() - start < timeout:
|
|
@@ -289,6 +340,7 @@ def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap
|
|
| 289 |
|
| 290 |
wait_for_files(all_outputs)
|
| 291 |
|
|
|
|
| 292 |
quoted_files = [f'"{f}"' for f in all_outputs]
|
| 293 |
timestamp = str(int(time.time()))
|
| 294 |
output_path = os.path.join(AUTO_ENSEMBLE_OUTPUT, f"ensemble_{timestamp}.wav")
|
|
@@ -308,12 +360,22 @@ def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap
|
|
| 308 |
check=True
|
| 309 |
)
|
| 310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
if not os.path.exists(output_path):
|
| 312 |
raise RuntimeError("Ensemble dosyası oluşturulamadı")
|
| 313 |
|
|
|
|
| 314 |
return output_path, "✅ Success!"
|
| 315 |
except Exception as e:
|
| 316 |
return None, f"❌ Error: {str(e)}"
|
| 317 |
finally:
|
| 318 |
shutil.rmtree(auto_ensemble_temp, ignore_errors=True)
|
| 319 |
-
gc.collect()
|
|
|
|
| 53 |
cleaned = cleaned[len(prefix):]
|
| 54 |
return cleaned.strip()
|
| 55 |
|
| 56 |
+
def run_command_and_process_files(model_type, config_path, start_check_point, INPUT_DIR, OUTPUT_DIR, extract_instrumental, use_tta, demud_phaseremix_inst, clean_model, progress=gr.Progress()):
|
| 57 |
try:
|
| 58 |
# inference.py'nin tam yolunu kullan
|
| 59 |
cmd_parts = [
|
|
|
|
| 81 |
universal_newlines=True
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Progress bar ile subprocess çıktısını izleme
|
| 85 |
+
progress(0, desc="Starting audio separation...", total=100)
|
| 86 |
+
progress_bar = tqdm(total=100, desc="Processing audio", unit="%", position=0, leave=False)
|
| 87 |
+
|
| 88 |
for line in process.stdout:
|
| 89 |
print(line.strip())
|
| 90 |
+
# İlerleme yüzdesini parse et (ondalık olarak)
|
| 91 |
+
if "Progress:" in line:
|
| 92 |
+
try:
|
| 93 |
+
percentage = float(re.search(r"Progress: (\d+\.\d+)%", line).group(1))
|
| 94 |
+
progress(percentage, desc=f"Separating audio... ({percentage:.1f}%)")
|
| 95 |
+
progress_bar.n = percentage # tqdm'i güncelle
|
| 96 |
+
progress_bar.refresh()
|
| 97 |
+
except (AttributeError, ValueError) as e:
|
| 98 |
+
print(f"Progress parsing error: {e}")
|
| 99 |
+
elif "Processing file" in line:
|
| 100 |
+
progress(0, desc=line.strip()) # Yeni dosya işleniyorsa sıfırla
|
| 101 |
+
|
| 102 |
for line in process.stderr:
|
| 103 |
print(line.strip())
|
| 104 |
|
| 105 |
process.wait()
|
| 106 |
+
progress_bar.close()
|
| 107 |
+
progress(100, desc="Separation complete!")
|
| 108 |
|
| 109 |
filename_model = clean_model_name(clean_model)
|
| 110 |
|
|
|
|
| 166 |
print(f"An error occurred: {e}")
|
| 167 |
return (None,) * 14
|
| 168 |
|
| 169 |
+
finally:
|
| 170 |
clear_directory(INPUT_DIR)
|
| 171 |
|
| 172 |
+
def process_audio(input_audio_file, model, chunk_size, overlap, export_format, use_tta, demud_phaseremix_inst, extract_instrumental, clean_model, progress=gr.Progress(track_tqdm=True), *args, **kwargs):
|
| 173 |
+
"""Processes audio using the specified model and returns separated stems with progress."""
|
| 174 |
if input_audio_file is not None:
|
| 175 |
audio_path = input_audio_file.name
|
| 176 |
else:
|
|
|
|
| 188 |
clean_model_name_full = extract_model_name(model)
|
| 189 |
print(f"Processing audio from: {audio_path} using model: {clean_model_name_full}")
|
| 190 |
|
| 191 |
+
progress(0, desc="Starting audio separation...", total=100)
|
| 192 |
model_type, config_path, start_check_point = get_model_config(clean_model_name_full, chunk_size, overlap)
|
| 193 |
|
| 194 |
outputs = run_command_and_process_files(
|
|
|
|
| 200 |
extract_instrumental=extract_instrumental,
|
| 201 |
use_tta=use_tta,
|
| 202 |
demud_phaseremix_inst=demud_phaseremix_inst,
|
| 203 |
+
clean_model=clean_model_name_full,
|
| 204 |
+
progress=progress
|
| 205 |
)
|
| 206 |
|
| 207 |
+
progress(100, desc="Audio processing completed!")
|
| 208 |
return outputs
|
| 209 |
|
| 210 |
+
def ensemble_audio_fn(files, method, weights, progress=gr.Progress()):
|
| 211 |
try:
|
| 212 |
if len(files) < 2:
|
| 213 |
return None, "⚠️ Minimum 2 files required"
|
|
|
|
| 232 |
weights_list = [str(w) for w in map(float, weights.split(','))]
|
| 233 |
ensemble_args += ["--weights", *weights_list]
|
| 234 |
|
| 235 |
+
progress(0, desc="Starting ensemble process...", total=100)
|
| 236 |
result = subprocess.run(
|
| 237 |
["python", "ensemble.py"] + ensemble_args,
|
| 238 |
capture_output=True,
|
| 239 |
text=True
|
| 240 |
)
|
| 241 |
|
| 242 |
+
# Ensemble için gerçek süreye dayalı ilerleme (0.1'lik adımlarla)
|
| 243 |
+
start_time = time.time()
|
| 244 |
+
total_estimated_time = 10.0 # Tahmini toplam süre (saniye, gerçek süreye göre ayarlanabilir)
|
| 245 |
+
for i in np.arange(0.1, 100.1, 0.1):
|
| 246 |
+
elapsed_time = time.time() - start_time
|
| 247 |
+
progress_value = min(i, (elapsed_time / total_estimated_time) * 100)
|
| 248 |
+
time.sleep(0.001) # Çok küçük bir gecikme, gerçek işlem süresiyle değiştirilebilir
|
| 249 |
+
progress(progress_value, desc=f"Ensembling... ({progress_value:.1f}%)")
|
| 250 |
+
|
| 251 |
+
progress(100, desc="Finalizing ensemble output...")
|
| 252 |
log = f"✅ Success!\n{result.stdout}" if not result.stderr else f"❌ Error!\n{result.stderr}"
|
| 253 |
return output_path, log
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
return None, f"⛔ Critical Error: {str(e)}"
|
| 257 |
+
finally:
|
| 258 |
+
progress(100, desc="Ensemble process completed!")
|
| 259 |
|
| 260 |
+
def auto_ensemble_process(input_audio_file, selected_models, chunk_size, overlap, export_format, use_tta, extract_instrumental, ensemble_type, _state, progress=gr.Progress(track_tqdm=True), *args, **kwargs):
|
| 261 |
+
"""Processes audio with multiple models and performs ensemble with progress."""
|
| 262 |
try:
|
| 263 |
if not selected_models or len(selected_models) < 1:
|
| 264 |
return None, "❌ No models selected"
|
|
|
|
| 271 |
else:
|
| 272 |
audio_path = input_audio_file.name
|
| 273 |
|
|
|
|
| 274 |
auto_ensemble_temp = os.path.join(BASE_DIR, "auto_ensemble_temp")
|
| 275 |
os.makedirs(auto_ensemble_temp, exist_ok=True)
|
| 276 |
os.makedirs(AUTO_ENSEMBLE_OUTPUT, exist_ok=True)
|
| 277 |
clear_directory(auto_ensemble_temp)
|
| 278 |
|
| 279 |
all_outputs = []
|
| 280 |
+
total_models = len(selected_models)
|
| 281 |
+
total_steps = int(total_models * 10 + 20) # Her model için 10 adım + final adımlar, ondalık için tam sayı
|
| 282 |
+
|
| 283 |
+
progress(0, desc="Starting ensemble process...", total=total_steps)
|
| 284 |
+
|
| 285 |
+
for i, model in enumerate(selected_models):
|
| 286 |
clean_model = extract_model_name(model)
|
| 287 |
model_output_dir = os.path.join(auto_ensemble_temp, clean_model)
|
| 288 |
os.makedirs(model_output_dir, exist_ok=True)
|
| 289 |
|
| 290 |
+
progress(i * 10, desc=f"Loading model {i+1}/{total_models}: {model}...")
|
| 291 |
model_type, config_path, start_check_point = get_model_config(clean_model, chunk_size, overlap)
|
| 292 |
|
| 293 |
cmd = [
|
|
|
|
| 313 |
except Exception as e:
|
| 314 |
return None, f"Critical error with {model}: {str(e)}"
|
| 315 |
|
| 316 |
+
# Her model için gerçek süreye dayalı ilerleme (0.1'lik adımlarla)
|
| 317 |
+
start_time = time.time()
|
| 318 |
+
total_estimated_time = 1.0 # Tahmini toplam süre (saniye, gerçek süreye göre ayarlanabilir)
|
| 319 |
+
for j in np.arange(0.1, 10.1, 0.1):
|
| 320 |
+
elapsed_time = time.time() - start_time
|
| 321 |
+
progress_value = (i * 10) + j
|
| 322 |
+
progress_value = min(progress_value, (i * 10) + (elapsed_time / total_estimated_time) * 10)
|
| 323 |
+
time.sleep(0.001) # Çok küçük bir gecikme, gerçek işlem süresiyle değiştirilebilir
|
| 324 |
+
progress(progress_value, desc=f"Separating with {model} ({progress_value:.1f}%)")
|
| 325 |
+
|
| 326 |
model_outputs = glob.glob(os.path.join(model_output_dir, "*.wav"))
|
| 327 |
if not model_outputs:
|
| 328 |
raise FileNotFoundError(f"{model} failed to produce output")
|
| 329 |
all_outputs.extend(model_outputs)
|
| 330 |
|
| 331 |
+
progress(total_models * 10 + 5, desc="Waiting for all files to be ready...")
|
| 332 |
def wait_for_files(files, timeout=300):
|
| 333 |
start = time.time()
|
| 334 |
while time.time() - start < timeout:
|
|
|
|
| 340 |
|
| 341 |
wait_for_files(all_outputs)
|
| 342 |
|
| 343 |
+
progress(total_models * 10 + 10, desc="Performing ensemble...")
|
| 344 |
quoted_files = [f'"{f}"' for f in all_outputs]
|
| 345 |
timestamp = str(int(time.time()))
|
| 346 |
output_path = os.path.join(AUTO_ENSEMBLE_OUTPUT, f"ensemble_{timestamp}.wav")
|
|
|
|
| 360 |
check=True
|
| 361 |
)
|
| 362 |
|
| 363 |
+
# Ensemble için gerçek süreye dayalı ilerleme (0.1'lik adımlarla)
|
| 364 |
+
start_time = time.time()
|
| 365 |
+
total_estimated_time = 10.0 # Tahmini toplam süre (saniye, gerçek süreye göre ayarlanabilir)
|
| 366 |
+
for i in np.arange(total_models * 10 + 11, total_steps - 0.1, 0.1):
|
| 367 |
+
elapsed_time = time.time() - start_time
|
| 368 |
+
progress_value = min(i, (elapsed_time / total_estimated_time) * (total_steps - (total_models * 10 + 10)) + (total_models * 10 + 10))
|
| 369 |
+
time.sleep(0.001) # Çok küçük bir gecikme, gerçek işlem süresiyle değiştirilebilir
|
| 370 |
+
progress(progress_value, desc=f"Ensembling... ({progress_value:.1f}%)")
|
| 371 |
+
|
| 372 |
if not os.path.exists(output_path):
|
| 373 |
raise RuntimeError("Ensemble dosyası oluşturulamadı")
|
| 374 |
|
| 375 |
+
progress(total_steps, desc="Ensemble completed successfully!")
|
| 376 |
return output_path, "✅ Success!"
|
| 377 |
except Exception as e:
|
| 378 |
return None, f"❌ Error: {str(e)}"
|
| 379 |
finally:
|
| 380 |
shutil.rmtree(auto_ensemble_temp, ignore_errors=True)
|
| 381 |
+
gc.collect()
|