| import gradio as gr | |
| import subprocess | |
| import os | |
| from datetime import datetime | |
| def run_infer_cli(ref_audio, ref_text, gen_text): | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| output_dir = "gradio_output" | |
| output_path = f"{output_dir}/gen_{timestamp}.wav" | |
| os.makedirs(output_dir, exist_ok=True) | |
| cmd = [ | |
| "python", "src/f5_tts/infer/infer_cli.py", | |
| "--model", "F5TTS_Base", | |
| "--ref_audio", ref_audio, | |
| "--ref_text", ref_text, | |
| "--gen_text", gen_text, | |
| "--speed", "1.0", | |
| "--vocoder_name", "vocos", | |
| "--vocab_file", "data/viVoice/vocab.txt", | |
| "--ckpt_file", "ckpts/viVoice/model_last.pt", | |
| "--output_dir", output_dir, | |
| "--output_file", f"gen_{timestamp}.wav" | |
| ] | |
| try: | |
| subprocess.run(cmd, check=True) | |
| return output_path | |
| except subprocess.CalledProcessError as e: | |
| return f"Lỗi khi chạy infer_cli.py: {e}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🔉 Tạo giọng nói bằng F5-TTS (dùng infer_cli.py)") | |
| ref_audio = gr.Audio(label="📁 Giọng tham chiếu (.wav)", type="filepath") | |
| ref_text = gr.Textbox(label="📝 Văn bản tham chiếu", lines=2) | |
| gen_text = gr.Textbox(label="📄 Văn bản cần sinh", lines=3) | |
| output_audio = gr.Audio(label="🎧 Kết quả", type="filepath") | |
| run_btn = gr.Button("🚀 Sinh giọng") | |
| run_btn.click(run_infer_cli, inputs=[ref_audio, ref_text, gen_text], outputs=output_audio) | |
| demo.launch() | |