Spaces:
Running
Running
Update app.py
Browse files3 unmodified lines4567891011123 unmodified lines161718192021227 unmodified lines303132333435363738394041421 unmodified line44454647484950515235 unmodified lines8889909192939495969798991001012 unmodified lines1041051061071081091101111121131141 unmodified line1161171181191201211221231241251261271281291301311323 unmodified linesimport difflibimport shutildict_path = "custom_dict.json"# ์ฌ์ ํ์ผ์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ํ์ผ ์์ฑ (์๋ฌ ์์ฒ ์ฐจ๋จ)if not os.path.exists(dict_path):with open(dict_path, "w", encoding="utf-8") as f:json.dump({"items":[], "meta": {"count": 0}}, f)3 unmodified linestry:with open(dict_path, "r", encoding="utf-8") as f:data = json.load(f)for item in data.get("items", []):if len(item) == 2:flat_dict[item[0]] = item[1]except Exception as e:7 unmodified lineschar_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์"processed = input_textflat_dict = load_dict()for word, pronun in flat_dict.items():processed = processed.replace(word, pronun)final_text = processed# ํ๊ดํ ํจ๊ณผseq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():1 unmodified lineif tag == 'equal':html_parts.append(chunk)elif tag in ['replace', 'insert']:html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold; padding: 2px;">{chunk}</span>')html_output = f'<div style="line-height:1.6; padding:10px; border:1px solid #ddd; border-radius:5px;">{"".join(html_parts)}</div>'return final_text, html_output, char_count35 unmodified linesreturn "โ ๋ณต๊ตฌ ์๋ฃ! ์ฌ์ ์ ์ ์์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค."return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."with gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ํ ์ต์ข ๋ณธ)")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):with gr.Row():with gr.Column():input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ")char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="์ ๋ ฅ ๊ธ์ ์: 0์", interactive=False)with gr.Row():2 unmodified lineswith gr.Column():# ์ฐ์ธก ์๋จ ๋ณต์ฌ ๋ฒํผ ํ์ฑํoutput_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ ์คํธ (์ฐ์ธก ์๋จ ๐ ์์ด์ฝ ํด๋ฆญ ์ ๋ณต์ฌ)", show_copy_button=True)output_html = gr.HTML(label="๐ ๋ณ๊ฒฝ์ ํ์ธ")btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count])btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count])gr.Markdown("---")with gr.Row():1 unmodified linenp = gr.Textbox(label="์ํ๋ ๋ฐ์")btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ", variant="primary")status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status])with gr.Tab("๐ฅ ๋ฐฑ์ /๋ณต๊ตฌ"):dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์ ํ๊ธฐ (PC ๋ค์ด๋ก๋)")dl_btn.click(fn=get_file, outputs=[dl_btn])gr.Markdown("---")# type="filepath" ๋ฅผ ๋ช ์ํ์ฌ Gradio 5.x ์๋ฌ ์ฐจ๋จup_file = gr.File(label="๐ค 2. ๋ฐฑ์ ํ์ผ ๋ณต๊ตฌํ๊ธฐ (์ ๋ก๋)", type="filepath")up_stat = gr.Textbox(label="๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat])if __name__ == "__main__":iface.launch()3 unmodified lines4567891011121314151617181920212223242526273 unmodified lines313233343536377 unmodified lines4546474849505152535455565758596061626364651 unmodified line67686970717273747535 unmodified lines1111121131141151161171181191201211221231241252 unmodified lines1281291301311321331341351361371381391 unmodified line1411421431441451461471481491501511521531541551563 unmodified linesimport difflibimport shutiltry:from g2pk import G2pexcept ImportError:G2p = Noneprint("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")try:if G2p:g2p = G2p()else:g2p = Noneexcept Exception as e:print(f"G2P ๋ก๋ฉ ์คํจ: {e}")g2p = Nonedict_path = "custom_dict.json"# ์๋ฌ ๋ฐฉ์ง๋ฅผ ์ํด ์์ ์ ๋น ์ฌ์ ํ์ผ ๋ณด์ฅif not os.path.exists(dict_path):with open(dict_path, "w", encoding="utf-8") as f:json.dump({"items":[], "meta": {"count": 0}}, f)3 unmodified linestry:with open(dict_path, "r", encoding="utf-8") as f:data = json.load(f)for item in data.get("items",[]):if len(item) == 2:flat_dict[item[0]] = item[1]except Exception as e:7 unmodified lineschar_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์"processed = input_text# 1. ์ฌ์ฉ์ ์ง์ ์ฌ์ ์นํflat_dict = load_dict()for word, pronun in flat_dict.items():processed = processed.replace(word, pronun)final_text = processed# 2. G2P ์๋ ๋ฐ์ ๋ณํ ์ ์ฉif g2p is not None:try:final_text = g2p(processed)except Exception as e:print(f"G2P ์๋ฌ: {e}")# 3. ํ๊ดํ ํจ๊ณผ (HTML)seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():1 unmodified lineif tag == 'equal':html_parts.append(chunk)elif tag in ['replace', 'insert']:html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold; padding: 2px; border-radius: 3px;">{chunk}</span>')html_output = f'<div style="line-height:1.6; font-size:16px; padding:10px; border:1px solid #ddd; border-radius:5px; background-color:#f9f9f9;">{"".join(html_parts)}</div>'return final_text, html_output, char_count35 unmodified linesreturn "โ ๋ณต๊ตฌ ์๋ฃ! ์ฌ์ ์ ์ ์์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค."return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."# UI ํ๋ฉด ๊ตฌ์ฑwith gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์ค๋ฅ ์๋ฒฝ ํด๊ฒฐ๋ณธ)")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):with gr.Row():with gr.Column():input_text = gr.Textbox(lines=8, label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ")char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="์ ๋ ฅ ๊ธ์ ์: 0์", interactive=False)with gr.Row():2 unmodified lineswith gr.Column():# ์ฐ์ธก ์๋จ ๋ณต์ฌ ๋ฒํผ ํ์ฑํoutput_text = gr.Textbox(lines=8, label="2๏ธโฃ ๋ณํ๋ ํ ์คํธ (์ฐ์ธก ์๋จ ๐ ์์ด์ฝ ํด๋ฆญ ์ ๋ณต์ฌ)", show_copy_button=True)output_html = gr.HTML(label="๐ ๋ณ๊ฒฝ์ ํ์ธ")# api_name=False ์์ฑ ์ถ๊ฐ๋ก ๋จนํต ๋ฒ๊ทธ ์๋ฒฝ ์ฐจ๋จ!btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count], api_name=False)btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], api_name=False)gr.Markdown("---")with gr.Row():1 unmodified linenp = gr.Textbox(label="์ํ๋ ๋ฐ์")btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ", variant="primary")status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status], api_name=False)with gr.Tab("๐ฅ ๋ฐฑ์ /๋ณต๊ตฌ"):dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์ ํ๊ธฐ (PC ๋ค์ด๋ก๋)")dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False)gr.Markdown("---")up_file = gr.File(label="๐ค 2. ๋ฐฑ์ ํ์ผ ๋ณต๊ตฌํ๊ธฐ (์ ๋ก๋)", type="filepath")up_stat = gr.Textbox(label="๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat], api_name=False)if __name__ == "__main__":iface.launch()
app.py+38-14