Spaces:
Running
Running
Upload 2 files
Browse files- gradio_app.py +34 -32
- inference.py +36 -33
gradio_app.py
CHANGED
|
@@ -28,7 +28,8 @@ from inference import (
|
|
| 28 |
compile_model,
|
| 29 |
compile_fish_ae,
|
| 30 |
sample_euler_cfg_independent_guidances,
|
| 31 |
-
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# --------------------------------------------------------------------
|
|
@@ -61,6 +62,16 @@ pca_state = load_pca_state_from_hf(device="cpu")
|
|
| 61 |
model_compiled = None
|
| 62 |
fish_ae_compiled = None
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# --------------------------------------------------------------------
|
| 65 |
# Helper functions
|
| 66 |
def make_stem(prefix: str, user_id: str | None = None) -> str:
|
|
@@ -635,42 +646,40 @@ def generate_voice_conversion(
|
|
| 635 |
cfg_max_t: float,
|
| 636 |
truncation_factor: float,
|
| 637 |
audio_format: str,
|
| 638 |
-
rhythm_strength: float,
|
| 639 |
session_id: str,
|
| 640 |
) -> Tuple[Any, Any, Any]:
|
| 641 |
-
"""Voice conversion
|
| 642 |
-
|
| 643 |
-
rhythm_strength>0: start denoising from noised source latent to preserve timing."""
|
| 644 |
cleanup_temp_audio(TEMP_AUDIO_DIR, session_id)
|
| 645 |
start_time = time.time()
|
| 646 |
|
| 647 |
if not source_audio_path:
|
| 648 |
raise gr.Error("Please provide source audio to convert.")
|
| 649 |
-
if
|
| 650 |
-
raise gr.Error("
|
| 651 |
|
| 652 |
sequence_length = _source_duration_to_latent_length(source_audio_path)
|
| 653 |
|
| 654 |
-
# Encode source audio into latent space
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
src_padded.unsqueeze(0).to(fish_ae.dtype)
|
| 666 |
-
) # (1, sequence_length, 80)
|
| 667 |
|
| 668 |
target_audio = load_audio(target_speaker_path).cpu() if target_speaker_path else None
|
| 669 |
-
|
|
|
|
|
|
|
| 670 |
|
| 671 |
num_steps_int = min(max(int(num_steps), 1), 80)
|
| 672 |
sample_fn = partial(
|
| 673 |
-
|
| 674 |
num_steps=num_steps_int,
|
| 675 |
cfg_scale_text=float(cfg_scale_text),
|
| 676 |
cfg_scale_speaker=float(cfg_scale_speaker) if cfg_scale_speaker is not None else None,
|
|
@@ -684,15 +693,14 @@ def generate_voice_conversion(
|
|
| 684 |
speaker_kv_max_layers=None,
|
| 685 |
sequence_length=sequence_length,
|
| 686 |
source_latent=source_latent,
|
| 687 |
-
rhythm_strength=float(rhythm_strength),
|
| 688 |
)
|
| 689 |
|
| 690 |
audio_out, normalized_text = sample_pipeline(
|
| 691 |
-
model=
|
| 692 |
fish_ae=fish_ae,
|
| 693 |
pca_state=pca_state,
|
| 694 |
sample_fn=sample_fn,
|
| 695 |
-
text_prompt=
|
| 696 |
speaker_audio=target_audio,
|
| 697 |
rng_seed=int(rng_seed) if rng_seed is not None else 0,
|
| 698 |
normalize_text=True,
|
|
@@ -703,7 +711,7 @@ def generate_voice_conversion(
|
|
| 703 |
output_path = save_audio_with_format(audio_to_save, TEMP_AUDIO_DIR, stem, 44100, audio_format)
|
| 704 |
|
| 705 |
generation_time = time.time() - start_time
|
| 706 |
-
time_str = f"\u23f1\ufe0f {generation_time:.2f}s |
|
| 707 |
text_display = f"**Transcript (normalized):**\n\n{normalized_text}"
|
| 708 |
|
| 709 |
return (
|
|
@@ -1161,11 +1169,6 @@ with gr.Blocks(title="Echo-TTS", css=LINK_CSS, js=JS_CODE) as demo:
|
|
| 1161 |
with gr.Column(scale=1):
|
| 1162 |
vc_audio_format = gr.Radio(choices=["wav", "mp3"], value="wav", label="Output Format")
|
| 1163 |
|
| 1164 |
-
vc_rhythm_strength = gr.Slider(
|
| 1165 |
-
label="🥁 Rhythm Strength",
|
| 1166 |
-
minimum=0.0, maximum=0.9, value=0.4, step=0.05,
|
| 1167 |
-
info="0 = ignore source rhythm (pure TTS) · 0.4 = preserve timing · 0.8 = very close to source pacing",
|
| 1168 |
-
)
|
| 1169 |
vc_generate_btn = gr.Button("Convert Voice", variant="primary", size="lg")
|
| 1170 |
|
| 1171 |
with gr.Group():
|
|
@@ -1199,7 +1202,6 @@ with gr.Blocks(title="Echo-TTS", css=LINK_CSS, js=JS_CODE) as demo:
|
|
| 1199 |
vc_cfg_max_t,
|
| 1200 |
vc_truncation,
|
| 1201 |
vc_audio_format,
|
| 1202 |
-
vc_rhythm_strength,
|
| 1203 |
session_id_state,
|
| 1204 |
],
|
| 1205 |
outputs=[vc_output_audio, vc_time_display, vc_text_display],
|
|
|
|
| 28 |
compile_model,
|
| 29 |
compile_fish_ae,
|
| 30 |
sample_euler_cfg_independent_guidances,
|
| 31 |
+
sample_euler_cfg_vc,
|
| 32 |
+
load_vc_model_from_hf
|
| 33 |
)
|
| 34 |
|
| 35 |
# --------------------------------------------------------------------
|
|
|
|
| 62 |
model_compiled = None
|
| 63 |
fish_ae_compiled = None
|
| 64 |
|
| 65 |
+
# Full model with latent encoder for VC (loaded lazily on first VC use)
|
| 66 |
+
_vc_model = None
|
| 67 |
+
|
| 68 |
+
def _get_vc_model():
|
| 69 |
+
global _vc_model
|
| 70 |
+
if _vc_model is None:
|
| 71 |
+
print("Loading full VC model (with latent encoder)...")
|
| 72 |
+
_vc_model = load_vc_model_from_hf(dtype=MODEL_DTYPE, device="cpu")
|
| 73 |
+
return _vc_model
|
| 74 |
+
|
| 75 |
# --------------------------------------------------------------------
|
| 76 |
# Helper functions
|
| 77 |
def make_stem(prefix: str, user_id: str | None = None) -> str:
|
|
|
|
| 646 |
cfg_max_t: float,
|
| 647 |
truncation_factor: float,
|
| 648 |
audio_format: str,
|
|
|
|
| 649 |
session_id: str,
|
| 650 |
) -> Tuple[Any, Any, Any]:
|
| 651 |
+
"""Voice conversion: encode source audio latent, condition diffusion on it
|
| 652 |
+
alongside target speaker to re-speak content in the target voice."""
|
|
|
|
| 653 |
cleanup_temp_audio(TEMP_AUDIO_DIR, session_id)
|
| 654 |
start_time = time.time()
|
| 655 |
|
| 656 |
if not source_audio_path:
|
| 657 |
raise gr.Error("Please provide source audio to convert.")
|
| 658 |
+
if not transcript or not transcript.strip():
|
| 659 |
+
raise gr.Error("Please provide a transcript (or use Auto-Transcribe).")
|
| 660 |
|
| 661 |
sequence_length = _source_duration_to_latent_length(source_audio_path)
|
| 662 |
|
| 663 |
+
# Encode source audio into AE latent space
|
| 664 |
+
source_audio_raw = load_audio(source_audio_path).cpu()
|
| 665 |
+
n_samples = sequence_length * 2048
|
| 666 |
+
src_padded = torch.nn.functional.pad(
|
| 667 |
+
source_audio_raw,
|
| 668 |
+
(0, max(0, n_samples - source_audio_raw.shape[-1]))
|
| 669 |
+
)[:, :n_samples]
|
| 670 |
+
source_latent = ae_encode(
|
| 671 |
+
fish_ae, pca_state,
|
| 672 |
+
src_padded.unsqueeze(0).to(fish_ae.dtype)
|
| 673 |
+
) # (1, sequence_length, 80)
|
|
|
|
|
|
|
| 674 |
|
| 675 |
target_audio = load_audio(target_speaker_path).cpu() if target_speaker_path else None
|
| 676 |
+
|
| 677 |
+
# Use the full model (with latent encoder) for VC
|
| 678 |
+
vc_model = _get_vc_model()
|
| 679 |
|
| 680 |
num_steps_int = min(max(int(num_steps), 1), 80)
|
| 681 |
sample_fn = partial(
|
| 682 |
+
sample_euler_cfg_vc,
|
| 683 |
num_steps=num_steps_int,
|
| 684 |
cfg_scale_text=float(cfg_scale_text),
|
| 685 |
cfg_scale_speaker=float(cfg_scale_speaker) if cfg_scale_speaker is not None else None,
|
|
|
|
| 693 |
speaker_kv_max_layers=None,
|
| 694 |
sequence_length=sequence_length,
|
| 695 |
source_latent=source_latent,
|
|
|
|
| 696 |
)
|
| 697 |
|
| 698 |
audio_out, normalized_text = sample_pipeline(
|
| 699 |
+
model=vc_model,
|
| 700 |
fish_ae=fish_ae,
|
| 701 |
pca_state=pca_state,
|
| 702 |
sample_fn=sample_fn,
|
| 703 |
+
text_prompt=transcript,
|
| 704 |
speaker_audio=target_audio,
|
| 705 |
rng_seed=int(rng_seed) if rng_seed is not None else 0,
|
| 706 |
normalize_text=True,
|
|
|
|
| 711 |
output_path = save_audio_with_format(audio_to_save, TEMP_AUDIO_DIR, stem, 44100, audio_format)
|
| 712 |
|
| 713 |
generation_time = time.time() - start_time
|
| 714 |
+
time_str = f"\u23f1\ufe0f {generation_time:.2f}s | latent={sequence_length} (~{sequence_length * 2048 / 44100:.1f}s)"
|
| 715 |
text_display = f"**Transcript (normalized):**\n\n{normalized_text}"
|
| 716 |
|
| 717 |
return (
|
|
|
|
| 1169 |
with gr.Column(scale=1):
|
| 1170 |
vc_audio_format = gr.Radio(choices=["wav", "mp3"], value="wav", label="Output Format")
|
| 1171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1172 |
vc_generate_btn = gr.Button("Convert Voice", variant="primary", size="lg")
|
| 1173 |
|
| 1174 |
with gr.Group():
|
|
|
|
| 1202 |
vc_cfg_max_t,
|
| 1203 |
vc_truncation,
|
| 1204 |
vc_audio_format,
|
|
|
|
| 1205 |
session_id_state,
|
| 1206 |
],
|
| 1207 |
outputs=[vc_output_audio, vc_time_display, vc_text_display],
|
inference.py
CHANGED
|
@@ -424,7 +424,7 @@ def sample_euler_cfg_independent_guidances(
|
|
| 424 |
|
| 425 |
|
| 426 |
@torch.inference_mode()
|
| 427 |
-
def
|
| 428 |
model: EchoDiT,
|
| 429 |
speaker_latent: torch.Tensor,
|
| 430 |
speaker_mask: torch.Tensor,
|
|
@@ -443,14 +443,11 @@ def sample_euler_cfg_sdedit(
|
|
| 443 |
speaker_kv_max_layers: int | None,
|
| 444 |
speaker_kv_min_t: float | None,
|
| 445 |
sequence_length: int | None = None,
|
| 446 |
-
source_latent: torch.Tensor | None = None,
|
| 447 |
-
rhythm_strength: float = 0.0,
|
| 448 |
) -> torch.Tensor:
|
| 449 |
-
"""
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
0.9 = start near end of schedule, very close to source timing.
|
| 453 |
-
source_latent: (1, T, 80) AE-encoded source audio."""
|
| 454 |
|
| 455 |
if sequence_length is None:
|
| 456 |
sequence_length = 640
|
|
@@ -462,30 +459,6 @@ def sample_euler_cfg_sdedit(
|
|
| 462 |
rng = torch.Generator(device=device).manual_seed(rng_seed)
|
| 463 |
t_schedule = torch.linspace(1., 0., num_steps + 1, device=device) * INIT_SCALE
|
| 464 |
|
| 465 |
-
# Which step to start from: 0 = full denoising from noise, num_steps-1 = skip almost everything
|
| 466 |
-
start_step = int(rhythm_strength * (num_steps - 1)) if rhythm_strength > 0.0 else 0
|
| 467 |
-
t_start = t_schedule[start_step].item()
|
| 468 |
-
|
| 469 |
-
if source_latent is not None and rhythm_strength > 0.0:
|
| 470 |
-
src = source_latent.float().to(device)
|
| 471 |
-
# Resize to sequence_length along time dim if needed
|
| 472 |
-
if src.shape[1] != sequence_length:
|
| 473 |
-
src = torch.nn.functional.interpolate(
|
| 474 |
-
src.permute(0, 2, 1),
|
| 475 |
-
size=sequence_length,
|
| 476 |
-
mode='linear',
|
| 477 |
-
align_corners=False,
|
| 478 |
-
).permute(0, 2, 1)
|
| 479 |
-
noise = torch.randn((batch_size, sequence_length, 80), device=device,
|
| 480 |
-
dtype=torch.float32, generator=rng)
|
| 481 |
-
# Flow-matching noise schedule: x_t = (1-t)*x0 + t*noise
|
| 482 |
-
x_t = (1.0 - t_start) * src + t_start * noise
|
| 483 |
-
else:
|
| 484 |
-
x_t = torch.randn((batch_size, sequence_length, 80), device=device,
|
| 485 |
-
dtype=torch.float32, generator=rng)
|
| 486 |
-
if truncation_factor is not None:
|
| 487 |
-
x_t = x_t * truncation_factor
|
| 488 |
-
|
| 489 |
text_mask_uncond = torch.zeros_like(text_mask)
|
| 490 |
speaker_mask_uncond = torch.zeros_like(speaker_mask)
|
| 491 |
|
|
@@ -495,15 +468,38 @@ def sample_euler_cfg_sdedit(
|
|
| 495 |
if speaker_kv_scale is not None:
|
| 496 |
_multiply_kv_cache(kv_speaker_cond, speaker_kv_scale, speaker_kv_max_layers)
|
| 497 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
kv_text_full = _concat_kv_caches(kv_text_cond, kv_text_cond, kv_text_cond)
|
| 499 |
kv_speaker_full = _concat_kv_caches(kv_speaker_cond, kv_speaker_cond, kv_speaker_cond)
|
|
|
|
|
|
|
| 500 |
full_text_mask = torch.cat([text_mask, text_mask_uncond, text_mask], dim=0)
|
| 501 |
full_speaker_mask = torch.cat([speaker_mask, speaker_mask, speaker_mask_uncond], dim=0)
|
| 502 |
|
| 503 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
t, t_next = t_schedule[i], t_schedule[i + 1]
|
| 505 |
has_cfg = ((t >= cfg_min_t) * (t <= cfg_max_t)).item()
|
| 506 |
|
|
|
|
|
|
|
|
|
|
| 507 |
if has_cfg:
|
| 508 |
v_cond, v_uncond_text, v_uncond_speaker = model(
|
| 509 |
x=torch.cat([x_t, x_t, x_t], dim=0).to(dtype),
|
|
@@ -512,6 +508,7 @@ def sample_euler_cfg_sdedit(
|
|
| 512 |
speaker_mask=full_speaker_mask,
|
| 513 |
kv_cache_text=kv_text_full,
|
| 514 |
kv_cache_speaker=kv_speaker_full,
|
|
|
|
| 515 |
).float().chunk(3, dim=0)
|
| 516 |
v_pred = v_cond + cfg_scale_text * (v_cond - v_uncond_text) + cfg_scale_speaker * (v_cond - v_uncond_speaker)
|
| 517 |
else:
|
|
@@ -522,6 +519,7 @@ def sample_euler_cfg_sdedit(
|
|
| 522 |
speaker_mask=speaker_mask,
|
| 523 |
kv_cache_text=kv_text_cond,
|
| 524 |
kv_cache_speaker=kv_speaker_cond,
|
|
|
|
| 525 |
).float()
|
| 526 |
|
| 527 |
if rescale_k is not None and rescale_sigma is not None:
|
|
@@ -535,6 +533,11 @@ def sample_euler_cfg_sdedit(
|
|
| 535 |
|
| 536 |
return x_t
|
| 537 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
# ___________________________________________________________
|
| 539 |
# simple example
|
| 540 |
|
|
|
|
| 424 |
|
| 425 |
|
| 426 |
@torch.inference_mode()
|
| 427 |
+
def sample_euler_cfg_vc(
|
| 428 |
model: EchoDiT,
|
| 429 |
speaker_latent: torch.Tensor,
|
| 430 |
speaker_mask: torch.Tensor,
|
|
|
|
| 443 |
speaker_kv_max_layers: int | None,
|
| 444 |
speaker_kv_min_t: float | None,
|
| 445 |
sequence_length: int | None = None,
|
| 446 |
+
source_latent: torch.Tensor | None = None, # (1, T, 80) AE-encoded source audio
|
|
|
|
| 447 |
) -> torch.Tensor:
|
| 448 |
+
"""Voice conversion sampler: uses the source audio latent as direct conditioning
|
| 449 |
+
via the model's latent cross-attention (kv_cache_latent), alongside speaker and text.
|
| 450 |
+
Requires the model to be loaded with delete_blockwise_modules=False."""
|
|
|
|
|
|
|
| 451 |
|
| 452 |
if sequence_length is None:
|
| 453 |
sequence_length = 640
|
|
|
|
| 459 |
rng = torch.Generator(device=device).manual_seed(rng_seed)
|
| 460 |
t_schedule = torch.linspace(1., 0., num_steps + 1, device=device) * INIT_SCALE
|
| 461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
text_mask_uncond = torch.zeros_like(text_mask)
|
| 463 |
speaker_mask_uncond = torch.zeros_like(speaker_mask)
|
| 464 |
|
|
|
|
| 468 |
if speaker_kv_scale is not None:
|
| 469 |
_multiply_kv_cache(kv_speaker_cond, speaker_kv_scale, speaker_kv_max_layers)
|
| 470 |
|
| 471 |
+
# Build latent KV cache from source audio if available
|
| 472 |
+
kv_latent_cond = None
|
| 473 |
+
if source_latent is not None:
|
| 474 |
+
src = source_latent.to(dtype).to(device)
|
| 475 |
+
# Resize to sequence_length if needed
|
| 476 |
+
if src.shape[1] != sequence_length:
|
| 477 |
+
src = torch.nn.functional.interpolate(
|
| 478 |
+
src.float().permute(0, 2, 1),
|
| 479 |
+
size=sequence_length,
|
| 480 |
+
mode='linear',
|
| 481 |
+
align_corners=False,
|
| 482 |
+
).permute(0, 2, 1).to(dtype)
|
| 483 |
+
kv_latent_cond = model.get_kv_cache_latent(src)
|
| 484 |
+
|
| 485 |
kv_text_full = _concat_kv_caches(kv_text_cond, kv_text_cond, kv_text_cond)
|
| 486 |
kv_speaker_full = _concat_kv_caches(kv_speaker_cond, kv_speaker_cond, kv_speaker_cond)
|
| 487 |
+
kv_latent_full = _concat_kv_caches(kv_latent_cond, kv_latent_cond, kv_latent_cond) if kv_latent_cond is not None else None
|
| 488 |
+
|
| 489 |
full_text_mask = torch.cat([text_mask, text_mask_uncond, text_mask], dim=0)
|
| 490 |
full_speaker_mask = torch.cat([speaker_mask, speaker_mask, speaker_mask_uncond], dim=0)
|
| 491 |
|
| 492 |
+
x_t = torch.randn((batch_size, sequence_length, 80), device=device, dtype=torch.float32, generator=rng)
|
| 493 |
+
if truncation_factor is not None:
|
| 494 |
+
x_t = x_t * truncation_factor
|
| 495 |
+
|
| 496 |
+
for i in range(num_steps):
|
| 497 |
t, t_next = t_schedule[i], t_schedule[i + 1]
|
| 498 |
has_cfg = ((t >= cfg_min_t) * (t <= cfg_max_t)).item()
|
| 499 |
|
| 500 |
+
latent_kwargs_full = {'kv_cache_latent': kv_latent_full} if kv_latent_full is not None else {}
|
| 501 |
+
latent_kwargs_cond = {'kv_cache_latent': kv_latent_cond} if kv_latent_cond is not None else {}
|
| 502 |
+
|
| 503 |
if has_cfg:
|
| 504 |
v_cond, v_uncond_text, v_uncond_speaker = model(
|
| 505 |
x=torch.cat([x_t, x_t, x_t], dim=0).to(dtype),
|
|
|
|
| 508 |
speaker_mask=full_speaker_mask,
|
| 509 |
kv_cache_text=kv_text_full,
|
| 510 |
kv_cache_speaker=kv_speaker_full,
|
| 511 |
+
**latent_kwargs_full,
|
| 512 |
).float().chunk(3, dim=0)
|
| 513 |
v_pred = v_cond + cfg_scale_text * (v_cond - v_uncond_text) + cfg_scale_speaker * (v_cond - v_uncond_speaker)
|
| 514 |
else:
|
|
|
|
| 519 |
speaker_mask=speaker_mask,
|
| 520 |
kv_cache_text=kv_text_cond,
|
| 521 |
kv_cache_speaker=kv_speaker_cond,
|
| 522 |
+
**latent_kwargs_cond,
|
| 523 |
).float()
|
| 524 |
|
| 525 |
if rescale_k is not None and rescale_sigma is not None:
|
|
|
|
| 533 |
|
| 534 |
return x_t
|
| 535 |
|
| 536 |
+
|
| 537 |
+
def load_vc_model_from_hf(dtype: torch.dtype = torch.float32, device: str = "cpu", token: str | None = None) -> EchoDiT:
|
| 538 |
+
"""Load the full model with blockwise (latent encoder) modules intact, for VC use."""
|
| 539 |
+
return load_model_from_hf(dtype=dtype, device=device, token=token, delete_blockwise_modules=False)
|
| 540 |
+
|
| 541 |
# ___________________________________________________________
|
| 542 |
# simple example
|
| 543 |
|