File size: 4,203 Bytes
36e257e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4a567c
 
36e257e
 
 
 
ddc7b60
 
 
 
 
 
 
 
6e3bfcd
36e257e
f4a567c
36e257e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4a567c
36e257e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68c6a96
2f260e9
36e257e
3f49c4e
7cfc0d4
36e257e
 
2185524
36e257e
3f49c4e
 
 
 
 
 
 
36e257e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddc7b60
 
8fa01fe
36e257e
 
b924ae7
36e257e
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
"""
A simple web interface to demonstrate the Turkish transliteration.
"""
import gradio as gr
from turkic_translit.core import to_latin, to_ipa
import unicodedata as ud


def transliterate(text, lang, include_arabic, output_format):
    if not text:
        return "", ""
    try:
        if output_format == "Latin":
            result = to_latin(text, lang, include_arabic=include_arabic)
            format_label = "Latin"
        else:
            result = to_ipa(text, lang)
            format_label = "IPA"
        result = ud.normalize("NFC", result)
        stats_md = (f"**Bytes** — Cyrillic : {len(text.encode('utf8'))}, "
                    f"{format_label} : {len(result.encode('utf8'))}")
        return result, stats_md
    except Exception as e:
        raise gr.Error(str(e))

# Function to reset & (de)activate the Arabic checkbox when mode changes
def _sync_arabic_checkbox(mode: str):
    """Uncheck and disable in IPA; leave state untouched in Latin."""
    if mode == "IPA":
        # Forced off + greyed-out
        return gr.update(value=False, interactive=False)
    # Latin: keep whatever the user last chose, stay interactive
    return gr.update(interactive=True)

# Create the Gradio interface
with gr.Blocks(title="Turkic Transliteration Demo") as demo:
    gr.Markdown("# Turkic Transliteration Demo")
    gr.Markdown("Enter Cyrillic text for Kazakh (kk) or Kyrgyz (ky) and see the Latin transliteration")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Input Text (Cyrillic)",
                placeholder="Enter Kazakh or Kyrgyz text in Cyrillic script...",
                lines=5
            )
            lang = gr.Radio(
                ["kk", "ky"], 
                label="Language", 
                info="kk = Kazakh, ky = Kyrgyz",
                value="kk"
            )
            output_format = gr.Radio(
                ["Latin", "IPA"],
                label="Output Format",
                info="Latin = Standard Latin alphabet, IPA = International Phonetic Alphabet",
                value="Latin"
            )
            include_arabic = gr.Checkbox(False, label="Also transliterate Arabic script (Latin mode only)")
        
        with gr.Column():
            output_text = gr.Textbox(
                label="Transliteration Output",
                lines=5,
                interactive=False
            )
            stats = gr.Markdown(value="")
    
    # Example inputs
    examples = [
        ["Қазақ тілі - Түркі тілдерінің бірі.", "kk", "Latin"],
        ["Қазақ тілі - Түркі тілдерінің бірі.", "kk", "IPA"],
        ["Кыргыз тили - Түрк тилдеринин бири.", "ky", "Latin"],
        ["Кыргыз тили - Түрк тилдеринин бири.", "ky", "IPA"]
    ]
    gr.Markdown("👉 **Tip:** Click an example below to instantly fill in the fields and see the transliteration!")
    gr.Examples(examples, [input_text, lang, output_format])
    
    # Live preview: update output while the user types
    input_text.input(
        fn=transliterate,
        inputs=[input_text, lang, include_arabic, output_format],
        outputs=[output_text, stats]
    )

    # Also fire when the textbox's value finishes changing (e.g., clicking an example)
    input_text.change(
        fn=transliterate,
        inputs=[input_text, lang, include_arabic, output_format],
        outputs=[output_text, stats]
    )
    
    lang.change(
        fn=transliterate,
        inputs=[input_text, lang, include_arabic, output_format],
        outputs=[output_text, stats]
    )
    
    include_arabic.change(
        fn=transliterate,
        inputs=[input_text, lang, include_arabic, output_format],
        outputs=[output_text, stats]
    )
    
    output_format.change(
        fn=transliterate,
        inputs=[input_text, lang, include_arabic, output_format],
        outputs=[output_text, stats]
    )
    # Sync Arabic checkbox with mode
    output_format.change(_sync_arabic_checkbox, output_format, include_arabic)

# Launch the app
if __name__ == "__main__":
    demo.queue().launch()