Update infer_gradio.py. Enable seed selecting for multistyle generation
Browse files- src/f5_tts/infer/infer_gradio.py +161 -93
src/f5_tts/infer/infer_gradio.py
CHANGED
@@ -129,7 +129,7 @@ def infer(
|
|
129 |
gen_text,
|
130 |
model,
|
131 |
remove_silence,
|
132 |
-
seed,
|
133 |
cross_fade_duration=0.15,
|
134 |
nfe_step=32,
|
135 |
speed=1,
|
@@ -140,7 +140,13 @@ def infer(
|
|
140 |
return gr.update(), gr.update(), ref_text
|
141 |
|
142 |
# Set inference seed
|
|
|
|
|
|
|
|
|
|
|
143 |
torch.manual_seed(seed)
|
|
|
144 |
|
145 |
if not gen_text.strip():
|
146 |
gr.Warning("Please enter text to generate or upload a text file.")
|
@@ -191,7 +197,7 @@ def infer(
|
|
191 |
spectrogram_path = tmp_spectrogram.name
|
192 |
save_spectrogram(combined_spectrogram, spectrogram_path)
|
193 |
|
194 |
-
return (final_sample_rate, final_wave), spectrogram_path, ref_text
|
195 |
|
196 |
|
197 |
with gr.Blocks() as app_credits:
|
@@ -277,27 +283,21 @@ with gr.Blocks() as app_tts:
|
|
277 |
nfe_slider,
|
278 |
speed_slider,
|
279 |
):
|
280 |
-
# Determine the seed to use
|
281 |
if randomize_seed:
|
282 |
-
|
283 |
-
else:
|
284 |
-
seed = seed_input
|
285 |
-
if seed < 0 or seed > 2**31 - 1:
|
286 |
-
gr.Warning("Seed must in range 0 ~ 2147483647. Using random seed instead.")
|
287 |
-
seed = np.random.randint(0, 2**31 - 1)
|
288 |
|
289 |
-
audio_out, spectrogram_path, ref_text_out = infer(
|
290 |
ref_audio_input,
|
291 |
ref_text_input,
|
292 |
gen_text_input,
|
293 |
tts_model_choice,
|
294 |
remove_silence,
|
295 |
-
seed=
|
296 |
cross_fade_duration=cross_fade_duration_slider,
|
297 |
nfe_step=nfe_slider,
|
298 |
speed=speed_slider,
|
299 |
)
|
300 |
-
return audio_out, spectrogram_path, ref_text_out,
|
301 |
|
302 |
gen_text_file.upload(
|
303 |
load_text_from_file,
|
@@ -329,26 +329,34 @@ with gr.Blocks() as app_tts:
|
|
329 |
|
330 |
|
331 |
def parse_speechtypes_text(gen_text):
|
332 |
-
# Pattern to find {
|
333 |
-
pattern = r"\{
|
334 |
|
335 |
# Split the text by the pattern
|
336 |
tokens = re.split(pattern, gen_text)
|
337 |
|
338 |
segments = []
|
339 |
|
340 |
-
|
|
|
|
|
|
|
|
|
341 |
|
342 |
for i in range(len(tokens)):
|
343 |
if i % 2 == 0:
|
344 |
# This is text
|
345 |
text = tokens[i].strip()
|
346 |
if text:
|
347 |
-
|
|
|
348 |
else:
|
349 |
-
# This is
|
350 |
-
|
351 |
-
|
|
|
|
|
|
|
352 |
|
353 |
return segments
|
354 |
|
@@ -366,41 +374,48 @@ with gr.Blocks() as app_multistyle:
|
|
366 |
with gr.Row():
|
367 |
gr.Markdown(
|
368 |
"""
|
369 |
-
**Example Input:**
|
370 |
-
{Regular} Hello, I'd like to order a sandwich please.
|
371 |
-
{Surprised} What do you mean you're out of bread?
|
372 |
-
{Sad} I really wanted a sandwich though...
|
373 |
-
{Angry} You know what, darn you and your little shop!
|
374 |
-
{Whisper} I'll just go back home and cry now.
|
375 |
{Shouting} Why me?!
|
376 |
"""
|
377 |
)
|
378 |
|
379 |
gr.Markdown(
|
380 |
"""
|
381 |
-
**Example Input 2:**
|
382 |
-
{Speaker1_Happy} Hello, I'd like to order a sandwich please.
|
383 |
-
{Speaker2_Regular} Sorry, we're out of bread.
|
384 |
-
{Speaker1_Sad} I really wanted a sandwich though...
|
385 |
-
{Speaker2_Whisper} I'll give you the last one I was hiding.
|
386 |
"""
|
387 |
)
|
388 |
|
389 |
gr.Markdown(
|
390 |
-
|
391 |
)
|
392 |
|
393 |
# Regular speech type (mandatory)
|
394 |
-
with gr.Row() as regular_row:
|
395 |
with gr.Column(scale=1, min_width=160):
|
396 |
regular_name = gr.Textbox(value="Regular", label="Speech Type Name")
|
397 |
regular_insert = gr.Button("Insert Label", variant="secondary")
|
398 |
with gr.Column(scale=3):
|
399 |
regular_audio = gr.Audio(label="Regular Reference Audio", type="filepath")
|
400 |
with gr.Column(scale=3):
|
401 |
-
regular_ref_text = gr.Textbox(label="Reference Text (Regular)", lines=
|
402 |
-
|
403 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
404 |
|
405 |
# Regular speech type (max 100)
|
406 |
max_speech_types = 100
|
@@ -409,32 +424,54 @@ with gr.Blocks() as app_multistyle:
|
|
409 |
speech_type_audios = [regular_audio]
|
410 |
speech_type_ref_texts = [regular_ref_text]
|
411 |
speech_type_ref_text_files = [regular_ref_text_file]
|
|
|
|
|
412 |
speech_type_delete_btns = [None]
|
413 |
speech_type_insert_btns = [regular_insert]
|
414 |
|
415 |
# Additional speech types (99 more)
|
416 |
for i in range(max_speech_types - 1):
|
417 |
-
with gr.Row(visible=False) as row:
|
418 |
with gr.Column(scale=1, min_width=160):
|
419 |
name_input = gr.Textbox(label="Speech Type Name")
|
420 |
-
delete_btn = gr.Button("Delete Type", variant="secondary")
|
421 |
insert_btn = gr.Button("Insert Label", variant="secondary")
|
|
|
422 |
with gr.Column(scale=3):
|
423 |
audio_input = gr.Audio(label="Reference Audio", type="filepath")
|
424 |
with gr.Column(scale=3):
|
425 |
-
ref_text_input = gr.Textbox(label="Reference Text", lines=
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
|
|
|
|
|
|
|
|
|
|
430 |
speech_type_rows.append(row)
|
431 |
speech_type_names.append(name_input)
|
432 |
speech_type_audios.append(audio_input)
|
433 |
speech_type_ref_texts.append(ref_text_input)
|
434 |
speech_type_ref_text_files.append(ref_text_file_input)
|
|
|
|
|
435 |
speech_type_delete_btns.append(delete_btn)
|
436 |
speech_type_insert_btns.append(insert_btn)
|
437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
# Button to add speech type
|
439 |
add_speech_type_btn = gr.Button("Add Speech Type")
|
440 |
|
@@ -470,18 +507,6 @@ with gr.Blocks() as app_multistyle:
|
|
470 |
speech_type_ref_text_files[i],
|
471 |
],
|
472 |
)
|
473 |
-
speech_type_ref_text_files[i].upload(
|
474 |
-
load_text_from_file,
|
475 |
-
inputs=[speech_type_ref_text_files[i]],
|
476 |
-
outputs=[speech_type_ref_texts[i]],
|
477 |
-
)
|
478 |
-
|
479 |
-
# Update regular speech type ref text file
|
480 |
-
regular_ref_text_file.upload(
|
481 |
-
load_text_from_file,
|
482 |
-
inputs=[regular_ref_text_file],
|
483 |
-
outputs=[regular_ref_text],
|
484 |
-
)
|
485 |
|
486 |
# Text input for the prompt
|
487 |
with gr.Row():
|
@@ -495,10 +520,17 @@ with gr.Blocks() as app_multistyle:
|
|
495 |
gen_text_file_multistyle = gr.File(label="Load Text to Generate from File (.txt)", file_types=[".txt"], scale=1)
|
496 |
|
497 |
def make_insert_speech_type_fn(index):
|
498 |
-
def insert_speech_type_fn(current_text, speech_type_name):
|
499 |
current_text = current_text or ""
|
500 |
-
|
501 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
502 |
return updated_text
|
503 |
|
504 |
return insert_speech_type_fn
|
@@ -507,16 +539,24 @@ with gr.Blocks() as app_multistyle:
|
|
507 |
insert_fn = make_insert_speech_type_fn(i)
|
508 |
insert_btn.click(
|
509 |
insert_fn,
|
510 |
-
inputs=[gen_text_input_multistyle, speech_type_names[i]],
|
511 |
outputs=gen_text_input_multistyle,
|
512 |
)
|
513 |
|
514 |
-
with gr.Accordion("Advanced Settings", open=
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
520 |
|
521 |
# Generate button
|
522 |
generate_multistyle_btn = gr.Button("Generate Multi-Style Speech", variant="primary")
|
@@ -524,6 +564,24 @@ with gr.Blocks() as app_multistyle:
|
|
524 |
# Output audio
|
525 |
audio_output_multistyle = gr.Audio(label="Synthesized Audio")
|
526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
gen_text_file_multistyle.upload(
|
528 |
load_text_from_file,
|
529 |
inputs=[gen_text_file_multistyle],
|
@@ -557,44 +615,60 @@ with gr.Blocks() as app_multistyle:
|
|
557 |
|
558 |
# For each segment, generate speech
|
559 |
generated_audio_segments = []
|
560 |
-
|
|
|
561 |
|
562 |
for segment in segments:
|
563 |
-
|
|
|
|
|
564 |
text = segment["text"]
|
565 |
|
566 |
-
if
|
567 |
-
|
568 |
else:
|
569 |
-
gr.Warning(f"Type {
|
570 |
-
|
571 |
|
572 |
try:
|
573 |
-
ref_audio = speech_types[
|
574 |
except KeyError:
|
575 |
-
gr.Warning(f"Please provide reference audio for type {
|
576 |
-
return [None] + [speech_types[
|
577 |
-
ref_text = speech_types[
|
578 |
|
579 |
-
|
580 |
-
|
581 |
|
582 |
# Generate speech for this segment
|
583 |
-
audio_out, _, ref_text_out = infer(
|
584 |
-
ref_audio,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
585 |
) # show_info=print no pull to top when generating
|
586 |
sr, audio_data = audio_out
|
587 |
|
588 |
generated_audio_segments.append(audio_data)
|
589 |
-
speech_types[
|
|
|
590 |
|
591 |
# Concatenate all audio segments
|
592 |
if generated_audio_segments:
|
593 |
final_audio_data = np.concatenate(generated_audio_segments)
|
594 |
-
return
|
|
|
|
|
|
|
|
|
595 |
else:
|
596 |
gr.Warning("No audio generated.")
|
597 |
-
return [None] + [speech_types[
|
598 |
|
599 |
generate_multistyle_btn.click(
|
600 |
generate_multistyle_speech,
|
@@ -607,7 +681,7 @@ with gr.Blocks() as app_multistyle:
|
|
607 |
+ [
|
608 |
remove_silence_multistyle,
|
609 |
],
|
610 |
-
outputs=[audio_output_multistyle] + speech_type_ref_texts,
|
611 |
)
|
612 |
|
613 |
# Validation function to disable Generate button if speech types are missing
|
@@ -624,7 +698,7 @@ with gr.Blocks() as app_multistyle:
|
|
624 |
|
625 |
# Parse the gen_text to get the speech types used
|
626 |
segments = parse_speechtypes_text(gen_text)
|
627 |
-
speech_types_in_text = set(segment["
|
628 |
|
629 |
# Check if all speech types in text are available
|
630 |
missing_speech_types = speech_types_in_text - speech_types_available
|
@@ -788,27 +862,21 @@ Have a conversation with an AI using your reference voice!
|
|
788 |
if not last_ai_response or conv_state[-1]["role"] != "assistant":
|
789 |
return None, ref_text, seed_input
|
790 |
|
791 |
-
# Determine the seed to use
|
792 |
if randomize_seed:
|
793 |
-
|
794 |
-
else:
|
795 |
-
seed = seed_input
|
796 |
-
if seed < 0 or seed > 2**31 - 1:
|
797 |
-
gr.Warning("Seed must in range 0 ~ 2147483647. Using random seed instead.")
|
798 |
-
seed = np.random.randint(0, 2**31 - 1)
|
799 |
|
800 |
-
audio_result, _, ref_text_out = infer(
|
801 |
ref_audio,
|
802 |
ref_text,
|
803 |
last_ai_response,
|
804 |
tts_model_choice,
|
805 |
remove_silence,
|
806 |
-
seed=
|
807 |
cross_fade_duration=0.15,
|
808 |
speed=1.0,
|
809 |
show_info=print, # show_info=print no pull to top when generating
|
810 |
)
|
811 |
-
return audio_result, ref_text_out,
|
812 |
|
813 |
def clear_conversation():
|
814 |
"""Reset the conversation"""
|
|
|
129 |
gen_text,
|
130 |
model,
|
131 |
remove_silence,
|
132 |
+
seed=None,
|
133 |
cross_fade_duration=0.15,
|
134 |
nfe_step=32,
|
135 |
speed=1,
|
|
|
140 |
return gr.update(), gr.update(), ref_text
|
141 |
|
142 |
# Set inference seed
|
143 |
+
if seed is None:
|
144 |
+
seed = np.random.randint(0, 2**31 - 1)
|
145 |
+
elif seed < 0 or seed > 2**31 - 1:
|
146 |
+
gr.Warning("Seed must in range 0 ~ 2147483647. Using random seed instead.")
|
147 |
+
seed = np.random.randint(0, 2**31 - 1)
|
148 |
torch.manual_seed(seed)
|
149 |
+
used_seed = seed
|
150 |
|
151 |
if not gen_text.strip():
|
152 |
gr.Warning("Please enter text to generate or upload a text file.")
|
|
|
197 |
spectrogram_path = tmp_spectrogram.name
|
198 |
save_spectrogram(combined_spectrogram, spectrogram_path)
|
199 |
|
200 |
+
return (final_sample_rate, final_wave), spectrogram_path, ref_text, used_seed
|
201 |
|
202 |
|
203 |
with gr.Blocks() as app_credits:
|
|
|
283 |
nfe_slider,
|
284 |
speed_slider,
|
285 |
):
|
|
|
286 |
if randomize_seed:
|
287 |
+
seed_input = None
|
|
|
|
|
|
|
|
|
|
|
288 |
|
289 |
+
audio_out, spectrogram_path, ref_text_out, used_seed = infer(
|
290 |
ref_audio_input,
|
291 |
ref_text_input,
|
292 |
gen_text_input,
|
293 |
tts_model_choice,
|
294 |
remove_silence,
|
295 |
+
seed=seed_input,
|
296 |
cross_fade_duration=cross_fade_duration_slider,
|
297 |
nfe_step=nfe_slider,
|
298 |
speed=speed_slider,
|
299 |
)
|
300 |
+
return audio_out, spectrogram_path, ref_text_out, used_seed
|
301 |
|
302 |
gen_text_file.upload(
|
303 |
load_text_from_file,
|
|
|
329 |
|
330 |
|
331 |
def parse_speechtypes_text(gen_text):
|
332 |
+
# Pattern to find {str} or {"name": str, "seed": int, "speed": float}
|
333 |
+
pattern = r"(\{.*?\})"
|
334 |
|
335 |
# Split the text by the pattern
|
336 |
tokens = re.split(pattern, gen_text)
|
337 |
|
338 |
segments = []
|
339 |
|
340 |
+
current_type_dict = {
|
341 |
+
"name": "Regular",
|
342 |
+
"seed": -1,
|
343 |
+
"speed": 1.0,
|
344 |
+
}
|
345 |
|
346 |
for i in range(len(tokens)):
|
347 |
if i % 2 == 0:
|
348 |
# This is text
|
349 |
text = tokens[i].strip()
|
350 |
if text:
|
351 |
+
current_type_dict["text"] = text
|
352 |
+
segments.append(current_type_dict)
|
353 |
else:
|
354 |
+
# This is type
|
355 |
+
type_str = tokens[i].strip()
|
356 |
+
try: # if type dict
|
357 |
+
current_type_dict = json.loads(type_str)
|
358 |
+
except json.decoder.JSONDecodeError:
|
359 |
+
current_type_dict = {"name": type_str, "seed": -1, "speed": 1.0}
|
360 |
|
361 |
return segments
|
362 |
|
|
|
374 |
with gr.Row():
|
375 |
gr.Markdown(
|
376 |
"""
|
377 |
+
**Example Input:** <br>
|
378 |
+
{Regular} Hello, I'd like to order a sandwich please. <br>
|
379 |
+
{Surprised} What do you mean you're out of bread? <br>
|
380 |
+
{Sad} I really wanted a sandwich though... <br>
|
381 |
+
{Angry} You know what, darn you and your little shop! <br>
|
382 |
+
{Whisper} I'll just go back home and cry now. <br>
|
383 |
{Shouting} Why me?!
|
384 |
"""
|
385 |
)
|
386 |
|
387 |
gr.Markdown(
|
388 |
"""
|
389 |
+
**Example Input 2:** <br>
|
390 |
+
{"name": "Speaker1_Happy", "seed": -1, "speed": 1} Hello, I'd like to order a sandwich please. <br>
|
391 |
+
{"name": "Speaker2_Regular", "seed": -1, "speed": 1} Sorry, we're out of bread. <br>
|
392 |
+
{"name": "Speaker1_Sad", "seed": -1, "speed": 1} I really wanted a sandwich though... <br>
|
393 |
+
{"name": "Speaker2_Whisper", "seed": -1, "speed": 1} I'll give you the last one I was hiding.
|
394 |
"""
|
395 |
)
|
396 |
|
397 |
gr.Markdown(
|
398 |
+
'Upload different audio clips for each speech type. The first speech type is mandatory. You can add additional speech types by clicking the "Add Speech Type" button.'
|
399 |
)
|
400 |
|
401 |
# Regular speech type (mandatory)
|
402 |
+
with gr.Row(variant="compact") as regular_row:
|
403 |
with gr.Column(scale=1, min_width=160):
|
404 |
regular_name = gr.Textbox(value="Regular", label="Speech Type Name")
|
405 |
regular_insert = gr.Button("Insert Label", variant="secondary")
|
406 |
with gr.Column(scale=3):
|
407 |
regular_audio = gr.Audio(label="Regular Reference Audio", type="filepath")
|
408 |
with gr.Column(scale=3):
|
409 |
+
regular_ref_text = gr.Textbox(label="Reference Text (Regular)", lines=4)
|
410 |
+
with gr.Row():
|
411 |
+
regular_seed_slider = gr.Slider(
|
412 |
+
show_label=False, minimum=-1, maximum=999, value=-1, step=1, info="Seed, -1 for random"
|
413 |
+
)
|
414 |
+
regular_speed_slider = gr.Slider(
|
415 |
+
show_label=False, minimum=0.3, maximum=2.0, value=1.0, step=0.1, info="Adjust the speed"
|
416 |
+
)
|
417 |
+
with gr.Column(scale=1, min_width=160):
|
418 |
+
regular_ref_text_file = gr.File(label="Load Reference Text from File (.txt)", file_types=[".txt"])
|
419 |
|
420 |
# Regular speech type (max 100)
|
421 |
max_speech_types = 100
|
|
|
424 |
speech_type_audios = [regular_audio]
|
425 |
speech_type_ref_texts = [regular_ref_text]
|
426 |
speech_type_ref_text_files = [regular_ref_text_file]
|
427 |
+
speech_type_seeds = [regular_seed_slider]
|
428 |
+
speech_type_speeds = [regular_speed_slider]
|
429 |
speech_type_delete_btns = [None]
|
430 |
speech_type_insert_btns = [regular_insert]
|
431 |
|
432 |
# Additional speech types (99 more)
|
433 |
for i in range(max_speech_types - 1):
|
434 |
+
with gr.Row(variant="compact", visible=False) as row:
|
435 |
with gr.Column(scale=1, min_width=160):
|
436 |
name_input = gr.Textbox(label="Speech Type Name")
|
|
|
437 |
insert_btn = gr.Button("Insert Label", variant="secondary")
|
438 |
+
delete_btn = gr.Button("Delete Type", variant="stop")
|
439 |
with gr.Column(scale=3):
|
440 |
audio_input = gr.Audio(label="Reference Audio", type="filepath")
|
441 |
with gr.Column(scale=3):
|
442 |
+
ref_text_input = gr.Textbox(label="Reference Text", lines=4)
|
443 |
+
with gr.Row():
|
444 |
+
seed_input = gr.Slider(
|
445 |
+
show_label=False, minimum=-1, maximum=999, value=-1, step=1, info="Seed. -1 for random"
|
446 |
+
)
|
447 |
+
speed_input = gr.Slider(
|
448 |
+
show_label=False, minimum=0.3, maximum=2.0, value=1.0, step=0.1, info="Adjust the speed"
|
449 |
+
)
|
450 |
+
with gr.Column(scale=1, min_width=160):
|
451 |
+
ref_text_file_input = gr.File(label="Load Reference Text from File (.txt)", file_types=[".txt"])
|
452 |
speech_type_rows.append(row)
|
453 |
speech_type_names.append(name_input)
|
454 |
speech_type_audios.append(audio_input)
|
455 |
speech_type_ref_texts.append(ref_text_input)
|
456 |
speech_type_ref_text_files.append(ref_text_file_input)
|
457 |
+
speech_type_seeds.append(seed_input)
|
458 |
+
speech_type_speeds.append(speed_input)
|
459 |
speech_type_delete_btns.append(delete_btn)
|
460 |
speech_type_insert_btns.append(insert_btn)
|
461 |
|
462 |
+
# Global logic for all speech types
|
463 |
+
for i in range(max_speech_types):
|
464 |
+
speech_type_audios[i].clear(
|
465 |
+
lambda: [None, None],
|
466 |
+
None,
|
467 |
+
[speech_type_ref_texts[i], speech_type_ref_text_files[i]],
|
468 |
+
)
|
469 |
+
speech_type_ref_text_files[i].upload(
|
470 |
+
load_text_from_file,
|
471 |
+
inputs=[speech_type_ref_text_files[i]],
|
472 |
+
outputs=[speech_type_ref_texts[i]],
|
473 |
+
)
|
474 |
+
|
475 |
# Button to add speech type
|
476 |
add_speech_type_btn = gr.Button("Add Speech Type")
|
477 |
|
|
|
507 |
speech_type_ref_text_files[i],
|
508 |
],
|
509 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
|
511 |
# Text input for the prompt
|
512 |
with gr.Row():
|
|
|
520 |
gen_text_file_multistyle = gr.File(label="Load Text to Generate from File (.txt)", file_types=[".txt"], scale=1)
|
521 |
|
522 |
def make_insert_speech_type_fn(index):
|
523 |
+
def insert_speech_type_fn(current_text, speech_type_name, speech_type_seed, speech_type_speed):
|
524 |
current_text = current_text or ""
|
525 |
+
if not speech_type_name:
|
526 |
+
gr.Warning("Please enter speech type name before insert.")
|
527 |
+
return current_text
|
528 |
+
speech_type_dict = {
|
529 |
+
"name": speech_type_name,
|
530 |
+
"seed": speech_type_seed,
|
531 |
+
"speed": speech_type_speed,
|
532 |
+
}
|
533 |
+
updated_text = current_text + json.dumps(speech_type_dict) + " "
|
534 |
return updated_text
|
535 |
|
536 |
return insert_speech_type_fn
|
|
|
539 |
insert_fn = make_insert_speech_type_fn(i)
|
540 |
insert_btn.click(
|
541 |
insert_fn,
|
542 |
+
inputs=[gen_text_input_multistyle, speech_type_names[i], speech_type_seeds[i], speech_type_speeds[i]],
|
543 |
outputs=gen_text_input_multistyle,
|
544 |
)
|
545 |
|
546 |
+
with gr.Accordion("Advanced Settings", open=True):
|
547 |
+
with gr.Row():
|
548 |
+
with gr.Column():
|
549 |
+
show_cherrypick_multistyle = gr.Checkbox(
|
550 |
+
label="Show Cherry-pick Interface",
|
551 |
+
info="Turn on to show interface, picking seeds from previous generations.",
|
552 |
+
value=False,
|
553 |
+
)
|
554 |
+
with gr.Column():
|
555 |
+
remove_silence_multistyle = gr.Checkbox(
|
556 |
+
label="Remove Silences",
|
557 |
+
info="Turn on to automatically detect and crop long silences.",
|
558 |
+
value=True,
|
559 |
+
)
|
560 |
|
561 |
# Generate button
|
562 |
generate_multistyle_btn = gr.Button("Generate Multi-Style Speech", variant="primary")
|
|
|
564 |
# Output audio
|
565 |
audio_output_multistyle = gr.Audio(label="Synthesized Audio")
|
566 |
|
567 |
+
# Used seed gallery
|
568 |
+
cherrypick_interface_multistyle = gr.Textbox(
|
569 |
+
label="Cherry-pick Interface",
|
570 |
+
lines=10,
|
571 |
+
max_lines=40,
|
572 |
+
show_copy_button=True,
|
573 |
+
interactive=False,
|
574 |
+
visible=False,
|
575 |
+
)
|
576 |
+
|
577 |
+
# Logic control to show/hide the cherrypick interface
|
578 |
+
show_cherrypick_multistyle.change(
|
579 |
+
lambda is_visible: gr.update(visible=is_visible),
|
580 |
+
show_cherrypick_multistyle,
|
581 |
+
cherrypick_interface_multistyle,
|
582 |
+
)
|
583 |
+
|
584 |
+
# Function to load text to generate from file
|
585 |
gen_text_file_multistyle.upload(
|
586 |
load_text_from_file,
|
587 |
inputs=[gen_text_file_multistyle],
|
|
|
615 |
|
616 |
# For each segment, generate speech
|
617 |
generated_audio_segments = []
|
618 |
+
current_type_name = "Regular"
|
619 |
+
inference_meta_data = ""
|
620 |
|
621 |
for segment in segments:
|
622 |
+
name = segment["name"]
|
623 |
+
seed = segment["seed"]
|
624 |
+
speed = segment["speed"]
|
625 |
text = segment["text"]
|
626 |
|
627 |
+
if name in speech_types:
|
628 |
+
current_type_name = name
|
629 |
else:
|
630 |
+
gr.Warning(f"Type {name} is not available, will use Regular as default.")
|
631 |
+
current_type_name = "Regular"
|
632 |
|
633 |
try:
|
634 |
+
ref_audio = speech_types[current_type_name]["audio"]
|
635 |
except KeyError:
|
636 |
+
gr.Warning(f"Please provide reference audio for type {current_type_name}.")
|
637 |
+
return [None] + [speech_types[name]["ref_text"] for name in speech_types] + [None]
|
638 |
+
ref_text = speech_types[current_type_name].get("ref_text", "")
|
639 |
|
640 |
+
if seed == -1:
|
641 |
+
seed_input = None
|
642 |
|
643 |
# Generate speech for this segment
|
644 |
+
audio_out, _, ref_text_out, used_seed = infer(
|
645 |
+
ref_audio,
|
646 |
+
ref_text,
|
647 |
+
text,
|
648 |
+
tts_model_choice,
|
649 |
+
remove_silence,
|
650 |
+
seed=seed_input,
|
651 |
+
cross_fade_duration=0,
|
652 |
+
speed=speed,
|
653 |
+
show_info=print,
|
654 |
) # show_info=print no pull to top when generating
|
655 |
sr, audio_data = audio_out
|
656 |
|
657 |
generated_audio_segments.append(audio_data)
|
658 |
+
speech_types[current_type_name]["ref_text"] = ref_text_out
|
659 |
+
inference_meta_data += json.dumps(dict(name=name, seed=used_seed, speed=speed)) + f" {text}\n"
|
660 |
|
661 |
# Concatenate all audio segments
|
662 |
if generated_audio_segments:
|
663 |
final_audio_data = np.concatenate(generated_audio_segments)
|
664 |
+
return (
|
665 |
+
[(sr, final_audio_data)]
|
666 |
+
+ [speech_types[name]["ref_text"] for name in speech_types]
|
667 |
+
+ [inference_meta_data]
|
668 |
+
)
|
669 |
else:
|
670 |
gr.Warning("No audio generated.")
|
671 |
+
return [None] + [speech_types[name]["ref_text"] for name in speech_types] + [None]
|
672 |
|
673 |
generate_multistyle_btn.click(
|
674 |
generate_multistyle_speech,
|
|
|
681 |
+ [
|
682 |
remove_silence_multistyle,
|
683 |
],
|
684 |
+
outputs=[audio_output_multistyle] + speech_type_ref_texts + [cherrypick_interface_multistyle],
|
685 |
)
|
686 |
|
687 |
# Validation function to disable Generate button if speech types are missing
|
|
|
698 |
|
699 |
# Parse the gen_text to get the speech types used
|
700 |
segments = parse_speechtypes_text(gen_text)
|
701 |
+
speech_types_in_text = set(segment["name"] for segment in segments)
|
702 |
|
703 |
# Check if all speech types in text are available
|
704 |
missing_speech_types = speech_types_in_text - speech_types_available
|
|
|
862 |
if not last_ai_response or conv_state[-1]["role"] != "assistant":
|
863 |
return None, ref_text, seed_input
|
864 |
|
|
|
865 |
if randomize_seed:
|
866 |
+
seed_input = None
|
|
|
|
|
|
|
|
|
|
|
867 |
|
868 |
+
audio_result, _, ref_text_out, used_seed = infer(
|
869 |
ref_audio,
|
870 |
ref_text,
|
871 |
last_ai_response,
|
872 |
tts_model_choice,
|
873 |
remove_silence,
|
874 |
+
seed=seed_input,
|
875 |
cross_fade_duration=0.15,
|
876 |
speed=1.0,
|
877 |
show_info=print, # show_info=print no pull to top when generating
|
878 |
)
|
879 |
+
return audio_result, ref_text_out, used_seed
|
880 |
|
881 |
def clear_conversation():
|
882 |
"""Reset the conversation"""
|