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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import logging
|
4 |
+
import yt_dlp
|
5 |
+
import spaces
|
6 |
+
import gradio as gr
|
7 |
+
from audio_separator.separator import Separator
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
use_autocast = device == "cuda"
|
13 |
+
|
14 |
+
#=========================#
|
15 |
+
# Roformer Models #
|
16 |
+
#=========================#
|
17 |
+
roformer_models = {
|
18 |
+
'BS-Roformer-Viperx-1297': 'model_bs_roformer_ep_317_sdr_12.9755.ckpt',
|
19 |
+
'BS-Roformer-Viperx-1296': 'model_bs_roformer_ep_368_sdr_12.9628.ckpt',
|
20 |
+
'BS-Roformer-Viperx-1053': 'model_bs_roformer_ep_937_sdr_10.5309.ckpt',
|
21 |
+
'Mel-Roformer-Viperx-1143': 'model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt',
|
22 |
+
'BS-Roformer-De-Reverb': 'deverb_bs_roformer_8_384dim_10depth.ckpt',
|
23 |
+
'Mel-Roformer-Crowd-Aufr33-Viperx': 'mel_band_roformer_crowd_aufr33_viperx_sdr_8.7144.ckpt',
|
24 |
+
'Mel-Roformer-Denoise-Aufr33': 'denoise_mel_band_roformer_aufr33_sdr_27.9959.ckpt',
|
25 |
+
'Mel-Roformer-Denoise-Aufr33-Aggr' : 'denoise_mel_band_roformer_aufr33_aggr_sdr_27.9768.ckpt',
|
26 |
+
'Mel-Roformer-Karaoke-Aufr33-Viperx': 'mel_band_roformer_karaoke_aufr33_viperx_sdr_10.1956.ckpt',
|
27 |
+
'MelBand Roformer Kim | Inst V1 by Unwa' : 'melband_roformer_inst_v1.ckpt',
|
28 |
+
'MelBand Roformer Kim | Inst V2 by Unwa' : 'melband_roformer_inst_v2.ckpt',
|
29 |
+
'MelBand Roformer Kim | InstVoc Duality V1 by Unwa' : 'melband_roformer_instvoc_duality_v1.ckpt',
|
30 |
+
'MelBand Roformer Kim | InstVoc Duality V2 by Unwa' : 'melband_roformer_instvox_duality_v2.ckpt',
|
31 |
+
}
|
32 |
+
|
33 |
+
#=========================#
|
34 |
+
# MDX23C Models #
|
35 |
+
#=========================#
|
36 |
+
mdx23c_models = [
|
37 |
+
'MDX23C_D1581.ckpt',
|
38 |
+
'MDX23C-8KFFT-InstVoc_HQ.ckpt',
|
39 |
+
'MDX23C-8KFFT-InstVoc_HQ_2.ckpt',
|
40 |
+
]
|
41 |
+
|
42 |
+
#=========================#
|
43 |
+
# MDXN-NET Models #
|
44 |
+
#=========================#
|
45 |
+
mdxnet_models = [
|
46 |
+
'UVR-MDX-NET-Inst_full_292.onnx',
|
47 |
+
'UVR-MDX-NET_Inst_187_beta.onnx',
|
48 |
+
'UVR-MDX-NET_Inst_82_beta.onnx',
|
49 |
+
'UVR-MDX-NET_Inst_90_beta.onnx',
|
50 |
+
'UVR-MDX-NET_Main_340.onnx',
|
51 |
+
'UVR-MDX-NET_Main_390.onnx',
|
52 |
+
'UVR-MDX-NET_Main_406.onnx',
|
53 |
+
'UVR-MDX-NET_Main_427.onnx',
|
54 |
+
'UVR-MDX-NET_Main_438.onnx',
|
55 |
+
'UVR-MDX-NET-Inst_HQ_1.onnx',
|
56 |
+
'UVR-MDX-NET-Inst_HQ_2.onnx',
|
57 |
+
'UVR-MDX-NET-Inst_HQ_3.onnx',
|
58 |
+
'UVR-MDX-NET-Inst_HQ_4.onnx',
|
59 |
+
'UVR-MDX-NET-Inst_HQ_5.onnx',
|
60 |
+
'UVR_MDXNET_Main.onnx',
|
61 |
+
'UVR-MDX-NET-Inst_Main.onnx',
|
62 |
+
'UVR_MDXNET_1_9703.onnx',
|
63 |
+
'UVR_MDXNET_2_9682.onnx',
|
64 |
+
'UVR_MDXNET_3_9662.onnx',
|
65 |
+
'UVR-MDX-NET-Inst_1.onnx',
|
66 |
+
'UVR-MDX-NET-Inst_2.onnx',
|
67 |
+
'UVR-MDX-NET-Inst_3.onnx',
|
68 |
+
'UVR_MDXNET_KARA.onnx',
|
69 |
+
'UVR_MDXNET_KARA_2.onnx',
|
70 |
+
'UVR_MDXNET_9482.onnx',
|
71 |
+
'UVR-MDX-NET-Voc_FT.onnx',
|
72 |
+
'Kim_Vocal_1.onnx',
|
73 |
+
'Kim_Vocal_2.onnx',
|
74 |
+
'Kim_Inst.onnx',
|
75 |
+
'Reverb_HQ_By_FoxJoy.onnx',
|
76 |
+
'UVR-MDX-NET_Crowd_HQ_1.onnx',
|
77 |
+
'kuielab_a_vocals.onnx',
|
78 |
+
'kuielab_a_other.onnx',
|
79 |
+
'kuielab_a_bass.onnx',
|
80 |
+
'kuielab_a_drums.onnx',
|
81 |
+
'kuielab_b_vocals.onnx',
|
82 |
+
'kuielab_b_other.onnx',
|
83 |
+
'kuielab_b_bass.onnx',
|
84 |
+
'kuielab_b_drums.onnx',
|
85 |
+
]
|
86 |
+
|
87 |
+
#========================#
|
88 |
+
# VR-ARCH Models #
|
89 |
+
#========================#
|
90 |
+
vrarch_models = [
|
91 |
+
'1_HP-UVR.pth',
|
92 |
+
'2_HP-UVR.pth',
|
93 |
+
'3_HP-Vocal-UVR.pth',
|
94 |
+
'4_HP-Vocal-UVR.pth',
|
95 |
+
'5_HP-Karaoke-UVR.pth',
|
96 |
+
'6_HP-Karaoke-UVR.pth',
|
97 |
+
'7_HP2-UVR.pth',
|
98 |
+
'8_HP2-UVR.pth',
|
99 |
+
'9_HP2-UVR.pth',
|
100 |
+
'10_SP-UVR-2B-32000-1.pth',
|
101 |
+
'11_SP-UVR-2B-32000-2.pth',
|
102 |
+
'12_SP-UVR-3B-44100.pth',
|
103 |
+
'13_SP-UVR-4B-44100-1.pth',
|
104 |
+
'14_SP-UVR-4B-44100-2.pth',
|
105 |
+
'15_SP-UVR-MID-44100-1.pth',
|
106 |
+
'16_SP-UVR-MID-44100-2.pth',
|
107 |
+
'17_HP-Wind_Inst-UVR.pth',
|
108 |
+
'UVR-De-Echo-Aggressive.pth',
|
109 |
+
'UVR-De-Echo-Normal.pth',
|
110 |
+
'UVR-DeEcho-DeReverb.pth',
|
111 |
+
'UVR-DeNoise-Lite.pth',
|
112 |
+
'UVR-DeNoise.pth',
|
113 |
+
'UVR-BVE-4B_SN-44100-1.pth',
|
114 |
+
'MGM_HIGHEND_v4.pth',
|
115 |
+
'MGM_LOWEND_A_v4.pth',
|
116 |
+
'MGM_LOWEND_B_v4.pth',
|
117 |
+
'MGM_MAIN_v4.pth',
|
118 |
+
]
|
119 |
+
|
120 |
+
#=======================#
|
121 |
+
# DEMUCS Models #
|
122 |
+
#=======================#
|
123 |
+
demucs_models = [
|
124 |
+
'htdemucs_ft.yaml',
|
125 |
+
'htdemucs_6s.yaml',
|
126 |
+
'htdemucs.yaml',
|
127 |
+
'hdemucs_mmi.yaml',
|
128 |
+
]
|
129 |
+
|
130 |
+
output_format = [
|
131 |
+
'wav',
|
132 |
+
'flac',
|
133 |
+
'mp3',
|
134 |
+
'ogg',
|
135 |
+
'opus',
|
136 |
+
'm4a',
|
137 |
+
'aiff',
|
138 |
+
'ac3'
|
139 |
+
]
|
140 |
+
|
141 |
+
found_files = []
|
142 |
+
logs = []
|
143 |
+
out_dir = "./outputs"
|
144 |
+
models_dir = "./models"
|
145 |
+
extensions = (".wav", ".flac", ".mp3", ".ogg", ".opus", ".m4a", ".aiff", ".ac3")
|
146 |
+
|
147 |
+
def download_audio(url, output_dir="ytdl"):
|
148 |
+
|
149 |
+
os.makedirs(output_dir, exist_ok=True)
|
150 |
+
|
151 |
+
ydl_opts = {
|
152 |
+
'format': 'bestaudio/best',
|
153 |
+
'postprocessors': [{
|
154 |
+
'key': 'FFmpegExtractAudio',
|
155 |
+
'preferredcodec': 'wav',
|
156 |
+
'preferredquality': '32',
|
157 |
+
}],
|
158 |
+
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
|
159 |
+
'postprocessor_args': [
|
160 |
+
'-acodec', 'pcm_f32le'
|
161 |
+
],
|
162 |
+
}
|
163 |
+
|
164 |
+
try:
|
165 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
166 |
+
info = ydl.extract_info(url, download=False)
|
167 |
+
video_title = info['title']
|
168 |
+
|
169 |
+
ydl.download([url])
|
170 |
+
|
171 |
+
file_path = os.path.join(output_dir, f"{video_title}.wav")
|
172 |
+
|
173 |
+
if os.path.exists(file_path):
|
174 |
+
return os.path.abspath(file_path)
|
175 |
+
else:
|
176 |
+
raise Exception("Something went wrong")
|
177 |
+
|
178 |
+
except Exception as e:
|
179 |
+
raise Exception(f"Error extracting audio with yt-dlp: {str(e)}")
|
180 |
+
|
181 |
+
@spaces.GPU(duration=60)
|
182 |
+
def roformer_separator(audio, model_key, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
|
183 |
+
base_name = os.path.splitext(os.path.basename(audio))[0]
|
184 |
+
roformer_model = roformer_models[model_key]
|
185 |
+
try:
|
186 |
+
separator = Separator(
|
187 |
+
log_level=logging.WARNING,
|
188 |
+
model_file_dir=models_dir,
|
189 |
+
output_dir=out_dir,
|
190 |
+
output_format=out_format,
|
191 |
+
use_autocast=use_autocast,
|
192 |
+
normalization_threshold=norm_thresh,
|
193 |
+
amplification_threshold=amp_thresh,
|
194 |
+
mdxc_params={
|
195 |
+
"segment_size": segment_size,
|
196 |
+
"override_model_segment_size": override_seg_size,
|
197 |
+
"batch_size": batch_size,
|
198 |
+
"overlap": overlap,
|
199 |
+
}
|
200 |
+
)
|
201 |
+
|
202 |
+
progress(0.2, desc="Loading model...")
|
203 |
+
separator.load_model(model_filename=roformer_model)
|
204 |
+
|
205 |
+
progress(0.7, desc="Separating audio...")
|
206 |
+
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
207 |
+
|
208 |
+
stems = [os.path.join(out_dir, file_name) for file_name in separation]
|
209 |
+
return stems[1], stems[0]
|
210 |
+
except Exception as e:
|
211 |
+
raise RuntimeError(f"Roformer separation failed: {e}") from e
|
212 |
+
|
213 |
+
@spaces.GPU(duration=60)
|
214 |
+
def mdxc_separator(audio, model, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
|
215 |
+
base_name = os.path.splitext(os.path.basename(audio))[0]
|
216 |
+
try:
|
217 |
+
separator = Separator(
|
218 |
+
log_level=logging.WARNING,
|
219 |
+
model_file_dir=models_dir,
|
220 |
+
output_dir=out_dir,
|
221 |
+
output_format=out_format,
|
222 |
+
use_autocast=use_autocast,
|
223 |
+
normalization_threshold=norm_thresh,
|
224 |
+
amplification_threshold=amp_thresh,
|
225 |
+
mdxc_params={
|
226 |
+
"segment_size": segment_size,
|
227 |
+
"override_model_segment_size": override_seg_size,
|
228 |
+
"batch_size": batch_size,
|
229 |
+
"overlap": overlap,
|
230 |
+
}
|
231 |
+
)
|
232 |
+
|
233 |
+
progress(0.2, desc="Loading model...")
|
234 |
+
separator.load_model(model_filename=model)
|
235 |
+
|
236 |
+
progress(0.7, desc="Separating audio...")
|
237 |
+
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
238 |
+
|
239 |
+
stems = [os.path.join(out_dir, file_name) for file_name in separation]
|
240 |
+
return stems[1], stems[0]
|
241 |
+
except Exception as e:
|
242 |
+
raise RuntimeError(f"MDX23C separation failed: {e}") from e
|
243 |
+
|
244 |
+
@spaces.GPU(duration=60)
|
245 |
+
def mdxnet_separator(audio, model, out_format, hop_length, segment_size, denoise, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
|
246 |
+
base_name = os.path.splitext(os.path.basename(audio))[0]
|
247 |
+
try:
|
248 |
+
separator = Separator(
|
249 |
+
log_level=logging.WARNING,
|
250 |
+
model_file_dir=models_dir,
|
251 |
+
output_dir=out_dir,
|
252 |
+
output_format=out_format,
|
253 |
+
use_autocast=use_autocast,
|
254 |
+
normalization_threshold=norm_thresh,
|
255 |
+
amplification_threshold=amp_thresh,
|
256 |
+
mdx_params={
|
257 |
+
"hop_length": hop_length,
|
258 |
+
"segment_size": segment_size,
|
259 |
+
"overlap": overlap,
|
260 |
+
"batch_size": batch_size,
|
261 |
+
"enable_denoise": denoise,
|
262 |
+
}
|
263 |
+
)
|
264 |
+
|
265 |
+
progress(0.2, desc="Loading model...")
|
266 |
+
separator.load_model(model_filename=model)
|
267 |
+
|
268 |
+
progress(0.7, desc="Separating audio...")
|
269 |
+
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
270 |
+
|
271 |
+
stems = [os.path.join(out_dir, file_name) for file_name in separation]
|
272 |
+
return stems[0], stems[1]
|
273 |
+
except Exception as e:
|
274 |
+
raise RuntimeError(f"MDX-NET separation failed: {e}") from e
|
275 |
+
|
276 |
+
@spaces.GPU(duration=60)
|
277 |
+
def vrarch_separator(audio, model, out_format, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
|
278 |
+
base_name = os.path.splitext(os.path.basename(audio))[0]
|
279 |
+
try:
|
280 |
+
separator = Separator(
|
281 |
+
log_level=logging.WARNING,
|
282 |
+
model_file_dir=models_dir,
|
283 |
+
output_dir=out_dir,
|
284 |
+
output_format=out_format,
|
285 |
+
use_autocast=use_autocast,
|
286 |
+
normalization_threshold=norm_thresh,
|
287 |
+
amplification_threshold=amp_thresh,
|
288 |
+
vr_params={
|
289 |
+
"batch_size": batch_size,
|
290 |
+
"window_size": window_size,
|
291 |
+
"aggression": aggression,
|
292 |
+
"enable_tta": tta,
|
293 |
+
"enable_post_process": post_process,
|
294 |
+
"post_process_threshold": post_process_threshold,
|
295 |
+
"high_end_process": high_end_process,
|
296 |
+
}
|
297 |
+
)
|
298 |
+
|
299 |
+
progress(0.2, desc="Loading model...")
|
300 |
+
separator.load_model(model_filename=model)
|
301 |
+
|
302 |
+
progress(0.7, desc="Separating audio...")
|
303 |
+
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
304 |
+
|
305 |
+
stems = [os.path.join(out_dir, file_name) for file_name in separation]
|
306 |
+
return stems[0], stems[1]
|
307 |
+
except Exception as e:
|
308 |
+
raise RuntimeError(f"VR ARCH separation failed: {e}") from e
|
309 |
+
|
310 |
+
@spaces.GPU(duration=60)
|
311 |
+
def demucs_separator(audio, model, out_format, shifts, segment_size, segments_enabled, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
|
312 |
+
base_name = os.path.splitext(os.path.basename(audio))[0]
|
313 |
+
try:
|
314 |
+
separator = Separator(
|
315 |
+
log_level=logging.WARNING,
|
316 |
+
model_file_dir=models_dir,
|
317 |
+
output_dir=out_dir,
|
318 |
+
output_format=out_format,
|
319 |
+
use_autocast=use_autocast,
|
320 |
+
normalization_threshold=norm_thresh,
|
321 |
+
amplification_threshold=amp_thresh,
|
322 |
+
demucs_params={
|
323 |
+
"batch_size": batch_size,
|
324 |
+
"segment_size": segment_size,
|
325 |
+
"shifts": shifts,
|
326 |
+
"overlap": overlap,
|
327 |
+
"segments_enabled": segments_enabled,
|
328 |
+
}
|
329 |
+
)
|
330 |
+
|
331 |
+
progress(0.2, desc="Loading model...")
|
332 |
+
separator.load_model(model_filename=model)
|
333 |
+
|
334 |
+
progress(0.7, desc="Separating audio...")
|
335 |
+
separation = separator.separate(audio)
|
336 |
+
|
337 |
+
stems = [os.path.join(out_dir, file_name) for file_name in separation]
|
338 |
+
|
339 |
+
if model == "htdemucs_6s.yaml":
|
340 |
+
return stems[0], stems[1], stems[2], stems[3], stems[4], stems[5]
|
341 |
+
else:
|
342 |
+
return stems[0], stems[1], stems[2], stems[3], None, None
|
343 |
+
except Exception as e:
|
344 |
+
raise RuntimeError(f"Demucs separation failed: {e}") from e
|
345 |
+
|
346 |
+
def update_stems(model):
|
347 |
+
if model == "htdemucs_6s.yaml":
|
348 |
+
return gr.update(visible=True)
|
349 |
+
else:
|
350 |
+
return gr.update(visible=False)
|
351 |
+
|
352 |
+
@spaces.GPU(duration=60)
|
353 |
+
def roformer_batch(path_input, path_output, model_key, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh):
|
354 |
+
found_files.clear()
|
355 |
+
logs.clear()
|
356 |
+
roformer_model = roformer_models[model_key]
|
357 |
+
|
358 |
+
for audio_files in os.listdir(path_input):
|
359 |
+
if audio_files.endswith(extensions):
|
360 |
+
found_files.append(audio_files)
|
361 |
+
total_files = len(found_files)
|
362 |
+
|
363 |
+
if total_files == 0:
|
364 |
+
logs.append("No valid audio files.")
|
365 |
+
yield "\n".join(logs)
|
366 |
+
else:
|
367 |
+
logs.append(f"{total_files} audio files found")
|
368 |
+
found_files.sort()
|
369 |
+
|
370 |
+
for audio_files in found_files:
|
371 |
+
file_path = os.path.join(path_input, audio_files)
|
372 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
373 |
+
try:
|
374 |
+
separator = Separator(
|
375 |
+
log_level=logging.WARNING,
|
376 |
+
model_file_dir=models_dir,
|
377 |
+
output_dir=path_output,
|
378 |
+
output_format=out_format,
|
379 |
+
use_autocast=use_autocast,
|
380 |
+
normalization_threshold=norm_thresh,
|
381 |
+
amplification_threshold=amp_thresh,
|
382 |
+
mdxc_params={
|
383 |
+
"segment_size": segment_size,
|
384 |
+
"override_model_segment_size": override_seg_size,
|
385 |
+
"batch_size": batch_size,
|
386 |
+
"overlap": overlap,
|
387 |
+
}
|
388 |
+
)
|
389 |
+
|
390 |
+
logs.append("Loading model...")
|
391 |
+
yield "\n".join(logs)
|
392 |
+
separator.load_model(model_filename=roformer_model)
|
393 |
+
|
394 |
+
logs.append(f"Separating file: {audio_files}")
|
395 |
+
yield "\n".join(logs)
|
396 |
+
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
397 |
+
logs.append(f"File: {audio_files} separated!")
|
398 |
+
yield "\n".join(logs)
|
399 |
+
except Exception as e:
|
400 |
+
raise RuntimeError(f"Roformer batch separation failed: {e}") from e
|
401 |
+
|
402 |
+
@spaces.GPU(duration=60)
|
403 |
+
def mdx23c_batch(path_input, path_output, model, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh):
|
404 |
+
found_files.clear()
|
405 |
+
logs.clear()
|
406 |
+
|
407 |
+
for audio_files in os.listdir(path_input):
|
408 |
+
if audio_files.endswith(extensions):
|
409 |
+
found_files.append(audio_files)
|
410 |
+
total_files = len(found_files)
|
411 |
+
|
412 |
+
if total_files == 0:
|
413 |
+
logs.append("No valid audio files.")
|
414 |
+
yield "\n".join(logs)
|
415 |
+
else:
|
416 |
+
logs.append(f"{total_files} audio files found")
|
417 |
+
found_files.sort()
|
418 |
+
|
419 |
+
for audio_files in found_files:
|
420 |
+
file_path = os.path.join(path_input, audio_files)
|
421 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
422 |
+
try:
|
423 |
+
separator = Separator(
|
424 |
+
log_level=logging.WARNING,
|
425 |
+
model_file_dir=models_dir,
|
426 |
+
output_dir=path_output,
|
427 |
+
output_format=out_format,
|
428 |
+
use_autocast=use_autocast,
|
429 |
+
normalization_threshold=norm_thresh,
|
430 |
+
amplification_threshold=amp_thresh,
|
431 |
+
mdxc_params={
|
432 |
+
"segment_size": segment_size,
|
433 |
+
"override_model_segment_size": override_seg_size,
|
434 |
+
"batch_size": batch_size,
|
435 |
+
"overlap": overlap,
|
436 |
+
}
|
437 |
+
)
|
438 |
+
|
439 |
+
logs.append("Loading model...")
|
440 |
+
yield "\n".join(logs)
|
441 |
+
separator.load_model(model_filename=model)
|
442 |
+
|
443 |
+
logs.append(f"Separating file: {audio_files}")
|
444 |
+
yield "\n".join(logs)
|
445 |
+
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
446 |
+
logs.append(f"File: {audio_files} separated!")
|
447 |
+
yield "\n".join(logs)
|
448 |
+
except Exception as e:
|
449 |
+
raise RuntimeError(f"Roformer batch separation failed: {e}") from e
|
450 |
+
|
451 |
+
@spaces.GPU(duration=60)
|
452 |
+
def mdxnet_batch(path_input, path_output, model, out_format, hop_length, segment_size, denoise, overlap, batch_size, norm_thresh, amp_thresh):
|
453 |
+
found_files.clear()
|
454 |
+
logs.clear()
|
455 |
+
|
456 |
+
for audio_files in os.listdir(path_input):
|
457 |
+
if audio_files.endswith(extensions):
|
458 |
+
found_files.append(audio_files)
|
459 |
+
total_files = len(found_files)
|
460 |
+
|
461 |
+
if total_files == 0:
|
462 |
+
logs.append("No valid audio files.")
|
463 |
+
yield "\n".join(logs)
|
464 |
+
else:
|
465 |
+
logs.append(f"{total_files} audio files found")
|
466 |
+
found_files.sort()
|
467 |
+
|
468 |
+
for audio_files in found_files:
|
469 |
+
file_path = os.path.join(path_input, audio_files)
|
470 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
471 |
+
try:
|
472 |
+
separator = Separator(
|
473 |
+
log_level=logging.WARNING,
|
474 |
+
model_file_dir=models_dir,
|
475 |
+
output_dir=path_output,
|
476 |
+
output_format=out_format,
|
477 |
+
use_autocast=use_autocast,
|
478 |
+
normalization_threshold=norm_thresh,
|
479 |
+
amplification_threshold=amp_thresh,
|
480 |
+
mdx_params={
|
481 |
+
"hop_length": hop_length,
|
482 |
+
"segment_size": segment_size,
|
483 |
+
"overlap": overlap,
|
484 |
+
"batch_size": batch_size,
|
485 |
+
"enable_denoise": denoise,
|
486 |
+
}
|
487 |
+
)
|
488 |
+
|
489 |
+
logs.append("Loading model...")
|
490 |
+
yield "\n".join(logs)
|
491 |
+
separator.load_model(model_filename=model)
|
492 |
+
|
493 |
+
logs.append(f"Separating file: {audio_files}")
|
494 |
+
yield "\n".join(logs)
|
495 |
+
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
496 |
+
logs.append(f"File: {audio_files} separated!")
|
497 |
+
yield "\n".join(logs)
|
498 |
+
except Exception as e:
|
499 |
+
raise RuntimeError(f"Roformer batch separation failed: {e}") from e
|
500 |
+
|
501 |
+
@spaces.GPU(duration=60)
|
502 |
+
def vrarch_batch(path_input, path_output, model, out_format, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, batch_size, norm_thresh, amp_thresh):
|
503 |
+
found_files.clear()
|
504 |
+
logs.clear()
|
505 |
+
|
506 |
+
for audio_files in os.listdir(path_input):
|
507 |
+
if audio_files.endswith(extensions):
|
508 |
+
found_files.append(audio_files)
|
509 |
+
total_files = len(found_files)
|
510 |
+
|
511 |
+
if total_files == 0:
|
512 |
+
logs.append("No valid audio files.")
|
513 |
+
yield "\n".join(logs)
|
514 |
+
else:
|
515 |
+
logs.append(f"{total_files} audio files found")
|
516 |
+
found_files.sort()
|
517 |
+
|
518 |
+
for audio_files in found_files:
|
519 |
+
file_path = os.path.join(path_input, audio_files)
|
520 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
521 |
+
try:
|
522 |
+
separator = Separator(
|
523 |
+
log_level=logging.WARNING,
|
524 |
+
model_file_dir=models_dir,
|
525 |
+
output_dir=path_output,
|
526 |
+
output_format=out_format,
|
527 |
+
use_autocast=use_autocast,
|
528 |
+
normalization_threshold=norm_thresh,
|
529 |
+
amplification_threshold=amp_thresh,
|
530 |
+
vr_params={
|
531 |
+
"batch_size": batch_size,
|
532 |
+
"window_size": window_size,
|
533 |
+
"aggression": aggression,
|
534 |
+
"enable_tta": tta,
|
535 |
+
"enable_post_process": post_process,
|
536 |
+
"post_process_threshold": post_process_threshold,
|
537 |
+
"high_end_process": high_end_process,
|
538 |
+
}
|
539 |
+
)
|
540 |
+
|
541 |
+
logs.append("Loading model...")
|
542 |
+
yield "\n".join(logs)
|
543 |
+
separator.load_model(model_filename=model)
|
544 |
+
|
545 |
+
logs.append(f"Separating file: {audio_files}")
|
546 |
+
yield "\n".join(logs)
|
547 |
+
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
|
548 |
+
logs.append(f"File: {audio_files} separated!")
|
549 |
+
yield "\n".join(logs)
|
550 |
+
except Exception as e:
|
551 |
+
raise RuntimeError(f"Roformer batch separation failed: {e}") from e
|
552 |
+
|
553 |
+
@spaces.GPU(duration=60)
|
554 |
+
def demucs_batch(path_input, path_output, model, out_format, shifts, segment_size, segments_enabled, overlap, batch_size, norm_thresh, amp_thresh):
|
555 |
+
found_files.clear()
|
556 |
+
logs.clear()
|
557 |
+
|
558 |
+
for audio_files in os.listdir(path_input):
|
559 |
+
if audio_files.endswith(extensions):
|
560 |
+
found_files.append(audio_files)
|
561 |
+
total_files = len(found_files)
|
562 |
+
|
563 |
+
if total_files == 0:
|
564 |
+
logs.append("No valid audio files.")
|
565 |
+
yield "\n".join(logs)
|
566 |
+
else:
|
567 |
+
logs.append(f"{total_files} audio files found")
|
568 |
+
found_files.sort()
|
569 |
+
|
570 |
+
for audio_files in found_files:
|
571 |
+
file_path = os.path.join(path_input, audio_files)
|
572 |
+
try:
|
573 |
+
separator = Separator(
|
574 |
+
log_level=logging.WARNING,
|
575 |
+
model_file_dir=models_dir,
|
576 |
+
output_dir=path_output,
|
577 |
+
output_format=out_format,
|
578 |
+
use_autocast=use_autocast,
|
579 |
+
normalization_threshold=norm_thresh,
|
580 |
+
amplification_threshold=amp_thresh,
|
581 |
+
demucs_params={
|
582 |
+
"batch_size": batch_size,
|
583 |
+
"segment_size": segment_size,
|
584 |
+
"shifts": shifts,
|
585 |
+
"overlap": overlap,
|
586 |
+
"segments_enabled": segments_enabled,
|
587 |
+
}
|
588 |
+
)
|
589 |
+
|
590 |
+
logs.append("Loading model...")
|
591 |
+
yield "\n".join(logs)
|
592 |
+
separator.load_model(model_filename=model)
|
593 |
+
|
594 |
+
logs.append(f"Separating file: {audio_files}")
|
595 |
+
yield "\n".join(logs)
|
596 |
+
separator.separate(file_path)
|
597 |
+
logs.append(f"File: {audio_files} separated!")
|
598 |
+
yield "\n".join(logs)
|
599 |
+
except Exception as e:
|
600 |
+
raise RuntimeError(f"Roformer batch separation failed: {e}") from e
|
601 |
+
|