Spaces:
Sleeping
Sleeping
mrsu0994
commited on
Commit
·
14100c7
1
Parent(s):
0d26c6c
upload f5-tts source
Browse files- app copy.py +405 -0
- app.py +139 -29
- requirements copy.txt +47 -0
app copy.py
ADDED
@@ -0,0 +1,405 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import subprocess
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
from pydub import AudioSegment
|
6 |
+
import gradio as gr
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Thêm thư mục src vào sys.path
|
10 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
11 |
+
|
12 |
+
def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
13 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
14 |
+
infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
15 |
+
tests_dir = os.path.join(current_dir, "tests")
|
16 |
+
|
17 |
+
print(f"Infer CLI path: {infer_cli_path}")
|
18 |
+
print(f"Does infer_cli.py exist? {os.path.exists(infer_cli_path)}")
|
19 |
+
if not os.path.exists(infer_cli_path):
|
20 |
+
return None, "File infer_cli.py không tồn tại!"
|
21 |
+
|
22 |
+
try:
|
23 |
+
vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
24 |
+
ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
25 |
+
except Exception as e:
|
26 |
+
return None, f"Lỗi khi tải model/vocab: {str(e)}"
|
27 |
+
|
28 |
+
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
29 |
+
env = os.environ.copy()
|
30 |
+
env['PYTHONPATH'] = os.path.abspath(os.path.join(current_dir, 'src'))
|
31 |
+
|
32 |
+
command = [
|
33 |
+
sys.executable,
|
34 |
+
infer_cli_path,
|
35 |
+
"--model", model,
|
36 |
+
"--ref_audio", ref_audio_path,
|
37 |
+
"--ref_text", ref_text,
|
38 |
+
"--gen_text", gen_text,
|
39 |
+
"--speed", str(speed),
|
40 |
+
"--vocoder_name", vocoder_name,
|
41 |
+
"--vocab_file", vocab_file,
|
42 |
+
"--ckpt_file", ckpt_file
|
43 |
+
]
|
44 |
+
|
45 |
+
print(f"Running command: {' '.join(command)}")
|
46 |
+
try:
|
47 |
+
result = subprocess.run(
|
48 |
+
command,
|
49 |
+
check=True,
|
50 |
+
capture_output=True,
|
51 |
+
text=True,
|
52 |
+
env=env
|
53 |
+
)
|
54 |
+
print("Subprocess stdout:", result.stdout)
|
55 |
+
if os.path.exists(tests_dir):
|
56 |
+
wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
57 |
+
if wav_files:
|
58 |
+
latest_wav = max(wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x)))
|
59 |
+
output_wav = os.path.join(tests_dir, latest_wav)
|
60 |
+
audio = AudioSegment.from_wav(output_wav)
|
61 |
+
output_mp3 = os.path.join(tests_dir, "output.mp3")
|
62 |
+
audio.export(output_mp3, format="mp3")
|
63 |
+
return output_mp3, "Suy luận thành công!"
|
64 |
+
return None, "Không tìm thấy file âm thanh trong thư mục tests"
|
65 |
+
except subprocess.CalledProcessError as e:
|
66 |
+
return None, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
67 |
+
except Exception as e:
|
68 |
+
return None, str(e)
|
69 |
+
|
70 |
+
def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
71 |
+
if ref_audio is None:
|
72 |
+
return None, "Vui lòng tải lên file audio tham chiếu!"
|
73 |
+
# ref_audio là đường dẫn file, tải bằng AudioSegment
|
74 |
+
audio_segment = AudioSegment.from_file(ref_audio)
|
75 |
+
audio_segment = audio_segment.set_channels(1) # Chuyển sang mono
|
76 |
+
ref_audio_path = f"temp_ref_{int(time.time())}.wav"
|
77 |
+
audio_segment.export(ref_audio_path, format="wav")
|
78 |
+
|
79 |
+
output_mp3, message = run_f5_tts(ref_audio_path, ref_text, gen_text, model, float(speed))
|
80 |
+
os.remove(ref_audio_path)
|
81 |
+
|
82 |
+
if output_mp3 and os.path.exists(output_mp3):
|
83 |
+
return output_mp3, message
|
84 |
+
return None, message
|
85 |
+
|
86 |
+
interface = gr.Interface(
|
87 |
+
fn=generate_speech,
|
88 |
+
inputs=[
|
89 |
+
gr.Audio(type="filepath", label="Tải lên file audio tham chiếu (.wav hoặc .mp3)"),
|
90 |
+
gr.Textbox(label="Text tham chiếu", placeholder="Nhập text của audio tham chiếu"),
|
91 |
+
gr.Textbox(label="Text cần sinh", placeholder="Nhập text bạn muốn sinh"),
|
92 |
+
gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Tốc độ"),
|
93 |
+
gr.Dropdown(choices=["F5TTS_Base"], value="F5TTS_Base", label="Mô hình")
|
94 |
+
],
|
95 |
+
outputs=[
|
96 |
+
gr.Audio(type="filepath", label="Kết quả audio (.mp3)"),
|
97 |
+
gr.Textbox(label="Trạng thái")
|
98 |
+
],
|
99 |
+
title="F5-TTS Suy luận",
|
100 |
+
description="Tải lên audio tham chiếu, nhập text, và sinh audio mới với F5-TTS."
|
101 |
+
)
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
# import os
|
109 |
+
# import sys
|
110 |
+
# import subprocess
|
111 |
+
# from huggingface_hub import hf_hub_download
|
112 |
+
# from pydub import AudioSegment
|
113 |
+
# import gradio as gr
|
114 |
+
# import time
|
115 |
+
|
116 |
+
# # Thêm thư mục src vào sys.path
|
117 |
+
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
118 |
+
|
119 |
+
# def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
120 |
+
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
121 |
+
# infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
122 |
+
# tests_dir = os.path.join(current_dir, "tests")
|
123 |
+
|
124 |
+
# # Debug: In đường dẫn để kiểm tra
|
125 |
+
# print(f"Infer CLI path: {infer_cli_path}")
|
126 |
+
# print(f"Tests dir: {tests_dir}")
|
127 |
+
|
128 |
+
# # Tải file từ Hugging Face Hub
|
129 |
+
# try:
|
130 |
+
# vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
131 |
+
# ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
132 |
+
# except Exception as e:
|
133 |
+
# return None, f"Lỗi khi tải model/vocab từ Hugging Face: {str(e)}"
|
134 |
+
|
135 |
+
# os.environ['PYTHONIOENCODING'] = 'utf-8'
|
136 |
+
# env = os.environ.copy()
|
137 |
+
# env['PYTHONPATH'] = os.path.abspath(os.path.join(current_dir, 'src'))
|
138 |
+
|
139 |
+
# command = [
|
140 |
+
# sys.executable,
|
141 |
+
# infer_cli_path,
|
142 |
+
# "--model", model,
|
143 |
+
# "--ref_audio", ref_audio_path,
|
144 |
+
# "--ref_text", ref_text,
|
145 |
+
# "--gen_text", gen_text,
|
146 |
+
# "--speed", str(speed),
|
147 |
+
# "--vocoder_name", vocoder_name,
|
148 |
+
# "--vocab_file", vocab_file,
|
149 |
+
# "--ckpt_file", ckpt_file
|
150 |
+
# ]
|
151 |
+
|
152 |
+
# print(f"Running command: {' '.join(command)}")
|
153 |
+
# try:
|
154 |
+
# result = subprocess.run(
|
155 |
+
# command,
|
156 |
+
# check=True,
|
157 |
+
# capture_output=True,
|
158 |
+
# text=True,
|
159 |
+
# env=env
|
160 |
+
# )
|
161 |
+
# print("Subprocess stdout:", result.stdout)
|
162 |
+
# if os.path.exists(tests_dir):
|
163 |
+
# wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
164 |
+
# if wav_files:
|
165 |
+
# latest_wav = max(wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x)))
|
166 |
+
# output_wav = os.path.join(tests_dir, latest_wav)
|
167 |
+
# audio = AudioSegment.from_wav(output_wav)
|
168 |
+
# output_mp3 = os.path.join(tests_dir, "output.mp3")
|
169 |
+
# audio.export(output_mp3, format="mp3")
|
170 |
+
# return output_mp3, "Suy luận thành công!"
|
171 |
+
|
172 |
+
# return None, "Không tìm thấy file âm thanh trong thư mục tests"
|
173 |
+
# except subprocess.CalledProcessError as e:
|
174 |
+
# return None, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
175 |
+
# except Exception as e:
|
176 |
+
# return None, str(e)
|
177 |
+
|
178 |
+
# def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
179 |
+
# if ref_audio is None:
|
180 |
+
# return None, "Vui lòng tải lên file audio tham chiếu!"
|
181 |
+
# ref_audio_path = f"temp_ref_{int(time.time())}.wav"
|
182 |
+
# ref_audio.convert_audio_channels(1) # Chuyển sang mono
|
183 |
+
# ref_audio.export(ref_audio_path, format="wav")
|
184 |
+
|
185 |
+
# output_mp3, message = run_f5_tts(ref_audio_path, ref_text, gen_text, model, float(speed))
|
186 |
+
# os.remove(ref_audio_path)
|
187 |
+
|
188 |
+
# if output_mp3 and os.path.exists(output_mp3):
|
189 |
+
# return output_mp3, message
|
190 |
+
# return None, message
|
191 |
+
|
192 |
+
# interface = gr.Interface(
|
193 |
+
# fn=generate_speech,
|
194 |
+
# inputs=[
|
195 |
+
# gr.Audio(type="filepath", label="Tải lên file audio tham chiếu (.wav hoặc .mp3)"),
|
196 |
+
# gr.Textbox(label="Text tham chiếu", placeholder="Nhập text của audio tham chiếu"),
|
197 |
+
# gr.Textbox(label="Text cần sinh", placeholder="Nhập text bạn muốn sinh"),
|
198 |
+
# gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Tốc độ"),
|
199 |
+
# gr.Dropdown(choices=["F5TTS_Base"], value="F5TTS_Base", label="Mô hình")
|
200 |
+
# ],
|
201 |
+
# outputs=[
|
202 |
+
# gr.Audio(type="filepath", label="Kết quả audio (.mp3)"),
|
203 |
+
# gr.Textbox(label="Trạng thái")
|
204 |
+
# ],
|
205 |
+
# title="F5-TTS Suy luận",
|
206 |
+
# description="Tải lên audio tham chiếu, nhập text, và sinh audio mới với F5-TTS."
|
207 |
+
# )
|
208 |
+
|
209 |
+
# if __name__ == "__main__":
|
210 |
+
# interface.launch(server_name="0.0.0.0", server_port=7860)
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
# from flask import Flask, request, send_file
|
216 |
+
# import subprocess
|
217 |
+
# import os
|
218 |
+
# import sys
|
219 |
+
# from huggingface_hub import hf_hub_download
|
220 |
+
# from pydub import AudioSegment
|
221 |
+
|
222 |
+
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
223 |
+
|
224 |
+
# app = Flask(__name__)
|
225 |
+
|
226 |
+
# def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
227 |
+
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
228 |
+
# infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
229 |
+
# tests_dir = os.path.join(current_dir, "tests")
|
230 |
+
|
231 |
+
# vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
232 |
+
# ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
233 |
+
|
234 |
+
# os.environ['PYTHONIOENCODING'] = 'utf-8'
|
235 |
+
# env = os.environ.copy()
|
236 |
+
# env['PYTHONPATH'] = os.path.abspath(os.path.join(current_dir, 'src'))
|
237 |
+
|
238 |
+
# command = [
|
239 |
+
# sys.executable,
|
240 |
+
# infer_cli_path,
|
241 |
+
# "--model", model,
|
242 |
+
# "--ref_audio", ref_audio_path,
|
243 |
+
# "--ref_text", ref_text,
|
244 |
+
# "--gen_text", gen_text,
|
245 |
+
# "--speed", str(speed),
|
246 |
+
# "--vocoder_name", vocoder_name,
|
247 |
+
# "--vocab_file", vocab_file,
|
248 |
+
# "--ckpt_file", ckpt_file
|
249 |
+
# ]
|
250 |
+
|
251 |
+
# try:
|
252 |
+
# result = subprocess.run(
|
253 |
+
# command,
|
254 |
+
# check=True,
|
255 |
+
# capture_output=True,
|
256 |
+
# text=True,
|
257 |
+
# encoding='utf-8',
|
258 |
+
# env=env
|
259 |
+
# )
|
260 |
+
|
261 |
+
# if os.path.exists(tests_dir):
|
262 |
+
# wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
263 |
+
# if wav_files:
|
264 |
+
# latest_wav = max(wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x)))
|
265 |
+
# output_wav = os.path.join(tests_dir, latest_wav)
|
266 |
+
# audio = AudioSegment.from_wav(output_wav)
|
267 |
+
# output_mp3 = os.path.join(tests_dir, "output.mp3")
|
268 |
+
# audio.export(output_mp3, format="mp3")
|
269 |
+
# return True, output_mp3
|
270 |
+
|
271 |
+
# return False, "Không tìm thấy file âm thanh trong thư mục tests"
|
272 |
+
# except subprocess.CalledProcessError as e:
|
273 |
+
# return False, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
274 |
+
# except Exception as e:
|
275 |
+
# return False, str(e)
|
276 |
+
|
277 |
+
# @app.route('/')
|
278 |
+
# def home():
|
279 |
+
# return "F5-TTS API is running. Use POST /api/generate to generate audio."
|
280 |
+
|
281 |
+
# @app.route('/api/generate', methods=['POST'])
|
282 |
+
# def generate_speech():
|
283 |
+
# if 'ref_audio' not in request.files:
|
284 |
+
# return {"error": "Missing ref_audio"}, 400
|
285 |
+
# ref_audio = request.files['ref_audio']
|
286 |
+
# ref_text = request.form.get('ref_text', '')
|
287 |
+
# gen_text = request.form.get('gen_text', '')
|
288 |
+
# model = request.form.get('model', 'F5TTS_Base')
|
289 |
+
# speed = float(request.form.get('speed', 1.2))
|
290 |
+
|
291 |
+
# import time
|
292 |
+
# ref_audio_path = f"temp_ref_{int(time.time())}.wav"
|
293 |
+
# ref_audio.save(ref_audio_path)
|
294 |
+
|
295 |
+
# success, result = run_f5_tts(ref_audio_path, ref_text, gen_text, model, speed)
|
296 |
+
# os.remove(ref_audio_path)
|
297 |
+
|
298 |
+
# if success:
|
299 |
+
# return send_file(result, mimetype='audio/mpeg')
|
300 |
+
# else:
|
301 |
+
# return {"error": result}, 500
|
302 |
+
|
303 |
+
# if __name__ == "__main__":
|
304 |
+
# port = int(os.environ.get("PORT", 7860))
|
305 |
+
# app.run(host="0.0.0.0", port=port, debug=False)
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
# from flask import Flask, request, send_file
|
310 |
+
# import subprocess
|
311 |
+
# import os
|
312 |
+
# import sys
|
313 |
+
# from huggingface_hub import hf_hub_download
|
314 |
+
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
315 |
+
|
316 |
+
# app = Flask(__name__)
|
317 |
+
|
318 |
+
# # =========================
|
319 |
+
# # Hàm chạy F5-TTS
|
320 |
+
# # =========================
|
321 |
+
# def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
322 |
+
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
323 |
+
# infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
324 |
+
# tests_dir = os.path.join(current_dir, "tests")
|
325 |
+
|
326 |
+
# # Dùng huggingface_hub để tải file model và vocab từ repo 'nguyensu27/TTS'
|
327 |
+
# vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
328 |
+
# ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
329 |
+
|
330 |
+
# os.environ['PYTHONIOENCODING'] = 'utf-8'
|
331 |
+
|
332 |
+
# command = [
|
333 |
+
# sys.executable,
|
334 |
+
# infer_cli_path,
|
335 |
+
# "--model", model,
|
336 |
+
# "--ref_audio", ref_audio_path,
|
337 |
+
# "--ref_text", ref_text,
|
338 |
+
# "--gen_text", gen_text,
|
339 |
+
# "--speed", str(speed),
|
340 |
+
# "--vocoder_name", vocoder_name,
|
341 |
+
# "--vocab_file", vocab_file,
|
342 |
+
# "--ckpt_file", ckpt_file
|
343 |
+
# ]
|
344 |
+
|
345 |
+
# try:
|
346 |
+
# result = subprocess.run(
|
347 |
+
# command,
|
348 |
+
# check=True,
|
349 |
+
# capture_output=True,
|
350 |
+
# text=True,
|
351 |
+
# encoding='utf-8'
|
352 |
+
# )
|
353 |
+
|
354 |
+
# if os.path.exists(tests_dir):
|
355 |
+
# wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
356 |
+
# if wav_files:
|
357 |
+
# latest_wav = max(
|
358 |
+
# wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x))
|
359 |
+
# )
|
360 |
+
# output_file = os.path.join(tests_dir, latest_wav)
|
361 |
+
# return True, output_file
|
362 |
+
|
363 |
+
# return False, "Không tìm thấy file âm thanh trong thư mục tests"
|
364 |
+
# except subprocess.CalledProcessError as e:
|
365 |
+
# return False, e.stderr
|
366 |
+
# except Exception as e:
|
367 |
+
# return False, str(e)
|
368 |
+
|
369 |
+
|
370 |
+
# # =========================
|
371 |
+
# # Routes
|
372 |
+
# # =========================
|
373 |
+
# @app.route('/')
|
374 |
+
# def home():
|
375 |
+
# return "F5-TTS API is running. Use POST /api/generate to generate audio."
|
376 |
+
|
377 |
+
|
378 |
+
# @app.route('/api/generate', methods=['POST'])
|
379 |
+
# def generate_speech():
|
380 |
+
# if 'ref_audio' not in request.files:
|
381 |
+
# return {"error": "Missing ref_audio"}, 400
|
382 |
+
# ref_audio = request.files['ref_audio']
|
383 |
+
# ref_text = request.form.get('ref_text', '')
|
384 |
+
# gen_text = request.form.get('gen_text', '')
|
385 |
+
# model = request.form.get('model', 'F5TTS_Base')
|
386 |
+
# speed = float(request.form.get('speed', 1.2))
|
387 |
+
|
388 |
+
# ref_audio_path = 'temp_ref.wav'
|
389 |
+
# ref_audio.save(ref_audio_path)
|
390 |
+
|
391 |
+
# success, result = run_f5_tts(ref_audio_path, ref_text, gen_text, model, speed)
|
392 |
+
# os.remove(ref_audio_path)
|
393 |
+
|
394 |
+
# if success:
|
395 |
+
# return send_file(result, mimetype='audio/wav')
|
396 |
+
# else:
|
397 |
+
# return {"error": result}, 500
|
398 |
+
|
399 |
+
|
400 |
+
# # =========================
|
401 |
+
# # Main
|
402 |
+
# # =========================
|
403 |
+
# if __name__ == "__main__":
|
404 |
+
# port = int(os.environ.get("PORT", 7860))
|
405 |
+
# app.run(host="0.0.0.0", port=port, debug=False)
|
app.py
CHANGED
@@ -14,8 +14,6 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
14 |
infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
15 |
tests_dir = os.path.join(current_dir, "tests")
|
16 |
|
17 |
-
print(f"Infer CLI path: {infer_cli_path}")
|
18 |
-
print(f"Does infer_cli.py exist? {os.path.exists(infer_cli_path)}")
|
19 |
if not os.path.exists(infer_cli_path):
|
20 |
return None, "File infer_cli.py không tồn tại!"
|
21 |
|
@@ -42,7 +40,6 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
42 |
"--ckpt_file", ckpt_file
|
43 |
]
|
44 |
|
45 |
-
print(f"Running command: {' '.join(command)}")
|
46 |
try:
|
47 |
result = subprocess.run(
|
48 |
command,
|
@@ -51,7 +48,7 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
51 |
text=True,
|
52 |
env=env
|
53 |
)
|
54 |
-
|
55 |
if os.path.exists(tests_dir):
|
56 |
wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
57 |
if wav_files:
|
@@ -60,8 +57,8 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
60 |
audio = AudioSegment.from_wav(output_wav)
|
61 |
output_mp3 = os.path.join(tests_dir, "output.mp3")
|
62 |
audio.export(output_mp3, format="mp3")
|
63 |
-
return output_mp3, "Suy luận thành công!"
|
64 |
-
return None, "Không tìm thấy file âm thanh trong thư mục tests"
|
65 |
except subprocess.CalledProcessError as e:
|
66 |
return None, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
67 |
except Exception as e:
|
@@ -69,7 +66,8 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
69 |
|
70 |
def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
71 |
if ref_audio is None:
|
72 |
-
return None, "Vui lòng tải lên file audio tham chiếu!"
|
|
|
73 |
# ref_audio là đường dẫn file, tải bằng AudioSegment
|
74 |
audio_segment = AudioSegment.from_file(ref_audio)
|
75 |
audio_segment = audio_segment.set_channels(1) # Chuyển sang mono
|
@@ -77,34 +75,146 @@ def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
|
77 |
audio_segment.export(ref_audio_path, format="wav")
|
78 |
|
79 |
output_mp3, message = run_f5_tts(ref_audio_path, ref_text, gen_text, model, float(speed))
|
80 |
-
|
81 |
-
|
82 |
-
if
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
gr.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
)
|
|
|
|
|
|
|
|
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
interface.launch(server_name="0.0.0.0", server_port=7860)
|
105 |
|
106 |
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# import os
|
109 |
# import sys
|
110 |
# import subprocess
|
|
|
14 |
infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
15 |
tests_dir = os.path.join(current_dir, "tests")
|
16 |
|
|
|
|
|
17 |
if not os.path.exists(infer_cli_path):
|
18 |
return None, "File infer_cli.py không tồn tại!"
|
19 |
|
|
|
40 |
"--ckpt_file", ckpt_file
|
41 |
]
|
42 |
|
|
|
43 |
try:
|
44 |
result = subprocess.run(
|
45 |
command,
|
|
|
48 |
text=True,
|
49 |
env=env
|
50 |
)
|
51 |
+
# Kiểm tra thư mục tests có file wav xuất ra không
|
52 |
if os.path.exists(tests_dir):
|
53 |
wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
54 |
if wav_files:
|
|
|
57 |
audio = AudioSegment.from_wav(output_wav)
|
58 |
output_mp3 = os.path.join(tests_dir, "output.mp3")
|
59 |
audio.export(output_mp3, format="mp3")
|
60 |
+
return output_mp3, "✅ Suy luận thành công!"
|
61 |
+
return None, "❌ Không tìm thấy file âm thanh trong thư mục tests"
|
62 |
except subprocess.CalledProcessError as e:
|
63 |
return None, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
64 |
except Exception as e:
|
|
|
66 |
|
67 |
def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
68 |
if ref_audio is None:
|
69 |
+
return None, "⚠️ Vui lòng tải lên file audio tham chiếu!"
|
70 |
+
|
71 |
# ref_audio là đường dẫn file, tải bằng AudioSegment
|
72 |
audio_segment = AudioSegment.from_file(ref_audio)
|
73 |
audio_segment = audio_segment.set_channels(1) # Chuyển sang mono
|
|
|
75 |
audio_segment.export(ref_audio_path, format="wav")
|
76 |
|
77 |
output_mp3, message = run_f5_tts(ref_audio_path, ref_text, gen_text, model, float(speed))
|
78 |
+
|
79 |
+
# Xóa file tạm
|
80 |
+
if os.path.exists(ref_audio_path):
|
81 |
+
os.remove(ref_audio_path)
|
82 |
+
|
83 |
+
return output_mp3, message
|
84 |
+
|
85 |
+
# ====================== Gradio UI ======================
|
86 |
+
with gr.Blocks() as interface:
|
87 |
+
gr.Markdown("## 🎙️ F5-TTS Suy luận")
|
88 |
+
gr.Markdown("Tải lên audio tham chiếu, nhập text, và sinh audio mới với F5-TTS.")
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
ref_audio = gr.Audio(type="filepath", label="📂 Tải lên file audio tham chiếu (.wav hoặc .mp3)")
|
93 |
+
ref_text = gr.Textbox(label="📝 Text tham chiếu")
|
94 |
+
gen_text = gr.Textbox(label="📝 Text cần sinh")
|
95 |
+
speed = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="⚡ Tốc độ")
|
96 |
+
model = gr.Dropdown(choices=["F5TTS_Base"], value="F5TTS_Base", label="🤖 Mô hình")
|
97 |
+
btn = gr.Button("🚀 Sinh giọng nói")
|
98 |
+
|
99 |
+
with gr.Column():
|
100 |
+
output_audio = gr.Audio(type="filepath", label="🔊 Kết quả audio (.mp3)")
|
101 |
+
output_status = gr.Textbox(label="📌 Trạng thái")
|
102 |
+
|
103 |
+
btn.click(generate_speech, [ref_audio, ref_text, gen_text, speed, model], [output_audio, output_status])
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
interface.launch(server_name="0.0.0.0", server_port=7860)
|
107 |
|
108 |
|
109 |
|
110 |
+
|
111 |
+
# import os
|
112 |
+
# import sys
|
113 |
+
# import subprocess
|
114 |
+
# from huggingface_hub import hf_hub_download
|
115 |
+
# from pydub import AudioSegment
|
116 |
+
# import gradio as gr
|
117 |
+
# import time
|
118 |
+
|
119 |
+
# # Thêm thư mục src vào sys.path
|
120 |
+
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
121 |
+
|
122 |
+
# def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
123 |
+
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
124 |
+
# infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
125 |
+
# tests_dir = os.path.join(current_dir, "tests")
|
126 |
+
|
127 |
+
# print(f"Infer CLI path: {infer_cli_path}")
|
128 |
+
# print(f"Does infer_cli.py exist? {os.path.exists(infer_cli_path)}")
|
129 |
+
# if not os.path.exists(infer_cli_path):
|
130 |
+
# return None, "File infer_cli.py không tồn tại!"
|
131 |
+
|
132 |
+
# try:
|
133 |
+
# vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
134 |
+
# ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
135 |
+
# except Exception as e:
|
136 |
+
# return None, f"Lỗi khi tải model/vocab: {str(e)}"
|
137 |
+
|
138 |
+
# os.environ['PYTHONIOENCODING'] = 'utf-8'
|
139 |
+
# env = os.environ.copy()
|
140 |
+
# env['PYTHONPATH'] = os.path.abspath(os.path.join(current_dir, 'src'))
|
141 |
+
|
142 |
+
# command = [
|
143 |
+
# sys.executable,
|
144 |
+
# infer_cli_path,
|
145 |
+
# "--model", model,
|
146 |
+
# "--ref_audio", ref_audio_path,
|
147 |
+
# "--ref_text", ref_text,
|
148 |
+
# "--gen_text", gen_text,
|
149 |
+
# "--speed", str(speed),
|
150 |
+
# "--vocoder_name", vocoder_name,
|
151 |
+
# "--vocab_file", vocab_file,
|
152 |
+
# "--ckpt_file", ckpt_file
|
153 |
+
# ]
|
154 |
+
|
155 |
+
# print(f"Running command: {' '.join(command)}")
|
156 |
+
# try:
|
157 |
+
# result = subprocess.run(
|
158 |
+
# command,
|
159 |
+
# check=True,
|
160 |
+
# capture_output=True,
|
161 |
+
# text=True,
|
162 |
+
# env=env
|
163 |
+
# )
|
164 |
+
# print("Subprocess stdout:", result.stdout)
|
165 |
+
# if os.path.exists(tests_dir):
|
166 |
+
# wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
167 |
+
# if wav_files:
|
168 |
+
# latest_wav = max(wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x)))
|
169 |
+
# output_wav = os.path.join(tests_dir, latest_wav)
|
170 |
+
# audio = AudioSegment.from_wav(output_wav)
|
171 |
+
# output_mp3 = os.path.join(tests_dir, "output.mp3")
|
172 |
+
# audio.export(output_mp3, format="mp3")
|
173 |
+
# return output_mp3, "Suy luận thành công!"
|
174 |
+
# return None, "Không tìm thấy file âm thanh trong thư mục tests"
|
175 |
+
# except subprocess.CalledProcessError as e:
|
176 |
+
# return None, f"Lỗi khi chạy infer_cli.py: {e.stderr}"
|
177 |
+
# except Exception as e:
|
178 |
+
# return None, str(e)
|
179 |
+
|
180 |
+
# def generate_speech(ref_audio, ref_text, gen_text, speed, model):
|
181 |
+
# if ref_audio is None:
|
182 |
+
# return None, "Vui lòng tải lên file audio tham chiếu!"
|
183 |
+
# # ref_audio là đường dẫn file, tải bằng AudioSegment
|
184 |
+
# audio_segment = AudioSegment.from_file(ref_audio)
|
185 |
+
# audio_segment = audio_segment.set_channels(1) # Chuyển sang mono
|
186 |
+
# ref_audio_path = f"temp_ref_{int(time.time())}.wav"
|
187 |
+
# audio_segment.export(ref_audio_path, format="wav")
|
188 |
+
|
189 |
+
# output_mp3, message = run_f5_tts(ref_audio_path, ref_text, gen_text, model, float(speed))
|
190 |
+
# os.remove(ref_audio_path)
|
191 |
+
|
192 |
+
# if output_mp3 and os.path.exists(output_mp3):
|
193 |
+
# return output_mp3, message
|
194 |
+
# return None, message
|
195 |
+
|
196 |
+
# interface = gr.Interface(
|
197 |
+
# fn=generate_speech,
|
198 |
+
# inputs=[
|
199 |
+
# gr.Audio(type="filepath", label="Tải lên file audio tham chiếu (.wav hoặc .mp3)"),
|
200 |
+
# gr.Textbox(label="Text tham chiếu", placeholder="Nhập text của audio tham chiếu"),
|
201 |
+
# gr.Textbox(label="Text cần sinh", placeholder="Nhập text bạn muốn sinh"),
|
202 |
+
# gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Tốc độ"),
|
203 |
+
# gr.Dropdown(choices=["F5TTS_Base"], value="F5TTS_Base", label="Mô hình")
|
204 |
+
# ],
|
205 |
+
# outputs=[
|
206 |
+
# gr.Audio(type="filepath", label="Kết quả audio (.mp3)"),
|
207 |
+
# gr.Textbox(label="Trạng thái")
|
208 |
+
# ],
|
209 |
+
# title="F5-TTS Suy luận",
|
210 |
+
# description="Tải lên audio tham chiếu, nhập text, và sinh audio mới với F5-TTS."
|
211 |
+
# )
|
212 |
+
|
213 |
+
# if __name__ == "__main__":
|
214 |
+
# interface.launch(server_name="0.0.0.0", server_port=7860)
|
215 |
+
|
216 |
+
|
217 |
+
|
218 |
# import os
|
219 |
# import sys
|
220 |
# import subprocess
|
requirements copy.txt
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
aiohttp
|
3 |
+
anyio
|
4 |
+
attrs
|
5 |
+
audioread
|
6 |
+
bitsandbytes
|
7 |
+
datasets
|
8 |
+
einops
|
9 |
+
ema-pytorch
|
10 |
+
encodec
|
11 |
+
fastapi
|
12 |
+
ffmpy
|
13 |
+
Flask
|
14 |
+
gradio
|
15 |
+
huggingface-hub
|
16 |
+
hydra-core
|
17 |
+
joblib
|
18 |
+
librosa
|
19 |
+
loguru
|
20 |
+
matplotlib
|
21 |
+
numpy
|
22 |
+
omegaconf
|
23 |
+
pandas
|
24 |
+
pillow
|
25 |
+
psutil
|
26 |
+
pydantic
|
27 |
+
pydub
|
28 |
+
pypinyin
|
29 |
+
requests
|
30 |
+
scikit-learn
|
31 |
+
scipy
|
32 |
+
soundfile
|
33 |
+
soxr
|
34 |
+
starlette
|
35 |
+
sympy
|
36 |
+
torch>=2.2
|
37 |
+
torchaudio>=2.2
|
38 |
+
tqdm
|
39 |
+
transformers>=4.40
|
40 |
+
uvicorn
|
41 |
+
vocos
|
42 |
+
x-transformers
|
43 |
+
huggingface_hub
|
44 |
+
tomli
|
45 |
+
cached-path
|
46 |
+
gradio
|
47 |
+
torchdiffeq
|