Spaces:
Running
Running
Update app.py
Browse files1 unmodified line23456789101112131415162 unmodified lines192021222324256 unmodified lines323334353637382 unmodified lines4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384854 unmodified lines909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271 unmodified lineimport jsonimport osimport difflibfrom g2pk import G2p# 1. G2p ์์ง ์ด๊ธฐํg2p = G2p()# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌ ๋ฐ ๊ตฌ์กฐ ์ ์งdict_path = "custom_dict.json"# ์๋ณธ JSON์ ๋ผ๋๋ฅผ ์ ์งํ๊ธฐ ์ํ ๋ณ์json_structure = {"items":[],"meta": {2 unmodified lines"count": 0}}flat_dict = {} # ์ค์ ๋ณํ์ ์ฌ์ฉํ 1:1 ๋งคํ ๋์ ๋๋ฆฌdef load_dict():global json_structure, flat_dict6 unmodified linesfor item in data["items"]:if len(item) == 2:flat_dict[item[0]] = item[1]else: # ์์ ๋ฐฉ์์ ๋จ์ ๋์ ๋๋ฆฌ์ผ ๊ฒฝ์ฐ ํธํflat_dict = datajson_structure["items"] = [[k, v] for k, v in data.items()]json_structure["meta"]["count"] = len(flat_dict)2 unmodified linesload_dict()# 3. ํ ์คํธ ๋ณํ ๋ฐ ํ์ด๋ผ์ดํธ ํจ์def process_text(input_text):if not input_text:return "",[], "๊ธ์ ์: 0์"char_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์ (๊ณต๋ฐฑ ํฌํจ)"processed_text = input_text# 1์์: ์ฌ์ฉ์ ์ฌ์ ์ ์๋ ๋จ์ด ์ฐ์ ์นํfor word, pronunciation in flat_dict.items():processed_text = processed_text.replace(word, pronunciation)# 2์์: G2P ์์ง์ผ๋ก ๋๋จธ์ง ๋ณํtry:final_text = g2p(processed_text)except Exception as e:final_text = f"๋ณํ ์ค ์ค๋ฅ ๋ฐ์: {e}"# ๋ณ๊ฒฝ๋ ๋ถ๋ถ ํ๊ดํ ์ฒ๋ฆฌseq = difflib.SequenceMatcher(None, input_text, final_text)highlights =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():if tag == 'equal':highlights.append((final_text[j1:j2], None))elif tag in['replace', 'insert']:highlights.append((final_text[j1:j2], "๋ณ๊ฒฝ๋จ"))return final_text, highlights, char_count# 4. ์ค์๊ฐ ์ฌ์ ์ถ๊ฐ ๋ฐ JSON ์๋ณธ ๊ตฌ์กฐ ์ ์ง ์ ์ฅdef add_to_dict(word, pronun, current_input):if not word or not pronun:return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์ ๋ ฅํ์ธ์.", gr.update(), gr.update()# 1. 1:1 ๋งคํ ๋์ ๋๋ฆฌ ์ ๋ฐ์ดํธflat_dict[word] = pronun# 2. ์๋ณธ JSON ๊ตฌ์กฐ(items ๋ฐฐ์ด)์ ์ถ๊ฐ# ์ด๋ฏธ ์กด์ฌํ๋ ๋จ์ด์ธ์ง ํ์ธ ํ ๋ฎ์ด์ฐ๊ธฐ ๋๋ ์๋ก ์ถ๊ฐfound = Falsefor item in json_structure["items"]:if item[0] == word:4 unmodified linesjson_structure["items"].append([word, pronun])json_structure["meta"]["count"] = len(json_structure["items"])# 3. ํ์ผ๋ก ์ ์ฅwith open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)# 4. ๋ฑ๋ก ํ ํ์ฌ ํ ์คํธ ์ฆ์ ์ฌ๋ณํnew_final_text, new_highlights, _ = process_text(current_input)return f"โ '{word}' -> '{pronun}' ์ ์ฅ ์๋ฃ!", new_final_text, new_highlights# ๋ค์ด๋ก๋ ๋ฒํผ์ฉ ํจ์def get_dict_file():return dict_path if os.path.exists(dict_path) else None# 5. UI ํ๋ฉด ๊ตฌ์ฑwith gr.Blocks(theme="default", title="๋๋ง์ TTS ๋ณํ๊ธฐ") as iface:gr.Markdown("# ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (PRO ๋ฒ์ )")gr.Markdown("์๋ณธ ๋๋ณธ์ ์์ ํ์ง ์๊ณ ์๋ฆฌ ๋๋ ๋๋ก๋ง ๋ณํํฉ๋๋ค. ์ฐ์ธก ์๋จ ์์ด์ฝ์ ๋๋ฌ ๋ณํ๋ ํ ์คํธ๋ฅผ ๋ณต์ฌํ์ธ์.")with gr.Row():with gr.Column():input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ", placeholder="์ฌ๊ธฐ์ ์๋ณธ ๋๋ณธ์ ์ ๋ ฅํ์ธ์...")char_count_display = gr.Markdown("๊ธ์ ์: 0์")convert_btn = gr.Button("โจ ์๋ฆฌ๋๋ ๋๋ก ๋ณํํ๊ธฐ", variant="primary")with gr.Column():output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ ์คํธ (๋ณต์ฌ์ฉ)", show_copy_button=True)output_highlight = gr.HighlightedText(label="๐ ๋ณ๊ฒฝ์ ํ์ธ (๋ ธ๋์ ํ๊ดํ)", color_map={"๋ณ๊ฒฝ๋จ": "yellow"})input_text.change(fn=lambda x: f"์ ๋ ฅ ๊ธ์ ์: {len(x)}์ (๊ณต๋ฐฑ ํฌํจ)", inputs=input_text, outputs=char_count_display)convert_btn.click(fn=process_text, inputs=input_text, outputs=[output_text, output_highlight, char_count_display])gr.Markdown("---")gr.Markdown("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ (์๋ณธ JSON ๊ตฌ์กฐ ์ ์ง)")with gr.Row():new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด (์: ์ฑ์งํผํฐ)", scale=1)new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์ (์: ์ฑ์ฐํผํฐ)", scale=1)1 unmodified line234567891011121314152 unmodified lines181920212223246 unmodified lines313233343536372 unmodified lines4041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586874 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271 unmodified lineimport jsonimport osimport difflibimport tracebackfrom g2pk import G2p# 1. G2p ์์ง ์ด๊ธฐํg2p = G2p()# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌdict_path = "custom_dict.json"json_structure = {"items":[],"meta": {2 unmodified lines"count": 0}}flat_dict = {}def load_dict():global json_structure, flat_dict6 unmodified linesfor item in data["items"]:if len(item) == 2:flat_dict[item[0]] = item[1]else:flat_dict = datajson_structure["items"] = [[k, v] for k, v in data.items()]json_structure["meta"]["count"] = len(flat_dict)2 unmodified linesload_dict()# 3. ํ ์คํธ ๋ณํ ๋ฐ ํ์ด๋ผ์ดํธ (์๋ฌ ์ถ์ ๊ธฐ๋ฅ ์ถ๊ฐ)def process_text(input_text):try:if not input_text:return "",[], "์ ๋ ฅ ๊ธ์ ์: 0์"char_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์ (๊ณต๋ฐฑ ํฌํจ)"processed_text = input_text# 1์์: ์ฌ์ ๋จ์ด ์นํfor word, pronunciation in flat_dict.items():processed_text = processed_text.replace(word, pronunciation)# 2์์: G2P ์์ง ๋ณํtry:final_text = g2p(processed_text)except Exception as e:final_text = f"G2P ์์ง ๋ณํ ์คํจ: {e}"# ํ๊ดํ ํ์ด๋ผ์ดํธ ์ฒ๋ฆฌseq = difflib.SequenceMatcher(None, input_text, final_text)highlights =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():if tag == 'equal':highlights.append((final_text[j1:j2], None))elif tag in ['replace', 'insert']:highlights.append((final_text[j1:j2], "๋ณ๊ฒฝ๋จ"))return final_text, highlights, char_countexcept Exception as e:# ์๋ฌ๊ฐ ๋๋ฉด ๋ฉ์ถ์ง ์๊ณ ๋ณํ์ฐฝ์ ๋นจ๊ฐ ๊ธ์จ๋ก ์๋ฌ ์์ธ์ ๋์์ค๋๋ค.error_msg = f"์์คํ ์๋ฌ ๋ฐ์:\n{traceback.format_exc()}"return error_msg,[], "์๋ฌ ๋ฐ์"# 4. ์ค์๊ฐ ์ฌ์ ์ถ๊ฐ ๊ธฐ๋ฅdef add_to_dict(word, pronun, current_input):if not word or not pronun:return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์ ๋ ฅํ์ธ์.", gr.update(), gr.update()flat_dict[word] = pronunfound = Falsefor item in json_structure["items"]:if item[0] == word:4 unmodified linesjson_structure["items"].append([word, pronun])json_structure["meta"]["count"] = len(json_structure["items"])with open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)new_final_text, new_highlights, _ = process_text(current_input)return f"โ '{word}' -> '{pronun}' ์ ์ฅ ์๋ฃ!", new_final_text, new_highlightsdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# 5. UI ํ๋ฉด ๊ตฌ์ฑ (Markdown ์ถฉ๋ ๋ฌธ์ ๋ฅผ Textbox๋ก ๊ต์ฒดํ์ฌ ํด๊ฒฐ)with gr.Blocks(theme="default", title="๋๋ง์ TTS ๋ณํ๊ธฐ") as iface:gr.Markdown("# ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (PRO ๋ฒ์ )")with gr.Row():with gr.Column():input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ", placeholder="์ฌ๊ธฐ์ ์๋ณธ ๋๋ณธ์ ์ ๋ ฅํ์ธ์...")# ๋ฒ๊ทธ๊ฐ ๋๋ Markdown ๋์ ์์ ์ ์ธ Textbox๋ก ๊ต์ฒดchar_count_display = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="์ ๋ ฅ ๊ธ์ ์: 0์", interactive=False, lines=1)convert_btn = gr.Button("โจ ์๋ฆฌ๋๋ ๋๋ก ๋ณํํ๊ธฐ", variant="primary")with gr.Column():output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ ์คํธ (๋ณต์ฌ์ฉ)", show_copy_button=True)output_highlight = gr.HighlightedText(label="๐ ๋ณ๊ฒฝ์ ํ์ธ (๋ ธ๋์ ํ๊ดํ)", color_map={"๋ณ๊ฒฝ๋จ": "yellow"})# ์ค์๊ฐ ๊ธ์์ ์ ๋ฐ์ดํธ ์ฐ๊ฒฐinput_text.change(fn=lambda x: f"์ ๋ ฅ ๊ธ์ ์: {len(x)}์ (๊ณต๋ฐฑ ํฌํจ)", inputs=input_text, outputs=char_count_display)convert_btn.click(fn=process_text, inputs=input_text, outputs=[output_text, output_highlight, char_count_display])gr.Markdown("---")gr.Markdown("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ")with gr.Row():new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด (์: ์ฑ์งํผํฐ)", scale=1)new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์ (์: ์ฑ์ฐํผํฐ)", scale=1)
app.py+41-41