|
import gradio as gr |
|
import tempfile |
|
from transformers import pipeline |
|
from gtts import gTTS |
|
|
|
|
|
asr_pipeline = pipeline( |
|
"automatic-speech-recognition", |
|
model="biodatlab/whisper-th-medium-combined", |
|
chunk_length_s=30, |
|
device=0 |
|
) |
|
|
|
|
|
translator_pipeline = pipeline( |
|
"translation", |
|
model="facebook/nllb-200-distilled-600M" |
|
) |
|
|
|
|
|
def translate_voice(audio_input, direction): |
|
if audio_input is None: |
|
return "กรุณาอัปโหลดหรืออัดเสียงก่อน", None |
|
|
|
|
|
if isinstance(audio_input, tuple): |
|
audio_path = audio_input[0] |
|
else: |
|
audio_path = audio_input |
|
|
|
|
|
asr_result = asr_pipeline(audio_path) |
|
original_text = asr_result["text"] |
|
|
|
|
|
if direction == "มลายู → ไทย": |
|
src_lang = "zsm_Latn" |
|
tgt_lang = "tha_Thai" |
|
tts_lang = "th" |
|
else: |
|
src_lang = "tha_Thai" |
|
tgt_lang = "zsm_Latn" |
|
tts_lang = "ms" |
|
|
|
|
|
translation = translator_pipeline( |
|
original_text, |
|
src_lang=src_lang, |
|
tgt_lang=tgt_lang |
|
)[0]["translation_text"] |
|
|
|
|
|
tts = gTTS(text=translation, lang=tts_lang) |
|
temp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name |
|
tts.save(temp_audio_path) |
|
|
|
return translation, temp_audio_path |
|
|
|
|
|
demo = gr.Interface( |
|
fn=translate_voice, |
|
inputs=[ |
|
gr.Audio(sources=["microphone", "upload"], type="filepath", label="พูดหรืออัปโหลดเสียง"), |
|
gr.Radio(["มลายู → ไทย", "ไทย → มลายู"], value="มลายู → ไทย", label="เลือกทิศทางแปล") |
|
], |
|
outputs=[ |
|
gr.Textbox(label="ข้อความที่แปลได้"), |
|
gr.Audio(label="เสียงที่แปลกลับ") |
|
], |
|
title="🩺 ระบบแปลเสียง มลายู ↔ ไทย สำหรับการแพทย์", |
|
theme="default" |
|
) |
|
|
|
demo.launch() |
|
|