Update demo.py
Browse files
demo.py
CHANGED
|
@@ -16,12 +16,12 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 16 |
with gr.Tabs():
|
| 17 |
with gr.Tab("Inference"):
|
| 18 |
with gr.Row():
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
voice_model = gr.Dropdown(
|
| 22 |
label="Model Voice",
|
| 23 |
-
choices=
|
| 24 |
-
value=
|
| 25 |
interactive=True
|
| 26 |
)
|
| 27 |
refresh_button = gr.Button("Refresh", variant="primary")
|
|
@@ -48,7 +48,7 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 48 |
with gr.Row():
|
| 49 |
input_audio0 = gr.Dropdown(
|
| 50 |
label="Input Path",
|
| 51 |
-
value=
|
| 52 |
choices=[],
|
| 53 |
allow_custom_value=True
|
| 54 |
)
|
|
@@ -69,7 +69,7 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 69 |
def handle_record(audio):
|
| 70 |
if audio:
|
| 71 |
return audio
|
| 72 |
-
return
|
| 73 |
|
| 74 |
record_button.change(
|
| 75 |
fn=handle_record,
|
|
@@ -80,7 +80,7 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 80 |
def handle_upload(audio):
|
| 81 |
if audio:
|
| 82 |
return audio
|
| 83 |
-
return
|
| 84 |
|
| 85 |
dropbox.change(
|
| 86 |
fn=handle_upload,
|
|
@@ -94,7 +94,7 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 94 |
label="Change Index",
|
| 95 |
choices=[],
|
| 96 |
interactive=True,
|
| 97 |
-
value=
|
| 98 |
)
|
| 99 |
index_rate1 = gr.Slider(
|
| 100 |
minimum=0,
|
|
@@ -154,31 +154,30 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 154 |
|
| 155 |
# Consolidated refresh logic
|
| 156 |
def refresh_ui():
|
| 157 |
-
# Get updated lists
|
| 158 |
try:
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
print(f"Error in change_choices: {e}")
|
| 162 |
model_choices = []
|
| 163 |
index_choices = []
|
| 164 |
|
| 165 |
-
# Ensure model_choices and index_choices are lists
|
| 166 |
-
if not isinstance(model_choices, list):
|
| 167 |
-
model_choices = []
|
| 168 |
-
if not isinstance(index_choices, list):
|
| 169 |
-
index_choices = []
|
| 170 |
-
|
| 171 |
audio_paths = get_audio_paths('audios')
|
| 172 |
|
| 173 |
-
# Get current values
|
| 174 |
-
current_model = voice_model.value
|
| 175 |
-
current_index = file_index2.value
|
| 176 |
-
current_audio = input_audio0.value
|
| 177 |
|
| 178 |
-
# Set defaults
|
| 179 |
-
default_model = current_model if current_model in model_choices
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
return (
|
| 184 |
gr.update(choices=model_choices, value=default_model), # voice_model
|
|
@@ -424,13 +423,20 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 424 |
interactive=True,
|
| 425 |
)
|
| 426 |
with gr.Accordion(label="Change pretrains", open=False):
|
| 427 |
-
def get_pretrained_choices(sr,
|
| 428 |
-
|
| 429 |
-
if
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
pretrained_G14 = gr.Dropdown(
|
| 436 |
label="pretrained G",
|
|
@@ -447,17 +453,18 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 447 |
interactive=True
|
| 448 |
)
|
| 449 |
|
| 450 |
-
def update_pretrained_dropdowns(sr,
|
| 451 |
sr_str = sr if isinstance(sr, str) else str(sr)
|
| 452 |
-
g_choices = get_pretrained_choices(sr_str,
|
| 453 |
-
d_choices = get_pretrained_choices(sr_str, 'D')
|
| 454 |
return (
|
| 455 |
gr.update(choices=g_choices, value=g_choices[0] if g_choices else ""),
|
| 456 |
gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
|
| 457 |
)
|
| 458 |
|
|
|
|
| 459 |
sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
|
| 460 |
version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
|
|
|
|
| 461 |
|
| 462 |
with gr.Row():
|
| 463 |
download_model = gr.Button('5.Download Model')
|
|
@@ -468,12 +475,12 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 468 |
if not name or name.strip() == "":
|
| 469 |
return [], "Please enter a model name"
|
| 470 |
|
| 471 |
-
model_path = f'
|
| 472 |
-
index_pattern = f'logs/{name
|
| 473 |
|
| 474 |
files = []
|
| 475 |
if os.path.exists(model_path):
|
| 476 |
-
files.extend([os.path.join(model_path, f) for f in os.listdir(model_path)])
|
| 477 |
files.extend(glob.glob(index_pattern))
|
| 478 |
|
| 479 |
return files, f"Found {len(files)} files"
|
|
@@ -485,9 +492,9 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 485 |
)
|
| 486 |
|
| 487 |
if_f0_3.change(
|
| 488 |
-
change_f0,
|
| 489 |
-
[if_f0_3, sr2, version19],
|
| 490 |
-
[f0method8, pretrained_G14, pretrained_D15],
|
| 491 |
)
|
| 492 |
|
| 493 |
but5 = gr.Button("1 Click Training", variant="primary", visible=False)
|
|
@@ -540,8 +547,23 @@ with gr.Blocks(title="π", theme=gr.themes.Base(primary_hue="blue", neutral_hu
|
|
| 540 |
)
|
| 541 |
|
| 542 |
# Populate UI on load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
app.load(
|
| 544 |
-
fn=
|
| 545 |
inputs=[],
|
| 546 |
outputs=[voice_model, file_index2, input_audio0]
|
| 547 |
)
|
|
|
|
| 16 |
with gr.Tabs():
|
| 17 |
with gr.Tab("Inference"):
|
| 18 |
with gr.Row():
|
| 19 |
+
# Get initial model choices from original.py
|
| 20 |
+
initial_model_choices = sorted(names) if names else []
|
| 21 |
voice_model = gr.Dropdown(
|
| 22 |
label="Model Voice",
|
| 23 |
+
choices=initial_model_choices,
|
| 24 |
+
value=initial_model_choices[0] if initial_model_choices else None,
|
| 25 |
interactive=True
|
| 26 |
)
|
| 27 |
refresh_button = gr.Button("Refresh", variant="primary")
|
|
|
|
| 48 |
with gr.Row():
|
| 49 |
input_audio0 = gr.Dropdown(
|
| 50 |
label="Input Path",
|
| 51 |
+
value=None,
|
| 52 |
choices=[],
|
| 53 |
allow_custom_value=True
|
| 54 |
)
|
|
|
|
| 69 |
def handle_record(audio):
|
| 70 |
if audio:
|
| 71 |
return audio
|
| 72 |
+
return None
|
| 73 |
|
| 74 |
record_button.change(
|
| 75 |
fn=handle_record,
|
|
|
|
| 80 |
def handle_upload(audio):
|
| 81 |
if audio:
|
| 82 |
return audio
|
| 83 |
+
return None
|
| 84 |
|
| 85 |
dropbox.change(
|
| 86 |
fn=handle_upload,
|
|
|
|
| 94 |
label="Change Index",
|
| 95 |
choices=[],
|
| 96 |
interactive=True,
|
| 97 |
+
value=None
|
| 98 |
)
|
| 99 |
index_rate1 = gr.Slider(
|
| 100 |
minimum=0,
|
|
|
|
| 154 |
|
| 155 |
# Consolidated refresh logic
|
| 156 |
def refresh_ui():
|
| 157 |
+
# Get updated lists from change_choices which returns dictionaries
|
| 158 |
try:
|
| 159 |
+
model_result, index_result = change_choices()
|
| 160 |
+
model_choices = model_result["choices"]
|
| 161 |
+
index_choices = index_result["choices"]
|
| 162 |
except Exception as e:
|
| 163 |
print(f"Error in change_choices: {e}")
|
| 164 |
model_choices = []
|
| 165 |
index_choices = []
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
audio_paths = get_audio_paths('audios')
|
| 168 |
|
| 169 |
+
# Get current values to preserve selection when possible
|
| 170 |
+
current_model = voice_model.value
|
| 171 |
+
current_index = file_index2.value
|
| 172 |
+
current_audio = input_audio0.value
|
| 173 |
|
| 174 |
+
# Set defaults with fallback logic
|
| 175 |
+
default_model = (current_model if current_model in model_choices
|
| 176 |
+
else (model_choices[0] if model_choices else None))
|
| 177 |
+
default_index = (current_index if current_index in index_choices
|
| 178 |
+
else (index_choices[0] if index_choices else None))
|
| 179 |
+
default_audio = (current_audio if current_audio in audio_paths
|
| 180 |
+
else (audio_paths[0] if audio_paths else None))
|
| 181 |
|
| 182 |
return (
|
| 183 |
gr.update(choices=model_choices, value=default_model), # voice_model
|
|
|
|
| 423 |
interactive=True,
|
| 424 |
)
|
| 425 |
with gr.Accordion(label="Change pretrains", open=False):
|
| 426 |
+
def get_pretrained_choices(sr, if_f0, version):
|
| 427 |
+
# Use the original functions from original.py
|
| 428 |
+
if version == "v1":
|
| 429 |
+
path_str = ""
|
| 430 |
+
else:
|
| 431 |
+
path_str = "_v2"
|
| 432 |
+
|
| 433 |
+
if if_f0:
|
| 434 |
+
f0_str = "f0"
|
| 435 |
+
else:
|
| 436 |
+
f0_str = ""
|
| 437 |
+
|
| 438 |
+
pretrained_G, pretrained_D = get_pretrained_models(path_str, f0_str, sr)
|
| 439 |
+
return [pretrained_G] if pretrained_G else [], [pretrained_D] if pretrained_D else []
|
| 440 |
|
| 441 |
pretrained_G14 = gr.Dropdown(
|
| 442 |
label="pretrained G",
|
|
|
|
| 453 |
interactive=True
|
| 454 |
)
|
| 455 |
|
| 456 |
+
def update_pretrained_dropdowns(sr, if_f0, ver):
|
| 457 |
sr_str = sr if isinstance(sr, str) else str(sr)
|
| 458 |
+
g_choices, d_choices = get_pretrained_choices(sr_str, if_f0, ver)
|
|
|
|
| 459 |
return (
|
| 460 |
gr.update(choices=g_choices, value=g_choices[0] if g_choices else ""),
|
| 461 |
gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
|
| 462 |
)
|
| 463 |
|
| 464 |
+
# Bind update function to changes
|
| 465 |
sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
|
| 466 |
version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
|
| 467 |
+
if_f0_3.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
|
| 468 |
|
| 469 |
with gr.Row():
|
| 470 |
download_model = gr.Button('5.Download Model')
|
|
|
|
| 475 |
if not name or name.strip() == "":
|
| 476 |
return [], "Please enter a model name"
|
| 477 |
|
| 478 |
+
model_path = f'logs/{name}'
|
| 479 |
+
index_pattern = f'logs/{name}/added_*.index'
|
| 480 |
|
| 481 |
files = []
|
| 482 |
if os.path.exists(model_path):
|
| 483 |
+
files.extend([os.path.join(model_path, f) for f in os.listdir(model_path) if f.endswith('.pth')])
|
| 484 |
files.extend(glob.glob(index_pattern))
|
| 485 |
|
| 486 |
return files, f"Found {len(files)} files"
|
|
|
|
| 492 |
)
|
| 493 |
|
| 494 |
if_f0_3.change(
|
| 495 |
+
fn=change_f0,
|
| 496 |
+
inputs=[if_f0_3, sr2, version19],
|
| 497 |
+
outputs=[f0method8, pretrained_G14, pretrained_D15],
|
| 498 |
)
|
| 499 |
|
| 500 |
but5 = gr.Button("1 Click Training", variant="primary", visible=False)
|
|
|
|
| 547 |
)
|
| 548 |
|
| 549 |
# Populate UI on load
|
| 550 |
+
def on_load():
|
| 551 |
+
# Initial refresh
|
| 552 |
+
model_result, index_result = change_choices()
|
| 553 |
+
audio_paths = get_audio_paths('audios')
|
| 554 |
+
|
| 555 |
+
default_model = model_result["choices"][0] if model_result["choices"] else None
|
| 556 |
+
default_index = index_result["choices"][0] if index_result["choices"] else None
|
| 557 |
+
default_audio = audio_paths[0] if audio_paths else None
|
| 558 |
+
|
| 559 |
+
return (
|
| 560 |
+
gr.update(choices=model_result["choices"], value=default_model), # voice_model
|
| 561 |
+
gr.update(choices=index_result["choices"], value=default_index), # file_index2
|
| 562 |
+
gr.update(choices=audio_paths, value=default_audio) # input_audio0
|
| 563 |
+
)
|
| 564 |
+
|
| 565 |
app.load(
|
| 566 |
+
fn=on_load,
|
| 567 |
inputs=[],
|
| 568 |
outputs=[voice_model, file_index2, input_audio0]
|
| 569 |
)
|