Spaces:
Running
Running
Update app.py
Browse files1 unmodified line234567891010111212131314152 unmodified lines18192022212223246 unmodified lines31323335343536372 unmodified lines40414244434446474849505152535455564558596061626364656667686946474849505152535455565758596061626364656667686970717172737475767773787980818278838481828586874 unmodified lines929394939596979798991001011011021031051041051061081071081091101131111121131141151161171181191201211221231241241251261271 unmodified lineimport jsonimport osimport difflibimport tracebackfrom g2pk import G2p
# 1. G2p ์์ง ์ด๊ธฐํg2p = G2p()
# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌ ๋ฐ ๊ตฌ์กฐ ์ ์ง# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌdict_path = "custom_dict.json"
# ์๋ณธ JSON์ ๋ผ๋๋ฅผ ์ ์งํ๊ธฐ ์ํ ๋ณ์json_structure = { "items":[], "meta": {2 unmodified lines "count": 0 }}flat_dict = {} # ์ค์ ๋ณํ์ ์ฌ์ฉํ 1:1 ๋งคํ ๋์
๋๋ฆฌflat_dict = {}
def load_dict(): global json_structure, flat_dict6 unmodified lines for item in data["items"]: if len(item) == 2: flat_dict[item[0]] = item[1] else: # ์์ ๋ฐฉ์์ ๋จ์ ๋์
๋๋ฆฌ์ผ ๊ฒฝ์ฐ ํธํ else: flat_dict = data json_structure["items"] = [[k, v] for k, v in data.items()] json_structure["meta"]["count"] = len(flat_dict)2 unmodified lines
load_dict()
# 3. ํ
์คํธ ๋ณํ ๋ฐ ํ์ด๋ผ์ดํธ ํจ์# 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], "๋ณ๊ฒฝ๋จ")) 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_count return final_text, highlights, char_count except Exception as e: # ์๋ฌ๊ฐ ๋๋ฉด ๋ฉ์ถ์ง ์๊ณ ๋ณํ์ฐฝ์ ๋นจ๊ฐ ๊ธ์จ๋ก ์๋ฌ ์์ธ์ ๋์์ค๋๋ค. error_msg = f"์์คํ
์๋ฌ ๋ฐ์:\n{traceback.format_exc()}" return error_msg,[], "์๋ฌ ๋ฐ์"
# 4. ์ค์๊ฐ ์ฌ์ ์ถ๊ฐ ๋ฐ JSON ์๋ณธ ๊ตฌ์กฐ ์ ์ง ์ ์ฅ# 4. ์ค์๊ฐ ์ฌ์ ์ถ๊ฐ ๊ธฐ๋ฅ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 = False for item in json_structure["items"]: if item[0] == word:4 unmodified lines json_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 ํ๋ฉด ๊ตฌ์ฑ# 5. UI ํ๋ฉด ๊ตฌ์ฑ (Markdown ์ถฉ๋ ๋ฌธ์ ๋ฅผ Textbox๋ก ๊ต์ฒดํ์ฌ ํด๊ฒฐ)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์") # ๋ฒ๊ทธ๊ฐ ๋๋ 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("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ (์๋ณธ JSON ๊ตฌ์กฐ ์ ์ง)") gr.Markdown("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ") with gr.Row(): new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด (์: ์ฑ์งํผํฐ)", scale=1) new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์ (์: ์ฑ์ฐํผํฐ)", scale=1)app.py+41-41