Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoProcessor, BarkModel
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import traceback
|
| 6 |
+
|
| 7 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 8 |
+
|
| 9 |
+
# Tải model và processor
|
| 10 |
+
processor = AutoProcessor.from_pretrained("facebook/mms-tts-bod")
|
| 11 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained("facebook/mms-tts-bod")
|
| 12 |
+
|
| 13 |
+
# Kiểm tra thiết bị
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
model.to(device)
|
| 16 |
+
|
| 17 |
+
def tts(text):
|
| 18 |
+
try:
|
| 19 |
+
print(f"Đầu vào: {text}")
|
| 20 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
output = model.generate(**inputs)
|
| 23 |
+
audio_arr = processor.batch_decode(output, return_tensors="pt")[0].numpy()
|
| 24 |
+
sf.write("output.wav", audio_arr, 16000)
|
| 25 |
+
return "output.wav"
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print("LỖI:\n", traceback.format_exc())
|
| 28 |
+
return f"Lỗi: {str(e)}"
|
| 29 |
+
|
| 30 |
+
gr.Interface(
|
| 31 |
+
fn=tts,
|
| 32 |
+
inputs=gr.Textbox(label="Nhập văn bản tiếng Tây Tạng"),
|
| 33 |
+
outputs=gr.Audio(label="Kết quả TTS"),
|
| 34 |
+
title="TTS Tiếng Tây Tạng (MMS)"
|
| 35 |
+
).launch()
|