Spaces:
Running
Running
Update app.py
Browse files7 unmodified lines89101112131425 unmodified lines40414243444546474849508 unmodified lines596061626364656667686970717273747576777879808113 unmodified lines95969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331347 unmodified lines# 1. G2p μμ§ μ΄κΈ°νg2p = G2p()# 2. μ¬μ νμΌ κ΄λ¦¬dict_path = "custom_dict.json"json_structure = {"items":[],25 unmodified linesload_dict()# 3. ν μ€νΈ λ³ν λ° νμ΄λΌμ΄νΈ (μλ¬ μΆμ κΈ°λ₯ μΆκ°)def process_text(input_text):try:if not input_text:return "",[], "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed_text = input_text8 unmodified linesexcept Exception as e:final_text = f"G2P μμ§ λ³ν μ€ν¨: {e}"# νκ΄ν νμ΄λΌμ΄νΈ μ²λ¦¬seq = difflib.SequenceMatcher(None, input_text, final_text)highlights =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():if tag == 'equal':highlights.append((final_text[j1:j2], None))elif tag in ['replace', 'insert']:highlights.append((final_text[j1:j2], "λ³κ²½λ¨"))return final_text, highlights, char_countexcept Exception as e:# μλ¬κ° λλ©΄ λ©μΆμ§ μκ³ λ³νμ°½μ λΉ¨κ° κΈμ¨λ‘ μλ¬ μμΈμ λμμ€λλ€.error_msg = f"μμ€ν μλ¬ λ°μ:\n{traceback.format_exc()}"return error_msg,[], "μλ¬ λ°μ"# 4. μ€μκ° μ¬μ μΆκ° κΈ°λ₯def add_to_dict(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update()13 unmodified lineswith open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)new_final_text, new_highlights, _ = process_text(current_input)return f"β '{word}' -> '{pronun}' μ μ₯ μλ£!", new_final_text, new_highlightsdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# 5. UI νλ©΄ κ΅¬μ± (Markdown μΆ©λ λ¬Έμ λ₯Ό Textboxλ‘ κ΅μ²΄νμ¬ ν΄κ²°)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="μ¬κΈ°μ μλ³Έ λλ³Έμ μ λ ₯νμΈμ...")# λ²κ·Έκ° λλ Markdown λμ μμ μ μΈ Textboxλ‘ κ΅μ²΄char_count_display = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False, lines=1)convert_btn = gr.Button("β¨ μ리λλ λλ‘ λ³ννκΈ°", variant="primary")with gr.Column():output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ (볡μ¬μ©)", show_copy_button=True)output_highlight = gr.HighlightedText(label="π λ³κ²½μ νμΈ (λ Έλμ νκ΄ν)", color_map={"λ³κ²½λ¨": "yellow"})# μ€μκ° κΈμμ μ λ°μ΄νΈ μ°κ²°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_highlight, char_count_display])gr.Markdown("---")gr.Markdown("### π οΈ λ¨μ΄/λ¬Έμ₯ μ¦μ λ±λ‘ λ° μ¬λ³ν")with gr.Row():new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ (μ: μ±μ§νΌν°)", scale=1)new_pronun = gr.Textbox(label="μνλ λ°μ (μ: μ±μ°νΌν°)", scale=1)add_btn = gr.Button("πΎ μ¬μ μ μΆκ°νκ³ μ¦μ μ¬λ³ν", variant="secondary", scale=1)dict_status = gr.Markdown("")add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_highlight])gr.Markdown("---")download_btn = gr.DownloadButton("π₯ νμ¬ JSON μ¬μ μμ νκ² PCλ‘ λ€μ΄λ‘λ")7 unmodified lines89101112131425 unmodified lines40414243444546474849508 unmodified lines596061626364656667686970717273747576777879808182838485868713 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621637 unmodified lines# 1. G2p μμ§ μ΄κΈ°νg2p = G2p()# 2. μ¬μ νμΌ κ΄λ¦¬ (μ΄κΈ°νν΄λ μ¬κΈ°λ μ λ μ§μμ§μ§ μμ)dict_path = "custom_dict.json"json_structure = {"items":[],25 unmodified linesload_dict()# 3. ν μ€νΈ λ³ν λ° μμ μ μΈ HTML νκ΄ν μ²λ¦¬def process_text(input_text):try:if not input_text:return "", "", "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed_text = input_text8 unmodified linesexcept Exception as e:final_text = f"G2P μμ§ λ³ν μ€ν¨: {e}"# νκ΄νμ HTMLλ‘ μμ νκ² μ²λ¦¬ (Gradio λ²κ·Έ μμ² μ°¨λ¨)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:error_msg = f"μμ€ν μλ¬ λ°μ:\n{traceback.format_exc()}"return error_msg, f"<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ"# 4. μ΄κΈ°ν ν¨μ (ν μ€νΈμ°½λ§ λΉμ°κ³ μ¬μ μ 건λλ¦¬μ§ μμ)def clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 5. μ€μκ° μ¬μ μΆκ° κΈ°λ₯def add_to_dict(word, pronun, current_input):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯νμΈμ.", gr.update(), gr.update()13 unmodified lineswith 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# 6. 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)# 3κ°μ λ²νΌμ λλν λ°°μΉ (λΉμ¨ λμΌ)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λ‘ λ€μ΄λ‘λ")
app.py+53-24