Spaces:
Running
Running
Update app.py
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243441 unmodified line464748495051525354555657585960616263646566676869707172737475764 unmodified lines818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591603 unmodified linesimport difflibimport shutil# 무κ²κ³ μλ¬λ₯Ό μ λ°νλ G2P μμ§μ μμ ν μ κ±°νκ³ , μμ νμ΄μ¬ λ‘μ§λ§ μ¬μ©ν©λλ€.dict_path = "custom_dict.json"# 1. μ¬μ λ‘λ©def load_dict():flat_dict = {}if os.path.exists(dict_path):try:with open(dict_path, "r", encoding="utf-8") as f:data = json.load(f)if "items" in data:for item in data["items"]:if len(item) == 2:# λμ΄μ°κΈ°κ° μλ κΈ΄ λ¬Έμ₯λ μ νν μΈμνλλ‘ κ³΅λ°± 보쑴flat_dict[item[0]] = item[1]except:passreturn flat_dict# 2. ν μ€νΈ λ³ν λ° νκ΄ν μ²λ¦¬ (μ¬μ κΈ°λ° 100%)def process_text(input_text):if not input_text or not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed = input_textflat_dict = load_dict()# λ±λ‘λ λ¨μ΄/λ¬Έμ₯μ μ°Ύμ μΉν (μμ ν μμ νμ΄μ¬ λ‘μ§)for word, pronun in flat_dict.items():processed = processed.replace(word, pronun)final_text = processed# νκ΄ν ν¨κ³Ό (λ³κ²½λ λΆλΆλ§ λ Έλμ μ²λ¦¬)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; 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:8px; background-color:#f9f9f9;">{"".join(html_parts)}</div>'return final_text, html_output, char_count# 3. μ΄κΈ°νdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 4. λ¨μ΄/λ¬Έμ₯ μ¬μ μΆκ°def add_dict(word, pronun):if not word or not pronun:return "β οΈ κΈ°μ‘΄ λ¨μ΄(λ¬Έμ₯)μ μνλ λ°μμ λͺ¨λ μ λ ₯νμΈμ."j_struct = {"items":[], "meta": {"count": 0}}if os.path.exists(dict_path):try:with open(dict_path, "r", encoding="utf-8") as f:j_struct = json.load(f)except:passfound = Falsefor item in j_struct["items"]:if item[0] == word:item[1] = pronunfound = True4 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}"# 5. λ°±μ λ° λ³΅κ΅¬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)return "β λ°±μ νμΌ λ³΅κ΅¬ μλ£! μ΄μ μ μ₯νλ μ¬μ μ΄ μ μ μ μ©λ©λλ€."return "β οΈ νμΌμ΄ μμ΅λλ€."# 6. μμ ν 볡μ¬μ© μλ°μ€ν¬λ¦½νΈcopy_js = """(text) => {if (!text) {alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€.");return text;}navigator.clipboard.writeText(text);alert("β οΏ½οΏ½οΏ½μ€νΈκ° ν΄λ¦½λ³΄λμ 볡μ¬λμμ΅λλ€. TTS νλ‘κ·Έλ¨μ λΆμ¬λ£μΌμΈμ!");return text;}"""# 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=8, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯", placeholder="μ΄κ³³μ λλ³Έμ λΆμ¬λ£μΌμΈμ.")char_count = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False)with gr.Row():btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary")btn_copy = gr.Button("π λ³ν볡μ¬", variant="secondary")btn_clr = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop")with gr.Column():output_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])btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count])btn_copy.click(fn=lambda x: x, inputs=[output_text], outputs=[output_text], js=copy_js)gr.Markdown("---")gr.Markdown("### π οΈ μ¬μ μ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ (λ¬Έμ₯ λ¨μ λμ΄μ°κΈ° λ±λ‘ κ°λ₯)")with gr.Row():nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ λλ λ¬Έμ₯", scale=1)np = gr.Textbox(label="μνλ λ°μ", scale=1)btn_add = gr.Button("πΎ μ¬μ μ μΆκ°", variant="primary", scale=1)status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False)btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status])# --- λ λ²μ§Έ ν: λ°±μ λ° λ³΅κ΅¬ ---with gr.Tab("π₯ λ°±μ /볡ꡬ (μλ² μ΄κΈ°ν λλΉ)"):gr.Markdown("π‘ **μλ΄:** λ¬΄λ£ μλ² νΉμ±μ μΌμ μκ° μ μμ΄ μμΌλ©΄ 리μ€ννΈ λλ©° μΆκ°ν΄λ λ°μ΄ν°κ° λ μκ°λλ€. μ¬μ μ λ§μ΄ μΆκ°ν λ μλ κΌ μλ λ²νΌμΌλ‘ PCμ λ°±μ ν΄λμΈμ!")gr.Markdown("### 1. νμ¬ μμ±λ μ¬μ λ°±μ νκΈ° (PCλ‘ λ€μ΄λ‘λ)")dl_btn = gr.DownloadButton("π₯ μ΅μ custom_dict.json λ°±μ νμΌ λ€μ΄λ‘λ")dl_btn.click(fn=get_file, outputs=[dl_btn])gr.Markdown("---")gr.Markdown("### 2. λ°±μ νμΌ λ³΅κ΅¬νκΈ° (μ λ‘λ)")gr.Markdown("μλ²κ° 리μ€ννΈ λμ΄ μ¬μ μ΄ μ΄κΈ°νλμλ€λ©΄, λ€μ΄λ‘λ ν΄λμλ `custom_dict.json` νμΌμ μ΄κ³³μ λλκ·Έ μ€ λλ‘ νμΈμ.")up_file = gr.File(label="λ°±μ νμΌ μ λ‘λ")up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat])3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041421 unmodified line4445464748495051525354555657585960616263646566676869704 unmodified lines757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291303 unmodified linesimport difflibimport shutildict_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)def load_dict():flat_dict = {}try:with open(dict_path, "r", encoding="utf-8") as f:data = json.load(f)for 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):if not input_text or not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ"processed = input_textflat_dict = load_dict()for word, pronun in flat_dict.items():processed = processed.replace(word, pronun)final_text = processed# νκ΄ν ν¨κ³Ό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; padding: 2px;">{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_countdef 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)except:j_struct = {"items":[], "meta": {"count": 0}}found = Falsefor item in j_struct.get("items",[]):if item[0] == word:item[1] = pronunfound = True4 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 Nonedef upload_file(file_path):if file_path is not None:shutil.copy(file_path, dict_path)return "β 볡ꡬ μλ£! μ¬μ μ μ μμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€."return "β οΈ νμΌμ΄ μμ΅λλ€."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():# μ°μΈ‘ μλ¨ λ³΅μ¬ λ²νΌ νμ±ν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("---")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("π₯ λ°±μ /볡ꡬ"):dl_btn = gr.DownloadButton("π₯ 1. νμ¬ μ¬μ λ°±μ νκΈ° (PC λ€μ΄λ‘λ)")dl_btn.click(fn=get_file, outputs=[dl_btn])gr.Markdown("---")# type="filepath" λ₯Ό λͺ μνμ¬ Gradio 5.x μλ¬ μ°¨λ¨up_file = gr.File(label="π€ 2. λ°±μ νμΌ λ³΅κ΅¬νκΈ° (μ λ‘λ)", type="filepath")up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False)up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat])
app.py+42-72