Spaces:
Running
Running
| import gradio as gr | |
| import jieba | |
| from script.pyPengIm import pyPengIm | |
| from script import utils | |
| import yaml | |
| # 自定义CSS样式 | |
| custom_css = """ | |
| .btn1 { | |
| background: #2563eb; /* 主蓝 */ | |
| color: white; | |
| border: none; | |
| border-radius: 6px; | |
| padding: 8px 18px; | |
| font-size: 14px; | |
| transition: background 0.2s ease; | |
| } | |
| .btn1:hover { | |
| background: #1d4ed8; /* 深蓝 */ | |
| } | |
| /* 按钮样式2:次要操作(灰色) */ | |
| .btn2 { | |
| background: #f3f4f6; /* 浅灰背景 */ | |
| color: #374151; /* 深灰字 */ | |
| border: 1px solid #d1d5db; | |
| border-radius: 6px; | |
| padding: 8px 18px; | |
| font-size: 14px; | |
| transition: background 0.2s ease, border-color 0.2s ease; | |
| } | |
| .btn2:hover { | |
| background: #e5e7eb; | |
| border-color: #9ca3af; | |
| } | |
| /* 按钮样式3:成功/确认(绿色) */ | |
| .btn3 { | |
| background: #10b981; /* 绿色 */ | |
| color: white; | |
| border: none; | |
| border-radius: 6px; | |
| padding: 8px 18px; | |
| font-size: 14px; | |
| transition: background 0.2s ease; | |
| } | |
| .btn3:hover { | |
| background: #059669; /* 深绿 */ | |
| } | |
| /* 按钮样式4:警告/强调(橙色) */ | |
| .btn4 { | |
| background: #f97316; /* 橙色 */ | |
| color: white; | |
| border: none; | |
| border-radius: 6px; | |
| padding: 8px 18px; | |
| font-size: 14px; | |
| transition: background 0.2s ease; | |
| } | |
| .btn4:hover { | |
| background: #ea580c; /* 深橙 */ | |
| } | |
| .btn5 { | |
| background: #fef2f2; /* 浅粉背景 */ | |
| color: #b91c1c; /* 深红文字 */ | |
| border: 1px solid #fecaca; | |
| border-radius: 6px; | |
| padding: 8px 18px; | |
| font-size: 14px; | |
| transition: background 0.2s ease, border-color 0.2s ease; | |
| } | |
| .btn5:hover { | |
| background: #fee2e2; /* hover 稍微加深 */ | |
| border-color: #fca5a5; | |
| } | |
| .gradio-container input[type="text"] { | |
| padding: 10px 15px; | |
| border-radius: 8px; | |
| border: 2px solid #e0e0e0; | |
| transition: all 0.3s ease; | |
| } | |
| .gradio-container input[type="text"]:focus { | |
| outline: none; | |
| border-color: #6e48aa; | |
| box-shadow: 0 0 0 3px rgba(110, 72, 170, 0.2); | |
| } | |
| .cool-textbox textarea:focus { | |
| border-color: #2563eb; | |
| box-shadow: 0 0 0 2px rgba(37,99,235,0.2); | |
| outline: none; | |
| } | |
| /* Textbox 的 label 样式 */ | |
| .cool-textbox label { | |
| font-weight: 600; /* 半粗体 */ | |
| font-size: 16px; /* 稍小一点 */ | |
| color: #374151; /* 深灰,温和一点 */ | |
| margin-bottom: 4px; /* 和输入框保持间距 */ | |
| display: inline-block; /* 让 margin 生效 */ | |
| """ | |
| pinyin_tool = pyPengIm() | |
| accent_dict={'tc':'府城'} | |
| with open("./dict_data/accent_convert/accent.yaml", 'r', encoding='utf-8') as file: | |
| accent_config = yaml.safe_load(file) | |
| for k,v in accent_config.items(): | |
| accent_dict[k] = v['name'] | |
| reversed_accent_dict = {value: key for key, value in accent_dict.items()} | |
| def auto_segment(input_text): | |
| return " ".join(jieba.cut(input_text)) | |
| def manual_segment(input_text): | |
| return " ".join(input_text.split(' ')) | |
| def single_char_query(input_text): | |
| result_dict = pinyin_tool.single_query(input_text.strip()[0]) | |
| result = [] | |
| for k, v in result_dict.items(): | |
| result.append(k+':') | |
| result.append(str(v)) | |
| result.append('\n') | |
| return "".join(result) | |
| def number_conversion_0(input_text): | |
| return utils.num_to_chinese(input_text) | |
| def number_conversion_1(input_text): | |
| return utils.num_to_chinese_smart(input_text) | |
| def clear_all(): | |
| return "", "", "" | |
| def to_ipa(input_text, location, return_seq): | |
| pinyin_seq = pinyin_tool.pinyin(input_text,accent=reversed_accent_dict[location])['pinyin_seq'] | |
| pinyin_seq = pinyin_tool.to_IPA(pinyin_seq, blank=False) | |
| if return_seq: | |
| return pinyin_seq | |
| zi_at_pinyin_seq = zi_and_pinyin(input_text,pinyin_seq) | |
| return zi_at_pinyin_seq | |
| def restrip(text): | |
| text=text.replace(' ','') | |
| text=text.replace('#','') | |
| return text | |
| def zi_and_pinyin(text, pinyin_seq): | |
| if isinstance(pinyin_seq, list): | |
| ls = pinyin_seq | |
| else: | |
| ls = pinyin_seq.split(' ') | |
| result = [] | |
| text = restrip(text) | |
| for i in range(len(text)): | |
| if text[i] == ls[i]: | |
| # 非法字符 | |
| result.append(text[i]) | |
| else: | |
| result.append(text[i]+'@'+ls[i]) | |
| return " ".join(result) | |
| def process_text(input_text, location, return_seq): | |
| # print(input_text) | |
| pinyin_seq = pinyin_tool.pinyin(input_text,accent=reversed_accent_dict[location],auto_split=False)['pinyin_seq'] | |
| if return_seq: | |
| return pinyin_seq | |
| zi_at_pinyin_seq = zi_and_pinyin(input_text,pinyin_seq) | |
| return zi_at_pinyin_seq | |
| def auto_translate(text): | |
| return pinyin_tool.to_oral(jieba.lcut(text)) | |
| def manual_translate(text): | |
| return pinyin_tool.to_oral(text.split(" ")) | |
| with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo: | |
| gr.Markdown("### 潮州话字素转音素处理工具") | |
| gr.Markdown("输入文本,选择地区,先进行分词,然后根据分词结果进行拼音转换") | |
| input_text = gr.Textbox(label="输入文本①", elem_classes="cool-textbox") | |
| with gr.Row(): | |
| return_type = gr.Dropdown( | |
| choices=[False,True], | |
| label="是否只输出纯拼音序列", | |
| value=False | |
| ) | |
| location = gr.Dropdown( | |
| choices=reversed_accent_dict.keys(), | |
| label="地区选择", | |
| value="府城" | |
| ) | |
| with gr.Row(): | |
| num_conv_btn_1 = gr.Button("阿拉伯数字转汉字(智能)", variant="btn1") | |
| num_conv_btn_0 = gr.Button("阿拉伯数字转汉字(直接)", variant="btn1") | |
| single_char_btn = gr.Button("单字查询(查询输入文本的第一个字)", variant='btn1') | |
| clear_btn = gr.Button("清空", variant='btn5') | |
| # with gr.Row(): | |
| seg_output = gr.Textbox(label="分词结果", elem_classes="cool-textbox") | |
| pinyin_output = gr.Textbox(lines=5, label="拼音转换结果", elem_classes="cool-textbox") | |
| with gr.Row(): | |
| auto_seg_btn = gr.Button("自动分词②", variant="btn3") | |
| manual_seg_btn = gr.Button("手动分词(自己手动打空格)②", variant="btn3") | |
| auto_translate_btn = gr.Button("替换成口语(自动分词)", variant="btn2") | |
| manual_translate_btn = gr.Button("替换成口语(手动分词)", variant="btn2") | |
| with gr.Row(): | |
| ipa_btn = gr.Button("转国际音标③", variant="btn4") | |
| pinyin_btn = gr.Button("拼音转换③", variant="btn4") | |
| # 按钮事件绑定 | |
| auto_seg_btn.click(auto_segment, input_text, seg_output) | |
| manual_seg_btn.click(manual_segment, input_text, seg_output) | |
| auto_translate_btn.click(auto_translate, input_text, seg_output) | |
| manual_translate_btn.click(manual_translate, input_text, seg_output) | |
| num_conv_btn_0.click(number_conversion_0, input_text, seg_output) | |
| num_conv_btn_1.click(number_conversion_1, input_text, seg_output) | |
| single_char_btn.click(single_char_query, input_text, pinyin_output) | |
| clear_btn.click(clear_all, None, [input_text, seg_output, pinyin_output]) | |
| ipa_btn.click(to_ipa, [seg_output, location], pinyin_output) | |
| pinyin_btn.click(process_text, [seg_output, location, return_type], pinyin_output) | |
| demo.launch() |