Spaces:
Running
Running
Update app.py
Browse files123426789345111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182836858687888990919293949596979899100101102103104789101110610710812131415import gradio as grimport jsonimport osfrom g2pk import G2p
# 1. μμ§ λ‘λ©print("G2P λ‘λ© μμ...")g2p = G2p()print("G2P λ‘λ© μλ£!")def test_click(text): return f"β
λ²νΌ μ μ μλ νμΈλ¨: {text}"
dict_path = "custom_dict.json"
# 2. μ¬μ νμΌ μ½κΈ°def load_dict(): flat_dict = {} if os.path.exists(dict_path): try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) 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. λ³ν κΈ°λ₯ (κ°μ₯ λ¨μν)def process_text(input_text): if not input_text: return "μ
λ ₯λ ν
μ€νΈκ° μμ΅λλ€." flat_dict = load_dict() processed = input_text # 1μ°¨: λ΄ μ¬μ μ μ© for word, pronun in flat_dict.items(): processed = processed.replace(word, pronun) # 2μ°¨: G2P μ μ© try: final_text = g2p(processed) return final_text except Exception as e: return f"λ³ν μ€ μλ¬ λ°μ: {e}"
# 4. μ¬μ μΆκ° κΈ°λ₯def add_word(word, pronun): if not word or not pronun: return "λ¨μ΄μ λ°μμ μ
λ ₯νμΈμ." # κΈ°μ‘΄ λ°μ΄ν° λΆλ¬μ€κΈ° json_structure = {"items":[], "meta": {"count": 0}} if os.path.exists(dict_path): try: with open(dict_path, "r", encoding="utf-8") as f: json_structure = json.load(f) except: pass # μ
λ°μ΄νΈ 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}' -> '{pronun}' μΆκ° μλ£!" except Exception as e: return f"μ μ₯ μλ¬: {e}"
# 5. λ€μ΄λ‘λ κΈ°λ₯def get_file(): return dict_path if os.path.exists(dict_path) else None
# 6. UI νλ©΄ (λ²νΌ λμ μ¬λΆλ§ νμ€ν ν
μ€νΈ)with gr.Blocks() as iface: gr.Markdown("## ποΈ κ°μ₯ λ¨μνκ³ νμ€ν TTS λ³νκΈ° (ν
μ€νΈμ©)") with gr.Row(): input_text = gr.Textbox(lines=5, label="1οΈβ£ μλ³Έ μ
λ ₯") output_text = gr.Textbox(lines=5, label="2οΈβ£ λ³ν κ²°κ³Ό (볡μ¬λ μ§μ λλκ·Έ)", show_copy_button=True) convert_btn = gr.Button("β¨ λ³ννκΈ°", variant="primary") # λ³ν λ²νΌ ν΄λ¦ μ μ¦κ° μ€ν convert_btn.click(fn=process_text, inputs=input_text, outputs=output_text) gr.Markdown("---") gr.Markdown("### π οΈ μ¬μ μΆκ°") with gr.Row(): new_w = gr.Textbox(label="λ¨μ΄") new_p = gr.Textbox(label="λ°μ") add_btn = gr.Button("πΎ μ μ₯") status_text = gr.Textbox(label="κ²°κ³Ό") add_btn.click(fn=add_word, inputs=[new_w, new_p], outputs=status_text) gr.Markdown("### π¨ ν΅μ μλ¬ μμΈ νμ
μ© ν
μ€νΈ") input_box = gr.Textbox(label="μ무 κΈμλ μ
λ ₯ν΄λ³΄μΈμ") output_box = gr.Textbox(label="κ²°κ³Όκ° λμ€λ©΄ λ²νΌ ν΅μ μ μ μμ
λλ€") btn = gr.Button("ν
μ€νΈ λ²νΌ") gr.Markdown("---") dl_btn = gr.DownloadButton("π₯ μ¬μ νμΌ λ€μ΄λ‘λ") dl_btn.click(fn=get_file, outputs=dl_btn) btn.click(fn=test_click, inputs=input_box, outputs=output_box)
if __name__ == "__main__": iface.launch()app.py+7-103