Spaces:
Running
Running
Update app.py
Browse files1234567891011128 unmodified lines2122232124252624272826293031323334353633373839404142434445464748495051525354365556575859406061626319 unmodified lines83848586878889909192939495969767989969707172100101102103104105106107108109747576777811011111211311411511611711811912080818283848586878889121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154import gradio as grimport jsonimport osimport difflibimport shutil
dict_path = "custom_dict.json"
# 1. μ¬μ λ‘λ©def load_dict(): flat_dict = {} if os.path.exists(dict_path):8 unmodified lines pass return flat_dict
# G2Pλ₯Ό μ μΈνκ³ 'μ¬μ λ¨μ΄'λ§ λ³κ²½νλ λ‘μ§# 2. ν
μ€νΈ λ³ν λ° νκ΄ν μ²λ¦¬def process_text(input_text): if not input_text or not input_text.strip(): return "", "μ
λ ₯ κΈμ μ: 0μ" return "", "", "μ
λ ₯ κΈμ μ: 0μ" char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ" char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)" processed = input_text flat_dict = load_dict() # μ¬μ μ λ±λ‘λ λ¨μ΄/λ¬Έμ₯μ μ°Ύμ μΉν for word, pronun in flat_dict.items(): processed = processed.replace(word, pronun) return processed, char_count final_text = processed
# νκ΄ν ν¨κ³Ό (λ³κ²½λ λΆλΆλ§ λ
Έλμ μ²λ¦¬) 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; border-radius: 3px;">{chunk}</span>') html_output = f'<div style="line-height:1.6; font-size: 16px; padding:10px; border:1px solid #ddd; border-radius:8px; background-color:#f9f9f9;">{"".join(html_parts)}</div>' return final_text, html_output, char_count
# 3. μ΄κΈ°νdef clear_all(): return "", "", "μ
λ ₯ κΈμ μ: 0μ" return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 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):19 unmodified lines except Exception as e: return f"β μ μ₯ μ€ν¨: {e}"
# 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 "β οΈ νμΌμ΄ μμ΅λλ€."
# 6. UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface: gr.Markdown("### π¨ G2P λͺ¨λ μ κ±° ν
μ€νΈ (μ¬μ κΈ°λ₯λ§ μλ)") gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )") 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.Tabs(): # --- 첫 λ²μ§Έ ν: λ©μΈ μμ
--- with gr.Tab("ποΈ λ©μΈ νλ©΄ (λ³ν λ° λλ³Έ μμ
)"): with gr.Row(): with gr.Column(): input_text = gr.Textbox(lines=8, label="1οΈβ£ μλ³Έ ν
μ€νΈ μ
λ ₯", placeholder="μ΄κ³³μ λλ³Έμ λΆμ¬λ£μΌμΈμ.") char_count = gr.Textbox(label="κΈμ μ νμΈ", value="μ
λ ₯ κΈμ μ: 0μ", interactive=False) with gr.Row(): btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary") 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) btn_copy = gr.Button("π λ³ν볡μ¬", variant="secondary") btn_clr = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") with gr.Column(): 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_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]) # μμ ν λ³νλ³΅μ¬ μλ°μ€ν¬λ¦½νΈ 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) 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])
# --- λ λ²μ§Έ ν: λ°±μ
λ° λ³΅κ΅¬ --- 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])
if __name__ == "__main__": iface.launch()app.py+88-26