Update app.py
Browse files
app.py
CHANGED
|
@@ -3,37 +3,40 @@ import torch
|
|
| 3 |
from transformers import AutoProcessor, VitsModel
|
| 4 |
from scipy.io.wavfile import write
|
| 5 |
|
| 6 |
-
# Khởi tạo model và processor
|
| 7 |
model_id = "facebook/mms-tts-nko"
|
| 8 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 9 |
model = VitsModel.from_pretrained(model_id)
|
| 10 |
model.to("cpu")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def transliterate_nko(text):
|
| 13 |
-
|
| 14 |
-
# Nếu văn bản đã là chữ N'Ko gốc (ߞߐߣߌ), thì dùng luôn.
|
| 15 |
-
return text
|
| 16 |
|
| 17 |
def tts_nko(text):
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
inputs = processor(text=
|
| 21 |
inputs["input_ids"] = inputs["input_ids"].long()
|
| 22 |
with torch.no_grad():
|
| 23 |
output = model(**inputs)
|
| 24 |
audio = output.waveform[0].numpy()
|
| 25 |
path = "nko.wav"
|
| 26 |
write(path, rate=model.config.sampling_rate, data=audio)
|
| 27 |
-
return "✅
|
| 28 |
except Exception as e:
|
| 29 |
return f"❌ Lỗi: {str(e)}", None
|
| 30 |
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("#
|
| 33 |
-
text_input = gr.Textbox(label="Nhập văn bản N'Ko (VD: ߞߐߣߌ)")
|
| 34 |
-
output_text = gr.Textbox(label="
|
| 35 |
output_audio = gr.Audio(label="Phát âm", type="filepath")
|
| 36 |
-
button = gr.Button("
|
| 37 |
|
| 38 |
button.click(fn=tts_nko, inputs=[text_input], outputs=[output_text, output_audio])
|
| 39 |
|
|
|
|
| 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 |
model.to("cpu")
|
| 10 |
|
| 11 |
+
# Bảng chuyển N'Ko sang Latin đơn giản (mở rộng dần)
|
| 12 |
+
nko_to_latin = {
|
| 13 |
+
"ߞ": "k", "ߐ": "a", "ߣ": "n", "ߌ": "i",
|
| 14 |
+
"ߔ": "b", "ߘ": "d"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
def transliterate_nko(text):
|
| 18 |
+
return ''.join(nko_to_latin.get(ch, ch) for ch in text)
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def tts_nko(text):
|
| 21 |
try:
|
| 22 |
+
latin_text = transliterate_nko(text)
|
| 23 |
+
inputs = processor(text=latin_text, return_tensors="pt")
|
| 24 |
inputs["input_ids"] = inputs["input_ids"].long()
|
| 25 |
with torch.no_grad():
|
| 26 |
output = model(**inputs)
|
| 27 |
audio = output.waveform[0].numpy()
|
| 28 |
path = "nko.wav"
|
| 29 |
write(path, rate=model.config.sampling_rate, data=audio)
|
| 30 |
+
return f"✅ Phát âm: {latin_text}", path
|
| 31 |
except Exception as e:
|
| 32 |
return f"❌ Lỗi: {str(e)}", None
|
| 33 |
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# 🔊 N'Ko TTS (ߞߐߣߌ ➝ /kani/)")
|
| 36 |
+
text_input = gr.Textbox(label="Nhập văn bản bằng chữ N'Ko (VD: ߞߐߣߌ)")
|
| 37 |
+
output_text = gr.Textbox(label="Ghi chú")
|
| 38 |
output_audio = gr.Audio(label="Phát âm", type="filepath")
|
| 39 |
+
button = gr.Button("Đọc")
|
| 40 |
|
| 41 |
button.click(fn=tts_nko, inputs=[text_input], outputs=[output_text, output_audio])
|
| 42 |
|