### Install PyAudio for Socket Client Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Installs the PyAudio library, a dependency for the real-time socket client. This is necessary for handling audio streams. ```bash sudo apt-get install portaudio19-dev pip install pyaudio ``` -------------------------------- ### Clone F5-TTS-THAI Repository and Install Dependencies Source: https://github.com/vyncx/f5-tts-thai/blob/main/Inference.ipynb This snippet clones the F5-TTS-THAI project from GitHub, changes the directory to the cloned repository, and installs the project using pip. It ensures all necessary dependencies are met for the project to run. ```python !git clone https://github.com/VYNCX/F5-TTS-THAI.git %cd F5-TTS-THAI !pip install git+https://github.com/VYNCX/F5-TTS-THAI.git ``` -------------------------------- ### Example TOML Configuration for Multi-Style F5-TTS Inference Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md An example TOML file illustrating configuration for multi-style generation with F5-TTS. It sets the model and reference audio, implying that additional parameters for style control would be present in a complete multi-style configuration. ```toml # F5-TTS | E2-TTS model = "F5-TTS" ref_audio = "infer/examples/multi/main.flac" ``` -------------------------------- ### Example TOML Configuration for Basic F5-TTS Inference Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md A sample TOML file demonstrating basic F5-TTS inference configuration. It specifies the model, reference audio, reference text (or enables ASR transcription if empty), text to generate, and output directory settings. ```toml # F5-TTS | E2-TTS model = "F5-TTS" ref_audio = "infer/examples/basic/basic_ref_en.wav" # If an empty "", transcribes the reference audio automatically. ref_text = "Some call me nature, others call me mother nature." gen_text = "I don't really care what you call me. I've been a silent spectator, watching species evolve, empires rise and fall. But always remember, I am mighty and enduring." # File with text to generate. Ignores the text above. gen_file = "" remove_silence = false output_dir = "tests" ``` -------------------------------- ### Start Socket Server Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Launches the socket server for real-time communication with the F5-TTS system. This server handles incoming requests for speech synthesis. ```bash python src/f5_tts/socket_server.py ``` -------------------------------- ### Launch F5-TTS Training with Accelerate Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/train/README.md This section demonstrates how to launch the F5-TTS training script using the 'accelerate' library. It covers configuring accelerate for multi-GPU and mixed-precision training, and launching training with specific configuration files and optional parameter overrides. ```bash # setup accelerate config, e.g. use multi-gpu ddp, fp16 # will be to: ~/.cache/huggingface/accelerate/default_config.yaml accelerate config # .yaml files are under src/f5_tts/configs directory accelerate launch src/f5_tts/train/train.py --config-name F5TTS_Base_train.yaml # possible to overwrite accelerate and hydra config accelerate launch --mixed_precision=fp16 src/f5_tts/train/train.py --config-name F5TTS_Small_train.yaml ++datasets.batch_size_per_gpu=19200 ``` -------------------------------- ### Start F5-TTS Socket Server Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Starts the F5-TTS socket server for real-time audio streaming. Requires host, port, model checkpoint, reference audio, and reference text as arguments. ```bash python src/f5_tts/socket_server.py \ --host 0.0.0.0 \ --port 9998 \ --ckpt_file "path/to/model.safetensors" \ --ref_audio "reference.wav" \ --ref_text "Reference audio transcript." ``` -------------------------------- ### Prepare Specific Datasets with Bash Scripts Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/train/README.md These bash scripts are used to prepare specific datasets for F5-TTS training. Ensure the corresponding dataset is downloaded and its path is correctly configured before running these scripts. ```bash # Prepare the Emilia dataset python src/f5_tts/train/datasets/prepare_emilia.py # Prepare the Wenetspeech4TTS dataset python src/f5_tts/train/datasets/prepare_wenetspeech4tts.py # Prepare the LibriTTS dataset python src/f5_tts/train/datasets/prepare_libritts.py # Prepare the LJSpeech dataset python src/f5_tts/train/datasets/prepare_ljspeech.py ``` -------------------------------- ### Launch F5-TTS-THAI Web UI Source: https://github.com/vyncx/f5-tts-thai/blob/main/Inference.ipynb This command launches the F5-TTS-THAI web user interface using Python. The `--share` flag is used to make the web UI accessible publicly, which is useful for demonstrations or shared access. ```python !python src/f5_tts/f5_tts_webui.py --share ``` -------------------------------- ### Configure F5-TTS CLI Inference with TOML File Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Utilizes a TOML configuration file for flexible F5-TTS command-line inference. This approach allows for defining various parameters such as model type, reference audio/text, generation text/file, and output directory, simplifying complex inference setups. ```bash f5-tts_infer-cli -c custom.toml ``` -------------------------------- ### Create Custom Dataset from Metadata CSV with Bash Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/train/README.md This bash script prepares a custom dataset using a metadata.csv file. Refer to the provided GitHub discussion link for detailed guidance on creating the metadata.csv. ```bash python src/f5_tts/train/datasets/prepare_csv_wavs.py ``` -------------------------------- ### Set Wandb API Key for Logging Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/train/README.md Instructions for setting the WANDB_API_KEY environment variable to enable wandb logging. This is required for both online and offline logging modes. Replace '' with your actual key. ```bash # On Mac & Linux: export WANDB_API_KEY= # On Windows: set WANDB_API_KEY= ``` -------------------------------- ### Launch Gradio App for F5-TTS Inference Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Launches a Gradio web interface for F5-TTS inference, supporting basic TTS, multi-style/speaker generation, and voice chat. The script automatically loads necessary models from Huggingface. It can be launched directly via CLI or integrated into larger Gradio applications. ```bash f5-tts_infer-gradio --inbrowser f5-tts_infer-gradio --root_path "/myapp" ``` -------------------------------- ### Run F5-TTS Inference via Command Line Interface Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Executes F5-TTS inference using the command-line interface. This method allows for specifying reference audio, text for generation, vocoder choice, and checkpoint paths. It supports ASR transcription if reference text is not provided. ```bash f5-tts_infer-cli \ --model "F5-TTS" \ --ref_audio "ref_audio.wav" \ --ref_text "The content, subtitle or transcription of reference audio." \ --gen_text "Some text you want TTS model generate for you." f5-tts_infer-cli --vocoder_name bigvgan --load_vocoder_from_local --ckpt_file f5-tts_infer-cli --vocoder_name vocos --load_vocoder_from_local --ckpt_file f5-tts_infer-cli --help ``` -------------------------------- ### Integrate F5-TTS Gradio App into Larger Application Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Demonstrates how to embed the F5-TTS Gradio inference interface as a component within a larger Gradio Blocks application. This allows for combining F5-TTS functionality with other Gradio components in a single interface. ```python import gradio as gr from f5_tts.infer.infer_gradio import app with gr.Blocks() as main_app: gr.Markdown("# This is an example of using F5-TTS within a bigger Gradio app") # ... other Gradio components app.render() main_app.launch() ``` -------------------------------- ### Enable Offline Wandb Logging Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/train/README.md This command sets the WANDB_MODE environment variable to 'offline', allowing metrics to be logged locally without an active internet connection. This is useful if you cannot access Wandb directly. ```bash export WANDB_MODE=offline ``` -------------------------------- ### Run Speech Editing Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md Executes the speech editing capabilities of the F5-TTS project. This script allows for modifications and enhancements to synthesized speech. ```bash python src/f5_tts/infer/speech_edit.py ``` -------------------------------- ### Real-time Socket Client for F5-TTS Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/README.md A Python script that acts as a real-time client to communicate with the F5-TTS socket server. It sends text and plays back the synthesized audio stream using PyAudio. ```python import socket import asyncio import pyaudio import numpy as np import logging import time logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) async def listen_to_F5TTS(text, server_ip="localhost", server_port=9998): client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) await asyncio.get_event_loop().run_in_executor(None, client_socket.connect, (server_ip, int(server_port))) start_time = time.time() first_chunk_time = None async def play_audio_stream(): nonlocal first_chunk_time p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paFloat32, channels=1, rate=24000, output=True, frames_per_buffer=2048) try: while True: data = await asyncio.get_event_loop().run_in_executor(None, client_socket.recv, 8192) if not data: break if data == b"END": logger.info("End of audio received.") break audio_array = np.frombuffer(data, dtype=np.float32) stream.write(audio_array.tobytes()) if first_chunk_time is None: first_chunk_time = time.time() finally: stream.stop_stream() stream.close() p.terminate() logger.info(f"Total time taken: {time.time() - start_time:.4f} seconds") try: data_to_send = f"{text}".encode("utf-8") await asyncio.get_event_loop().run_in_executor(None, client_socket.sendall, data_to_send) await play_audio_stream() except Exception as e: logger.error(f"Error in listen_to_F5TTS: {e}") finally: client_socket.close() if __name__ == "__main__": text_to_send = "As a Reader assistant, I'm familiar with new technology. which are key to its improved performance in terms of both training speed and inference efficiency. Let's break down the components" asyncio.run(listen_to_F5TTS(text_to_send)) ``` -------------------------------- ### Multi-voice Text File Format Source: https://context7.com/vyncx/f5-tts-thai/llms.txt This text file format is used to specify different speakers for different parts of the text in multi-voice synthesis. Each line starts with a speaker tag in brackets (e.g., [main], [town]) followed by the text for that speaker. ```text [main] Once upon a time, there was a small village. [town] The townspeople were friendly and welcoming. [country] But in the countryside, life was more peaceful. [main] And they all lived happily ever after. ``` -------------------------------- ### Launch Gradio Web UI for F5-TTS (Bash) Source: https://context7.com/vyncx/f5-tts-thai/llms.txt These bash commands demonstrate how to launch the Gradio web interface for the F5-TTS Thai model. Options include launching the default UI, running the script directly, enabling public sharing, and launching the standard inference UI with browser auto-launch. ```bash # Launch the Thai-optimized web UI f5-tts_webui # Or run directly python src/f5_tts/f5_tts_webui.py # Launch with public sharing python src/f5_tts/f5_tts_webui.py --share # Launch the standard Gradio inference UI f5-tts_infer-gradio # With browser auto-launch and custom root path f5-tts_infer-gradio --inbrowser --root_path "/myapp" ``` -------------------------------- ### Dataset Preparation Scripts for F5-TTS Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Scripts for preparing various audio datasets for F5-TTS training. Supports CSV with audio files, Emilia, LibriTTS, LJSpeech, and WenetSpeech4TTS formats. ```bash # Prepare custom dataset from CSV with audio files python src/f5_tts/train/datasets/prepare_csv_wavs.py # Prepare Emilia dataset python src/f5_tts/train/datasets/prepare_emilia.py # Prepare LibriTTS dataset python src/f5_tts/train/datasets/prepare_libritts.py # Prepare LJSpeech dataset python src/f5_tts/train/datasets/prepare_ljspeech.py # Prepare WenetSpeech4TTS dataset python src/f5_tts/train/datasets/prepare_wenetspeech4tts.py ``` -------------------------------- ### Multilingual F5-TTS Base Model Configuration (Bash) Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/SHARED.md Configuration details for the multilingual F5-TTS Base model, including paths to the model checkpoint, vocabulary file, and model parameters. This is typically used for inference or further fine-tuning. ```bash Model: hf://SWivid/F5-TTS/F5TTS_Base/model_1200000.safetensors Vocab: hf://SWivid/F5-TTS/F5TTS_Base/vocab.txt Config: {"dim": 1024, "depth": 22, "heads": 16, "ff_mult": 2, "text_dim": 512, "conv_layers": 4} ``` -------------------------------- ### Fine-tune F5-TTS Model via CLI Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Command-line interface for fine-tuning F5-TTS models. Supports custom datasets, pre-trained checkpoints, and various training parameters like learning rate, batch size, and epochs. Includes options for different model versions and IPA support. ```bash # Start fine-tuning Gradio UI f5-tts_finetune-gradio # Fine-tune using CLI with custom dataset python src/f5_tts/train/finetune_cli.py \ --exp_name "F5TTS_Base" \ --dataset_name "my_custom_dataset" \ --finetune \ --pretrain "hf://VIZINTZOR/F5-TTS-THAI/model_1000000.pt" \ --learning_rate 1e-5 \ --batch_size_per_gpu 3200 \ --epochs 100 \ --save_per_updates 50000 \ --tokenizer "custom" \ --tokenizer_path "path/to/vocab.txt" \ --logger "wandb" # Fine-tune V2 model with IPA support python src/f5_tts/train/finetune_cli.py \ --exp_name "F5TTS_V2_Base" \ --dataset_name "my_dataset" \ --finetune \ --pretrain "hf://VIZINTZOR/F5-TTS-TH-V2/model_250000.pt" \ --learning_rate 1e-5 \ --batch_size_per_gpu 3200 \ --epochs 50 \ --num_warmup_updates 20000 \ --grad_accumulation_steps 1 \ --max_grad_norm 1.0 \ --keep_last_n_checkpoints 5 \ --log_samples \ --bnb_optimizer # Use 8-bit Adam for memory efficiency ``` -------------------------------- ### Load F5-TTS v2 Model Configuration and Checkpoint Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Python code to load the v2 F5-TTS model configuration and checkpoint. This version supports IPA and aims to reduce pronunciation errors. It uses different configuration parameters compared to v1. ```python from cached_path import cached_path from f5_tts.infer.utils_infer import load_model, load_vocoder from f5_tts.model import DiT # Model v2 configuration (IPA-based, reduced errors) v2_model_cfg = dict( dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, text_mask_padding=True, conv_layers=4, pe_attn_head=None ) # Load v2 model from HuggingFace (example, actual path may vary) v2_ckpt = str(cached_path("hf://VIZINTZOR/F5-TTS-TH-V2/model_250000.pt")) # Assuming a different vocab file for v2 if needed, or reuse if compatible v2_vocab = "./vocab/vocab_ipa.txt" v2_model = load_model(DiT, v2_model_cfg, v2_ckpt, vocab_file=v2_vocab, use_ema=True) ``` -------------------------------- ### Finnish F5-TTS Base Model Configuration (Bash) Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/SHARED.md Configuration details for the Finnish F5-TTS Base model, specifying the model checkpoint, vocabulary file, and architectural parameters. This configuration is essential for utilizing the Finnish language model. ```bash Model: hf://AsmoKoskinen/F5-TTS_Finnish_Model/model_common_voice_fi_vox_populi_fi_20241206.safetensors Vocab: hf://AsmoKoskinen/F5-TTS_Finnish_Model/vocab.txt Config: {"dim": 1024, "depth": 22, "heads": 16, "ff_mult": 2, "text_dim": 512, "conv_layers": 4} ``` -------------------------------- ### Load F5-TTS Thai v2 Model and Perform Inference Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Loads the v2 model from HuggingFace and performs inference using standard and IPA text processing. Requires `f5_tts`, `torch`, and `transformers` libraries. Inputs include reference audio, reference text, generation text, models, and vocoder. Outputs are audio waveforms, sample rate, and other inference details. ```python from f5_tts.infer.utils_infer import infer_process from f5_tts.models.model_utils import load_model, load_vocoder from f5_tts.models.config import DiT from huggingface_hub import cached_path # Load v2 model from HuggingFace v2_ckpt = str(cached_path("hf://VIZINTZOR/F5-TTS-TH-V2/model_350000.pt")) v2_vocab = "./vocab/vocab_ipa.txt" v2_model = load_model(DiT, v2_model_cfg, v2_ckpt, vocab_file=v2_vocab, use_ema=True) # Load vocoder (shared between model versions) vocoder = load_vocoder(vocoder_name="vocos", is_local=False) # For v1 model wav_v1, sr, _ = infer_process( ref_audio, ref_text, gen_text, v1_model, vocoder, use_ipa=False # Standard text processing ) # For v2 model wav_v2, sr, _ = infer_process( ref_audio, ref_text, gen_text, v2_model, vocoder, use_ipa=True # IPA text processing ) ``` -------------------------------- ### Hindi F5-TTS Small Model Configuration (Bash) Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/SHARED.md Configuration details for the Hindi F5-TTS Small model, specifying the model checkpoint, vocabulary file, and architectural parameters. This configuration is used for Hindi text-to-speech synthesis. ```bash Model: hf://SPRINGLab/F5-Hindi-24KHz/model_2500000.safetensors Vocab: hf://SPRINGLab/F5-Hindi-24KHz/vocab.txt Config: {"dim": 768, "depth": 18, "heads": 12, "ff_mult": 2, "text_dim": 512, "conv_layers": 4} ``` -------------------------------- ### Load F5-TTS v1 Model Configuration and Checkpoint Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Python code to load the v1 F5-TTS model configuration and checkpoint using `cached_path` and custom loading utilities. This configuration is optimized for Thai pronunciation. ```python from cached_path import cached_path from f5_tts.infer.utils_infer import load_model, load_vocoder from f5_tts.model import DiT # Model v1 configuration (better Thai pronunciation) v1_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 ) # Load v1 model from HuggingFace v1_ckpt = str(cached_path("hf://VIZINTZOR/F5-TTS-THAI/model_1000000.pt")) v1_vocab = "./vocab/vocab.txt" v1_model = load_model(DiT, v1_model_cfg, v1_ckpt, vocab_file=v1_vocab, use_ema=True) ``` -------------------------------- ### French F5-TTS Base Model Configuration (Bash) Source: https://github.com/vyncx/f5-tts-thai/blob/main/src/f5_tts/infer/SHARED.md Configuration details for the French F5-TTS Base model, including the path to the model checkpoint, vocabulary file, and model configuration parameters. This is used for French text-to-speech synthesis. ```bash Model: hf://RASPIAUDIO/F5-French-MixedSpeakers-reduced/model_last_reduced.pt Vocab: hf://RASPIAUDIO/F5-French-MixedSpeakers-reduced/vocab.txt Config: {"dim": 1024, "depth": 22, "heads": 16, "ff_mult": 2, "text_dim": 512, "conv_layers": 4} ``` -------------------------------- ### Python Client for Streaming Audio from F5-TTS Source: https://context7.com/vyncx/f5-tts-thai/llms.txt A Python client using asyncio and sockets to connect to the F5-TTS server, send text, and stream the generated audio in real-time. It utilizes pyaudio for audio playback and numpy for audio data manipulation. ```python import socket import asyncio import pyaudio import numpy as np import time async def listen_to_f5tts(text, server_ip="localhost", server_port=9998): """Connect to F5-TTS socket server and stream generated audio.""" client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) await asyncio.get_event_loop().run_in_executor( None, client_socket.connect, (server_ip, int(server_port)) ) start_time = time.time() first_chunk_time = None async def play_audio_stream(): nonlocal first_chunk_time p = pyaudio.PyAudio() stream = p.open( format=pyaudio.paFloat32, channels=1, rate=24000, output=True, frames_per_buffer=2048 ) try: while True: data = await asyncio.get_event_loop().run_in_executor( None, client_socket.recv, 8192 ) if not data or data == b"END": print("End of audio stream received.") break audio_array = np.frombuffer(data, dtype=np.float32) stream.write(audio_array.tobytes()) if first_chunk_time is None: first_chunk_time = time.time() print(f"Time to first audio: {first_chunk_time - start_time:.3f}s") finally: stream.stop_stream() stream.close() p.terminate() print(f"Total streaming time: {time.time() - start_time:.3f}s") try: # Send text to server data_to_send = text.encode("utf-8") await asyncio.get_event_loop().run_in_executor( None, client_socket.sendall, data_to_send ) await play_audio_stream() finally: client_socket.close() # Usage if __name__ == "__main__": text = "สวัสดีครับ นี่คือตัวอย่างการสตรีมเสียงพูดแบบเรียลไทม์" asyncio.run(listen_to_f5tts(text)) ``` -------------------------------- ### CLI Inference for F5-TTS Thai Text-to-Speech Source: https://context7.com/vyncx/f5-tts-thai/llms.txt The command-line interface (CLI) `f5-tts_infer-cli` offers flexible TTS generation with support for configuration files and automatic chunking for long texts. It enables multi-voice synthesis via TOML configuration. Basic usage involves specifying model, reference audio, generated text, and output directory. Custom model checkpoints and alternative vocoders like BigVGAN can also be used. ```bash # Basic inference with command-line arguments f5-tts_infer-cli \ --model "F5-TTS" \ --ref_audio "reference.wav" \ --ref_text "ได้รับข่าวคราวของเราที่จะหาที่มันเป็นไปที่จะจัดขึ้น." \ --gen_text "สวัสดีครับ นี่คือเสียงพูดภาษาไทย ที่สร้างจากโมเดล F5-TTS" \ --output_dir "./output" \ --output_file "generated.wav" \ --nfe_step 32 \ --speed 1.0 \ --remove_silence # Use custom model checkpoint f5-tts_infer-cli \ --model "F5-TTS" \ --ckpt_file "path/to/custom_model.pt" \ --vocab_file "path/to/vocab.txt" \ --ref_audio "reference.wav" \ --gen_text "Custom model inference test." # Use BigVGAN vocoder instead of Vocos f5-tts_infer-cli \ --vocoder_name bigvgan \ --load_vocoder_from_local \ --ckpt_file "ckpts/F5TTS_Base_bigvgan/model_1250000.pt" \ --ref_audio "reference.wav" \ --gen_text "Using BigVGAN vocoder for synthesis." # Inference using TOML configuration file f5-tts_infer-cli -c config.toml ``` ```toml # Basic TTS configuration model = "F5-TTS" ref_audio = "infer/examples/basic/basic_ref_en.wav" ref_text = "Some call me nature, others call me mother nature." gen_text = "I don't really care what you call me. I've been a silent spectator." gen_file = "" # Optional: file with text to generate (ignores gen_text) remove_silence = false output_dir = "tests" output_file = "output.wav" ``` -------------------------------- ### Python API for F5-TTS Thai Text-to-Speech Inference Source: https://context7.com/vyncx/f5-tts-thai/llms.txt The F5TTS class in the Python API allows for high-level text-to-speech inference. It handles model loading, vocoder initialization, and audio generation with configurable parameters for quality and speed. Dependencies include `f5_tts.api` and `soundfile`. It takes reference audio and text for voice cloning, generates speech, and can save the audio and spectrogram. It also includes a transcription feature using Whisper. ```python from f5_tts.api import F5TTS import soundfile as sf # Initialize the TTS model (downloads from HuggingFace if not cached) f5tts = F5TTS( model="F5TTS_Base", # Model architecture name ckpt_file="", # Leave empty for auto-download, or specify custom checkpoint vocab_file="", # Leave empty for default vocab ode_method="euler", # ODE solver method use_ema=True, # Use exponential moving average weights device=None # Auto-detect: cuda, xpu, mps, or cpu ) # Generate speech from text wav, sr, spec = f5tts.infer( ref_file="reference_audio.wav", # Reference audio for voice cloning (< 15s recommended) ref_text="This is the reference text.", # Transcript of reference audio (leave empty for auto-transcribe) gen_text="Hello, this is generated Thai speech. สวัสดีครับ นี่คือเสียงพูดภาษาไทย", nfe_step=32, # Number of denoising steps (higher = better quality) cfg_strength=2.0, # Classifier-free guidance strength speed=1.0, # Speech speed (0.3-1.5) cross_fade_duration=0.15, # Cross-fade duration between chunks (seconds) target_rms=0.1, # Target RMS for loudness normalization remove_silence=False, # Remove long silences from output seed=42 # Random seed for reproducibility (None for random) ) # Save the generated audio sf.write("output.wav", wav, sr) print(f"Generated audio with seed: {f5tts.seed}") # Optionally save spectrogram f5tts.export_spectrogram(spec, "output_spectrogram.png") # Transcribe audio using built-in Whisper transcription = f5tts.transcribe("audio_file.wav", language="th") print(f"Transcription: {transcription}") ``` -------------------------------- ### CLI Inference - f5-tts_infer-cli Source: https://context7.com/vyncx/f5-tts-thai/llms.txt The command-line interface provides flexible TTS generation with support for configuration files. It handles long texts automatically through chunking and supports multi-voice synthesis via TOML configuration. ```APIDOC ## CLI Inference - f5-tts_infer-cli ### Description The command-line interface provides flexible TTS generation with support for configuration files. It handles long texts automatically through chunking and supports multi-voice synthesis via TOML configuration. ### Method `f5-tts_infer-cli` ### Usage Examples #### Basic inference with command-line arguments ```bash f5-tts_infer-cli \ --model "F5-TTS" \ --ref_audio "reference.wav" \ --ref_text "ได้รับข่าวคราวของเราที่จะหาที่มันเป็นไปที่จะจัดขึ้น." \ --gen_text "สวัสดีครับ นี่คือเสียงพูดภาษาไทย ที่สร้างจากโมเดล F5-TTS" \ --output_dir "./output" \ --output_file "generated.wav" \ --nfe_step 32 \ --speed 1.0 \ --remove_silence ``` #### Use custom model checkpoint ```bash f5-tts_infer-cli \ --model "F5-TTS" \ --ckpt_file "path/to/custom_model.pt" \ --vocab_file "path/to/vocab.txt" \ --ref_audio "reference.wav" \ --gen_text "Custom model inference test." ``` #### Use BigVGAN vocoder instead of Vocos ```bash f5-tts_infer-cli \ --vocoder_name bigvgan \ --load_vocoder_from_local \ --ckpt_file "ckpts/F5TTS_Base_bigvgan/model_1250000.pt" \ --ref_audio "reference.wav" \ --gen_text "Using BigVGAN vocoder for synthesis." ``` #### Inference using TOML configuration file ```bash f5-tts_infer-cli -c config.toml ``` ### Parameters (Command-line arguments) - **--model** (str) - Required - Model architecture name (e.g., "F5-TTS"). - **--ckpt_file** (str) - Optional - Path to a custom checkpoint file. - **--vocab_file** (str) - Optional - Path to a custom vocabulary file. - **--ref_audio** (str) - Required - Path to the reference audio file for voice cloning. - **--ref_text** (str) - Optional - Transcript of the reference audio. - **--gen_text** (str) - Optional - The text to generate speech from. Ignored if `--gen_file` is used. - **--gen_file** (str) - Optional - Path to a file containing the text to generate. - **--output_dir** (str) - Optional - Directory to save the output files (default: current directory). - **--output_file** (str) - Optional - Name of the output audio file. - **--nfe_step** (int) - Optional - Number of denoising steps (default: 32). - **--cfg_strength** (float) - Optional - Classifier-free guidance strength (default: 2.0). - **--speed** (float) - Optional - Speech speed multiplier (0.3-1.5, default: 1.0). - **--remove_silence** (bool) - Optional - Whether to remove long silences from the output (default: false). - **--seed** (int) - Optional - Random seed for reproducibility. - **--vocoder_name** (str) - Optional - Name of the vocoder to use (e.g., "bigvgan"). - **--load_vocoder_from_local** (bool) - Optional - Whether to load the vocoder from local files. - **-c, --config** (str) - Optional - Path to a TOML configuration file. ### Example TOML Configuration File (`basic.toml`) ```toml # Basic TTS configuration model = "F5-TTS" ref_audio = "infer/examples/basic/basic_ref_en.wav" ref_text = "Some call me nature, others call me mother nature." gen_text = "I don't really care what you call me. I've been a silent spectator." gen_file = "" # Optional: file with text to generate (ignores gen_text) remove_silence = false output_dir = "tests" output_file = "output.wav" ``` ### Response - The CLI generates audio files and saves them to the specified output directory and file name. It also prints status messages to the console. ``` -------------------------------- ### Thai Text Normalization and IPA Conversion Utilities Source: https://context7.com/vyncx/f5-tts-thai/llms.txt Provides utilities for normalizing Thai text and converting it to IPA format, specifically for the v2 model. Requires `f5_tts` library. Inputs are raw Thai text strings. Outputs are normalized text and IPA strings. Includes a helper function to chain normalization and IPA conversion. ```python from f5_tts.cleantext.th_normalize import normalize_text from f5_tts.cleantext.TH2IPA import th_to_g2p # Normalize Thai text (handles numbers, special characters, etc.) raw_text = "วันที่ 25 ธ.ค. 2024 เวลา 10:30 น." normalized = normalize_text(raw_text) print(f"Normalized: {normalized}") # Convert Thai text to IPA for v2 model thai_text = "สวัสดีครับ ผมชื่อโรบอท" ipa_text = th_to_g2p(thai_text) print(f"IPA: {ipa_text}") # Full preprocessing pipeline for v2 model def preprocess_for_v2(text): """Prepare text for v2 model inference.""" # First normalize normalized = normalize_text(text) # Then convert to IPA ipa = th_to_g2p(normalized) return ipa gen_text_ipa = preprocess_for_v2("พรุ่งนี้มีประชุมสำคัญ อย่าลืมเตรียมเอกสาร") ``` -------------------------------- ### Multi-voice TOML Configuration for F5-TTS Source: https://context7.com/vyncx/f5-tts-thai/llms.txt This TOML configuration file defines settings for multi-speaker synthesis using the F5-TTS model. It specifies the main model, reference audio and text, output directory, and configurations for additional voices like 'town' and 'country'. ```toml # Multi-speaker synthesis configuration model = "F5-TTS" ref_audio = "infer/examples/multi/main.flac" ref_text = "" # Auto-transcribe gen_file = "infer/examples/multi/story.txt" remove_silence = true output_dir = "tests" # Additional voice definitions [voices.town] ref_audio = "infer/examples/multi/town.flac" ref_text = "" [voices.country] ref_audio = "infer/examples/multi/country.flac" ref_text = "" ``` -------------------------------- ### Python API - F5TTS Class Source: https://context7.com/vyncx/f5-tts-thai/llms.txt The `F5TTS` class provides a high-level Python API for text-to-speech inference. It handles model loading, vocoder initialization, and audio generation with configurable parameters for quality and speed control. ```APIDOC ## Python API - F5TTS Class ### Description The `F5TTS` class provides a high-level Python API for text-to-speech inference. It handles model loading, vocoder initialization, and audio generation with configurable parameters for quality and speed control. ### Method `F5TTS()` ### Parameters #### Initialization Parameters - **model** (str) - Required - Model architecture name (e.g., "F5TTS_Base"). - **ckpt_file** (str) - Optional - Path to a custom checkpoint file. Leave empty for auto-download. - **vocab_file** (str) - Optional - Path to a custom vocabulary file. Leave empty for default. - **ode_method** (str) - Optional - ODE solver method (default: "euler"). - **use_ema** (bool) - Optional - Whether to use exponential moving average weights (default: true). - **device** (str) - Optional - Device to use (e.g., "cuda", "cpu"). Auto-detects if None. #### Inference Parameters (`infer` method) - **ref_file** (str) - Required - Path to the reference audio file for voice cloning (< 15s recommended). - **ref_text** (str) - Optional - Transcript of the reference audio. Leave empty for auto-transcription. - **gen_text** (str) - Required - The text to generate speech from. - **nfe_step** (int) - Optional - Number of denoising steps (higher quality, default: 32). - **cfg_strength** (float) - Optional - Classifier-free guidance strength (default: 2.0). - **speed** (float) - Optional - Speech speed multiplier (0.3-1.5, default: 1.0). - **cross_fade_duration** (float) - Optional - Cross-fade duration between chunks in seconds (default: 0.15). - **target_rms** (float) - Optional - Target RMS for loudness normalization (default: 0.1). - **remove_silence** (bool) - Optional - Whether to remove long silences from the output (default: false). - **seed** (int) - Optional - Random seed for reproducibility (None for random). #### Transcription Parameters (`transcribe` method) - **audio_file** (str) - Required - Path to the audio file to transcribe. - **language** (str) - Optional - Language code for transcription (e.g., "th" for Thai). ### Request Example (Initialization and Inference) ```python from f5_tts.api import F5TTS import soundfile as sf # Initialize the TTS model f5tts = F5TTS( model="F5TTS_Base", ckpt_file="", vocab_file="", ode_method="euler", use_ema=True, device=None ) # Generate speech from text wav, sr, spec = f5tts.infer( ref_file="reference_audio.wav", ref_text="This is the reference text.", gen_text="สวัสดีครับ นี่คือเสียงพูดภาษาไทย", nfe_step=32, cfg_strength=2.0, speed=1.0, cross_fade_duration=0.15, target_rms=0.1, remove_silence=False, seed=42 ) # Save the generated audio sf.write("output.wav", wav, sr) print(f"Generated audio with seed: {f5tts.seed}") # Optionally save spectrogram f5tts.export_spectrogram(spec, "output_spectrogram.png") # Transcribe audio using built-in Whisper transcription = f5tts.transcribe("audio_file.wav", language="th") print(f"Transcription: {transcription}") ``` ### Response #### Success Response (`infer` method) - **wav** (numpy.ndarray) - The generated audio waveform. - **sr** (int) - The sampling rate of the generated audio. - **spec** (numpy.ndarray) - The generated spectrogram. #### Success Response (`transcribe` method) - **transcription** (str) - The transcribed text. #### Response Example (`infer` method) ```json { "audio_waveform": "[numpy array data]", "sampling_rate": 22050, "spectrogram": "[numpy array data]" } ``` #### Response Example (`transcribe` method) ```json { "transcription": "สวัสดีครับ นี่คือเสียงพูดภาษาไทย" } ``` ``` -------------------------------- ### Low-Level TTS Inference Functions (Python) Source: https://context7.com/vyncx/f5-tts-thai/llms.txt This Python code demonstrates using the low-level inference functions from `utils_infer` for custom TTS pipelines. It covers loading models and vocoders, preprocessing reference audio, performing inference, chunking text, and transcribing audio using Whisper. ```python import torch import soundfile as sf from f5_tts.infer.utils_infer import ( load_model, load_vocoder, preprocess_ref_audio_text, infer_process, chunk_text, transcribe ) from f5_tts.model import DiT # Load vocoder (Vocos or BigVGAN) vocoder = load_vocoder( vocoder_name="vocos", # "vocos" or "bigvgan" is_local=False, # Load from HuggingFace local_path="", # Path if is_local=True device="cuda" ) # Define model configuration 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 ) # Load the TTS model ema_model = load_model( model_cls=DiT, model_cfg=model_cfg, ckpt_path="path/to/model.pt", mel_spec_type="vocos", vocab_file="path/to/vocab.txt", ode_method="euler", use_ema=True, device="cuda" ) # Preprocess reference audio (clips to ~15s, removes silence edges) ref_audio, ref_text = preprocess_ref_audio_text( ref_audio_orig="reference.wav", ref_text="", # Empty for auto-transcription clip_short=True, device="cuda" ) # Generate speech gen_text = "สวัสดีครับ นี่คือตัวอย่างการสร้างเสียงพูดภาษาไทย" final_wave, sample_rate, spectrogram = infer_process( ref_audio=ref_audio, ref_text=ref_text, gen_text=gen_text, model_obj=ema_model, vocoder=vocoder, mel_spec_type="vocos", target_rms=0.1, cross_fade_duration=0.15, nfe_step=32, cfg_strength=2.0, sway_sampling_coef=-1.0, speed=1.0, fix_duration=None, device="cuda", set_max_chars=250, # Max characters per chunk use_ipa=False # True for v2 model with IPA ) sf.write("output.wav", final_wave, sample_rate) # Manual text chunking for batch processing text_batches = chunk_text( "This is a very long text that needs to be split into smaller chunks for processing.", max_chars=200 ) print(f"Text split into {len(text_batches)} chunks") # Transcribe audio using Whisper transcription = transcribe("audio.wav", language="th") print(f"Transcription: {transcription}") ``` -------------------------------- ### Embed Gradio F5-TTS App in Python Source: https://context7.com/vyncx/f5-tts-thai/llms.txt This Python code snippet shows how to embed the F5-TTS Gradio interface into a larger Gradio Blocks application. It uses `gr.Blocks` to create a multi-tab interface where the TTS app is rendered within a 'Text-to-Speech' tab. ```python import gradio as gr from f5_tts.infer.infer_gradio import app with gr.Blocks() as main_app: gr.Markdown("# My Custom TTS Application") with gr.Tab("Text-to-Speech"): app.render() # Embed F5-TTS interface with gr.Tab("Other Features"): gr.Markdown("Additional application features here...") main_app.launch(server_name="0.0.0.0", server_port=7860) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.