Views
No views yet
1import torch
2import soundfile as sf
3from f5_tts.model import CFM, DiT
4from f5_tts.model.utils import get_tokenizer, convert_char_to_pinyin
5from vocos import Vocos
6
7# Load model
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10# Download checkpoint
11from huggingface_hub import hf_hub_download
12checkpoint_path = hf_hub_download(repo_id="YOUR_USERNAME/F5-TTS-Vietnamese", filename="model_71000.pt")
13vocab_path = hf_hub_download(repo_id="YOUR_USERNAME/F5-TTS-Vietnamese", filename="vocab.txt")
14
15# Load vocab
16vocab_char_map, vocab_size = get_tokenizer(vocab_path, tokenizer="custom")
17
18# Initialize model
19model = CFM(
20 transformer=DiT(
21 dim=1024,
22 depth=22,
23 heads=16,
24 ff_mult=2,
25 text_dim=512,
26 conv_layers=4,
27 text_num_embeds=vocab_size,
28 mel_dim=100
29 ),
30 mel_spec_kwargs=dict(
31 n_fft=1024,
32 hop_length=256,
33 win_length=1024,
34 n_mel_channels=100,
35 target_sample_rate=24000,
36 mel_spec_type="vocos",
37 ),
38 odeint_kwargs=dict(method="euler"),
39 vocab_char_map=vocab_char_map,
40).to(device)
41
42# Load checkpoint
43checkpoint = torch.load(checkpoint_path, map_location=device)
44state_dict = {k.replace("ema_model.", ""): v for k, v in checkpoint["ema_model_state_dict"].items() if k not in ["initted", "step"]}
45model.load_state_dict(state_dict)
46model.eval()
47
48# Load vocoder
49vocoder = Vocos.from_pretrained("charactr/vocos-mel-24khz").to(device)
50
51# Inference
52ref_audio = "reference.wav" # Your reference audio
53ref_text = "Đây là văn bản tham chiếu"
54gen_text = "Đây là văn bản cần tạo giọng nói"
55
56# ... (see full example in repository)1@article{chen2024f5tts,
2 title={F5-TTS: A Fairerr, Faster, and Fully Non-Autoregressive Text-to-Speech System},
3 author={Chen, Yushen and others},
4 journal={arXiv preprint},
5 year={2024}
6}