1 from huggingface_hub import snapshot_download
2
3 # Download the model to the local directory 'Vi-F5-TTS'
4 snapshot_download ( repo_id = "danhtran2mind/Vi-F5-TTS" , local_dir = "Vi-F5-TTS" )
f5-tts_infer-cli \
--model_cfg "vi-fine-tuned-f5-tts.yaml" \
--ckpt_file "model_last.pt" \
--vocab_file "vocab.txt" \
--ref_audio <path_to_your_reference_audio> \
--ref_text <text_of_your_reference_audio> \
--gen_text "Theo đơn vị này, hiện nay do chịu ảnh hưởng của cơn bão số một, lượng rác từ đầu nguồn tấp vào bờ biển rất nhiều. Để giữ cho bãi biển luôn xanh, sạch, đẹp, ban quản lý xin kêu gọi các bạn đoàn viên, tình nguyện viên và bà con nhân dân hãy chung tay cùng ban quản lý dọn vệ sinh môi trường tại tuyến biển Hoàng Sa - Võ Nguyên Giáp - Trường Sa và tuyến Nguyễn Tất Thành."
1 from f5_tts . infer . utils_infer import (
2 cfg_strength ,
3 cross_fade_duration ,
4 device ,
5 fix_duration ,
6 infer_process ,
7 load_model ,
8 load_vocoder ,
9 mel_spec_type ,
10 nfe_step ,
11 preprocess_ref_audio_text ,
12 remove_silence_for_generated_wav ,
13 speed ,
14 sway_sampling_coef ,
15 target_rms ,
16 )
17 from omegaconf import OmegaConf
18 from hydra . utils import get_class
19 import torch
20
21 import re
22 import os
23 import soundfile as sf
24 from pathlib import Path
25 import numpy as np
26 import tomli
27 from importlib . resources import files
28 from unidecode import unidecode
1 ckpt_file = "ckpts/model_last.pt"
2 vocoder_name = "vocos"
3 vocab_file = "vocab.txt"
4 device = "cuda" if torch . cuda . is_available ( ) else "cpu"
5 # Load TTS model
6 model_cfg = OmegaConf . load ( vi - fine - tuned - f5 - tts . yaml" )
7 model_cls = get_class ( f"f5_tts.model. { model_cfg . model . backbone } " )
8 model_arc = model_cfg . model . arch
1 ema_model = load_model (
2 model_cls , model_arc , ckpt_file , mel_spec_type = vocoder_name , vocab_file = vocab_file , device = device
3 )
1 ref_audio = < path_to_your_reference_audio >
2 ref_text = < path_to_your_reference_audio >
3 gen_text = "Theo đơn vị này, hiện nay do chịu ảnh hưởng của cơn bão số một, lượng rác từ đầu nguồn tấp vào bờ biển rất nhiều. Để giữ cho bãi biển luôn xanh, sạch, đẹp, ban quản lý xin kêu gọi các bạn đoàn viên, tình nguyện viên và bà con nhân dân hãy chung tay cùng ban quản lý dọn vệ sinh môi trường tại tuyến biển Hoàng Sa - Võ Nguyên Giáp - Trường Sa và tuyến Nguyễn Tất Thành."
1 voices = { }
2 save_chunk = True
3 output_dir = "test"
4 output_file = "basic_test.wav"
5 wave_path = Path ( output_dir ) / output_file
6 remove_silence = True
7
8 if vocoder_name == "vocos" :
9 vocoder_local_path = "ckpts/vocos-mel-24khz"
10 elif vocoder_name == "bigvgan" :
11 vocoder_local_path = "ckpts/bigvgan_v2_24khz_100band_256x"
12
13 vocoder = load_vocoder (
14 vocoder_name = vocoder_name ,
15 is_local = False ,
16 local_path = vocoder_local_path ,
17 device = device
18 )
19
20 if save_chunk :
21 output_chunk_dir = os . path . join ( output_dir , f" { Path ( output_file ) . stem } _chunks" )
22 if not os . path . exists ( output_chunk_dir ) :
23 os . makedirs ( output_chunk_dir )
1 def infer ( ) :
2 main_voice = { "ref_audio" : ref_audio , "ref_text" : ref_text }
3 # if "voices" not in config:
4 # voices = {"main": main_voice}
5 # else:
6 # voices = config["voices"]
7 voices [ "main" ] = main_voice
8
9 for voice in voices :
10 print ( "Voice:" , voice )
11 print ( "ref_audio " , voices [ voice ] [ "ref_audio" ] )
12 voices [ voice ] [ "ref_audio" ] , voices [ voice ] [ "ref_text" ] = preprocess_ref_audio_text (
13 voices [ voice ] [ "ref_audio" ] , voices [ voice ] [ "ref_text" ]
14 )
15 print ( "ref_audio_" , voices [ voice ] [ "ref_audio" ] , "\n\n" )
16
17 generated_audio_segments = [ ]
18 reg1 = r"(?=\[\w+\])"
19 chunks = re . split ( reg1 , gen_text )
20 print ( "chunks chunks " , chunks )
21 reg2 = r"\[(\w+)\]"
22 for text in chunks :
23 if not text . strip ( ) :
24 continue
25 match = re . match ( reg2 , text )
26 if match :
27 voice = match [ 1 ]
28 else :
29 print ( "No voice tag found, using main." )
30 voice = "main"
31 if voice not in voices :
32 print ( f"Voice { voice } not found, using main." )
33 voice = "main"
34 text = re . sub ( reg2 , "" , text )
35 ref_audio_ = voices [ voice ] [ "ref_audio" ]
36 ref_text_ = voices [ voice ] [ "ref_text" ]
37 gen_text_ = text . strip ( )
38 print ( f"Voice: { voice } " )
39 audio_segment , final_sample_rate , spectrogram = infer_process (
40 ref_audio_ ,
41 ref_text_ ,
42 gen_text_ ,
43 ema_model ,
44 vocoder ,
45 mel_spec_type = vocoder_name ,
46 target_rms = target_rms ,
47 cross_fade_duration = cross_fade_duration ,
48 nfe_step = nfe_step ,
49 cfg_strength = cfg_strength ,
50 sway_sampling_coef = sway_sampling_coef ,
51 speed = speed ,
52 fix_duration = fix_duration ,
53 device = device ,
54 )
55 generated_audio_segments . append ( audio_segment )
56
57 if save_chunk :
58 if len ( gen_text_ ) > 200 :
59 gen_text_ = gen_text_ [ : 200 ] + " ... "
60 sf . write (
61 os . path . join ( output_chunk_dir , f" { len ( generated_audio_segments ) - 1 } _ { unidecode ( gen_text_ ) } .wav" ) ,
62 audio_segment ,
63 final_sample_rate ,
64 )
65
66 if generated_audio_segments :
67 final_wave = np . concatenate ( generated_audio_segments )
68
69 if not os . path . exists ( output_dir ) :
70 os . makedirs ( output_dir )
71
72 with open ( wave_path , "wb" ) as f :
73 sf . write ( f . name , final_wave , final_sample_rate )
74 # Remove silence
75 if remove_silence :
76 remove_silence_for_generated_wav ( f . name )
77 print ( f . name )
78 return final_sample_rate , final_wave , wave_path
79
80 final_sample_rate , final_wave , wave_path = infer ( )
1 from IPython . display import Audio
2 Audio ( data = final_wave , rate = final_sample_rate )