File size: 1,354 Bytes
2a4860b
 
 
 
 
 
 
 
5696fb6
2a4860b
 
 
 
 
 
 
 
471f0d4
6ac8a12
 
 
 
 
 
471f0d4
2a4860b
471f0d4
 
 
2a4860b
 
 
5696fb6
2a4860b
 
 
 
 
471f0d4
2a4860b
 
 
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
import json

import gradio as gr
from opencc import OpenCC

with open("config.json", "rt", encoding="UTF-8") as fp:
    config: dict = json.load(fp)

rev_config = {f"{v} ({k})": OpenCC(k) for k, v in config.items()}
rev_config_keys = list(rev_config.keys())

title = "簡繁轉換小工具 OpenCC Converter"
font = gr.themes.GoogleFont("Noto Sans Mono")
theme = gr.themes.Soft(font=font)

with gr.Blocks(title=title, theme=theme) as app:
    gr.Markdown("# 簡繁轉換小工具 OpenCC Converter")

    with gr.Row():
        with gr.Column():
            inn_text = gr.TextArea(label="輸入文字", show_copy_button=True)
        with gr.Column():
            out_text = gr.TextArea(label="輸出文字", show_copy_button=True)
    options = gr.Dropdown(rev_config_keys, value=rev_config_keys[0], label="轉換選項")

    with gr.Row():
        conv_btn = gr.Button("轉換")
        clear = gr.ClearButton([inn_text, out_text], value="清除")

    gr.Markdown("Powered By [OpenCC](https://github.com/BYVoid/OpenCC)")

    def convert(inn, opt):
        return rev_config[opt].convert(inn)

    args = (convert, [inn_text, options], [out_text])
    kwargs = {"show_progress": "hidden", "queue": False}
    inn_text.change(*args, **kwargs)
    options.change(*args, **kwargs)
    conv_btn.click(*args, **kwargs)


app.launch(favicon_path="icon.png")