danhtran2mind commited on
Commit
9a4e67f
·
verified ·
1 Parent(s): 39e8ad8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +154 -0
README.md CHANGED
@@ -20,4 +20,158 @@ f5-tts_infer-cli \
20
  --ref_audio "./whatvn/examples/pc-02.wav" \
21
  --ref_text "sông Đồng Nai là con sông nội địa dài nhất Việt Nam, lớn thứ nhì nam bộ về lưu vực chỉ sau sông Cửu long" \
22
  --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."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  ```
 
20
  --ref_audio "./whatvn/examples/pc-02.wav" \
21
  --ref_text "sông Đồng Nai là con sông nội địa dài nhất Việt Nam, lớn thứ nhì nam bộ về lưu vực chỉ sau sông Cửu long" \
22
  --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."
23
+ ```
24
+ ## Clear Inference code
25
+ ```python
26
+ from f5_tts.infer.utils_infer import (
27
+ cfg_strength,
28
+ cross_fade_duration,
29
+ device,
30
+ fix_duration,
31
+ infer_process,
32
+ load_model,
33
+ load_vocoder,
34
+ mel_spec_type,
35
+ nfe_step,
36
+ preprocess_ref_audio_text,
37
+ remove_silence_for_generated_wav,
38
+ speed,
39
+ sway_sampling_coef,
40
+ target_rms,
41
+ )
42
+ from omegaconf import OmegaConf
43
+ from hydra.utils import get_class
44
+ import torch
45
+
46
+ import re
47
+ import os
48
+ import soundfile as sf
49
+ from pathlib import Path
50
+ import numpy as np
51
+ import tomli
52
+ from importlib.resources import files
53
+ from unidecode import unidecode
54
+ ```
55
+
56
+ ```python
57
+ ckpt_file = "vi-f5-tts/ckpts/model_last.pt"
58
+ vocoder_name = "vocos"
59
+ vocab_file = "vi-f5-tts/vocab.txt"
60
+ device = "cuda" if torch.cuda.is_available() else "cpu"
61
+ # Load TTS model
62
+ model_cfg = OmegaConf.load("vi-f5-tts/vi-fine-tuned-f5-tts.yaml")
63
+ model_cls = get_class(f"f5_tts.model.{model_cfg.model.backbone}")
64
+ model_arc = model_cfg.model.arch
65
+ ```
66
+
67
+ ```python
68
+ ema_model = load_model(
69
+ model_cls, model_arc, ckpt_file, mel_spec_type=vocoder_name, vocab_file=vocab_file, device=device
70
+ )
71
+ ```
72
+ ```python
73
+ ref_audio = "./whatvn/examples/pc-02.wav"
74
+ ref_text = "sông Đồng Nai là con sông nội địa dài nhất Việt Nam, lớn thứ nhì nam bộ về lưu vực chỉ sau sông Cửu long"
75
+ 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."
76
+ ```
77
+
78
+ ```python
79
+ voices = {}
80
+ save_chunk = True
81
+ output_dir = "test"
82
+ output_file = "basic_test.wav"
83
+ wave_path = Path(output_dir) / output_file
84
+ remove_silence = True
85
+
86
+ if save_chunk:
87
+ output_chunk_dir = os.path.join(output_dir, f"{Path(output_file).stem}_chunks")
88
+ if not os.path.exists(output_chunk_dir):
89
+ os.makedirs(output_chunk_dir)
90
+ ```
91
+
92
+ ```python
93
+ def infer():
94
+ main_voice = {"ref_audio": ref_audio, "ref_text": ref_text}
95
+ # if "voices" not in config:
96
+ # voices = {"main": main_voice}
97
+ # else:
98
+ # voices = config["voices"]
99
+ voices["main"] = main_voice
100
+
101
+ for voice in voices:
102
+ print("Voice:", voice)
103
+ print("ref_audio ", voices[voice]["ref_audio"])
104
+ voices[voice]["ref_audio"], voices[voice]["ref_text"] = preprocess_ref_audio_text(
105
+ voices[voice]["ref_audio"], voices[voice]["ref_text"]
106
+ )
107
+ print("ref_audio_", voices[voice]["ref_audio"], "\n\n")
108
+
109
+ generated_audio_segments = []
110
+ reg1 = r"(?=\[\w+\])"
111
+ chunks = re.split(reg1, gen_text)
112
+ print("chunks chunks ", chunks)
113
+ reg2 = r"\[(\w+)\]"
114
+ for text in chunks:
115
+ if not text.strip():
116
+ continue
117
+ match = re.match(reg2, text)
118
+ if match:
119
+ voice = match[1]
120
+ else:
121
+ print("No voice tag found, using main.")
122
+ voice = "main"
123
+ if voice not in voices:
124
+ print(f"Voice {voice} not found, using main.")
125
+ voice = "main"
126
+ text = re.sub(reg2, "", text)
127
+ ref_audio_ = voices[voice]["ref_audio"]
128
+ ref_text_ = voices[voice]["ref_text"]
129
+ gen_text_ = text.strip()
130
+ print(f"Voice: {voice}")
131
+ audio_segment, final_sample_rate, spectrogram = infer_process(
132
+ ref_audio_,
133
+ ref_text_,
134
+ gen_text_,
135
+ ema_model,
136
+ vocoder,
137
+ mel_spec_type=vocoder_name,
138
+ target_rms=target_rms,
139
+ cross_fade_duration=cross_fade_duration,
140
+ nfe_step=nfe_step,
141
+ cfg_strength=cfg_strength,
142
+ sway_sampling_coef=sway_sampling_coef,
143
+ speed=speed,
144
+ fix_duration=fix_duration,
145
+ device=device,
146
+ )
147
+ generated_audio_segments.append(audio_segment)
148
+
149
+ if save_chunk:
150
+ if len(gen_text_) > 200:
151
+ gen_text_ = gen_text_[:200] + " ... "
152
+ sf.write(
153
+ os.path.join(output_chunk_dir, f"{len(generated_audio_segments) - 1}_{unidecode(gen_text_)}.wav"),
154
+ audio_segment,
155
+ final_sample_rate,
156
+ )
157
+
158
+ if generated_audio_segments:
159
+ final_wave = np.concatenate(generated_audio_segments)
160
+
161
+ if not os.path.exists(output_dir):
162
+ os.makedirs(output_dir)
163
+
164
+ with open(wave_path, "wb") as f:
165
+ sf.write(f.name, final_wave, final_sample_rate)
166
+ # Remove silence
167
+ if remove_silence:
168
+ remove_silence_for_generated_wav(f.name)
169
+ print(f.name)
170
+ return final_sample_rate, final_wave, wave_path
171
+
172
+ final_sample_rate, final_wave, wave_path = infer()
173
+ ```
174
+ ```python
175
+ from IPython.display import Audio
176
+ Audio(data=final_wave, rate=final_sample_rate)
177
  ```