Spaces:
Running
Running
Update app.py
Browse files20 unmodified lines212223242425266 unmodified lines333435373836373839403 unmodified lines4445464847505152534849555051525354555657585859606061626364656667626869707172737469757677783 unmodified lines8283847985868187888990919212 unmodified lines1051061071011081091031101111121131141151161171181191201 unmodified line1221231241111251261271141281291161301311321332 unmodified lines1361371381251391401413 unmodified lines1451461471351481491381391501411421431441451511521531541552 unmodified lines15815916015416116216316416516616716816917017120 unmodified lines
dict_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 lines for item in data.get("items",[]): if len(item) == 2: flat_dict[item[0]] = item[1] except Exception as e: print(f"μ¬μ λ‘λ© μλ¬: {e}") except: pass return flat_dict
def process_text(input_text):3 unmodified lines char_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 placeholders = {} # 2. G2P μλ λ°μ λ³ν μ μ© # λ΄ λ¨μ΄ λ³΄νΈ for i, (word, pronun) in enumerate(flat_dict.items()): ph = f"__PH_{i}__" placeholders[ph] = pronun processed = processed.replace(word, ph) if g2p is not None: try: final_text = g2p(processed) processed = g2p(processed) except Exception as e: print(f"G2P μλ¬: {e}") pass
# λ΄ λ¨μ΄ 볡ꡬ for ph, pronun in placeholders.items(): processed = processed.replace(ph, pronun) final_text = processed
# 3. νκ΄ν ν¨κ³Ό (HTML) # νκ΄ν (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']: 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):def add_and_update(word, pronun, current_input): if not word or not pronun: return "β οΈ λ¨μ΄/λ¬Έμ₯κ³Ό λ°μμ λͺ¨λ μ
λ ₯νμΈμ." 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 lines try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(j_struct, f, ensure_ascii=False, indent=2) return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£!" status_msg = f"β
'{word}' -> '{pronun}' μ μ₯ λ° μ¦μ λ°μ μλ£!" except Exception as e: return f"β μ μ₯ μ€ν¨: {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_count else: 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 "β
λ°±μ
볡ꡬ μλ£!" return "β οΈ νμΌμ΄ μμ΅λλ€."
# UI νλ©΄ ꡬμ±# UI νλ©΄with gr.Blocks(theme="default") as iface: gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (μ€λ₯ μλ²½ ν΄κ²°λ³Έ)") gr.Markdown("## ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°") with gr.Tabs(): with gr.Tab("ποΈ λ©μΈ νλ©΄"):2 unmodified lines input_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 lines output_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 fn=None, inputs=[output_text], outputs=None, js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('β
λ³΅μ¬ μλ£!'); } }", api_name=False ) gr.Markdown("---")2 unmodified lines 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], api_name=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