File size: 1,452 Bytes
d087f61 3e6e49c d087f61 7cc66cf a90294a 6ff52b5 a90294a 6ff52b5 d087f61 6ff52b5 a90294a d087f61 a90294a 6ff52b5 d087f61 6ff52b5 a90294a 6ff52b5 d087f61 a90294a d087f61 |
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 |
import gradio as gr
import torch
from transformers import AutoProcessor, VitsModel
from scipy.io.wavfile import write
model_id = "facebook/mms-tts-nko"
processor = AutoProcessor.from_pretrained(model_id)
model = VitsModel.from_pretrained(model_id)
model.to("cpu")
# Bảng chuyển N'Ko sang Latin đơn giản (mở rộng dần)
nko_to_latin = {
"ߞ": "k", "ߐ": "a", "ߣ": "n", "ߌ": "i",
"ߔ": "b", "ߘ": "d"
}
def transliterate_nko(text):
return ''.join(nko_to_latin.get(ch, ch) for ch in text)
def tts_nko(text):
try:
latin_text = transliterate_nko(text)
inputs = processor(text=latin_text, return_tensors="pt")
inputs["input_ids"] = inputs["input_ids"].long()
with torch.no_grad():
output = model(**inputs)
audio = output.waveform[0].numpy()
path = "nko.wav"
write(path, rate=model.config.sampling_rate, data=audio)
return f"✅ Phát âm: {latin_text}", path
except Exception as e:
return f"❌ Lỗi: {str(e)}", None
with gr.Blocks() as demo:
gr.Markdown("# 🔊 N'Ko TTS (ߞߐߣߌ ➝ /kani/)")
text_input = gr.Textbox(label="Nhập văn bản bằng chữ N'Ko (VD: ߞߐߣߌ)")
output_text = gr.Textbox(label="Ghi chú")
output_audio = gr.Audio(label="Phát âm", type="filepath")
button = gr.Button("Đọc")
button.click(fn=tts_nko, inputs=[text_input], outputs=[output_text, output_audio])
demo.launch() |