Spaces:
Running
Running
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import gradio as grimport jsonimport osfrom g2pk import G2p# 1. μμ§ λ‘λ©print("G2P λ‘λ© μμ...")g2p = G2p()print("G2P λ‘λ© μλ£!")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:passreturn 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_textexcept 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 = Falsefor item in json_structure["items"]:if item[0] == word:item[1] = pronunfound = Truebreakif 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("---")dl_btn = gr.DownloadButton("π₯ μ¬μ νμΌ λ€μ΄λ‘λ")dl_btn.click(fn=get_file, outputs=dl_btn)if __name__ == "__main__":iface.launch()123456789101112131415import gradio as grdef test_click(text):return f"β λ²νΌ μ μ μλ νμΈλ¨: {text}"with gr.Blocks() as iface:gr.Markdown("### π¨ ν΅μ μλ¬ μμΈ νμ μ© ν μ€νΈ")input_box = gr.Textbox(label="μ무 κΈμλ μ λ ₯ν΄λ³΄μΈμ")output_box = gr.Textbox(label="κ²°κ³Όκ° λμ€λ©΄ λ²νΌ ν΅μ μ μ μμ λλ€")btn = gr.Button("ν μ€νΈ λ²νΌ")btn.click(fn=test_click, inputs=input_box, outputs=output_box)if __name__ == "__main__":iface.launch()
app.py+7-103