Spaces:
Running
on
Zero
Running
on
Zero
Update processing.py
Browse files- processing.py +132 -101
processing.py
CHANGED
@@ -110,9 +110,9 @@ def run_command_and_process_files(
|
|
110 |
start_check_point,
|
111 |
INPUT_DIR,
|
112 |
OUTPUT_DIR,
|
113 |
-
extract_instrumental,
|
114 |
-
use_tta,
|
115 |
-
demud_phaseremix_inst,
|
116 |
progress=None,
|
117 |
use_apollo=True,
|
118 |
apollo_normal_model="Apollo Universal Model",
|
@@ -124,113 +124,137 @@ def run_command_and_process_files(
|
|
124 |
apollo_midside_model=None,
|
125 |
output_format="wav"
|
126 |
):
|
127 |
-
"""
|
128 |
-
Run inference.py with specified parameters and process output files.
|
129 |
-
"""
|
130 |
try:
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
141 |
|
142 |
-
|
143 |
-
try:
|
144 |
-
inference_chunk_size = int(inference_chunk_size)
|
145 |
-
inference_overlap = int(inference_overlap)
|
146 |
-
except (TypeError, ValueError) as e:
|
147 |
-
print(f"Invalid inference_chunk_size or inference_overlap: {e}. Defaulting to: inference_chunk_size=352800, inference_overlap=2")
|
148 |
-
inference_chunk_size = 352800
|
149 |
-
inference_overlap = 2
|
150 |
-
|
151 |
-
# Validate Apollo parameters
|
152 |
-
try:
|
153 |
-
apollo_chunk_size = int(apollo_chunk_size)
|
154 |
-
apollo_overlap = int(apollo_overlap)
|
155 |
-
except (TypeError, ValueError) as e:
|
156 |
-
print(f"Invalid apollo_chunk_size or apollo_overlap: {e}. Defaulting to: apollo_chunk_size=19, apollo_overlap=2")
|
157 |
-
apollo_chunk_size = 19
|
158 |
-
apollo_overlap = 2
|
159 |
|
|
|
160 |
cmd_parts = [
|
161 |
-
"python",
|
162 |
-
"--model_type", model_type,
|
163 |
-
"--config_path", config_path,
|
164 |
-
"--start_check_point", start_check_point,
|
165 |
-
"--input_folder", INPUT_DIR,
|
166 |
-
"--store_dir", OUTPUT_DIR,
|
167 |
"--chunk_size", str(inference_chunk_size),
|
168 |
"--overlap", str(inference_overlap),
|
169 |
"--export_format", f"{output_format} FLOAT"
|
170 |
]
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
180 |
cmd_parts,
|
181 |
-
cwd=
|
182 |
-
|
|
|
183 |
text=True,
|
184 |
-
|
|
|
185 |
)
|
186 |
|
187 |
-
#
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
filename_model = extract_model_name_from_checkpoint(start_check_point)
|
200 |
-
output_files = os.listdir(OUTPUT_DIR)
|
201 |
if not output_files:
|
202 |
raise FileNotFoundError("No output files created in OUTPUT_DIR")
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
227 |
if not output_files:
|
228 |
raise FileNotFoundError("No output files in OUTPUT_DIR after renaming")
|
229 |
|
|
|
230 |
def find_file(keyword):
|
231 |
matching_files = [
|
232 |
-
os.path.join(OUTPUT_DIR, f) for f in output_files
|
233 |
-
if keyword in f.lower()
|
234 |
]
|
235 |
return matching_files[0] if matching_files else None
|
236 |
|
@@ -241,8 +265,9 @@ def run_command_and_process_files(
|
|
241 |
find_file('female'), find_file('bleed'), find_file('karaoke')
|
242 |
]
|
243 |
|
|
|
244 |
normalized_outputs = []
|
245 |
-
for output_file in output_list:
|
246 |
if output_file and os.path.exists(output_file):
|
247 |
normalized_file = os.path.join(OUTPUT_DIR, f"{sanitize_filename(os.path.splitext(os.path.basename(output_file))[0])}.{output_format}")
|
248 |
if output_file.endswith(f".{output_format}") and output_file != normalized_file:
|
@@ -253,11 +278,17 @@ def run_command_and_process_files(
|
|
253 |
else:
|
254 |
normalized_file = output_file
|
255 |
normalized_outputs.append(normalized_file)
|
|
|
|
|
|
|
|
|
|
|
256 |
else:
|
257 |
-
normalized_outputs.append(
|
258 |
|
259 |
-
# Apollo
|
260 |
if use_apollo:
|
|
|
261 |
normalized_outputs = process_with_apollo(
|
262 |
output_files=normalized_outputs,
|
263 |
output_dir=OUTPUT_DIR,
|
@@ -267,23 +298,23 @@ def run_command_and_process_files(
|
|
267 |
apollo_normal_model=apollo_normal_model,
|
268 |
apollo_midside_model=apollo_midside_model,
|
269 |
output_format=output_format,
|
270 |
-
progress=progress,
|
271 |
-
total_progress_start=
|
272 |
total_progress_end=100
|
273 |
)
|
274 |
|
275 |
-
#
|
276 |
if progress is not None and callable(getattr(progress, '__call__', None)):
|
277 |
-
progress(
|
|
|
|
|
|
|
278 |
return tuple(normalized_outputs)
|
279 |
|
280 |
-
except subprocess.CalledProcessError as e:
|
281 |
-
print(f"Subprocess failed, code: {e.returncode}: {e.stderr}")
|
282 |
-
return (None,) * 14
|
283 |
except Exception as e:
|
284 |
-
|
285 |
import traceback
|
286 |
-
traceback.print_exc()
|
287 |
return (None,) * 14
|
288 |
|
289 |
def process_audio(
|
|
|
110 |
start_check_point,
|
111 |
INPUT_DIR,
|
112 |
OUTPUT_DIR,
|
113 |
+
extract_instrumental=False,
|
114 |
+
use_tta=False,
|
115 |
+
demud_phaseremix_inst=False,
|
116 |
progress=None,
|
117 |
use_apollo=True,
|
118 |
apollo_normal_model="Apollo Universal Model",
|
|
|
124 |
apollo_midside_model=None,
|
125 |
output_format="wav"
|
126 |
):
|
|
|
|
|
|
|
127 |
try:
|
128 |
+
logging.info(f"Starting run_command_and_process_files: model_type={model_type}, config_path={config_path}, inference_chunk_size={inference_chunk_size}, inference_overlap={inference_overlap}")
|
129 |
+
|
130 |
+
# Doğrulama
|
131 |
+
for path, name in [
|
132 |
+
(config_path, "Configuration file"),
|
133 |
+
(start_check_point, "Checkpoint file"),
|
134 |
+
(INPUT_DIR, "Input directory")
|
135 |
+
]:
|
136 |
+
if not path:
|
137 |
+
raise ValueError(f"{name} is empty")
|
138 |
+
if not os.path.exists(path):
|
139 |
+
raise FileNotFoundError(f"{name} not found: {path}")
|
140 |
|
141 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
# Komut oluştur
|
144 |
cmd_parts = [
|
145 |
+
"python", "-m", "inference",
|
146 |
+
"--model_type", str(model_type),
|
147 |
+
"--config_path", str(config_path),
|
148 |
+
"--start_check_point", str(start_check_point),
|
149 |
+
"--input_folder", str(INPUT_DIR),
|
150 |
+
"--store_dir", str(OUTPUT_DIR),
|
151 |
"--chunk_size", str(inference_chunk_size),
|
152 |
"--overlap", str(inference_overlap),
|
153 |
"--export_format", f"{output_format} FLOAT"
|
154 |
]
|
155 |
+
for flag, value in [
|
156 |
+
("--extract_instrumental", extract_instrumental),
|
157 |
+
("--use_tta", use_tta),
|
158 |
+
("--demud_phaseremix_inst", demud_phaseremix_inst)
|
159 |
+
]:
|
160 |
+
if value:
|
161 |
+
cmd_parts.append(flag)
|
162 |
+
|
163 |
+
logging.info(f"Executing command: {' '.join(cmd_parts)}")
|
164 |
+
start_time = time.time()
|
165 |
+
|
166 |
+
# Subprocess başlat
|
167 |
+
process = subprocess.Popen(
|
168 |
cmd_parts,
|
169 |
+
cwd=os.path.dirname(os.path.abspath(__file__)),
|
170 |
+
stdout=subprocess.PIPE,
|
171 |
+
stderr=subprocess.PIPE,
|
172 |
text=True,
|
173 |
+
bufsize=1,
|
174 |
+
universal_newlines=True
|
175 |
)
|
176 |
|
177 |
+
# İlerleme takibi
|
178 |
+
mixture_paths = sorted(glob.glob(os.path.join(INPUT_DIR, '*.*')))
|
179 |
+
total_files = len(mixture_paths)
|
180 |
+
processed_files = 0
|
181 |
+
base_progress_per_file = 80 / total_files if total_files > 0 else 80 # 0-80% ayrıştırma
|
182 |
+
|
183 |
+
stderr_output = ""
|
184 |
+
stdout_output = ""
|
185 |
+
while process.poll() is None:
|
186 |
+
line = process.stdout.readline().strip()
|
187 |
+
if line:
|
188 |
+
stdout_output += line + "\n"
|
189 |
+
if i18n("loaded_audio").lower() in line.lower():
|
190 |
+
processed_files += 1
|
191 |
+
progress_value = round((processed_files / total_files) * 80)
|
192 |
+
if progress is not None and callable(getattr(progress, '__call__', None)):
|
193 |
+
progress(progress_value / 100, desc=i18n("running_separation").format(processed_files, total_files))
|
194 |
+
update_progress_html(i18n("running_separation").format(processed_files, total_files), progress_value)
|
195 |
+
logging.debug(line)
|
196 |
+
|
197 |
+
err_line = process.stderr.readline().strip()
|
198 |
+
if err_line:
|
199 |
+
stderr_output += err_line + "\n"
|
200 |
+
logging.error(err_line)
|
201 |
+
|
202 |
+
# Kalan çıktıları topla
|
203 |
+
stdout, stderr = process.communicate()
|
204 |
+
stdout_output += stdout
|
205 |
+
stderr_output += stderr
|
206 |
+
logging.debug(f"Subprocess stdout: {stdout_output}")
|
207 |
+
if stderr_output:
|
208 |
+
logging.error(f"Subprocess stderr: {stderr_output}")
|
209 |
+
|
210 |
+
if process.returncode != 0:
|
211 |
+
raise RuntimeError(f"Subprocess failed with code {process.returncode}: {stderr_output}")
|
212 |
+
|
213 |
+
elapsed_time = time.time() - start_time
|
214 |
+
logging.info(f"Subprocess completed in {elapsed_time:.2f} seconds")
|
215 |
+
|
216 |
+
# Çıktıları işle
|
217 |
filename_model = extract_model_name_from_checkpoint(start_check_point)
|
218 |
+
output_files = sorted(os.listdir(OUTPUT_DIR))
|
219 |
if not output_files:
|
220 |
raise FileNotFoundError("No output files created in OUTPUT_DIR")
|
221 |
|
222 |
+
# Dosya yeniden adlandırma: 80-90%
|
223 |
+
total_output_files = len(output_files)
|
224 |
+
renamed_files = 0
|
225 |
+
for filename in output_files:
|
226 |
+
file_path = os.path.join(OUTPUT_DIR, filename)
|
227 |
+
if not any(filename.lower().endswith(ext) for ext in ['.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a']):
|
228 |
+
continue
|
229 |
+
base, ext = os.path.splitext(filename)
|
230 |
+
detected_type = None
|
231 |
+
for type_key in ['vocals', 'instrumental', 'phaseremix', 'drum', 'bass', 'other', 'effects', 'speech', 'music', 'dry', 'male', 'female', 'bleed', 'karaoke']:
|
232 |
+
if type_key.lower() in base.lower():
|
233 |
+
detected_type = type_key
|
234 |
+
break
|
235 |
+
type_suffix = detected_type.capitalize() if detected_type else 'Processed'
|
236 |
+
clean_base = sanitize_filename(base.split('_')[0]).rsplit('.', 1)[0]
|
237 |
+
new_filename = f"{clean_base}_{type_suffix}_{filename_model}{ext}"
|
238 |
+
new_file_path = os.path.join(OUTPUT_DIR, new_filename)
|
239 |
+
try:
|
240 |
+
os.rename(file_path, new_file_path)
|
241 |
+
renamed_files += 1
|
242 |
+
progress_value = round(80 + (renamed_files / total_output_files) * 10)
|
243 |
+
if progress is not None and callable(getattr(progress, '__call__', None)):
|
244 |
+
progress(progress_value / 100, desc=i18n("renaming_files").format(renamed_files, total_output_files))
|
245 |
+
update_progress_html(i18n("renaming_files").format(renamed_files, total_output_files), progress_value)
|
246 |
+
except Exception as e:
|
247 |
+
logging.error(f"Could not rename {file_path} to {new_file_path}: {e}")
|
248 |
+
|
249 |
+
output_files = sorted(os.listdir(OUTPUT_DIR))
|
250 |
if not output_files:
|
251 |
raise FileNotFoundError("No output files in OUTPUT_DIR after renaming")
|
252 |
|
253 |
+
# Çıktıları eşleştir
|
254 |
def find_file(keyword):
|
255 |
matching_files = [
|
256 |
+
os.path.join(OUTPUT_DIR, f) for f in output_files
|
257 |
+
if keyword.lower() in f.lower()
|
258 |
]
|
259 |
return matching_files[0] if matching_files else None
|
260 |
|
|
|
265 |
find_file('female'), find_file('bleed'), find_file('karaoke')
|
266 |
]
|
267 |
|
268 |
+
# Normalizasyon: 90-95%
|
269 |
normalized_outputs = []
|
270 |
+
for i, output_file in enumerate(output_list):
|
271 |
if output_file and os.path.exists(output_file):
|
272 |
normalized_file = os.path.join(OUTPUT_DIR, f"{sanitize_filename(os.path.splitext(os.path.basename(output_file))[0])}.{output_format}")
|
273 |
if output_file.endswith(f".{output_format}") and output_file != normalized_file:
|
|
|
278 |
else:
|
279 |
normalized_file = output_file
|
280 |
normalized_outputs.append(normalized_file)
|
281 |
+
|
282 |
+
progress_value = round(90 + (i + 1) / len(output_list) * 5)
|
283 |
+
if progress is not None and callable(getattr(progress, '__call__', None)):
|
284 |
+
progress(progress_value / 100, desc=f"Normalizing output {i+1}/{len(output_list)}")
|
285 |
+
update_progress_html(f"Normalizing output {i+1}/{len(output_list)}", progress_value)
|
286 |
else:
|
287 |
+
normalized_outputs.append(None)
|
288 |
|
289 |
+
# Apollo işlemi: 95-100%
|
290 |
if use_apollo:
|
291 |
+
from apollo_processing import process_with_apollo # Varsayılan modül
|
292 |
normalized_outputs = process_with_apollo(
|
293 |
output_files=normalized_outputs,
|
294 |
output_dir=OUTPUT_DIR,
|
|
|
298 |
apollo_normal_model=apollo_normal_model,
|
299 |
apollo_midside_model=apollo_midside_model,
|
300 |
output_format=output_format,
|
301 |
+
progress=lambda p, desc: progress((95 + p * 5) / 100, desc=desc) if progress else None,
|
302 |
+
total_progress_start=95,
|
303 |
total_progress_end=100
|
304 |
)
|
305 |
|
306 |
+
# Tamamlandı
|
307 |
if progress is not None and callable(getattr(progress, '__call__', None)):
|
308 |
+
progress(1.0, desc=i18n("separation_complete"))
|
309 |
+
update_progress_html(i18n("separation_complete"), 100)
|
310 |
+
|
311 |
+
logging.info(f"Processing completed successfully. Outputs: {normalized_outputs}")
|
312 |
return tuple(normalized_outputs)
|
313 |
|
|
|
|
|
|
|
314 |
except Exception as e:
|
315 |
+
logging.error(f"run_command_and_process_files error: {str(e)}")
|
316 |
import traceback
|
317 |
+
traceback.print_exc(file=sys.stderr)
|
318 |
return (None,) * 14
|
319 |
|
320 |
def process_audio(
|