Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoProcessor, VitsModel
|
|
|
|
| 4 |
|
| 5 |
-
# Load mô hình và processor
|
| 6 |
model_id = "facebook/mms-tts-nko"
|
| 7 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 8 |
model = VitsModel.from_pretrained(model_id)
|
| 9 |
-
|
| 10 |
-
# Đặt thiết bị phù hợp
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
def tts_nko(text):
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
inputs =
|
|
|
|
| 18 |
|
| 19 |
-
# Dự đoán bằng mô hình
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model(**inputs)
|
| 22 |
|
| 23 |
-
# Xuất audio
|
| 24 |
waveform = outputs.audio[0].cpu().numpy()
|
| 25 |
-
|
| 26 |
-
# Lưu file WAV
|
| 27 |
-
from scipy.io.wavfile import write
|
| 28 |
output_path = "nko_output.wav"
|
| 29 |
write(output_path, rate=model.config.sampling_rate, data=waveform)
|
| 30 |
|
| 31 |
-
return "✅
|
| 32 |
-
|
| 33 |
except Exception as e:
|
| 34 |
return f"❌ Lỗi: {str(e)}", None
|
| 35 |
|
| 36 |
-
# Giao diện Gradio
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
-
gr.Markdown("## 🗣️ N'Ko
|
| 39 |
txt = gr.Textbox(label="Nhập văn bản bằng chữ N'Ko", value="ߞߊߟߊ ߛߌ߬")
|
| 40 |
out_txt = gr.Textbox(label="Trạng thái")
|
| 41 |
out_audio = gr.Audio(label="Phát âm", type="filepath")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoProcessor, VitsModel
|
| 4 |
+
from scipy.io.wavfile import write
|
| 5 |
|
|
|
|
| 6 |
model_id = "facebook/mms-tts-nko"
|
| 7 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 8 |
model = VitsModel.from_pretrained(model_id)
|
|
|
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
model.to(device)
|
| 11 |
|
| 12 |
def tts_nko(text):
|
| 13 |
try:
|
| 14 |
+
inputs = processor(text=text, return_tensors="pt")
|
| 15 |
+
inputs["input_ids"] = inputs["input_ids"].long() # Ép kiểu ở đây
|
| 16 |
+
inputs = inputs.to(device)
|
| 17 |
|
|
|
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model(**inputs)
|
| 20 |
|
|
|
|
| 21 |
waveform = outputs.audio[0].cpu().numpy()
|
|
|
|
|
|
|
|
|
|
| 22 |
output_path = "nko_output.wav"
|
| 23 |
write(output_path, rate=model.config.sampling_rate, data=waveform)
|
| 24 |
|
| 25 |
+
return "✅ Đã tạo giọng đọc N'Ko", output_path
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"❌ Lỗi: {str(e)}", None
|
| 28 |
|
|
|
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("## 🗣️ TTS N'Ko (ߒߞߏ)")
|
| 31 |
txt = gr.Textbox(label="Nhập văn bản bằng chữ N'Ko", value="ߞߊߟߊ ߛߌ߬")
|
| 32 |
out_txt = gr.Textbox(label="Trạng thái")
|
| 33 |
out_audio = gr.Audio(label="Phát âm", type="filepath")
|