Spaces:
Running
Running
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374753 unmodified lines798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145import gradio as grimport jsonimport osimport difflibimport shutilfrom g2pk import G2p# 1. ๋ชจ๋ธ ๋ก๋ฉprint("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")g2p = G2p()print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")# 2. ์ฌ์ ๊ด๋ฆฌdict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"count": 0}}flat_dict = {}def load_dict():global json_structure, flat_dictif 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 = datafor item in data["items"]:if len(item) == 2:flat_dict[item[0]] = item[1]except:passload_dict()# 3. ํต์ฌ ๋ณํ ๊ธฐ๋ฅ (์๋ฌ ์์ธ ์ฐจ๋จ)def process_text(input_text):if not input_text or not input_text.strip():return "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"char_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์"processed = input_textfor 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# ํ๊ดํ (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# 4. ์ด๊ธฐํ ๊ธฐ๋ฅdef clear_all():return "", "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"# 5. ์ฌ์ ์ถ๊ฐ ๊ธฐ๋ฅdef add_dict(word, pronun):if not word or not pronun:return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์ ๋ ฅํ์ธ์."flat_dict[word] = pronunfound = Falsefor item in json_structure["items"]:if item[0] == word:3 unmodified linesif 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}"# 6. ํ์ผ ๋ฐฑ์ /๋ณต๊ตฌ ๊ธฐ๋ฅdef get_file():return dict_path if os.path.exists(dict_path) else Nonedef 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 ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ")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])if __name__ == "__main__":iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263643 unmodified lines6869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import 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:3 unmodified linesif 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()
app.py+56-90