Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567891011121314678918101112133 unmodified lines17181920212223242533262728291 unmodified line313233413435363738464748495051395253544056414243447 unmodified lines525354705556575874596061776263646515 unmodified lines818283998485868710388899091929394110959697989910010110210310410510610710810911011111211511311411511612 unmodified lines1291301311341321331341371381391401411421431441351361371471381391401413 unmodified lines1451461471571481491591601501511521531541551651561571581592 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 = None
# 무κ²κ³ μλ¬λ₯Ό μ λ°νλ G2P μμ§μ μμ ν μ κ±°νκ³ , μμ νμ΄μ¬ λ‘μ§λ§ μ¬μ©ν©λλ€.dict_path = "custom_dict.json"
# 2. μ¬μ λ‘λ©# 1. μ¬μ λ‘λ©def load_dict(): flat_dict = {} if os.path.exists(dict_path):3 unmodified lines if "items" in data: for item in data["items"]: if len(item) == 2: # λμ΄μ°κΈ°κ° μλ κΈ΄ λ¬Έμ₯λ μ νν μΈμνλλ‘ κ³΅λ°± 보쑴 flat_dict[item[0]] = item[1] except: pass return flat_dict
# 3. ν
μ€νΈ λ³ν (μ¬μ μ μ© ν -> G2P μλ λ³ν -> νκ΄ν)# 2. ν
μ€νΈ λ³ν λ° νκ΄ν μ²λ¦¬ (μ¬μ κΈ°λ° 100%)def process_text(input_text): if not input_text or not input_text.strip(): return "", "", "μ
λ ₯ κΈμ μ: 0μ"1 unmodified line 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) # 2λ¨κ³: G2P μΈκ³΅μ§λ₯ μλ λ³ν μ μ© (볡ꡬλ¨!) if g2p is not None: try: final_text = g2p(processed) except Exception as e: final_text = processed final_text = processed print(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 lines return final_text, html_output, char_count
# 4. μ΄κΈ°ν# 3. μ΄κΈ°νdef clear_all(): return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 5. λ¨μ΄/λ¬Έμ₯ μ¬μ μΆκ°# 4. λ¨μ΄/λ¬Έμ₯ μ¬μ μΆκ°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):15 unmodified lines try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(j_struct, f, ensure_ascii=False, indent=2) return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£!" return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£! (λ€μ λ³ννκΈ°λ₯Ό λλ₯΄λ©΄ μ μ©λ©λλ€)" except Exception as e: return f"β μ μ₯ μ€ν¨: {e}"
# 6. λ°±μ
λ° λ³΅κ΅¬# 5. λ°±μ
λ° λ³΅κ΅¬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 "β
λ°±μ
νμΌ λ³΅κ΅¬ μλ£! μ΄μ μ μ₯νλ μ¬μ μ΄ μ μ μ μ©λ©λλ€." return "β οΈ νμΌμ΄ μμ΅λλ€."
# 6. μμ ν 볡μ¬μ© μλ°μ€ν¬λ¦½νΈcopy_js = """(text) => { if (!text) { alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€."); return text; } navigator.clipboard.writeText(text); alert("β
ν
μ€νΈκ° ν΄λ¦½λ³΄λμ 볡μ¬λμμ΅λλ€. TTS νλ‘κ·Έλ¨μ λΆμ¬λ£μΌμΈμ!"); return text;}"""
# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface: gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO μμ±λ³Έ)") gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (μλ² μ΅μ ν λ²μ )") with gr.Tabs(): # --- 첫 λ²μ§Έ ν: λ©μΈ μμ
---12 unmodified lines output_text = gr.Textbox(lines=8, 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=None, inputs=[output_text], outputs=None, js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('β
λ³΅μ¬ μλ£! TTSμ λΆμ¬λ£μΌμΈμ.'); } else { alert('볡μ¬ν λ΄μ©μ΄ μμ΅λλ€.'); } }" ) btn_copy.click(fn=lambda x: x, inputs=[output_text], outputs=[output_text], js=copy_js) gr.Markdown("---") gr.Markdown("### π οΈ μ¬μ μ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘") gr.Markdown("### π οΈ μ¬μ μ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ (λ¬Έμ₯ λ¨μ λμ΄μ°κΈ° λ±λ‘ κ°λ₯)") with gr.Row(): nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ λλ λ¬Έμ₯", scale=1) np = gr.Textbox(label="μνλ λ°μ", scale=1)3 unmodified lines
# --- λ λ²μ§Έ ν: λ°±μ
λ° λ³΅κ΅¬ --- with gr.Tab("π₯ λ°±μ
/볡ꡬ (μλ² μ΄κΈ°ν λλΉ)"): gr.Markdown("π‘ **μλ΄:** λ¬΄λ£ μλ² νΉμ±μ μΌμ μκ° μ μμ΄ μμΌλ©΄ 리μ€ννΈ λλ©° λ°μ΄ν°κ° λ μκ°λλ€. λ¨μ΄λ₯Ό μΆκ°ν νμλ κΌ μλ λ²νΌμΌλ‘ λ°±μ
ν΄λμΈμ!") gr.Markdown("π‘ **μλ΄:** λ¬΄λ£ μλ² νΉμ±μ μΌμ μκ° μ μμ΄ μμΌλ©΄ 리μ€ννΈ λλ©° μΆκ°ν΄λ λ°μ΄ν°κ° λ μκ°λλ€. μ¬μ μ λ§μ΄ μΆκ°ν λ μλ κΌ μλ λ²νΌμΌλ‘ PCμ λ°±μ
ν΄λμΈμ!") gr.Markdown("### 1. νμ¬ μ¬μ λ°±μ
νκΈ° (PCλ‘ λ€μ΄λ‘λ)") dl_btn = gr.DownloadButton("π₯ μ΅μ custom_dict.json νμΌ λ€μ΄λ‘λ") gr.Markdown("### 1. νμ¬ μμ±λ μ¬μ λ°±μ
νκΈ° (PCλ‘ λ€μ΄λ‘λ)") dl_btn = gr.DownloadButton("π₯ μ΅μ custom_dict.json λ°±μ
νμΌ λ€μ΄λ‘λ") dl_btn.click(fn=get_file, outputs=[dl_btn]) gr.Markdown("---") gr.Markdown("### 2. λ°±μ
νμΌ λ³΅κ΅¬νκΈ° (μ
λ‘λ)") gr.Markdown("μλ²κ° 리μ€ννΈ λμ΄ μ¬μ μ΄ μ΄κΈ°νλμλ€λ©΄, λ€μ΄λ‘λ ν΄λμλ `custom_dict.json` νμΌμ μλμ μ
λ‘λ νμΈμ.") 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+34-43