Spaces:
Running
Running
Update app.py
Browse files12345678910111213141545611 unmodified lines1819203321222336242526272 unmodified lines303132454633474849505134535455565758596061626364653567686970713637733839764041424319 unmodified lines636465102103104105106107108109110111112661146768116117118119120121122697071721231241251261271281291301311321331341351361377313914014114214314414514614714814974757677787915115215315480818283848586878889909192import gradio as grimport jsonimport osimport difflibimport shutilimport tracebackfrom g2pk import G2p
# 1. G2P λͺ¨λΈ μμ λ‘λ©print("G2P λ‘λ© μ€...")try: g2p = G2p()except Exception as e: print(f"G2P λ‘λ© μ€ν¨: {e}") g2p = None
dict_path = "custom_dict.json"
11 unmodified lines pass return flat_dict
# 2. ν
μ€νΈ λ³ν λ‘μ§# G2Pλ₯Ό μ μΈνκ³ 'μ¬μ λ¨μ΄'λ§ λ³κ²½νλ λ‘μ§def process_text(input_text): if not input_text or not input_text.strip(): return "", "", "μ
λ ₯ κΈμ μ: 0μ" return "", "μ
λ ₯ κΈμ μ: 0μ" char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ" processed = input_text2 unmodified lines for word, pronun in flat_dict.items(): processed = processed.replace(word, pronun) if g2p is None: return "G2P λͺ¨λΈ λ‘λ© μ€ν¨", "μλ¬", char_count return processed, char_count
try: final_text = g2p(processed) except Exception as e: return f"λ³ν μλ¬: {e}", "μλ¬", char_count
seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[] for tag, i1, i2, j1, j2 in seq.get_opcodes(): chunk = final_text[j1:j2].replace('\n', '<br>') 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_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_count
# 3. μ΄κΈ°ν λ‘μ§def clear_all(): return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 4. μμ ν λ³΅μ¬ μλ΄def safe_copy_notice(): gr.Info("ν
μ€νΈμ°½ μ°μΈ‘ μλ¨μ π μμ΄μ½μ λλ₯΄μλ©΄ μμ νκ² λ³΅μ¬λ©λλ€.") return "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 5. μ¬μ μΆκ° λ‘μ§def add_dict(word, pronun): if not word or not pronun: return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ
λ ₯νμΈμ." return "β οΈ λ¨μ΄λ₯Ό μ
λ ₯νμΈμ." j_struct = {"items":[], "meta": {"count": 0}} if os.path.exists(dict_path):19 unmodified lines except Exception as e: return f"β μ μ₯ μ€ν¨: {e}"
# 6. λ°±μ
/볡ꡬ λ‘μ§def get_file(): return dict_path if os.path.exists(dict_path) else None
def upload_file(f): if f is not None: shutil.copy(f.name, dict_path) return "β
λ°±μ
νμΌ λ³΅κ΅¬ μλ£!" return "β οΈ νμΌμ΄ μμ΅λλ€."
# UI ꡬμ±with gr.Blocks(theme="default") as iface: gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (μ€λ₯ μμ μ΅μ’
λ³Έ)") gr.Markdown("### π¨ G2P λͺ¨λ μ κ±° ν
μ€νΈ (μ¬μ κΈ°λ₯λ§ μλ)") 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(): with gr.Column(): input_text = gr.Textbox(lines=5, label="1οΈβ£ μλ³Έ ν
μ€νΈ") char_count = gr.Textbox(label="κΈμ μ", value="0μ", interactive=False) with gr.Row(): btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary") btn_copy = gr.Button("π λ³νλ³΅μ¬ μλ΄") btn_clr = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") with 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]) btn_copy.click(fn=safe_copy_notice, inputs=None, outputs=None) gr.Markdown("---") gr.Markdown("#### π οΈ μ¬μ μ λ¨μ΄ μ¦μ λ±λ‘") with gr.Row(): nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄") 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])
# λ°±μ
ν with gr.Tab("λ°±μ
/볡ꡬ (μλ² λ¦¬μ€ννΈ λλΉ)"): gr.Markdown("μλ² μ¬μμμΌλ‘ λ°μ΄ν°κ° μ§μμ‘μ λλ₯Ό λλΉν΄ νμΌμ λ°±μ
νκ³ λ³΅κ΅¬νμΈμ.") dl_btn = gr.DownloadButton("π₯ νμ¬ μ¬μ PCλ‘ λ°±μ
νκΈ° (λ€μ΄λ‘λ)") dl_btn.click(fn=get_file, outputs=[dl_btn]) btn_conv = gr.Button("β¨ λ³ννκΈ° (μ¬μ λ§ μ μ©)", variant="primary") btn_clr = gr.Button("ποΈ μ΄κΈ°ν", variant="stop") with gr.Column(): output_text = gr.Textbox(lines=5, label="2οΈβ£ λ³νλ ν
μ€νΈ", show_copy_button=True) gr.Markdown("---") up_file = gr.File(label="π€ λ°±μ
ν΄λ νμΌ μ
λ‘λνμ¬ λ³΅κ΅¬νκΈ°") up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat]) btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, char_count]) btn_clr.click(fn=clear_all, outputs=[input_text, output_text, char_count]) gr.Markdown("---") with gr.Row(): nw = gr.Textbox(label="λ¨μ΄") np = gr.Textbox(label="λ°μ") btn_add = gr.Button("πΎ μ μ₯") status = gr.Textbox(label="μν", interactive=False) btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status])
if __name__ == "__main__": iface.launch()app.py+25-90