Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines345678910111213141517 unmodified lines333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677786 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351362 unmodified linesimport 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 = {}17 unmodified linesload_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, ""flat_dict[word] = pronun6 unmodified linesif 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}' μ μ₯λ¨!", "", ""# 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="μνλ λ°μ")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 μ¬μ λ°±μ (PCλ‘ λ€μ΄λ‘λ)")dl_btn.click(fn=get_dict_file, outputs=[dl_btn])if __name__ == "__main__":iface.launch()2 unmodified lines34567891011121314151617 unmodified lines34353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879806 unmodified lines878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521532 unmodified linesimport osimport difflibfrom g2pk import G2pimport traceback# 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 = {}17 unmodified linesload_dict()# 3. ν μ€νΈ λ³ν λ‘μ§def process_text(input_text):try:if not input_text or not input_text.strip():return "", "", "μ λ ₯ κΈμ μ: 0μ"char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)"processed_text = input_text# 1μμ: λ΄ μ¬μ λ³νfor word, pronun in flat_dict.items():processed_text = processed_text.replace(word, pronun)# 2μμ: G2P μλ λ³ν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:error_msg = traceback.format_exc()return f"λ³ν μ€ν¨: {e}", f"<div style='color:red;'>μλ¬ λ°μ: {e}</div>", "μλ¬ λ°μ"# 4. νλ©΄ μ΄κΈ°ν (μ¬μ μ 보쑴λ¨)def clear_all():return "", "", "", "μ λ ₯ κΈμ μ: 0μ"# 5. μ¬μ μ λ¨μ΄ μΆκ°def add_to_dict(word, pronun):if not word or not pronun:return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ λ ₯ν΄μ£ΌμΈμ."flat_dict[word] = pronun6 unmodified linesif not found:json_structure["items"].append([word, pronun])try:with open(dict_path, "w", encoding="utf-8") as f:json.dump(json_structure, f, ensure_ascii=False, indent=2)return f"β μ±κ³΅μ μΌλ‘ μ μ₯λμμ΅λλ€: '{word}' -> '{pronun}'"except Exception as e:return f"β μ μ₯ μ€ν¨: {e}"# 6. νμΌ λ€μ΄λ‘λdef get_dict_file():return dict_path if os.path.exists(dict_path) else None# 7. UI κ΅¬μ± (ν λΆλ¦¬ λ° 3λ²νΌ 볡ꡬ)with gr.Blocks(theme="default") as iface:gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°")with gr.Tabs():# 첫 λ²μ§Έ ν: λ³νκΈ°with gr.Tab("ποΈ λ³νκΈ° (λλ³Έ μμ )"):with gr.Row():with gr.Column():input_text = gr.Textbox(lines=8, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯", placeholder="μ΄κ³³μ μλ³Έ λλ³Έμ λΆμ¬λ£μΌμΈμ.")char_count = gr.Textbox(label="κΈμ μ (λ³ννκΈ° ν΄λ¦ μ κ³μ°λ¨)", value="μ λ ₯ κΈμ μ: 0μ", interactive=False)# μμ²νμ 3κ°μ λ²νΌ μλ²½ 볡ꡬ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=8, label="2οΈβ£ λ³νλ ν μ€νΈ κ²°κ³Όμ°½")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])# ν΄λ¦½λ³΄λ λ³΅μ¬ μ΄λ²€νΈ (μμ ν μλ°μ€ν¬λ¦½νΈ μ¬μ©)copy_btn.click(fn=lambda x: x,inputs=[output_text],outputs=[output_text],js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('β λ³νλ ν μ€νΈκ° ν΄λ¦½λ³΄λμ 볡μ¬λμμ΅λλ€.'); } else { alert('볡μ¬ν λ΄μ©μ΄ μμ΅λλ€.'); } return text; }")# λ λ²μ§Έ ν: μ¬μ κ΄λ¦¬with gr.Tab("π μ¬μ κ΄λ¦¬ (λ¨μ΄ λ±λ‘)"):gr.Markdown("μλ² μ¬μμ(Sleep) μ λ°μ΄ν°κ° μ΄κΈ°νλ μ μμΌλ λ±λ‘ ν νλ¨μ λ°±μ λ€μ΄λ‘λλ₯Ό κΆμ₯ν©λλ€.")with gr.Row():new_word = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ (μ: μ±μ§νΌν°)", scale=1)new_pronun = gr.Textbox(label="μνλ λ°μ (μ: μ±μ°νΌν°)", scale=1)add_btn = gr.Button("πΎ μ¬μ μ λ¨μ΄ μΆκ°", variant="primary", scale=1)dict_status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False)add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun], outputs=[dict_status])gr.Markdown("---")dl_btn = gr.DownloadButton("π₯ μ΅μ JSON μ¬μ λ°±μ (PCλ‘ λ€μ΄λ‘λ)")dl_btn.click(fn=get_dict_file, outputs=[dl_btn])if __name__ == "__main__":iface.launch()
app.py+86-69