File size: 3,820 Bytes
5312d13
 
eb69d70
5312d13
d116b47
eb69d70
 
d116b47
 
 
 
 
 
 
 
 
 
 
 
5312d13
 
eb69d70
 
 
 
 
8d3b8de
 
5312d13
 
 
b3c881e
f71a878
 
975e2cd
383e1e6
 
f71a878
383e1e6
975e2cd
 
 
 
f71a878
 
 
975e2cd
 
 
 
5312d13
 
 
dfebd5e
5312d13
975e2cd
5312d13
975e2cd
f71a878
 
5312d13
eb69d70
 
0af98f2
eb69d70
 
 
975e2cd
5312d13
ab60391
 
dce85e5
24736b4
ab60391
 
 
24736b4
 
ab60391
 
 
 
 
 
 
 
 
 
 
 
eb69d70
 
5312d13
 
dfebd5e
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
import gradio as gr

from segmentation import has_myanmar, preprocess, segment


def segment_myanmar_only(text: str) -> str:
    """Segment only Myanmar text, preserving English/Latin characters as-is."""
    tokens = preprocess(text)

    segmented_texts = []
    for token in tokens:
        if has_myanmar(token):
            segmented_texts.append(segment(token))
        else:
            segmented_texts.append(token)

    result = " ".join(segmented_texts)

    return result


def process_text(text: str, should_preserve_english: bool) -> str:
    """Process text based on the selected mode."""
    if should_preserve_english:
        return segment_myanmar_only(text)
    return segment(text)


css = """
#col-container {
    margin: 0 auto;
    max-width: 1200px;
    padding: 1rem;
    gap: 2.5rem;
}
#col-container h1 {
    text-align: center;
    margin-bottom: 1rem;
}
#input-output-row {
    flex-direction: row;
    gap: 1rem;
}
#input-output-row label {
    text-align: center;
}
@media (max-width: 768px) {
    #input-output-row {
        flex-direction: column;
    }
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# Myanmar Text Segmentation")

        with gr.Row(elem_id="input-output-row", equal_height=True):
            input_text = gr.Textbox(label="Input Text", placeholder="Enter Myanmar text here...", lines=8)
            output_text = gr.Textbox(label="Segmented Text", lines=8)

        preserve_english = gr.Checkbox(
            label="Preserve English text",
            value=True,
            info="Only segment Myanmar text and add spaces between English/Myanmar boundaries",
        )

        run_button = gr.Button("Segment", variant="primary")

        gr.Examples(
            examples=[
                ["အချစ်ဆိုတာလူတွေရှင်သန်ဖို့သဘာဝကပေးတဲ့လက်နက်လား၊ဒါမှမဟုတ်ယဉ်ကျေးမှုအရတီထွင်ထားတဲ့စိတ်ကူးယဉ်မှုသက်သက်လား။"],
                ["iPhone 15 Pro Maxဖုန်းအသစ်ရောင်းရန်ရှိပြီးဈေးနှုန်း$1,199(ကျပ်၂၅သိန်း)နှင့်အလေးချိန်221gရှိပါသည်။"],
                ["Mathematics(သင်္ချာ)ဘာသာရပ်Chapter-5[အခန်း၅]ကိုစာမျက်နှာ{page 45-60}တွင်လေ့လာနိုင်ပါသည်။"],
                [
                    (
                        "ယနေ့Manchester United(မန်ယူ)အသင်းကLiverpoolကို3-2ဖြင့်အနိုင်ရရှိခဲ့သည်။ဒီပွဲမှာMarcus Rashfordက2ဂိုးသွင်းပြီး"
                        "Man of the Match(ပွဲစဉ်အကောင်းဆုံးကစားသမား)ဆုရရှိခဲ့သည်။"
                    )
                ],
                [
                    (
                        "အမှတ်စဉ်REF:2024/MM/001ဖြင့်ရက်စွဲ15-Jan-2024(၂၀၂၄ခုနှစ်ဇန်နဝါရီလ၁၅ရက်)တွင်လျှောက်လွှာ(Application Form)"
                        "ကိုတင်သွင်းရမည်ဖြစ်ပါသည်။"
                    )
                ],
            ],
            inputs=input_text,
        )

    run_button.click(fn=process_text, inputs=[input_text, preserve_english], outputs=output_text)
    input_text.submit(fn=process_text, inputs=[input_text, preserve_english], outputs=output_text)

if __name__ == "__main__":
    demo.launch()