Spaces:
Running
Running
Update app.py
Browse files1 unmodified line2345678910111213141512 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737417 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331342 unmodified lines1371381391401411421431 unmodified lineimport jsonimport osimport difflibimport tracebackfrom g2pk import G2p# 1. λͺ¨λΈ λ‘λ© (μλ² μΌμ§ λ ν λ²λ§ νμ€νκ² λ‘λ©)print("G2P μμ§ λ‘λ© μ€...")g2p = G2p()print("G2P μμ§ λ‘λ© μλ£!")dict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"app_title": "VoiceScript AI", "app_version": "146.0", "count": 0}}flat_dict = {}12 unmodified lineselse:flat_dict = datajson_structure["items"] = [[k, v] for k, v in data.items()]json_structure["meta"]["count"] = len(flat_dict)except Exception as e:print(f"μ¬μ λ‘λ© μλ¬: {e}")load_dict()# 2. ν΅μ¬ λ³ν λ‘μ§def process_text(input_text):try:if not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed_text = input_textfor word, pronun in flat_dict.items():processed_text = processed_text.replace(word, pronun)final_text = g2p(processed_text)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: 8px; background-color: #f9f9f9;">{"".join(html_parts)}</div>'return final_text, html_output, char_countexcept Exception as e:return f"μλ¬: {e}", "<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ"# 3. μ΄κΈ°ν (μ¬μ μ μ μ§)def clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 4. μ¬μ μΆκ°def add_to_dict(word, pronun, current_input):if not word or not pronun:return "λ¨μ΄μ λ°μμ μ λ ₯νμΈμ.", current_input, ""17 unmodified linesreturn f"β '{word}' μ μ₯λ¨!", new_final, new_htmlreturn f"β '{word}' μ μ₯λ¨!", "", ""def get_dict_file():return dict_path if os.path.exists(dict_path) else None# ν΄λ¦½λ³΄λ 볡μ¬μ© JScopy_js = """function(text) {if (!text) { alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€."); return text; }navigator.clipboard.writeText(text);alert("λ³΅μ¬ μλ£!");return text;}"""# 5. UIwith gr.Blocks(theme="default") as iface:gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°")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():convert_btn = gr.Button("β¨ λ³ννκΈ°", variant="primary")copy_btn = gr.Button("π 볡μ¬νκΈ°", variant="secondary")clear_btn = gr.Button("ποΈ μ΄κΈ°ν", variant="stop")with gr.Column():output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ")output_html = gr.HTML(label="π λ³κ²½μ νμΈ")# λͺ¨λ ν΅μ μ μ€μ§ 'λ²νΌ'μ λλ₯Ό λλ§ μλ²λ‘ μ μ‘λλλ‘ λΆλ¦¬convert_btn.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count])copy_btn.click(fn=lambda x: x, inputs=[output_text], outputs=[output_text], js=copy_js)clear_btn.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count])gr.Markdown("---")with gr.Row():new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄")new_pronun = gr.Textbox(label="μνλ λ°μ")2 unmodified linesdict_status = gr.Markdown("")add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_html])dl_btn = gr.DownloadButton("π₯ JSON μ¬μ λ€μ΄λ‘λ")dl_btn.click(fn=get_dict_file, outputs=[dl_btn])if __name__ == "__main__":1 unmodified line2345678910111213141512 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747517 unmodified lines939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251262 unmodified lines1291301311321331341351 unmodified lineimport jsonimport osimport difflibfrom g2pk import G2p# 1. G2P λͺ¨λΈ λ‘λ©print("G2P λͺ¨λΈ λ‘λ© μ€...")g2p = G2p()print("G2P λͺ¨λΈ λ‘λ© μλ£!")# 2. μ¬μ νμΌ λ‘λ©dict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"app_title": "VoiceScript AI", "app_version": "146.0", "count": 0}}flat_dict = {}12 unmodified lineselse:flat_dict = datajson_structure["items"] = [[k, v] for k, v in data.items()]except Exception as e:print(f"μ¬μ λ‘λ© μλ¬: {e}")load_dict()# 3. λ³ν λ° κΈμ μ μΈκΈ° (λ²νΌ λλ₯Ό λλ§ μλ)def process_text(input_text):if not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"# κΈμ μ μΈ‘μ char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed_text = input_text# μ¬μ λ¨μ΄ λ³νfor word, pronun in flat_dict.items():processed_text = processed_text.replace(word, pronun)try:final_text = g2p(processed_text)except Exception as e:return f"λ³ν μλ¬: {e}", "<div style='color:red;'>μλ¬ λ°μ</div>", char_count# 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: 8px; background-color: #f9f9f9;">{"".join(html_parts)}</div>'return final_text, html_output, char_count# 4. μ΄κΈ°νdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 5. μ¬μ μ μΆκ°def add_to_dict(word, pronun, current_input):if not word or not pronun:return "λ¨μ΄μ λ°μμ μ λ ₯νμΈμ.", current_input, ""17 unmodified linesreturn f"β '{word}' μ μ₯λ¨!", new_final, new_htmlreturn f"β '{word}' μ μ₯λ¨!", "", ""# 6. νμΌ λ€μ΄λ‘λdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# UI ꡬμ±with gr.Blocks(theme="default") as iface:gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°")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():convert_btn = gr.Button("β¨ λ³ννκΈ°", variant="primary")clear_btn = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop")with gr.Column():# show_copy_button=True λ₯Ό ν΅ν΄ μμ νκ² κΈ°λ³Έ λ³΅μ¬ λ²νΌ μ 곡output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ (μ°μΈ‘ μλ¨ μμ΄μ½ ν΄λ¦νμ¬ λ³΅μ¬)", show_copy_button=True)output_html = gr.HTML(label="π λ³κ²½μ νμΈ")# λ²νΌ ν΄λ¦ μμλ§ ν¨μ μ€ν (μλ² λ€μ΄ λ°©μ§)convert_btn.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count])clear_btn.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count])gr.Markdown("---")gr.Markdown("### π οΈ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ λ° μ¬λ³ν (μ¬μ μ μ΄κΈ°νν΄λ μ§μμ§μ§ μμ΅λλ€)")with gr.Row():new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄")new_pronun = gr.Textbox(label="μνλ λ°μ")2 unmodified linesdict_status = gr.Markdown("")add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_html])dl_btn = gr.DownloadButton("π₯ JSON μ¬μ λ°±μ (PCλ‘ λ€μ΄λ‘λ)")dl_btn.click(fn=get_dict_file, outputs=[dl_btn])if __name__ == "__main__":
app.py+41-49