Spaces:
Running
Running
Update app.py
Browse files4 unmodified lines56789101112131414 unmodified lines2930313233341 unmodified line36373839404142434445464748491 unmodified line515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231244 unmodified linesimport shutilfrom g2pk import G2pprint("G2P ๋ก๋ฉ ์ค...")g2p = G2p()print("๋ก๋ฉ ์๋ฃ")dict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"count": 0}}flat_dict = {}14 unmodified linesload_dict()def process_text(input_text):if not input_text or not input_text.strip():return "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"1 unmodified linechar_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์"processed = input_textfor w, p in flat_dict.items():processed = processed.replace(w, p)try:final_text = g2p(processed)except Exception as e:return f"์๋ฌ: {e}", "์๋ฌ", char_countseq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():1 unmodified lineif tag == 'equal':html_parts.append(chunk)elif tag in ['replace', 'insert']:html_parts.append(f'<span style="background-color: yellow;">{chunk}</span>')html_output = f'<div style="line-height:1.6; padding:10px; border:1px solid #ddd;">{"".join(html_parts)}</div>'return final_text, html_output, char_countdef clear_all():return "", "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"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:item[1] = pronunfound = Trueif not found:json_structure["items"].append([word, pronun])with open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False)return f"โ '{word}' ์ ์ฅ ์๋ฃ!"def get_file(): return dict_path if os.path.exists(dict_path) else Nonedef upload_file(f):if f:shutil.copy(f.name, dict_path)load_dict()return "๋ณต๊ตฌ ์๋ฃ!"return "ํ์ผ ์์"# ์์ ํ ๋ณต์ฌ ํจ์def dummy_copy(text):return textwith gr.Blocks() as iface: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_copy = gr.Button("๐ ๋ณํ๋ณต์ฌ")btn_clr = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop")with gr.Column():output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ ์คํธ")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;}")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])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])iface.launch()4 unmodified lines567891011121314151614 unmodified lines313233343536371 unmodified line3940414243444546474849505152531 unmodified line5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441454 unmodified linesimport 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 = {}14 unmodified linesload_dict()# 3. ํต์ฌ ๋ณํ ๊ธฐ๋ฅ (์๋ฌ ์์ธ ์ฐจ๋จ)def process_text(input_text):if not input_text or not input_text.strip():return "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"1 unmodified linechar_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():1 unmodified lineif 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: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}"# 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()
app.py+61-40