Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567891011121314151617181920213 unmodified lines2526272829303132333435361 unmodified line383940414243444546474849505152535455565758597 unmodified lines676869707172737475767778798015 unmodified lines9697989910010110210310410510610710810911011111211311411511611711812 unmodified lines1311321331341351361371381391401411421431441451461471481491503 unmodified lines1541551561571581591601611621631641651661671682 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):3 unmodified linesif "items" in data:for item in data["items"]:if len(item) == 2:flat_dict[item[0]] = item[1]except:passreturn 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 "β οΈ κΈ°μ‘΄ λ¨μ΄μ μνλ λ°μμ λͺ¨λ μ λ ₯νμΈμ."j_struct = {"items":[], "meta": {"count": 0}}if os.path.exists(dict_path):15 unmodified linestry:with open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)return f"β '{word}' -> '{pronun}' μ μ₯ μλ£!"except Exception as e:return f"β μ μ₯ μ€ν¨: {e}"# 6. λ°±μ λ° λ³΅κ΅¬def get_file():return dict_path if os.path.exists(dict_path) else Nonedef 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 μ μ²λ¦¬ λ³νκΈ° (PRO μμ±λ³Έ)")with gr.Tabs():# --- 첫 λ²μ§Έ ν: λ©μΈ μμ ---12 unmodified linesoutput_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('볡μ¬ν λ΄μ©μ΄ μμ΅λλ€.'); } }")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("### 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` νμΌμ μλμ μ λ‘λ νμΈμ.")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 lines3456789101112133 unmodified lines171819202122232425262728291 unmodified line31323334353637383940414243447 unmodified lines525354555657585960616263646515 unmodified lines8182838485868788899091929394959697989910010110210310410510610710810911011111211311411511612 unmodified lines1291301311321331341351361371381391401413 unmodified lines1451461471481491501511521531541551561571581592 unmodified linesimport osimport difflibimport shutil# 무κ²κ³ μλ¬λ₯Ό μ λ°νλ G2P μμ§μ μμ ν μ κ±°νκ³ , μμ νμ΄μ¬ λ‘μ§λ§ μ¬μ©ν©λλ€.dict_path = "custom_dict.json"# 1. μ¬μ λ‘λ©def load_dict():flat_dict = {}if os.path.exists(dict_path):3 unmodified linesif "items" in data:for item in data["items"]:if len(item) == 2:# λμ΄μ°κΈ°κ° μλ κΈ΄ λ¬Έμ₯λ μ νν μΈμνλλ‘ κ³΅λ°± 보쑴flat_dict[item[0]] = item[1]except:passreturn flat_dict# 2. ν μ€νΈ λ³ν λ° νκ΄ν μ²λ¦¬ (μ¬μ κΈ°λ° 100%)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 "β οΈ κΈ°μ‘΄ λ¨μ΄(λ¬Έμ₯)μ μνλ λ°μμ λͺ¨λ μ λ ₯νμΈμ."j_struct = {"items":[], "meta": {"count": 0}}if os.path.exists(dict_path):15 unmodified linestry:with open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)return f"β '{word}' -> '{pronun}' μ μ₯ μλ£! (λ€μ λ³ννκΈ°λ₯Ό λλ₯΄λ©΄ μ μ©λ©λλ€)"except Exception as e:return f"β μ μ₯ μ€ν¨: {e}"# 5. λ°±μ λ° λ³΅κ΅¬def get_file():return dict_path if os.path.exists(dict_path) else Nonedef upload_file(f):if f is not None:shutil.copy(f.name, dict_path)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 μ μ²λ¦¬ λ³νκΈ° (μλ² μ΅μ ν λ²μ )")with gr.Tabs():# --- 첫 λ²μ§Έ ν: λ©μΈ μμ ---12 unmodified linesoutput_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=lambda x: x, inputs=[output_text], outputs=[output_text], js=copy_js)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("π‘ **μλ΄:** λ¬΄λ£ μλ² νΉμ±μ μΌμ μκ° μ μμ΄ μμΌλ©΄ 리μ€ννΈ λλ©° μΆκ°ν΄λ λ°μ΄ν°κ° λ μκ°λλ€. μ¬μ μ λ§μ΄ μΆκ°ν λ μλ κΌ μλ λ²νΌμΌλ‘ PCμ λ°±μ ν΄λμΈμ!")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` νμΌμ μ΄κ³³μ λλκ·Έ μ€ λλ‘ νμΈμ.")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