espnet3.systems.f5tts.src.inference.F5TTSInference
espnet3.systems.f5tts.src.inference.F5TTSInference
class espnet3.systems.f5tts.src.inference.F5TTSInference(train_config: str, checkpoint_path: str, device: str = 'cpu', use_ema: bool = True, vocoder_path: str | None = None, target_sample_rate: int | None = None, ode_solver_steps: int = 32, guidance_strength: float = 2.0, sway_sampling_coefficient: float = -1.0, speed: float = 1.0, target_rms: float = 0.1, cross_fade_duration: float = 0.15, seed: int | None = None)
Bases: object
Load a trained F5-TTS model + vocoder and synthesize waveforms.
Build the model, tokenizer and vocoder for inference.
- Parameters:
- train_config – Path to the training config the checkpoint was trained with; its
modelblock and its preprocessor tokenization settings rebuild the model and the tokenizer. - checkpoint_path – Lightning checkpoint (
.ckpt) from training. - device – Torch device string.
- use_ema – Load EMA-averaged weights (
ema_model_state_dict) when present; otherwise the rawstate_dict. - vocoder_path – Local directory holding the Vocos
config.yamlandpytorch_model.bin. When unset the default checkpoint is fetched from the Hugging Face Hub. - target_sample_rate – Rate of the reference audio and of the synthesized waveform. It is fixed by the model’s mel front end (
feats_extract_config.fs), so leave it unset to use that rate; a value that differs is refused. - ode_solver_steps – Number of ODE solver steps, upstream’s
nfe_step(number of function evaluations). - guidance_strength – Classifier-free guidance scale, upstream’s
cfg_strength. - sway_sampling_coefficient – Sway sampling coefficient of the ODE time steps, upstream’s
sway_sampling_coef; forwarded toCFM.sample. - speed – Speaking-rate factor: the output duration estimated from the reference is divided by it, so above 1 speaks faster, and it scales the text length of each chunk. A chunk shorter than 10 bytes always uses 0.3.
- target_rms – Loudness a quieter reference is raised to before synthesis; the output is scaled back to the reference’s own level afterwards.
- cross_fade_duration – Seconds of linear cross-fade between the waveforms of consecutive text chunks;
0concatenates them. - seed – Random seed of the initial noise, for reproducible output;
Noneleaves it unseeded.
- train_config – Path to the training config the checkpoint was trained with; its
- Raises:ValueError – If
train_confighas nomodel._target_or if itsdataset.preprocessor.token_listis missing; iftarget_sample_ratediffers from the model’s rate; or if the vocoder was trained at a rate other than the model’s.
Example
model:
_target_: espnet3.systems.f5tts.src.inference.F5TTSInference
train_config: ${exp_dir}/config.yaml
checkpoint_path: ${exp_dir}/last.ckpt
device: cuda
ode_solver_steps: 32
guidance_strength: 2.0NOTE
A recipe passes ${exp_dir}/config.yaml, the training config F5TTSSystem.train writes beside the checkpoint, so the model is rebuilt from the exact config it was trained with; its model: block is instantiated via Hydra. Construction is eager, loading the checkpoint and the vocoder up front.
infer_one(target_text: str, reference_audio: ndarray | Audio, reference_text: str) → ndarray
Synthesize target_text in the voice of reference_audio.
Mirrors upstream F5-TTS infer_process: RMS-normalize the reference, split the target text into reference-length-dependent chunks, sample each chunk, vocode, then cross-fade the pieces back together.
- Parameters:
- target_text – Target text to speak.
- reference_audio – Reference waveform at
target_sample_rate: an array, or theAudiothe inference contract hands over. Multi-channel input is averaged down to mono. - reference_text – Transcript of
reference_audio. Required: the model reads the reference through it, and the output duration is extrapolated from the reference’s audio-to-text ratio.
- Returns: Mono waveform as
float32attarget_sample_rate. Returns a single zero sample when the text yields no synthesizable chunk. - Raises:ValueError – If
reference_textis empty, or if the reference is anAudioat another rate thantarget_sample_rate.
Example
>>> wav = tts.infer_one(
... "hello world", reference_audio, reference_text="hi"
... )
>>> wav.dtype, wav.ndim
(dtype('float32'), 1)NOTE
Output length is governed by the reference: the per-chunk duration is extrapolated from the reference audio-to-text ratio, so a mismatched reference_text skews it. The reference loudness is restored after vocoding, so the result matches the input level rather than target_rms.
property sample_rate : int
Return the rate of the reference the engine takes and the audio it gives.
- Returns:
target_sample_rate, the vocoder’s rate. The inference contract reads it as the rate of an audio field that declares none.
