AnhP commited on
Commit
7ad5037
·
verified ·
1 Parent(s): 15c7856

Update main/app/core/ui.py

Browse files
Files changed (1) hide show
  1. main/app/core/ui.py +361 -361
main/app/core/ui.py CHANGED
@@ -1,362 +1,362 @@
1
- import os
2
- import re
3
- import sys
4
- import json
5
- import torch
6
- import shutil
7
-
8
- import gradio as gr
9
- import sounddevice as sd
10
-
11
- sys.path.append(os.getcwd())
12
-
13
- from main.library.backends import directml, opencl
14
- from main.inference.realtime.audio import list_audio_device
15
- from main.app.variables import config, configs, configs_json, logger, translations, edgetts, google_tts_voice, method_f0, method_f0_full, vr_models, mdx_models, demucs_models, embedders_model, spin_model, whisper_model
16
-
17
- def gr_info(message):
18
- gr.Info(message, duration=2)
19
- logger.info(message)
20
-
21
- def gr_warning(message):
22
- gr.Warning(message, duration=2)
23
- logger.warning(message)
24
-
25
- def gr_error(message):
26
- gr.Error(message=message, duration=6)
27
- logger.error(message)
28
-
29
- def get_gpu_info():
30
- ngpu = torch.cuda.device_count()
31
- gpu_infos = [
32
- f"{i}: {torch.cuda.get_device_name(i)} ({int(torch.cuda.get_device_properties(i).total_memory / 1024 / 1024 / 1024 + 0.4)} GB)"
33
- for i in range(ngpu)
34
- if torch.cuda.is_available() or ngpu != 0
35
- ]
36
-
37
- if len(gpu_infos) == 0:
38
- if directml.torch_available:
39
- ngpu = directml.device_count()
40
- gpu_infos = [f"{i}: {directml.device_name(i)}" for i in range(ngpu) if directml.is_available() or ngpu != 0]
41
- elif opencl.torch_available:
42
- ngpu = opencl.device_count()
43
- gpu_infos = [f"{i}: {opencl.device_name(i)}" for i in range(ngpu) if opencl.is_available() or ngpu != 0]
44
- else:
45
- ngpu = 0
46
- gpu_infos = []
47
-
48
- return "\n".join(gpu_infos) if len(gpu_infos) > 0 and not config.cpu_mode else translations["no_support_gpu"]
49
-
50
- def gpu_number_str():
51
- if config.cpu_mode: return "-"
52
-
53
- ngpu = torch.cuda.device_count()
54
- if ngpu == 0: ngpu = directml.device_count() if directml.torch_available else opencl.device_count()
55
-
56
- return str("-".join(map(str, range(ngpu))) if torch.cuda.is_available() or directml.is_available() or opencl.is_available() else "-")
57
-
58
- def change_f0_choices():
59
- f0_file = sorted([os.path.abspath(os.path.join(root, f)) for root, _, files in os.walk(configs["f0_path"]) for f in files if f.endswith(".txt")])
60
- return {"value": f0_file[0] if len(f0_file) >= 1 else "", "choices": f0_file, "__type__": "update"}
61
-
62
- def change_audios_choices(input_audio):
63
- audios = sorted([os.path.abspath(os.path.join(root, f)) for root, _, files in os.walk(configs["audios_path"]) for f in files if os.path.splitext(f)[1].lower() in (".wav", ".mp3", ".flac", ".ogg", ".opus", ".m4a", ".mp4", ".aac", ".alac", ".wma", ".aiff", ".webm", ".ac3")])
64
- return {"value": input_audio if input_audio != "" else (audios[0] if len(audios) >= 1 else ""), "choices": audios, "__type__": "update"}
65
-
66
- def change_reference_choices():
67
- reference = sorted([re.sub(r'_v\d+_(?:[A-Za-z0-9_]+?)_(True|False)_(True|False)$', '', name) for name in os.listdir(configs["reference_path"]) if os.path.exists(os.path.join(configs["reference_path"], name)) and os.path.isdir(os.path.join(configs["reference_path"], name))])
68
- return {"value": reference[0] if len(reference) >= 1 else "", "choices": reference, "__type__": "update"}
69
-
70
- def change_models_choices():
71
- model, index = sorted(list(model for model in os.listdir(configs["weights_path"]) if model.endswith((".pth", ".onnx")) and not model.startswith("G_") and not model.startswith("D_"))), sorted([os.path.join(root, name) for root, _, files in os.walk(configs["logs_path"], topdown=False) for name in files if name.endswith(".index") and "trained" not in name])
72
- return [{"value": model[0] if len(model) >= 1 else "", "choices": model, "__type__": "update"}, {"value": index[0] if len(index) >= 1 else "", "choices": index, "__type__": "update"}]
73
-
74
- def change_pretrained_choices():
75
- pretrainD = sorted([model for model in os.listdir(configs["pretrained_custom_path"]) if model.endswith(".pth") and "D" in model])
76
- pretrainG = sorted([model for model in os.listdir(configs["pretrained_custom_path"]) if model.endswith(".pth") and "G" in model])
77
-
78
- return [{"choices": pretrainD, "value": pretrainD[0] if len(pretrainD) >= 1 else "", "__type__": "update"}, {"choices": pretrainG, "value": pretrainG[0] if len(pretrainG) >= 1 else "", "__type__": "update"}]
79
-
80
- def change_choices_del():
81
- return [{"choices": sorted(list(model for model in os.listdir(configs["weights_path"]) if model.endswith(".pth") and not model.startswith("G_") and not model.startswith("D_"))), "__type__": "update"}, {"choices": sorted([os.path.join(configs["logs_path"], f) for f in os.listdir(configs["logs_path"]) if f not in ["mute", "reference"] and os.path.isdir(os.path.join(configs["logs_path"], f))]), "__type__": "update"}]
82
-
83
- def change_preset_choices():
84
- return {"value": "", "choices": sorted(list(f for f in os.listdir(configs["presets_path"]) if f.endswith(".conversion.json"))), "__type__": "update"}
85
-
86
- def change_effect_preset_choices():
87
- return {"value": "", "choices": sorted(list(f for f in os.listdir(configs["presets_path"]) if f.endswith(".effect.json"))), "__type__": "update"}
88
-
89
- def change_tts_voice_choices(google):
90
- return {"choices": google_tts_voice if google else edgetts, "value": google_tts_voice[0] if google else edgetts[0], "__type__": "update"}
91
-
92
- def change_backing_choices(backing, merge):
93
- if backing or merge: return {"value": False, "interactive": False, "__type__": "update"}
94
- elif not backing or not merge: return {"interactive": True, "__type__": "update"}
95
- else: gr_warning(translations["option_not_valid"])
96
-
97
- def change_download_choices(select):
98
- selects = [False]*10
99
-
100
- if select == translations["download_url"]: selects[0] = selects[1] = selects[2] = True
101
- elif select == translations["download_from_csv"]: selects[3] = selects[4] = True
102
- elif select == translations["search_models"]: selects[5] = selects[6] = True
103
- elif select == translations["upload"]: selects[9] = True
104
- else: gr_warning(translations["option_not_valid"])
105
-
106
- return [{"visible": selects[i], "__type__": "update"} for i in range(len(selects))]
107
-
108
- def change_download_pretrained_choices(select):
109
- selects = [False]*7
110
-
111
- if select == translations["download_url"]: selects[0] = selects[1] = selects[2] = True
112
- elif select == translations["list_model"]: selects[3] = selects[4] = selects[5] = True
113
- elif select == translations["upload"]: selects[6] = True
114
- else: gr_warning(translations["option_not_valid"])
115
-
116
- return [{"visible": selects[i], "__type__": "update"} for i in range(len(selects))]
117
-
118
- def get_index(model):
119
- model = os.path.basename(model).split("_")[0]
120
- return {"value": next((f for f in [os.path.join(root, name) for root, _, files in os.walk(configs["logs_path"], topdown=False) for name in files if name.endswith(".index") and "trained" not in name] if model.split(".")[0] in f), ""), "__type__": "update"} if model else None
121
-
122
- def index_strength_show(index):
123
- return {"visible": index != "" and index != None and os.path.exists(index) and os.path.isfile(index), "value": 0.5, "__type__": "update"}
124
-
125
- def hoplength_show(method, hybrid_method=None):
126
- visible = False
127
-
128
- for m in ["mangio-crepe", "fcpe", "yin", "piptrack", "mangio-penn"]:
129
- if m in method: visible = True
130
- if hybrid_method is not None and m in hybrid_method: visible = True
131
-
132
- if visible: break
133
- else: visible = False
134
-
135
- return {"visible": visible, "__type__": "update"}
136
-
137
- def visible(value):
138
- return {"visible": value, "__type__": "update"}
139
-
140
- def valueFalse_interactive(value):
141
- return {"value": False, "interactive": value, "__type__": "update"}
142
-
143
- def valueEmpty_visible1(value):
144
- return {"value": "", "visible": value, "__type__": "update"}
145
-
146
- def pitch_guidance_lock(vocoders):
147
- return {"value": True, "interactive": vocoders == "Default", "__type__": "update"}
148
-
149
- def vocoders_lock(pitch, vocoders):
150
- return {"value": vocoders if pitch else "Default", "interactive": pitch, "__type__": "update"}
151
-
152
- def unlock_f0(value):
153
- return {"choices": method_f0_full if value else method_f0, "value": "rmvpe", "__type__": "update"}
154
-
155
- def unlock_vocoder(value, vocoder):
156
- return {"value": vocoder if value == "v2" else "Default", "interactive": value == "v2", "__type__": "update"}
157
-
158
- def unlock_ver(value, vocoder):
159
- return {"value": "v2" if vocoder == "Default" else value, "interactive": vocoder == "Default", "__type__": "update"}
160
-
161
- def change_embedders_mode(value):
162
- if value == "spin":
163
- return {"value": spin_model[0], "choices": spin_model, "__type__": "update"}
164
- elif value == "whisper":
165
- return {"value": whisper_model[0], "choices": whisper_model, "__type__": "update"}
166
- else:
167
- return {"value": embedders_model[0], "choices": embedders_model, "__type__": "update"}
168
-
169
- def change_fp(fp):
170
- fp16 = fp == "fp16"
171
-
172
- if fp16 and config.device in ["cpu", "mps", "ocl:0"]:
173
- gr_warning(translations["fp16_not_support"])
174
- return "fp32"
175
- else:
176
- gr_info(translations["start_update_precision"])
177
-
178
- configs = json.load(open(configs_json, "r"))
179
- configs["fp16"] = config.is_half = fp16
180
-
181
- with open(configs_json, "w") as f:
182
- json.dump(configs, f, indent=4)
183
-
184
- gr_info(translations["success"])
185
- return "fp16" if fp16 else "fp32"
186
-
187
- def process_output(file_path):
188
- if config.configs.get("delete_exists_file", True):
189
- if os.path.exists(file_path) and os.path.isfile(file_path): os.remove(file_path)
190
- return file_path
191
- else:
192
- if not os.path.exists(file_path): return file_path
193
- file = os.path.splitext(os.path.basename(file_path))
194
-
195
- index = 1
196
- while 1:
197
- file_path = os.path.join(os.path.dirname(file_path), f"{file[0]}_{index}{file[1]}")
198
- if not os.path.exists(file_path): return file_path
199
- index += 1
200
-
201
- def shutil_move(input_path, output_path):
202
- output_path = os.path.join(output_path, os.path.basename(input_path)) if os.path.isdir(output_path) else output_path
203
-
204
- return shutil.move(input_path, process_output(output_path)) if os.path.exists(output_path) else shutil.move(input_path, output_path)
205
-
206
- def separate_change(model_name, karaoke_model, reverb_model, enable_post_process, separate_backing, separate_reverb, enable_denoise):
207
- model_type = "vr" if model_name in list(vr_models.keys()) else "mdx" if model_name in list(mdx_models.keys()) else "demucs" if model_name in list(demucs_models.keys()) else ""
208
- karaoke_type = ("vr" if karaoke_model.startswith("VR") else "mdx") if separate_backing else None
209
- reverb_type = ("vr" if not reverb_model.startswith("MDX") else "mdx") if separate_reverb else None
210
-
211
- all_types = {model_type, karaoke_type, reverb_type}
212
-
213
- is_vr = "vr" in all_types
214
- is_mdx = "mdx" in all_types
215
- is_demucs = "demucs" in all_types
216
-
217
- return [
218
- visible(separate_backing),
219
- visible(separate_reverb),
220
- visible(is_mdx or is_demucs),
221
- visible(is_mdx or is_demucs),
222
- visible(is_mdx),
223
- visible(is_mdx or is_vr),
224
- visible(is_demucs),
225
- visible(is_vr),
226
- visible(is_vr),
227
- visible(is_vr and enable_post_process),
228
- visible(is_vr and enable_denoise),
229
- valueFalse_interactive(is_vr),
230
- valueFalse_interactive(is_vr),
231
- valueFalse_interactive(is_vr)
232
- ]
233
-
234
- def create_dataset_change(model_name, reverb_model, enable_post_process, separate_reverb, enable_denoise):
235
- model_type = "vr" if model_name in list(vr_models.keys()) else "mdx" if model_name in list(mdx_models.keys()) else "demucs" if model_name in list(demucs_models.keys()) else ""
236
- reverb_type = ("vr" if not reverb_model.startswith("MDX") else "mdx") if separate_reverb else None
237
- all_types = {model_type, reverb_type}
238
-
239
- is_vr = "vr" in all_types
240
- is_mdx = "mdx" in all_types
241
- is_demucs = "demucs" in all_types
242
-
243
- return [
244
- visible(separate_reverb),
245
- visible(is_mdx or is_demucs),
246
- visible(is_mdx or is_demucs),
247
- visible(is_mdx),
248
- visible(is_mdx or is_vr),
249
- visible(is_demucs),
250
- visible(is_vr),
251
- visible(is_vr),
252
- visible(is_vr and enable_post_process),
253
- visible(is_vr and enable_denoise),
254
- valueFalse_interactive(is_vr),
255
- valueFalse_interactive(is_vr),
256
- valueFalse_interactive(is_vr)
257
- ]
258
-
259
- def audio_device():
260
- try:
261
- input_devices, output_devices = list_audio_device()
262
-
263
- def priority(name):
264
- n = name.lower()
265
- if "virtual" in n:
266
- return 0
267
- if "vb" in n:
268
- return 1
269
- return 2
270
-
271
- output_sorted = sorted(output_devices, key=lambda d: priority(d.name))
272
- input_sorted = sorted(
273
- input_devices, key=lambda d: priority(d.name), reverse=True
274
- )
275
-
276
- input_device_list = {
277
- f"{input_sorted.index(d)+1}: {d.name} ({d.host_api})": [d.index, d.max_input_channels] for d in input_sorted
278
- }
279
- output_device_list = {
280
- f"{output_sorted.index(d)+1}: {d.name} ({d.host_api})": [d.index, d.max_output_channels] for d in output_sorted
281
- }
282
-
283
- return input_device_list, output_device_list
284
- except Exception:
285
- return [], []
286
-
287
- def update_audio_device(input_device, output_device, monitor_device, monitor):
288
- input_channels_map, output_channels_map = audio_device()
289
-
290
- input_is_asio = "ASIO" in input_device if input_device else False
291
- output_is_asio = "ASIO" in output_device if output_device else False
292
- monitor_is_asio = "ASIO" in monitor_device if monitor_device else False
293
-
294
- try:
295
- input_max_ch = input_channels_map.get(input_device, [])[1]
296
- output_max_ch = output_channels_map.get(output_device, [])[1]
297
- monitor_max_ch = output_channels_map.get(monitor_device, [])[1] if monitor else 128
298
- except:
299
- input_max_ch = output_max_ch = monitor_max_ch = -1
300
-
301
- return [
302
- visible(monitor),
303
- visible(monitor),
304
- visible(monitor_is_asio),
305
- visible(input_is_asio or output_is_asio or monitor_is_asio),
306
- gr.update(visible=input_is_asio, maximum=input_max_ch),
307
- gr.update(visible=output_is_asio, maximum=output_max_ch),
308
- gr.update(visible=monitor_is_asio, maximum=monitor_max_ch)
309
- ]
310
-
311
- def change_audio_device_choices():
312
- sd._terminate()
313
- sd._initialize()
314
-
315
- input_channels_map, output_channels_map = audio_device()
316
- input_channels_map, output_channels_map = list(input_channels_map.keys()), list(output_channels_map.keys())
317
-
318
- return [
319
- {"value": input_channels_map[0] if len(input_channels_map) >= 1 else "", "choices": input_channels_map, "__type__": "update"},
320
- {"value": output_channels_map[0] if len(output_channels_map) >= 1 else "", "choices": output_channels_map, "__type__": "update"},
321
- {"value": output_channels_map[0] if len(output_channels_map) >= 1 else "", "choices": output_channels_map, "__type__": "update"}
322
- ]
323
-
324
- def replace_punctuation(filename):
325
- return filename.replace(" ", "_").replace("-", "").replace("(", "").replace(")", "").replace("[", "").replace("]", "").replace(",", "").replace('"', "").replace("'", "").replace("|", "_").replace("{", "").replace("}", "").replace("-_-", "_").replace("_-_", "_").replace("-", "_").replace("---", "_").replace("___", "_").strip()
326
-
327
- def replace_url(url):
328
- return url.replace("/blob/", "/resolve/").replace("?download=true", "").strip()
329
-
330
- def replace_modelname(modelname):
331
- return replace_punctuation(modelname.replace(".onnx", "").replace(".pth", "").replace(".index", "").replace(".zip", ""))
332
-
333
- def replace_export_format(audio_path, export_format = "wav"):
334
- export_format = f".{export_format}"
335
-
336
- return audio_path if audio_path.endswith(export_format) else audio_path.replace(f".{os.path.basename(audio_path).split('.')[-1]}", export_format)
337
-
338
- def update_dropdowns_from_json(data):
339
- if not data:
340
- return [
341
- gr.update(choices=[], value=None),
342
- gr.update(choices=[], value=None),
343
- gr.update(choices=[], value=None)
344
- ]
345
-
346
- inputs = [tuple(d) for d in data.get("inputs", [])]
347
- outputs = [tuple(d) for d in data.get("outputs", [])]
348
-
349
- return [
350
- gr.update(choices=inputs, value=None),
351
- gr.update(choices=outputs, value=None),
352
- gr.update(choices=outputs, value=None),
353
- ]
354
-
355
- def update_button_from_json(data):
356
- if not data:
357
- return [gr.update(interactive=True), gr.update(interactive=False)]
358
-
359
- return [
360
- gr.update(interactive=data.get("start_button", True)),
361
- gr.update(interactive=data.get("stop_button", False))
362
  ]
 
1
+ import os
2
+ import re
3
+ import sys
4
+ import json
5
+ import torch
6
+ import shutil
7
+
8
+ import gradio as gr
9
+ import sounddevice as sd
10
+
11
+ sys.path.append(os.getcwd())
12
+
13
+ from main.library.backends import directml, opencl
14
+ from main.inference.realtime.audio import list_audio_device
15
+ from main.app.variables import config, configs, configs_json, logger, translations, edgetts, google_tts_voice, method_f0, method_f0_full, vr_models, mdx_models, demucs_models, embedders_model, spin_model, whisper_model
16
+
17
+ def gr_info(message):
18
+ gr.Info(message, duration=2)
19
+ logger.info(message)
20
+
21
+ def gr_warning(message):
22
+ gr.Warning(message, duration=2)
23
+ logger.warning(message)
24
+
25
+ def gr_error(message):
26
+ gr.Error(message=message, duration=6)
27
+ logger.error(message)
28
+
29
+ def get_gpu_info():
30
+ ngpu = torch.cuda.device_count()
31
+ gpu_infos = [
32
+ f"{i}: {torch.cuda.get_device_name(i)} ({int(torch.cuda.get_device_properties(i).total_memory / 1024 / 1024 / 1024 + 0.4)} GB)"
33
+ for i in range(ngpu)
34
+ if torch.cuda.is_available() or ngpu != 0
35
+ ]
36
+
37
+ if len(gpu_infos) == 0:
38
+ if directml.torch_available:
39
+ ngpu = directml.device_count()
40
+ gpu_infos = [f"{i}: {directml.device_name(i)}" for i in range(ngpu) if directml.is_available() or ngpu != 0]
41
+ elif opencl.torch_available:
42
+ ngpu = opencl.device_count()
43
+ gpu_infos = [f"{i}: {opencl.device_name(i)}" for i in range(ngpu) if opencl.is_available() or ngpu != 0]
44
+ else:
45
+ ngpu = 0
46
+ gpu_infos = []
47
+
48
+ return "\n".join(gpu_infos) if len(gpu_infos) > 0 and not config.cpu_mode else translations["no_support_gpu"]
49
+
50
+ def gpu_number_str():
51
+ if config.cpu_mode: return "-"
52
+
53
+ ngpu = torch.cuda.device_count()
54
+ if ngpu == 0: ngpu = directml.device_count() if directml.torch_available else opencl.device_count()
55
+
56
+ return str("-".join(map(str, range(ngpu))) if torch.cuda.is_available() or directml.is_available() or opencl.is_available() else "-")
57
+
58
+ def change_f0_choices():
59
+ f0_file = sorted([os.path.abspath(os.path.join(root, f)) for root, _, files in os.walk(configs["f0_path"]) for f in files if f.endswith(".txt")])
60
+ return {"value": f0_file[0] if len(f0_file) >= 1 else "", "choices": f0_file, "__type__": "update"}
61
+
62
+ def change_audios_choices(input_audio):
63
+ audios = sorted([os.path.abspath(os.path.join(root, f)) for root, _, files in os.walk(configs["audios_path"]) for f in files if os.path.splitext(f)[1].lower() in (".wav", ".mp3", ".flac", ".ogg", ".opus", ".m4a", ".mp4", ".aac", ".alac", ".wma", ".aiff", ".webm", ".ac3")])
64
+ return {"value": input_audio if input_audio != "" else (audios[0] if len(audios) >= 1 else ""), "choices": audios, "__type__": "update"}
65
+
66
+ def change_reference_choices():
67
+ reference = sorted([re.sub(r'_v\d+_(?:[A-Za-z0-9_]+?)_(True|False)_(True|False)$', '', name) for name in os.listdir(configs["reference_path"]) if os.path.exists(os.path.join(configs["reference_path"], name)) and os.path.isdir(os.path.join(configs["reference_path"], name))])
68
+ return {"value": reference[0] if len(reference) >= 1 else "", "choices": reference, "__type__": "update"}
69
+
70
+ def change_models_choices():
71
+ model, index = sorted(list(model for model in os.listdir(configs["weights_path"]) if model.endswith((".pth", ".onnx")) and not model.startswith("G_") and not model.startswith("D_"))), sorted([os.path.join(root, name) for root, _, files in os.walk(configs["logs_path"], topdown=False) for name in files if name.endswith(".index") and "trained" not in name])
72
+ return [{"value": model[0] if len(model) >= 1 else "", "choices": model, "__type__": "update"}, {"value": index[0] if len(index) >= 1 else "", "choices": index, "__type__": "update"}]
73
+
74
+ def change_pretrained_choices():
75
+ pretrainD = sorted([model for model in os.listdir(configs["pretrained_custom_path"]) if model.endswith(".pth") and "D" in model])
76
+ pretrainG = sorted([model for model in os.listdir(configs["pretrained_custom_path"]) if model.endswith(".pth") and "G" in model])
77
+
78
+ return [{"choices": pretrainD, "value": pretrainD[0] if len(pretrainD) >= 1 else "", "__type__": "update"}, {"choices": pretrainG, "value": pretrainG[0] if len(pretrainG) >= 1 else "", "__type__": "update"}]
79
+
80
+ def change_choices_del():
81
+ return [{"choices": sorted(list(model for model in os.listdir(configs["weights_path"]) if model.endswith(".pth") and not model.startswith("G_") and not model.startswith("D_"))), "__type__": "update"}, {"choices": sorted([os.path.join(configs["logs_path"], f) for f in os.listdir(configs["logs_path"]) if f not in ["mute", "reference"] and os.path.isdir(os.path.join(configs["logs_path"], f))]), "__type__": "update"}]
82
+
83
+ def change_preset_choices():
84
+ return {"value": "", "choices": sorted(list(f for f in os.listdir(configs["presets_path"]) if f.endswith(".conversion.json"))), "__type__": "update"}
85
+
86
+ def change_effect_preset_choices():
87
+ return {"value": "", "choices": sorted(list(f for f in os.listdir(configs["presets_path"]) if f.endswith(".effect.json"))), "__type__": "update"}
88
+
89
+ def change_tts_voice_choices(google):
90
+ return {"choices": google_tts_voice if google else edgetts, "value": google_tts_voice[0] if google else edgetts[0], "__type__": "update"}
91
+
92
+ def change_backing_choices(backing, merge):
93
+ if backing or merge: return {"value": False, "interactive": False, "__type__": "update"}
94
+ elif not backing or not merge: return {"interactive": True, "__type__": "update"}
95
+ else: gr_warning(translations["option_not_valid"])
96
+
97
+ def change_download_choices(select):
98
+ selects = [False]*10
99
+
100
+ if select == translations["download_url"]: selects[0] = selects[1] = selects[2] = True
101
+ elif select == translations["download_from_csv"]: selects[3] = selects[4] = True
102
+ elif select == translations["search_models"]: selects[5] = selects[6] = True
103
+ elif select == translations["upload"]: selects[9] = True
104
+ else: gr_warning(translations["option_not_valid"])
105
+
106
+ return [{"visible": selects[i], "__type__": "update"} for i in range(len(selects))]
107
+
108
+ def change_download_pretrained_choices(select):
109
+ selects = [False]*7
110
+
111
+ if select == translations["download_url"]: selects[0] = selects[1] = selects[2] = True
112
+ elif select == translations["list_model"]: selects[3] = selects[4] = selects[5] = True
113
+ elif select == translations["upload"]: selects[6] = True
114
+ else: gr_warning(translations["option_not_valid"])
115
+
116
+ return [{"visible": selects[i], "__type__": "update"} for i in range(len(selects))]
117
+
118
+ def get_index(model):
119
+ model = os.path.basename(model).split("_")[0]
120
+ return {"value": next((f for f in [os.path.join(root, name) for root, _, files in os.walk(configs["logs_path"], topdown=False) for name in files if name.endswith(".index") and "trained" not in name] if model.split(".")[0] in f), ""), "__type__": "update"} if model else None
121
+
122
+ def index_strength_show(index):
123
+ return {"visible": index != "" and index != None and os.path.exists(index) and os.path.isfile(index), "value": 0.5, "__type__": "update"}
124
+
125
+ def hoplength_show(method, hybrid_method=None):
126
+ visible = False
127
+
128
+ for m in ["mangio-crepe", "fcpe", "yin", "piptrack", "mangio-penn"]:
129
+ if m in method: visible = True
130
+ if hybrid_method is not None and m in hybrid_method: visible = True
131
+
132
+ if visible: break
133
+ else: visible = False
134
+
135
+ return {"visible": visible, "__type__": "update"}
136
+
137
+ def visible(value):
138
+ return {"visible": value, "__type__": "update"}
139
+
140
+ def valueFalse_interactive(value):
141
+ return {"value": False, "interactive": value, "__type__": "update"}
142
+
143
+ def valueEmpty_visible1(value):
144
+ return {"value": "", "visible": value, "__type__": "update"}
145
+
146
+ def pitch_guidance_lock(vocoders):
147
+ return {"value": True, "interactive": vocoders == "Default", "__type__": "update"}
148
+
149
+ def vocoders_lock(pitch, vocoders):
150
+ return {"value": vocoders if pitch else "Default", "interactive": pitch, "__type__": "update"}
151
+
152
+ def unlock_f0(value):
153
+ return {"choices": method_f0_full if value else method_f0, "value": "rmvpe", "__type__": "update"}
154
+
155
+ def unlock_vocoder(value, vocoder):
156
+ return {"value": vocoder if value == "v2" else "Default", "interactive": value == "v2", "__type__": "update"}
157
+
158
+ def unlock_ver(value, vocoder):
159
+ return {"value": "v2" if vocoder == "Default" else value, "interactive": vocoder == "Default", "__type__": "update"}
160
+
161
+ def change_embedders_mode(value):
162
+ if value == "spin":
163
+ return {"value": spin_model[0], "choices": spin_model, "__type__": "update"}
164
+ elif value == "whisper":
165
+ return {"value": whisper_model[0], "choices": whisper_model, "__type__": "update"}
166
+ else:
167
+ return {"value": embedders_model[0], "choices": embedders_model, "__type__": "update"}
168
+
169
+ def change_fp(fp):
170
+ fp16 = fp == "fp16"
171
+
172
+ if fp16 and config.device in ["cpu", "mps", "ocl:0"]:
173
+ gr_warning(translations["fp16_not_support"])
174
+ return "fp32"
175
+ else:
176
+ gr_info(translations["start_update_precision"])
177
+
178
+ configs = json.load(open(configs_json, "r"))
179
+ configs["fp16"] = config.is_half = fp16
180
+
181
+ with open(configs_json, "w") as f:
182
+ json.dump(configs, f, indent=4)
183
+
184
+ gr_info(translations["success"])
185
+ return "fp16" if fp16 else "fp32"
186
+
187
+ def process_output(file_path):
188
+ if config.configs.get("delete_exists_file", True):
189
+ if os.path.exists(file_path) and os.path.isfile(file_path): os.remove(file_path)
190
+ return file_path
191
+ else:
192
+ if not os.path.exists(file_path): return file_path
193
+ file = os.path.splitext(os.path.basename(file_path))
194
+
195
+ index = 1
196
+ while 1:
197
+ file_path = os.path.join(os.path.dirname(file_path), f"{file[0]}_{index}{file[1]}")
198
+ if not os.path.exists(file_path): return file_path
199
+ index += 1
200
+
201
+ def shutil_move(input_path, output_path):
202
+ output_path = os.path.join(output_path, os.path.basename(input_path)) if os.path.isdir(output_path) else output_path
203
+
204
+ return shutil.move(input_path, process_output(output_path)) if os.path.exists(output_path) else shutil.move(input_path, output_path)
205
+
206
+ def separate_change(model_name, karaoke_model, reverb_model, enable_post_process, separate_backing, separate_reverb, enable_denoise):
207
+ model_type = "vr" if model_name in list(vr_models.keys()) else "mdx" if model_name in list(mdx_models.keys()) else "demucs" if model_name in list(demucs_models.keys()) else ""
208
+ karaoke_type = ("vr" if karaoke_model.startswith("VR") else "mdx") if separate_backing else None
209
+ reverb_type = ("vr" if not reverb_model.startswith("MDX") else "mdx") if separate_reverb else None
210
+
211
+ all_types = {model_type, karaoke_type, reverb_type}
212
+
213
+ is_vr = "vr" in all_types
214
+ is_mdx = "mdx" in all_types
215
+ is_demucs = "demucs" in all_types
216
+
217
+ return [
218
+ visible(separate_backing),
219
+ visible(separate_reverb),
220
+ visible(is_mdx or is_demucs),
221
+ visible(is_mdx or is_demucs),
222
+ visible(is_mdx),
223
+ visible(is_mdx or is_vr),
224
+ visible(is_demucs),
225
+ visible(is_vr),
226
+ visible(is_vr),
227
+ visible(is_vr and enable_post_process),
228
+ visible(is_vr and enable_denoise),
229
+ valueFalse_interactive(is_vr),
230
+ valueFalse_interactive(is_vr),
231
+ valueFalse_interactive(is_vr)
232
+ ]
233
+
234
+ def create_dataset_change(model_name, reverb_model, enable_post_process, separate_reverb, enable_denoise):
235
+ model_type = "vr" if model_name in list(vr_models.keys()) else "mdx" if model_name in list(mdx_models.keys()) else "demucs" if model_name in list(demucs_models.keys()) else ""
236
+ reverb_type = ("vr" if not reverb_model.startswith("MDX") else "mdx") if separate_reverb else None
237
+ all_types = {model_type, reverb_type}
238
+
239
+ is_vr = "vr" in all_types
240
+ is_mdx = "mdx" in all_types
241
+ is_demucs = "demucs" in all_types
242
+
243
+ return [
244
+ visible(separate_reverb),
245
+ visible(is_mdx or is_demucs),
246
+ visible(is_mdx or is_demucs),
247
+ visible(is_mdx),
248
+ visible(is_mdx or is_vr),
249
+ visible(is_demucs),
250
+ visible(is_vr),
251
+ visible(is_vr),
252
+ visible(is_vr and enable_post_process),
253
+ visible(is_vr and enable_denoise),
254
+ valueFalse_interactive(is_vr),
255
+ valueFalse_interactive(is_vr),
256
+ valueFalse_interactive(is_vr)
257
+ ]
258
+
259
+ def audio_device():
260
+ try:
261
+ input_devices, output_devices = list_audio_device()
262
+
263
+ def priority(name):
264
+ n = name.lower()
265
+ if "virtual" in n:
266
+ return 0
267
+ if "vb" in n:
268
+ return 1
269
+ return 2
270
+
271
+ output_sorted = sorted(output_devices, key=lambda d: priority(d.name))
272
+ input_sorted = sorted(
273
+ input_devices, key=lambda d: priority(d.name), reverse=True
274
+ )
275
+
276
+ input_device_list = {
277
+ f"{input_sorted.index(d)+1}: {d.name} ({d.host_api})": [d.index, d.max_input_channels] for d in input_sorted
278
+ }
279
+ output_device_list = {
280
+ f"{output_sorted.index(d)+1}: {d.name} ({d.host_api})": [d.index, d.max_output_channels] for d in output_sorted
281
+ }
282
+
283
+ return input_device_list, output_device_list
284
+ except Exception:
285
+ return [], []
286
+
287
+ def update_audio_device(input_device, output_device, monitor_device, monitor):
288
+ input_channels_map, output_channels_map = audio_device()
289
+
290
+ input_is_asio = "ASIO" in input_device if input_device else False
291
+ output_is_asio = "ASIO" in output_device if output_device else False
292
+ monitor_is_asio = "ASIO" in monitor_device if monitor_device else False
293
+
294
+ try:
295
+ input_max_ch = input_channels_map.get(input_device, [])[1]
296
+ output_max_ch = output_channels_map.get(output_device, [])[1]
297
+ monitor_max_ch = output_channels_map.get(monitor_device, [])[1] if monitor else 128
298
+ except:
299
+ input_max_ch = output_max_ch = monitor_max_ch = -1
300
+
301
+ return [
302
+ visible(monitor),
303
+ visible(monitor),
304
+ visible(monitor_is_asio),
305
+ visible(input_is_asio or output_is_asio or monitor_is_asio),
306
+ gr.update(visible=input_is_asio, maximum=input_max_ch),
307
+ gr.update(visible=output_is_asio, maximum=output_max_ch),
308
+ gr.update(visible=monitor_is_asio, maximum=monitor_max_ch)
309
+ ]
310
+
311
+ def change_audio_device_choices():
312
+ sd._terminate()
313
+ sd._initialize()
314
+
315
+ input_channels_map, output_channels_map = audio_device()
316
+ input_channels_map, output_channels_map = list(input_channels_map.keys()), list(output_channels_map.keys())
317
+
318
+ return [
319
+ {"value": input_channels_map[0] if len(input_channels_map) >= 1 else "", "choices": input_channels_map, "__type__": "update"},
320
+ {"value": output_channels_map[0] if len(output_channels_map) >= 1 else "", "choices": output_channels_map, "__type__": "update"},
321
+ {"value": output_channels_map[0] if len(output_channels_map) >= 1 else "", "choices": output_channels_map, "__type__": "update"}
322
+ ]
323
+
324
+ def replace_punctuation(filename):
325
+ return filename.replace(" ", "_").replace("-", "").replace("(", "").replace(")", "").replace("[", "").replace("]", "").replace(",", "").replace('"', "").replace("'", "").replace("|", "_").replace("{", "").replace("}", "").replace("-_-", "_").replace("_-_", "_").replace("-", "_").replace("---", "_").replace("___", "_").strip()
326
+
327
+ def replace_url(url):
328
+ return url.replace("/blob/", "/resolve/").replace("?download=true", "").strip()
329
+
330
+ def replace_modelname(modelname):
331
+ return replace_punctuation(modelname.replace(".onnx", "").replace(".pth", "").replace(".index", "").replace(".zip", ""))
332
+
333
+ def replace_export_format(audio_path, export_format = "wav"):
334
+ export_format = f".{export_format}"
335
+
336
+ return audio_path if audio_path.endswith(export_format) else audio_path.replace(f".{os.path.basename(audio_path).split('.')[-1]}", export_format)
337
+
338
+ def update_dropdowns_from_json(data):
339
+ if not data:
340
+ return [
341
+ gr.update(choices=[], value=None),
342
+ gr.update(choices=[], value=None),
343
+ gr.update(choices=[], value=None)
344
+ ]
345
+
346
+ inputs = list(data.get("inputs", {}).keys())
347
+ outputs = list(data.get("outputs", {}).keys())
348
+
349
+ return [
350
+ gr.update(choices=inputs, value=inputs[0] if len(inputs) > 0 else None),
351
+ gr.update(choices=outputs, value=outputs[0] if len(outputs) > 0 else None),
352
+ gr.update(choices=outputs, value=outputs[0] if len(outputs) > 0 else None),
353
+ ]
354
+
355
+ def update_button_from_json(data):
356
+ if not data:
357
+ return [gr.update(interactive=True), gr.update(interactive=False)]
358
+
359
+ return [
360
+ gr.update(interactive=data.get("start_button", True)),
361
+ gr.update(interactive=data.get("stop_button", False))
362
  ]