File size: 5,060 Bytes
7f0cc16
 
0cfd3da
67673a1
9b231ac
67673a1
7f0cc16
9b231ac
7f0cc16
9b231ac
53016e3
67673a1
 
f64d86f
67673a1
 
 
 
 
 
 
 
7f0cc16
67673a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f47653c
67673a1
 
 
 
 
 
 
53016e3
bc05f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67673a1
 
 
 
bc05f02
 
 
67673a1
 
 
 
23ce701
7f0cc16
9b231ac
c8b6889
7f0cc16
5cd4ae2
adc69be
 
4c93ec9
7f0cc16
9b231ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cfd3da
9b231ac
 
 
 
 
34d86b6
9b231ac
 
 
 
 
 
 
 
bc05f02
34d86b6
9b231ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36506c1
9b231ac
 
 
 
 
 
34d86b6
9b231ac
34d86b6
 
9b231ac
34d86b6
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import gradio as gr
import os
from AinaTheme import theme
import phonemizer
from dotenv import load_dotenv
import logging

load_dotenv()

MAX_INPUT_TEXT_LEN = int(os.environ.get("MAX_INPUT_TEXT_LEN", default=325))

critical_logger = logging.getLogger("phonemizer")
critical_logger.setLevel(logging.CRITICAL)

# init phonemizers
global_phonemizer_central = phonemizer.backend.EspeakBackend(
    language="ca",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

global_phonemizer_valencia = phonemizer.backend.EspeakBackend(
    language="ca-va",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

global_phonemizer_occidental = phonemizer.backend.EspeakBackend(
    language="ca-nw",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

global_phonemizer_balear = phonemizer.backend.EspeakBackend(
    language="ca-ba",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

global_phonemizer_algueres = phonemizer.backend.EspeakBackend(
    language="ca-al",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

global_phonemizer_rossellones = phonemizer.backend.EspeakBackend(
    language="ca-ro",
    preserve_punctuation=True,
    with_stress=True,
    language_switch="remove-flags",
    logger=critical_logger,
)

def phonemiser(text, dialect):
    phonemizers = {"Central": global_phonemizer_central,
                "Valencian": global_phonemizer_valencia,
                "North-West": global_phonemizer_occidental,
                "Balear": global_phonemizer_balear,
                "Alguerés": global_phonemizer_algueres, 
                "Rosellonés": global_phonemizer_rossellones}
    
    fonemitzador = phonemizers[dialect] #Set correct dialect for the phonemiser
    # synthesize
    fonemes = fonemitzador.phonemize([text], strip=True, njobs=1)[0]
    return fonemes


title = "Transcripció fonètica multidialectal en català️"
description="""
Transcripció fonètica per a diferents dialectes del català mitjançant eSpeak.
Phonetic transcription for different dialects of Catalan 
using eSpeak.
repo: https://github.com/projecte-aina/espeak-ng/tree/dev-ca 
"""

def submit_input(input_, dialect):
    output = None
    if input_ is not None and len(input_) < MAX_INPUT_TEXT_LEN:
        output = phonemiser(input_, dialect)
    else:
        gr.Warning(f"Your text exceeds the {MAX_INPUT_TEXT_LEN}-character limit.")
    return output

def change_interactive(text):
    input_state = text
    if input_state.strip() != "":
        return gr.update(interactive = True)
    else:
        return gr.update(interactive = False)
def clean(): 
    return (
        None, 
        None,
    )



with gr.Blocks(theme=theme) as app:
        
        gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>")

        gr.Markdown(description)

        with gr.Row(equal_height=False, variant="panel"):
    
            with gr.Column(variant='panel'):
                input_ = gr.Textbox(
                    label="Text",
                    value="Les coses importants són les que no ho semblen.",
                    lines=4
                )

                dialect = gr.Dropdown(label="Dialect", choices=["Central", "Valencian", "North-West", "Balear", "Alguerés", "Rosellonés"], value="Central")
                with gr.Row(variant="panel"):
                    clear_btn = gr.Button(
                        "Clean", 
                    )
                    submit_btn = gr.Button(
                        "Submit", 
                        variant="primary", 
                    )
            with gr.Column(variant='panel'):
                output = gr.Textbox( 
                    label="Output", 
                    interactive=False, 
                    show_copy_button=True
                )
        gr.Examples(
            label="Examples",
            examples=[
                ["Les coses importants són les que no ho semblen.", "Central"],
                ["Les coses importants són les que no ho semblen.", "Valencian",],
                ["Les coses importants són les que no ho semblen.", "North-West",],
                ["Les coses importants són les que no ho semblen.","Balear"],
            ],
            inputs=[input_, dialect],
            outputs=output,
            fn=submit_input)

        for button in [submit_btn, clear_btn]:
            input_.change(fn=change_interactive, inputs=[input_], outputs=button, api_name=False)

        clear_btn.click(fn=clean, inputs=[], outputs=[input_, output] , queue=False, api_name=False)
        submit_btn.click(fn=submit_input, inputs=[input_, dialect], outputs=output, api_name="get-results", concurrency_limit=2)
        
app.launch(show_api=True, server_name="0.0.0.0", server_port=7860)