Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines3456789101112131415161718192021222324252627282930313212 unmodified lines454647484950515253545556575859606162636465666768697071727374758 unmodified lines848586878889909192939495969798991005 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711722 unmodified linesimport osimport difflibimport traceback# G2P μμ§μ μ²μλΆν° λΆλ₯΄μ§ μκ³ , νμν λ λΆλ₯΄λλ‘ μ§μ° λ‘λ©(Lazy Loading)g2p_engine = Nonedef get_g2p():global g2p_engineif g2p_engine is None:try:from g2pk import G2pg2p_engine = G2p()except Exception as e:print(f"G2P λ‘λ© μλ¬: {e}")return g2p_engine# μ¬μ νμΌ κ΄λ¦¬dict_path = "custom_dict.json"json_structure = {"items":[],"meta": {"app_title": "VoiceScript AI","app_version": "146.0","count": 0}}flat_dict = {}def load_dict():12 unmodified linesjson_structure["items"] = [[k, v] for k, v in data.items()]json_structure["meta"]["count"] = len(flat_dict)except Exception as e:print(f"β οΈ JSON μ½κΈ° μλ¬: {e}")load_dict()# ν μ€νΈ λ³νdef process_text(input_text):try: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 μμ§ λ³ν (μ¬κΈ°μ μ΅μ΄ 1ν λ‘λ©λ¨)engine = get_g2p()if engine:final_text = engine(processed_text)else:final_text = processed_text + "\n\n[μλ¬: G2P μμ§μ λΆλ¬μ€μ§ λͺ»νμ΅λλ€.]"# νκ΄ν HTML μ²λ¦¬seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():8 unmodified linesreturn final_text, html_output, char_countexcept Exception as e:error_msg = f"μμ€ν μλ¬ λ°μ:\n{traceback.format_exc()}"return error_msg, f"<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ"# μ΄κΈ°ν ν¨μdef clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# μ€μκ° μ¬μ μΆκ°def add_to_dict(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update()flat_dict[word] = pronun5 unmodified linesbreakif not found:json_structure["items"].append([word, pronun])json_structure["meta"]["count"] = len(json_structure["items"])with open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)new_final_text, new_html, _ = process_text(current_input)return f"β '{word}' -> '{pronun}' μ μ₯ μλ£!", new_final_text, new_html# ν΄λ¦½λ³΄λ λ³΅μ¬ μλ°μ€ν¬λ¦½νΈcopy_js = """function(text) {if (!text) {alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€!");return text;}navigator.clipboard.writeText(text);alert("β λ³νλ ν μ€νΈκ° 볡μ¬λμμ΅λλ€! TTSμ λΆμ¬λ£μΌμΈμ.");return text;}"""def get_dict_file():return dict_path if os.path.exists(dict_path) else None# UI νλ©΄ ꡬμ±with gr.Blocks(theme="default", title="λλ§μ TTS λ³νκΈ°") as iface:gr.Markdown("# ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ° (PRO λ²μ )")with gr.Row():with gr.Column():input_text = gr.Textbox(lines=7, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯", placeholder="μ¬κΈ°μ μλ³Έ λλ³Έμ μ λ ₯νμΈμ...")char_count_display = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False, lines=1)with gr.Row():convert_btn = gr.Button("β¨ μ리λλ λλ‘ λ³ννκΈ°", variant="primary")copy_btn = gr.Button("π λ³νλ³΅μ¬ (TTSμ©)", variant="secondary")clear_btn = gr.Button("ποΈ μ΄κΈ°ν", variant="stop")with gr.Column():output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ")output_html = gr.HTML(label="π λ³κ²½μ νμΈ (λ Έλμ νκ΄ν)")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_html, char_count_display])copy_btn.click(fn=None, inputs=[output_text], outputs=[output_text], js=copy_js)clear_btn.click(fn=clear_all, inputs=[], outputs=[input_text, output_text, output_html, char_count_display])gr.Markdown("---")gr.Markdown("### π οΈ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ λ° μ¬λ³ν")with gr.Row():new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ (μ: μ±μ§νΌν°)")new_pronun = gr.Textbox(label="μνλ λ°μ (μ: μ±μ°νΌν°)")add_btn = gr.Button("πΎ μ¬μ μ μΆκ°νκ³ μ¦μ μ¬λ³ν", variant="primary")dict_status = gr.Markdown("")add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_html])gr.Markdown("---")download_btn = gr.DownloadButton("π₯ νμ¬ JSON μ¬μ μμ νκ² PCλ‘ λ€μ΄λ‘λ")download_btn.click(fn=get_dict_file, outputs=download_btn)if __name__ == "__main__":iface.launch()2 unmodified lines3456789101112131415161712 unmodified lines3031323334353637383940414243444546474849505152538 unmodified lines626364656667686970717273747576775 unmodified lines83848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431442 unmodified linesimport 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 = {}def load_dict():12 unmodified linesjson_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():8 unmodified linesreturn 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, ""flat_dict[word] = pronun5 unmodified linesbreakif not found:json_structure["items"].append([word, pronun])with open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)if current_input.strip():new_final, new_html, _ = process_text(current_input)return 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="μνλ λ°μ")add_btn = gr.Button("πΎ μ¬μ μ μΆκ°")dict_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__":iface.launch()
app.py+46-74