Spaces:
Running
Running
Update app.py
Browse files3 unmodified lines45678910111213141516171819202122239242526273 unmodified lines31323319343536377 unmodified lines454647484950515253545556575859606139626364651 unmodified line676869477071497273747535 unmodified lines11111211311411592116117118119120121981221231241252 unmodified lines1281291301071311321331341101111351361371381391 unmodified line1411421431191441451461471231481491501261511521291531541551563 unmodified linesimport difflibimport shutil
try: from g2pk import G2pexcept ImportError: G2p = None
print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")try: if G2p: g2p = G2p() else: g2p = Noneexcept Exception as e: print(f"G2P ๋ก๋ฉ ์คํจ: {e}") g2p = None
dict_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 lines try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) for item in data.get("items", []): for item in data.get("items",[]): if len(item) == 2: flat_dict[item[0]] = item[1] except Exception as e:7 unmodified lines char_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 line if 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_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; padding:10px; border:1px solid #ddd; border-radius:5px;">{"".join(html_parts)}</div>' 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_count
35 unmodified lines return "โ
๋ณต๊ตฌ ์๋ฃ! ์ฌ์ ์ ์ ์์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค." return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."
# UI ํ๋ฉด ๊ตฌ์ฑwith gr.Blocks(theme="default") as iface: gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ํ ์ต์ข
๋ณธ)") gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์ค๋ฅ ์๋ฒฝ ํด๊ฒฐ๋ณธ)") with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"): with gr.Row(): with gr.Column(): input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ") input_text = gr.Textbox(lines=8, label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ") char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="์
๋ ฅ ๊ธ์ ์: 0์", interactive=False) with gr.Row():2 unmodified lines with gr.Column(): # ์ฐ์ธก ์๋จ ๋ณต์ฌ ๋ฒํผ ํ์ฑํ output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ
์คํธ (์ฐ์ธก ์๋จ ๐ ์์ด์ฝ ํด๋ฆญ ์ ๋ณต์ฌ)", show_copy_button=True) 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]) btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count]) 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 line np = 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]) 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]) dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) 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]) up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat], api_name=False)
if __name__ == "__main__": iface.launch()app.py+38-14