Spaces:
Running
Running
Update app.py
Browse files4 unmodified lines567889101011121314151614 unmodified lines313233343536371 unmodified line3940413940424344454647454849505152531 unmodified line5556575458595660616263646566676863646970717273747576777879808182747576838485868788899078919293819495968485979899878889909110010110210310410593106107108961091101111121001011131141151161031041171181191061071081201211221231241251261271281131141151291301311321171181191201211221331341351361371381391401411421431441241454 unmodified linesimport shutilfrom g2pk import G2p
# 1. λͺ¨λΈ λ‘λ©print("G2P λ‘λ© μ€...")print("G2P λͺ¨λΈ λ‘λ© μ€...")g2p = G2p()print("λ‘λ© μλ£")print("G2P λͺ¨λΈ λ‘λ© μλ£!")
# 2. μ¬μ κ΄λ¦¬dict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"count": 0}}flat_dict = {}14 unmodified lines
load_dict()
# 3. ν΅μ¬ λ³ν κΈ°λ₯ (μλ¬ μμΈ μ°¨λ¨)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 for w, p in flat_dict.items(): processed = processed.replace(w, p) for word, pronun in flat_dict.items(): processed = processed.replace(word, pronun) try: final_text = g2p(processed) except Exception as e: return f"μλ¬: {e}", "μλ¬", char_count return f"λ³ν μλ¬: {e}", "μλ¬ λ°μ", char_count
# νκ΄ν (HTML μ€λ₯ λ°©μ§) seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[] for tag, i1, i2, j1, j2 in seq.get_opcodes():1 unmodified line if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: yellow;">{chunk}</span>') html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold;">{chunk}</span>') html_output = f'<div style="line-height:1.6; padding:10px; border:1px solid #ddd;">{"".join(html_parts)}</div>' 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
# 4. μ΄κΈ°ν κΈ°λ₯def clear_all(): return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 5. μ¬μ μΆκ° κΈ°λ₯def add_dict(word, pronun): if not word or not pronun: return "μ
λ ₯ μ€λ₯" flat_dict[word] = pronun if not word or not pronun: return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ
λ ₯νμΈμ." flat_dict[word] = pronun found = False for item in json_structure["items"]: if item[0] == word: item[1] = pronun found = True break if not found: json_structure["items"].append([word, pronun]) try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False) return f"β
'{word}' μ μ₯ μλ£!" with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False) return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£!" except Exception as e: return f"β μ μ₯ μ€ν¨: {e}"
# 6. νμΌ λ°±μ
/볡ꡬ κΈ°λ₯def get_file():def get_file(): return dict_path if os.path.exists(dict_path) else None return dict_path if os.path.exists(dict_path) else None
def upload_file(f): if f: if f is not None: shutil.copy(f.name, dict_path) load_dict() return "볡ꡬ μλ£!" return "νμΌ μμ" return "β
볡ꡬ μλ£! μ¬μ μ μ μμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€." return "β οΈ νμΌμ΄ μμ΅λλ€."
# μμ ν λ³΅μ¬ ν¨μdef dummy_copy(text): return text
with gr.Blocks() as iface:# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface: gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°") with gr.Tabs(): # 첫 λ²μ§Έ ν: λ©μΈ μμ
with gr.Tab("λ©μΈ νλ©΄"): with gr.Tab("λ©μΈ νλ©΄ (λ³ν λ° μ¬μ λ±λ‘)"): with gr.Row(): with gr.Column(): input_text = gr.Textbox(lines=7, label="1οΈβ£ μλ³Έ ν
μ€νΈ") input_text = gr.Textbox(lines=7, 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") btn_clr = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") with gr.Column(): # μ체 λ΄μ₯λ λ³΅μ¬ μμ΄μ½(show_copy_button=True) μ¬μ© output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν
μ€νΈ") output_html = gr.HTML(label="π λ³κ²½μ ") output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν
μ€νΈ (μ°μΈ‘ μλ¨ π μμ΄μ½ ν΄λ¦ μ μ¦μ 볡μ¬)", show_copy_button=True) output_html = gr.HTML(label="π λ³κ²½μ νμΈ") btn_conv.click(process_text, inputs=[input_text], outputs=[output_text, output_html, char_count]) btn_clr.click(clear_all, outputs=[input_text, output_text, output_html, char_count]) btn_copy.click(dummy_copy, inputs=[output_text], outputs=[output_text], js="(t) => {navigator.clipboard.writeText(t); alert('λ³΅μ¬ μλ£!'); return t;}") # λ²νΌ μ°κ²° (κ°μ₯ λ¨μνκ³ μμ ν λ°©μ) 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]) gr.Markdown("---") gr.Markdown("#### π οΈ μ¬μ μ λ¨μ΄ μ¦μ λ±λ‘") with gr.Row(): nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄") np = gr.Textbox(label="μνλ λ°μ") btn_add = gr.Button("πΎ μΆκ°") status = gr.Textbox(label="μν") btn_add.click(add_dict, inputs=[nw, np], outputs=[status]) 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("λ°±μ
/볡ꡬ"): dl_btn = gr.DownloadButton("π₯ λ€μ΄λ‘λ (λ°±μ
)") dl_btn.click(get_file, outputs=[dl_btn]) up_file = gr.File(label="μ
λ‘λ (볡ꡬ)") up_stat = gr.Textbox(label="μν") up_file.upload(upload_file, inputs=[up_file], outputs=[up_stat]) # λ λ²μ§Έ ν: λ°±μ
λ° λ³΅κ΅¬ with gr.Tab("λ°±μ
/볡ꡬ (μλ² λ¦¬μ€ννΈ λλΉ)"): gr.Markdown("μλ² μ¬μμμΌλ‘ λ°μ΄ν°κ° μ§μμ‘μ λλ₯Ό λλΉν΄ νμΌμ λ°±μ
νκ³ λ³΅κ΅¬νμΈμ.") dl_btn = gr.DownloadButton("π₯ νμ¬ μ¬μ PCλ‘ λ°±μ
νκΈ° (λ€μ΄λ‘λ)") dl_btn.click(fn=get_file, outputs=[dl_btn]) 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])
if __name__ == "__main__":iface.launch() iface.launch()app.py+61-40