Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines3456789101112131425 unmodified lines404142434445466 unmodified lines5354555657585960616263646511 unmodified lines777879808182838485868732 unmodified lines1201211221231241251262 unmodified lines1291301311321331341353 unmodified lines1391401411421431441451461471481491501511522 unmodified linesimport osimport difflibimport tracebackfrom g2pk import G2p# 1. G2p μμ§ μ΄κΈ°νg2p = G2p()# 2. μ¬μ νμΌ κ΄λ¦¬ (μ΄κΈ°νν΄λ μ¬κΈ°λ μ λ μ§μμ§μ§ μμ)dict_path = "custom_dict.json"json_structure = {"items":[],25 unmodified linesload_dict()# 3. ν μ€νΈ λ³ν λ° μμ μ μΈ HTML νκ΄ν μ²λ¦¬def process_text(input_text):try:if not input_text:6 unmodified linesfor word, pronunciation in flat_dict.items():processed_text = processed_text.replace(word, pronunciation)# 2μμ: G2P μμ§ λ³νtry:final_text = g2p(processed_text)except Exception as e:final_text = f"G2P μμ§ λ³ν μ€ν¨: {e}"# νκ΄νμ HTMLλ‘ μμ νκ² μ²λ¦¬ (Gradio λ²κ·Έ μμ² μ°¨λ¨)seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():11 unmodified lineserror_msg = f"μμ€ν μλ¬ λ°μ:\n{traceback.format_exc()}"return error_msg, f"<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ"# 4. μ΄κΈ°ν ν¨μ (ν μ€νΈμ°½λ§ λΉμ°κ³ μ¬μ μ 건λλ¦¬μ§ μμ)def clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 5. μ€μκ° μ¬μ μΆκ° κΈ°λ₯def add_to_dict(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update()32 unmodified linesdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# 6. UI νλ©΄ ꡬμ±with gr.Blocks(theme="default", title="λλ§μ TTS λ³νκΈ°") as iface:gr.Markdown("# ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )")2 unmodified linesinput_text = gr.Textbox(lines=7, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯", placeholder="μ¬κΈ°μ μλ³Έ λλ³Έμ μ λ ₯νμΈμ...")char_count_display = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False, lines=1)# 3κ°μ λ²νΌμ λλν λ°°μΉ (λΉμ¨ λμΌ)with gr.Row():convert_btn = gr.Button("β¨ μ리λλ λλ‘ λ³ννκΈ°", variant="primary")copy_btn = gr.Button("π λ³νλ³΅μ¬ (TTSμ©)", variant="secondary")3 unmodified linesoutput_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ")output_html = gr.HTML(label="π λ³κ²½μ νμΈ (λ Έλμ νκ΄ν)")# μ΄λ²€νΈ μ°κ²°input_text.change(fn=lambda x: f"μ λ ₯ κΈμ μ: {len(x)}μ (곡백 ν¬ν¨)", inputs=input_text, outputs=char_count_display)convert_btn.click(fn=process_text, inputs=input_text, outputs=[output_text, output_html, char_count_display])# λ³νλ³΅μ¬ λ²νΌ (μλ°μ€ν¬λ¦½νΈλ‘ λΈλΌμ°μ ν΄λ¦½λ³΄λμ μ§μ 볡μ¬)copy_btn.click(fn=None, inputs=[output_text], outputs=[output_text], js=copy_js)# μ΄κΈ°ν λ²νΌclear_btn.click(fn=clear_all, inputs=[], outputs=[input_text, output_text, output_html, char_count_display])gr.Markdown("---")2 unmodified lines3456789101112131415161718192021222325 unmodified lines495051525354556 unmodified lines626364656667686970717273747511 unmodified lines878889909192939495969732 unmodified lines1301311321331341351362 unmodified lines1391401411421431443 unmodified lines1481491501511521531541551561572 unmodified linesimport osimport difflibimport traceback# G2P μμ§μ μ²μλΆν° λΆλ₯΄μ§ μκ³ , νμν λ λΆλ₯΄λλ‘ μ§μ° λ‘λ©(Lazy Loading)g2p_engine = Nonedef get_g2p():global g2p_engineif g2p_engine is None:try:from g2pk import G2pg2p_engine = G2p()except Exception as e:print(f"G2P λ‘λ© μλ¬: {e}")return g2p_engine# μ¬μ νμΌ κ΄λ¦¬dict_path = "custom_dict.json"json_structure = {"items":[],25 unmodified linesload_dict()# ν μ€νΈ λ³νdef process_text(input_text):try:if not input_text:6 unmodified linesfor word, pronunciation in flat_dict.items():processed_text = processed_text.replace(word, pronunciation)# 2μμ: G2P μμ§ λ³ν (μ¬κΈ°μ μ΅μ΄ 1ν λ‘λ©λ¨)engine = get_g2p()if engine:final_text = engine(processed_text)else:final_text = processed_text + "\n\n[μλ¬: G2P μμ§μ λΆλ¬μ€μ§ λͺ»νμ΅λλ€.]"# νκ΄ν HTML μ²λ¦¬seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():11 unmodified lineserror_msg = f"μμ€ν μλ¬ λ°μ:\n{traceback.format_exc()}"return error_msg, f"<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ"# μ΄κΈ°ν ν¨μdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# μ€μκ° μ¬μ μΆκ°def add_to_dict(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update()32 unmodified linesdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default", title="λλ§μ TTS λ³νκΈ°") as iface:gr.Markdown("# ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )")2 unmodified linesinput_text = gr.Textbox(lines=7, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯", placeholder="μ¬κΈ°μ μλ³Έ λλ³Έμ μ λ ₯νμΈμ...")char_count_display = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False, lines=1)with gr.Row():convert_btn = gr.Button("β¨ μ리λλ λλ‘ λ³ννκΈ°", variant="primary")copy_btn = gr.Button("π λ³νλ³΅μ¬ (TTSμ©)", variant="secondary")3 unmodified linesoutput_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ")output_html = gr.HTML(label="π λ³κ²½μ νμΈ (λ Έλμ νκ΄ν)")input_text.change(fn=lambda x: f"μ λ ₯ κΈμ μ: {len(x)}μ (곡백 ν¬ν¨)", inputs=input_text, outputs=char_count_display)convert_btn.click(fn=process_text, inputs=input_text, outputs=[output_text, output_html, char_count_display])copy_btn.click(fn=None, inputs=[output_text], outputs=[output_text], js=copy_js)clear_btn.click(fn=clear_all, inputs=[], outputs=[input_text, output_text, output_html, char_count_display])gr.Markdown("---")
app.py+24-19