import gradio as gr import phonetisaurus from collections import namedtuple import re import shlex import tempfile import subprocess def phonemizer(text: str): if not text: return "" text = text.lower() env = phonetisaurus.guess_environment() model_path = "./model_2.fst" with tempfile.NamedTemporaryFile(suffix=".txt", mode="w+") as temp_file: print(text, file=temp_file) temp_file.seek(0) phonetisaurus_cmd = [ "phonetisaurus-apply", "--model", shlex.quote(str(model_path)), "--word_list", shlex.quote(str(temp_file.name)), "--nbest", str(1), ] result_str = subprocess.check_output(phonetisaurus_cmd, env=env, universal_newlines=True) phoneme = result_str.split('\t')[1].strip() phoneme = re.sub(r'\s', "", phoneme) return f"/{phoneme}/" iface = gr.Interface( title="Khmer Phonemizer", description="Convert Khmer & English into Phonetic Symbols", fn=phonemizer, inputs="text", outputs="text", allow_flagging="never", ) iface.launch(show_api=False)