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.Box(): 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")