Spaces:
Running
Running
Update app.py
Browse files20 unmodified lines212223242526276 unmodified lines34353637383940413 unmodified lines454647484950515253545556575859606162636465666768697071723 unmodified lines7677787980818283848512 unmodified lines98991001011021031041051061 unmodified line1081091101111121131141151161171181192 unmodified lines1221231241251261271283 unmodified lines1321331341351361371381391401411421431441451461471482 unmodified lines15115215315415515615720 unmodified linesdict_path = "custom_dict.json"# μλ¬ λ°©μ§λ₯Ό μν΄ μμ μ λΉ μ¬μ νμΌ λ³΄μ₯if not os.path.exists(dict_path):with open(dict_path, "w", encoding="utf-8") as f:json.dump({"items":[], "meta": {"count": 0}}, f)6 unmodified linesfor item in data.get("items",[]):if len(item) == 2:flat_dict[item[0]] = item[1]except Exception as e:print(f"μ¬μ λ‘λ© μλ¬: {e}")return flat_dictdef process_text(input_text):3 unmodified lineschar_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ"processed = input_text# 1. μ¬μ©μ μ§μ μ¬μ μΉνflat_dict = load_dict()for word, pronun in flat_dict.items():processed = processed.replace(word, pronun)final_text = processed# 2. G2P μλ λ°μ λ³ν μ μ©if g2p is not None:try:final_text = g2p(processed)except Exception as e:print(f"G2P μλ¬: {e}")# 3. νκ΄ν ν¨κ³Ό (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; padding: 2px; border-radius: 3px;">{chunk}</span>')html_output = f'<div style="line-height:1.6; font-size:16px; padding:10px; border:1px solid #ddd; border-radius:5px; background-color:#f9f9f9;">{"".join(html_parts)}</div>'3 unmodified linesdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"def add_dict(word, pronun):if not word or not pronun:return "β οΈ λ¨μ΄/λ¬Έμ₯κ³Ό λ°μμ λͺ¨λ μ λ ₯νμΈμ."try:with open(dict_path, "r", encoding="utf-8") as f:j_struct = json.load(f)12 unmodified linestry:with open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)return f"β '{word}' -> '{pronun}' μ μ₯ μλ£!"except Exception as e:return f"β μ μ₯ μ€ν¨: {e}"def get_file():return dict_path if os.path.exists(dict_path) else None1 unmodified linedef upload_file(file_path):if file_path is not None:shutil.copy(file_path, dict_path)return "β 볡ꡬ μλ£! μ¬μ μ μ μμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€."return "β οΈ νμΌμ΄ μμ΅λλ€."# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default") as iface:gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (μ€λ₯ μλ²½ ν΄κ²°λ³Έ)")with gr.Tabs():with gr.Tab("ποΈ λ©μΈ νλ©΄"):2 unmodified linesinput_text = gr.Textbox(lines=8, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯")char_count = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False)# μμ²νμ 3κ°μ λ²νΌ λλν λ°°μΉwith gr.Row():btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary")btn_copy = gr.Button("π λ³ν볡μ¬", variant="secondary")3 unmodified linesoutput_text = gr.Textbox(lines=8, label="2οΈβ£ λ³νλ ν μ€νΈ", show_copy_button=True)output_html = gr.HTML(label="π λ³κ²½μ νμΈ")# λμ μ°κ²° (api_name=False λ‘ μλ¬ μμ² μ°¨λ¨)btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count], api_name=False)btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], api_name=False)# μμ νκ² λ³΅κ΅¬λ λ³νλ³΅μ¬ λ²νΌ λ‘μ§btn_copy.click(fn=None,inputs=[output_text],outputs=None,js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('β λ³΅μ¬ μλ£! TTSμ λΆμ¬λ£μΌμΈμ.'); } else { alert('볡μ¬ν λ΄μ©μ΄ μμ΅λλ€.'); } }",api_name=False)gr.Markdown("---")2 unmodified linesnp = 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], api_name=False)with gr.Tab("π₯ λ°±μ /볡ꡬ"):dl_btn = gr.DownloadButton("π₯ 1. νμ¬ μ¬μ λ°±μ νκΈ° (PC λ€μ΄λ‘λ)")20 unmodified lines2122232425266 unmodified lines33343536373839403 unmodified lines44454647484950515253545556575859606162636465666768697071727374757677783 unmodified lines828384858687888990919212 unmodified lines1051061071081091101111121131141151161171181191201 unmodified line1221231241251261271281291301311321332 unmodified lines1361371381391401413 unmodified lines1451461471481491501511521531541552 unmodified lines15815916016116216316416516616716816917017120 unmodified linesdict_path = "custom_dict.json"if not os.path.exists(dict_path):with open(dict_path, "w", encoding="utf-8") as f:json.dump({"items":[], "meta": {"count": 0}}, f)6 unmodified linesfor item in data.get("items",[]):if len(item) == 2:flat_dict[item[0]] = item[1]except:passreturn flat_dictdef process_text(input_text):3 unmodified lineschar_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ"processed = input_textflat_dict = load_dict()placeholders = {}# λ΄ λ¨μ΄ 보νΈfor i, (word, pronun) in enumerate(flat_dict.items()):ph = f"__PH_{i}__"placeholders[ph] = pronunprocessed = processed.replace(word, ph)if g2p is not None:try:processed = g2p(processed)except Exception as e:pass# λ΄ λ¨μ΄ 볡ꡬfor ph, pronun in placeholders.items():processed = processed.replace(ph, pronun)final_text = processed# νκ΄ν (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; padding: 2px; border-radius: 3px;">{chunk}</span>')html_output = f'<div style="line-height:1.6; font-size:16px; padding:10px; border:1px solid #ddd; border-radius:5px; background-color:#f9f9f9;">{"".join(html_parts)}</div>'3 unmodified linesdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"def add_and_update(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update(), gr.update()# μ¬μ μ μ₯try:with open(dict_path, "r", encoding="utf-8") as f:j_struct = json.load(f)12 unmodified linestry:with open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)status_msg = f"β '{word}' -> '{pronun}' μ μ₯ λ° μ¦μ λ°μ μλ£!"except Exception as e:status_msg = f"β μ μ₯ μ€ν¨: {e}"# μ¦μ νλ©΄ κ°±μif current_input and current_input.strip():final_text, html_output, char_count = process_text(current_input)return status_msg, final_text, html_output, char_countelse:return status_msg, "", "", "μ λ ₯ κΈμ μ: 0μ"def get_file():return dict_path if os.path.exists(dict_path) else None1 unmodified linedef upload_file(file_path):if file_path is not None:shutil.copy(file_path, dict_path)return "β λ°±μ 볡ꡬ μλ£!"return "β οΈ νμΌμ΄ μμ΅λλ€."# UI νλ©΄with gr.Blocks(theme="default") as iface:gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°")with gr.Tabs():with gr.Tab("ποΈ λ©μΈ νλ©΄"):2 unmodified linesinput_text = gr.Textbox(lines=8, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯")char_count = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False)with gr.Row():btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary")btn_copy = gr.Button("π λ³ν볡μ¬", variant="secondary")3 unmodified linesoutput_text = gr.Textbox(lines=8, 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], api_name=False)btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], api_name=False)btn_copy.click(fn=None, inputs=[output_text], outputs=None,js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('β λ³΅μ¬ μλ£!'); } }", api_name=False)gr.Markdown("---")2 unmodified linesnp = gr.Textbox(label="μνλ λ°μ")btn_add = gr.Button("πΎ μ¬μ μ μΆκ°", variant="primary")status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False)# μ¬μ μΆκ°μ λμμ λ³ν κ²°κ³Ό μ¦μ μ λ°μ΄νΈbtn_add.click(fn=add_and_update,inputs=[nw, np, input_text],outputs=[status, output_text, output_html, char_count],api_name=False)with gr.Tab("π₯ λ°±μ /볡ꡬ"):dl_btn = gr.DownloadButton("π₯ 1. νμ¬ μ¬μ λ°±μ νκΈ° (PC λ€μ΄λ‘λ)")
app.py+44-30