Spaces:
Running
Running
Update app.py
Browse files123454589678119101311151612131419151617181920252122232425262732333428293637303132393334353637383940414243444849505152535455565758596061454663646566676847484970505172525354555657585960616263643 unmodified lines686970717273748575768777788979808182939495969798991001011028384858610410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114287888990919293949596979899100101102103104105106107108109110111import gradio as grimport jsonimport osimport difflibimport shutilfrom g2pk import G2p
# 1. ๋ชจ๋ธ ๋ก๋ฉprint("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")# 1. ์์ง ๋ก๋ฉprint("G2P ๋ก๋ฉ ์์...")g2p = G2p()print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")print("G2P ๋ก๋ฉ ์๋ฃ!")
# 2. ์ฌ์ ๊ด๋ฆฌdict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"count": 0}}flat_dict = {}
# 2. ์ฌ์ ํ์ผ ์ฝ๊ธฐdef load_dict(): global json_structure, flat_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: json_structure = data for item in data["items"]: if len(item) == 2: flat_dict[item[0]] = item[1] except: pass return flat_dict
load_dict()
# 3. ํต์ฌ ๋ณํ ๊ธฐ๋ฅ (์๋ฌ ์์ธ ์ฐจ๋จ)# 3. ๋ณํ ๊ธฐ๋ฅ (๊ฐ์ฅ ๋จ์ํ)def process_text(input_text): if not input_text or not input_text.strip(): return "", "", "์
๋ ฅ ๊ธ์ ์: 0์" if not input_text: return "์
๋ ฅ๋ ํ
์คํธ๊ฐ ์์ต๋๋ค." char_count = f"์
๋ ฅ ๊ธ์ ์: {len(input_text)}์" 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}", "์๋ฌ ๋ฐ์", char_count
# ํ๊ดํ (HTML ์ค๋ฅ ๋ฐฉ์ง) 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;">{chunk}</span>') 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 return f"๋ณํ ์ค ์๋ฌ ๋ฐ์: {e}"
# 4. ์ด๊ธฐํ ๊ธฐ๋ฅdef clear_all(): return "", "", "", "์
๋ ฅ ๊ธ์ ์: 0์"
# 5. ์ฌ์ ์ถ๊ฐ ๊ธฐ๋ฅdef add_dict(word, pronun):# 4. ์ฌ์ ์ถ๊ฐ ๊ธฐ๋ฅdef add_word(word, pronun): if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์." return "๋จ์ด์ ๋ฐ์์ ์
๋ ฅํ์ธ์." flat_dict[word] = pronun # ๊ธฐ์กด ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ 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:3 unmodified lines 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}' ์ ์ฅ ์๋ฃ!" return f"โ
'{word}' -> '{pronun}' ์ถ๊ฐ ์๋ฃ!" except Exception as e: return f"โ ์ ์ฅ ์คํจ: {e}" return f"์ ์ฅ ์๋ฌ: {e}"
# 6. ํ์ผ ๋ฐฑ์
/๋ณต๊ตฌ ๊ธฐ๋ฅ# 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) load_dict() return "โ
๋ณต๊ตฌ ์๋ฃ! ์ฌ์ ์ ์ ์์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค." return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."
# UI ํ๋ฉด ๊ตฌ์ฑwith gr.Blocks(theme="default") as iface: gr.Markdown("### ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ")# 6. UI ํ๋ฉด (๋ฒํผ ๋์ ์ฌ๋ถ๋ง ํ์คํ ํ
์คํธ)with gr.Blocks() as iface: gr.Markdown("## ๐๏ธ ๊ฐ์ฅ ๋จ์ํ๊ณ ํ์คํ TTS ๋ณํ๊ธฐ (ํ
์คํธ์ฉ)") with gr.Tabs(): # ์ฒซ ๋ฒ์งธ ํญ: ๋ฉ์ธ ์์
with gr.Tab("๋ฉ์ธ ํ๋ฉด (๋ณํ ๋ฐ ์ฌ์ ๋ฑ๋ก)"): with gr.Row(): with gr.Column(): 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_clr = gr.Button("๐๏ธ ๋๋ณธ ์ด๊ธฐํ", variant="stop") with gr.Column(): # ์์ฒด ๋ด์ฅ๋ ๋ณต์ฌ ์์ด์ฝ(show_copy_button=True) ์ฌ์ฉ output_text = gr.Textbox(lines=7, 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]) gr.Markdown("---") gr.Markdown("#### ๐ ๏ธ ์ฌ์ ์ ๋จ์ด ์ฆ์ ๋ฑ๋ก") with gr.Row(): nw = gr.Textbox(label="๊ธฐ์กด ๋จ์ด") np = gr.Textbox(label="์ํ๋ ๋ฐ์") 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("๋ฐฑ์
/๋ณต๊ตฌ (์๋ฒ ๋ฆฌ์คํํธ ๋๋น)"): 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]) 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()app.py+56-90