Spaces:
Running
Running
Update app.py
Browse files1 unmodified line234556897891110111213141512 unmodified lines28293031313233343537363739404142434438394041424344454647484647484950515253515253545556575859606162545556575859606162636465646566676768697071717273747517 unmodified lines9394959697989998991001011021031041051061071081001011021031041051061151071081091101191201111121131141231151161171261181191281201211221231241251262 unmodified lines1291301311401321331341351 unmodified lineimport jsonimport osimport difflibimport tracebackfrom g2pk import G2p
# 1. λͺ¨λΈ λ‘λ© (μλ² μΌμ§ λ ν λ²λ§ νμ€νκ² λ‘λ©)print("G2P μμ§ λ‘λ© μ€...")# 1. G2P λͺ¨λΈ λ‘λ©print("G2P λͺ¨λΈ λ‘λ© μ€...")g2p = G2p()print("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 lines else: flat_dict = data json_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. ν΅μ¬ λ³ν λ‘μ§# 3. λ³ν λ° κΈμ μ μΈκΈ° (λ²νΌ λλ₯Ό λλ§ μλ)def process_text(input_text): try: if not input_text.strip(): return "", "", "μ
λ ₯ κΈμ μ: 0μ" char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)" processed_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) 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
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 # 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>') except Exception as e: return f"μλ¬: {e}", "<div style='color:red;'>μλ¬ λ°μ</div>", "μλ¬ λ°μ" 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. μ΄κΈ°ν (μ¬μ μ μ μ§)# 4. μ΄κΈ°νdef clear_all(): return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 4. μ¬μ μΆκ°# 5. μ¬μ μ μΆκ°def add_to_dict(word, pronun, current_input): if not word or not pronun: return "λ¨μ΄μ λ°μμ μ
λ ₯νμΈμ.", current_input, ""17 unmodified lines return f"β
'{word}' μ μ₯λ¨!", new_final, new_html return f"β
'{word}' μ μ₯λ¨!", "", ""
# 6. νμΌ λ€μ΄λ‘λ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. UI# 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) 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") clear_btn = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") with gr.Column(): # show_copy_button=True λ₯Ό ν΅ν΄ μμ νκ² κΈ°λ³Έ λ³΅μ¬ λ²νΌ μ 곡 output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν
μ€νΈ") 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]) 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="μνλ λ°μ")2 unmodified lines 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 = gr.DownloadButton("π₯ JSON μ¬μ λ°±μ
(PCλ‘ λ€μ΄λ‘λ)") dl_btn.click(fn=get_dict_file, outputs=[dl_btn])
if __name__ == "__main__":app.py+41-49