Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567788910111210111213141516171819201322232425262728291415161712 unmodified lines303132483334353652373839554041424344456162634647486566676849697050725152538 unmodified lines6263648788656690676869709471727397747576775 unmodified lines83848510911086878889901141159192939495969711798991001201211221231011021251031041051061071301311321331341351081091101111121131391401141151161171431441181191201461211221231491241251511521531541551261271281291301311581321601611621331341351361371381661671681691391401411421431442 unmodified linesimport osimport difflibimport tracebackfrom g2pk import G2p
# G2P μμ§μ μ²μλΆν° λΆλ₯΄μ§ μκ³ , νμν λ λΆλ₯΄λλ‘ μ§μ° λ‘λ©(Lazy Loading)g2p_engine = None# 1. λͺ¨λΈ λ‘λ© (μλ² μΌμ§ λ ν λ²λ§ νμ€νκ² λ‘λ©)print("G2P μμ§ λ‘λ© μ€...")g2p = G2p()print("G2P μμ§ λ‘λ© μλ£!")
def get_g2p(): global g2p_engine if g2p_engine is None: try: from g2pk import G2p g2p_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 }}json_structure = {"items":[], "meta": {"app_title": "VoiceScript AI", "app_version": "146.0", "count": 0}}flat_dict = {}
def load_dict():12 unmodified lines json_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}") print(f"μ¬μ λ‘λ© μλ¬: {e}")
load_dict()
# ν
μ€νΈ λ³ν# 2. ν΅μ¬ λ³ν λ‘μ§def process_text(input_text): try: if not input_text: if not input_text.strip(): 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) for word, pronun in flat_dict.items(): processed_text = processed_text.replace(word, pronun) # 2μμ: G2P μμ§ λ³ν (μ¬κΈ°μ μ΅μ΄ 1ν λ‘λ©λ¨) engine = get_g2p() if engine: final_text = engine(processed_text) final_text = g2p(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 lines return final_text, html_output, char_count except Exception as e: error_msg = f"μμ€ν
μλ¬ λ°μ:\n{traceback.format_exc()}" return error_msg, f"<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ" 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 "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ
λ ₯νμΈμ.", gr.update(), gr.update() return "λ¨μ΄μ λ°μμ μ
λ ₯νμΈμ.", current_input, "" flat_dict[word] = pronun 5 unmodified lines break if 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) if current_input.strip(): new_final_text, new_html, _ = process_text(current_input) return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£!", new_final_text, new_html new_final, new_html, _ = process_text(current_input) return f"β
'{word}' μ μ₯λ¨!", new_final, new_html return 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; } if (!text) { alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€."); return text; } navigator.clipboard.writeText(text); alert("β
λ³νλ ν
μ€νΈκ° 볡μ¬λμμ΅λλ€! TTSμ λΆμ¬λ£μΌμΈμ."); alert("λ³΅μ¬ μλ£!"); 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 λ²μ )")# 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οΈβ£ μλ³Έ ν
μ€νΈ μ
λ ₯", placeholder="μ¬κΈ°μ μλ³Έ λλ³Έμ μ
λ ₯νμΈμ...") char_count_display = gr.Textbox(label="κΈμ μ νμΈ", value="μ
λ ₯ κΈμ μ: 0μ", interactive=False, lines=1) 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("π λ³νλ³΅μ¬ (TTSμ©)", variant="secondary") 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="π λ³κ²½μ νμΈ (λ
Έλμ νκ΄ν)") 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]) # λͺ¨λ ν΅μ μ μ€μ§ 'λ²νΌ'μ λλ₯Ό λλ§ μλ²λ‘ μ μ‘λλλ‘ λΆλ¦¬ 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("---") gr.Markdown("### π οΈ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ λ° μ¬λ³ν") with gr.Row(): new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ (μ: μ±μ§νΌν°)") new_pronun = gr.Textbox(label="μνλ λ°μ (μ: μ±μ°νΌν°)") add_btn = gr.Button("πΎ μ¬μ μ μΆκ°νκ³ μ¦μ μ¬λ³ν", variant="primary") 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])
gr.Markdown("---") download_btn = gr.DownloadButton("π₯ νμ¬ JSON μ¬μ μμ νκ² PCλ‘ λ€μ΄λ‘λ") download_btn.click(fn=get_dict_file, outputs=download_btn) dl_btn = gr.DownloadButton("π₯ JSON μ¬μ λ€μ΄λ‘λ") dl_btn.click(fn=get_dict_file, outputs=[dl_btn])
if __name__ == "__main__": iface.launch()app.py+46-74