File size: 1,651 Bytes
6f8e13e 12ccec8 6f8e13e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import gradio as gr
import torch
from TTS.api import TTS
import os
# Sử dụng GPU nếu có, nếu không thì dùng CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# Tải mô hình TTS một lần khi ứng dụng khởi động
print("Đang tải mô hình TTS...")
tts = TTS("thinhlpg/vixtts-test").to(device)
print("Đã tải xong mô hình.")
def clone_voice(text, reference_audio):
if text and reference_audio:
# Lưu file âm thanh tham chiếu vào một đường dẫn tạm thời
reference_file_path = reference_audio
# Tạo giọng nói và lưu vào file output.wav
output_file_path = "output.wav"
tts.tts_to_file(
text=text,
speaker_wav=reference_file_path,
language="vi",
file_path=output_file_path
)
return output_file_path
return None
# Thiết kế giao diện Gradio
app = gr.Interface(
fn=clone_voice,
inputs=[
gr.Textbox(label="Văn bản cần chuyển đổi"),
gr.Audio(type="filepath", label="Tải lên tệp âm thanh mẫu (.wav)")
],
outputs=gr.Audio(label="Giọng nói đã nhân bản"),
title="👩💻 Demo nhân bản giọng nói Tiếng Việt",
description="Nhập một đoạn văn bản và tải lên một file âm thanh mẫu (dưới 15 giây) để tạo ra giọng nói mới.",
examples=[
["Xin chào, đây là một thử nghiệm về nhân bản giọng nói.", None],
["Trí tuệ nhân tạo đang thay đổi thế giới của chúng ta.", None]
]
)
# Khởi chạy ứng dụng
app.launch() |