Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
from transformers import AutoTokenizer, VitsForTextToSpeech
|
| 5 |
+
|
| 6 |
+
# Tải mô hình và tokenizer
|
| 7 |
+
model_name = "facebook/mms-tts-bod"
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = VitsForTextToSpeech.from_pretrained(model_name).to(device)
|
| 12 |
+
|
| 13 |
+
# Hàm chuyển văn bản thành giọng nói
|
| 14 |
+
def tts_bod(text):
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
output = model(**inputs)
|
| 18 |
+
audio = output.waveform.squeeze().cpu().numpy()
|
| 19 |
+
|
| 20 |
+
# Lưu tạm âm thanh ra file
|
| 21 |
+
sf.write("output.wav", audio, 16000)
|
| 22 |
+
return "output.wav"
|
| 23 |
+
|
| 24 |
+
# Giao diện Gradio
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=tts_bod,
|
| 27 |
+
inputs=gr.Textbox(label="Nhập văn bản tiếng Tây Tạng"),
|
| 28 |
+
outputs=gr.Audio(label="Âm thanh đầu ra", type="filepath"),
|
| 29 |
+
title="TTS Tiếng Tây Tạng (facebook/mms-tts-bod)"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
interface.launch()
|