Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines345668978911101112131415161718192021222325 unmodified lines49505143525354556 unmodified lines626364565758596065666768697071627273747511 unmodified lines8788898090919293849495969732 unmodified lines1301311321231331341351362 unmodified lines1391401411321421431443 unmodified lines1481491501421511521531461541481491551561572 unmodified linesimport osimport difflibimport tracebackfrom g2pk import G2p
# 1. G2p μμ§ μ΄κΈ°νg2p = G2p()# G2P μμ§μ μ²μλΆν° λΆλ₯΄μ§ μκ³ , νμν λ λΆλ₯΄λλ‘ μ§μ° λ‘λ©(Lazy Loading)g2p_engine = None
# 2. μ¬μ νμΌ κ΄λ¦¬ (μ΄κΈ°νν΄λ μ¬κΈ°λ μ λ μ§μμ§μ§ μμ)def get_g2p(): global g2p_engine if g2p_engine is None: try: from g2pk import G2p g2p_engine = G2p() except Exception as e: print(f"G2P λ‘λ© μλ¬: {e}") return g2p_engine
# μ¬μ νμΌ κ΄λ¦¬dict_path = "custom_dict.json"json_structure = { "items":[],25 unmodified lines
load_dict()
# 3. ν
μ€νΈ λ³ν λ° μμ μ μΈ HTML νκ΄ν μ²λ¦¬# ν
μ€νΈ λ³νdef process_text(input_text): try: if not input_text:6 unmodified lines for 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}" # 2μμ: G2P μμ§ λ³ν (μ¬κΈ°μ μ΅μ΄ 1ν λ‘λ©λ¨) engine = get_g2p() if engine: final_text = engine(processed_text) else: final_text = processed_text + "\n\n[μλ¬: G2P μμ§μ λΆλ¬μ€μ§ λͺ»νμ΅λλ€.]"
# νκ΄νμ HTMLλ‘ μμ νκ² μ²λ¦¬ (Gradio λ²κ·Έ μμ² μ°¨λ¨) # νκ΄ν HTML μ²λ¦¬ seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[] for tag, i1, i2, j1, j2 in seq.get_opcodes():11 unmodified lines error_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 νλ©΄ ꡬμ±# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default", title="λλ§μ TTS λ³νκΈ°") as iface: gr.Markdown("# ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )") 2 unmodified lines input_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 lines output_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