File size: 1,180 Bytes
3d783a8
664d5a5
d1c3c7d
 
 
 
 
664d5a5
 
 
 
 
 
c48190a
 
d1c3c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664d5a5
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
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.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)