Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567891011128 unmodified lines212223242526271 unmodified line29303132333435363738394041427 unmodified lines505152535455565758596022 unmodified lines838485868788893 unmodified lines9394959697989910010127 unmodified lines1291301311321331341351368 unmodified lines1451461471481491501512 unmodified linesimport osimport difflibimport shutildict_path = "custom_dict.json"# 1. μ¬μ λ‘λ©def load_dict():flat_dict = {}if os.path.exists(dict_path):8 unmodified linespassreturn flat_dict# 2. ν μ€νΈ λ³ν λ° νκ΄ν μ²λ¦¬def process_text(input_text):if not input_text or not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"1 unmodified linechar_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():7 unmodified linesreturn final_text, html_output, char_count# 3. μ΄κΈ°νdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 4. λ¨μ΄/λ¬Έμ₯ μ¬μ μΆκ°def add_dict(word, pronun):if not word or not pronun:return "β οΈ κΈ°μ‘΄ λ¨μ΄μ μνλ λ°μμ λͺ¨λ μ λ ₯νμΈμ."22 unmodified linesexcept Exception as e:return f"β μ μ₯ μ€ν¨: {e}"# 5. λ°±μ λ° λ³΅κ΅¬def get_file():return dict_path if os.path.exists(dict_path) else None3 unmodified linesreturn "β λ°±μ νμΌ λ³΅κ΅¬ μλ£! μ΄μ μ¬μ μ΄ μ μ μ μ©λ©λλ€."return "β οΈ νμΌμ΄ μμ΅λλ€."# 6. UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface:gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )")with gr.Tabs():# --- 첫 λ²μ§Έ ν: λ©μΈ μμ ---27 unmodified linesgr.Markdown("---")gr.Markdown("### π οΈ μ¬μ μ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘")with gr.Row():nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ λλ λ¬Έμ₯ (μ: μ±μ§νΌν° λͺ¨λΈμ)", scale=1)np = gr.Textbox(label="μνλ λ°μ (μ: μ±μ°νΌν° λͺ¨λΈμ)", scale=1)btn_add = gr.Button("πΎ μ¬μ μ μΆκ°", variant="primary", scale=1)status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False)btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status])8 unmodified linesgr.Markdown("---")gr.Markdown("### 2. λ°±μ νμΌ λ³΅κ΅¬νκΈ° (μ λ‘λ)")gr.Markdown("μλ²κ° 리μ€ννΈ λμ΄ μ¬μ μ΄ μ΄κΈ°νλμλ€λ©΄, λ€μ΄λ‘λ ν΄λμλ `custom_dict.json` νμΌμ μλμ λλκ·Έ μ€ λλ‘ νμΈμ.")up_file = gr.File(label="λ°±μ νμΌ μ λ‘λ")up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat])2 unmodified lines34567891011121314151617181920218 unmodified lines303132333435361 unmodified line383940414243444546474849505152535455565758597 unmodified lines676869707172737475767722 unmodified lines1001011021031041051063 unmodified lines11011111211311411511611711827 unmodified lines1461471481491501511521538 unmodified lines1621631641651661671682 unmodified linesimport osimport difflibimport shutilfrom g2pk import G2p# 1. G2P μλ λ°μ λ³ν AI λ€μ μμ νκ² λ‘λ©print("G2P λͺ¨λΈ λ‘λ© μ€...")try:g2p = G2p()except Exception as e:print(f"G2P λ‘λ© μ€ν¨: {e}")g2p = Nonedict_path = "custom_dict.json"# 2. μ¬μ λ‘λ©def load_dict():flat_dict = {}if os.path.exists(dict_path):8 unmodified linespassreturn flat_dict# 3. ν μ€νΈ λ³ν (μ¬μ μ μ© ν -> G2P μλ λ³ν -> νκ΄ν)def process_text(input_text):if not input_text or not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"1 unmodified linechar_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)# 2λ¨κ³: G2P μΈκ³΅μ§λ₯ μλ λ³ν μ μ© (볡ꡬλ¨!)if g2p is not None:try:final_text = g2p(processed)except Exception as e:final_text = processedprint(f"G2P λ³ν μλ¬: {e}")else:final_text = processed# 3λ¨κ³: νκ΄ν ν¨κ³Ό (λ³κ²½λ λΆλΆλ§ λ Έλμ μ²λ¦¬)seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():7 unmodified linesreturn final_text, html_output, char_count# 4. μ΄κΈ°νdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 5. λ¨μ΄/λ¬Έμ₯ μ¬μ μΆκ°def add_dict(word, pronun):if not word or not pronun:return "β οΈ κΈ°μ‘΄ λ¨μ΄μ μνλ λ°μμ λͺ¨λ μ λ ₯νμΈμ."22 unmodified linesexcept Exception as e:return f"β μ μ₯ μ€ν¨: {e}"# 6. λ°±μ λ° λ³΅κ΅¬def get_file():return dict_path if os.path.exists(dict_path) else None3 unmodified linesreturn "β λ°±μ νμΌ λ³΅κ΅¬ μλ£! μ΄μ μ¬μ μ΄ μ μ μ μ©λ©λλ€."return "β οΈ νμΌμ΄ μμ΅λλ€."# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface:gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO μμ±λ³Έ)")with gr.Tabs():# --- 첫 λ²μ§Έ ν: λ©μΈ μμ ---27 unmodified linesgr.Markdown("---")gr.Markdown("### π οΈ μ¬μ μ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘")with gr.Row():nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ λλ λ¬Έμ₯", scale=1)np = gr.Textbox(label="μνλ λ°μ", scale=1)btn_add = gr.Button("πΎ μ¬μ μ μΆκ°", variant="primary", scale=1)status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False)btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status])8 unmodified linesgr.Markdown("---")gr.Markdown("### 2. λ°±μ νμΌ λ³΅κ΅¬νκΈ° (μ λ‘λ)")gr.Markdown("μλ²κ° 리μ€ννΈ λμ΄ μ¬μ μ΄ μ΄κΈ°νλμλ€λ©΄, λ€μ΄λ‘λ ν΄λμλ `custom_dict.json` νμΌμ μλμ μ λ‘λ νμΈμ.")up_file = gr.File(label="λ°±μ νμΌ μ λ‘λ")up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat])
app.py+30-13