105 lines
3.3 KiB
Python
105 lines
3.3 KiB
Python
import os
|
|
import sys
|
|
import torch
|
|
import soundfile as sf
|
|
|
|
from f5_tts.infer.utils_infer import (
|
|
load_vocoder,
|
|
infer_process,
|
|
preprocess_ref_audio_text,
|
|
)
|
|
from f5_tts.model import CFM, DiT
|
|
from f5_tts.model.utils import get_tokenizer
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
print(f"Using device: {device} ({torch.cuda.get_device_name(0) if device=='cuda' else 'CPU'})")
|
|
|
|
BASE_DIR = r"C:\GitHub\VocoLoom"
|
|
VOCAB_PATH = os.path.join(BASE_DIR, "models", "F5-TTS-German", "vocab.txt")
|
|
CKPT_DIR = os.path.join(BASE_DIR, "ckpts", "Juergen_Seiferth")
|
|
|
|
# The beloved Snippet 10 reference
|
|
REF_AUDIO = os.path.join(BASE_DIR, "cleaned_dataset", "snippet_10_00-06-13-10_to_00-06-23-10_clean.wav")
|
|
REF_TEXT = "Ich bin immer zufrieden. Ich muss das mal so sagen. Was nicht ganz läuft so wie ich das will, das wird so gemacht, damit das läuft. Zum Endeffekt ist passend."
|
|
|
|
# Target text
|
|
TARGET_TEXT = "Frag mal ob du die Fotos vom Tobias bekommst. Wir haben keine gemacht. Aber nicht in die Gruppen rein senden! Der Kenny stellt es bloß wieder online!"
|
|
|
|
vocab_char_map, vocab_size = get_tokenizer(VOCAB_PATH, "custom")
|
|
mel_spec_kwargs = dict(
|
|
n_fft=1024,
|
|
hop_length=256,
|
|
win_length=1024,
|
|
n_mel_channels=100,
|
|
target_sample_rate=24000,
|
|
mel_spec_type="vocos",
|
|
)
|
|
|
|
model_cfg = dict(
|
|
dim=1024,
|
|
depth=22,
|
|
heads=16,
|
|
ff_mult=2,
|
|
text_dim=512,
|
|
text_mask_padding=False,
|
|
conv_layers=4,
|
|
pe_attn_head=1,
|
|
)
|
|
|
|
print("Loading Vocos vocoder...")
|
|
vocoder = load_vocoder(vocoder_name="vocos", is_local=False, device=device)
|
|
|
|
print("Preprocessing reference audio...")
|
|
ref_audio_proc, ref_text_proc = preprocess_ref_audio_text(REF_AUDIO, REF_TEXT)
|
|
|
|
def generate_with_checkpoint(ckpt_path, out_filename, nfe=64, cfg=1.8):
|
|
print(f"\n--- Testing Checkpoint: {os.path.basename(ckpt_path)} ---")
|
|
model = CFM(
|
|
transformer=DiT(**model_cfg, text_num_embeds=vocab_size, mel_dim=100),
|
|
mel_spec_kwargs=mel_spec_kwargs,
|
|
vocab_char_map=vocab_char_map,
|
|
)
|
|
|
|
checkpoint = torch.load(ckpt_path, map_location=device, weights_only=True)
|
|
if "ema_model_state_dict" in checkpoint:
|
|
ema_state = {
|
|
k.replace("ema_model.", ""): v
|
|
for k, v in checkpoint["ema_model_state_dict"].items()
|
|
if k not in ["initted", "step"]
|
|
}
|
|
else:
|
|
ema_state = checkpoint
|
|
|
|
for key in ["mel_spec.mel_stft.mel_scale.fb", "mel_spec.mel_stft.spectrogram.window"]:
|
|
if key in ema_state:
|
|
del ema_state[key]
|
|
|
|
model.load_state_dict(ema_state)
|
|
model = model.to(device).eval()
|
|
|
|
out_path = os.path.join(BASE_DIR, out_filename)
|
|
audio, sr, _ = infer_process(
|
|
ref_audio_proc,
|
|
ref_text_proc,
|
|
TARGET_TEXT,
|
|
model,
|
|
vocoder,
|
|
mel_spec_type="vocos",
|
|
nfe_step=nfe,
|
|
cfg_strength=cfg,
|
|
target_rms=0.1,
|
|
speed=1.0,
|
|
device=device,
|
|
)
|
|
sf.write(out_path, audio, sr)
|
|
print(f"[Done] Generated: {out_path}")
|
|
return out_path
|
|
|
|
# Compare checkpoints: 200, 400, 600, 800 with Chef reference
|
|
for step in [200, 400, 600, 800]:
|
|
ckpt = os.path.join(CKPT_DIR, f"model_{step}.pt")
|
|
if os.path.exists(ckpt):
|
|
generate_with_checkpoint(ckpt, f"juergen_chef_step{step}.wav", nfe=64, cfg=1.8)
|
|
|
|
print("\n[ALL CHEF COMPARISONS COMPLETE]")
|