import gradio as gr import json import os import difflib import shutil from g2pk import G2p print("G2P 로딩 중...") g2p = G2p() print("로딩 완료") dict_path = "custom_dict.json" json_structure = {"items":[], "meta": {"count": 0}} flat_dict = {} def load_dict(): global json_structure, flat_dict if os.path.exists(dict_path): try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) if "items" in data: json_structure = data for item in data["items"]: if len(item) == 2: flat_dict[item[0]] = item[1] except: pass load_dict() def process_text(input_text): if not input_text or not input_text.strip(): return "", "", "입력 글자 수: 0자" char_count = f"입력 글자 수: {len(input_text)}자" processed = input_text for w, p in flat_dict.items(): processed = processed.replace(w, p) try: final_text = g2p(processed) except Exception as e: return f"에러: {e}", "에러", 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', '
') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'{chunk}') html_output = f'
{"".join(html_parts)}
' return final_text, html_output, char_count def clear_all(): return "", "", "", "입력 글자 수: 0자" def add_dict(word, pronun): if not word or not pronun: return "입력 오류" flat_dict[word] = pronun found = False for item in json_structure["items"]: if item[0] == word: item[1] = pronun found = True if 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) return f"✅ '{word}' 저장 완료!" def get_file(): return dict_path if os.path.exists(dict_path) else None def upload_file(f): if f: shutil.copy(f.name, dict_path) load_dict() return "복구 완료!" return "파일 없음" # 안전한 복사 함수 def dummy_copy(text): return text with gr.Blocks() as iface: with gr.Tabs(): with gr.Tab("메인 화면"): 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(): btn_conv = gr.Button("✨ 변환하기", variant="primary") btn_copy = gr.Button("📋 변환복사") btn_clr = gr.Button("🗑️ 초기화", variant="stop") with gr.Column(): output_text = gr.Textbox(lines=7, label="2️⃣ 변환된 텍스트") output_html = gr.HTML(label="🔍 변경점") btn_conv.click(process_text, inputs=[input_text], outputs=[output_text, output_html, char_count]) btn_clr.click(clear_all, outputs=[input_text, output_text, output_html, char_count]) btn_copy.click(dummy_copy, inputs=[output_text], outputs=[output_text], js="(t) => {navigator.clipboard.writeText(t); alert('복사 완료!'); return t;}") with gr.Row(): nw = gr.Textbox(label="기존 단어") np = gr.Textbox(label="원하는 발음") btn_add = gr.Button("💾 추가") status = gr.Textbox(label="상태") btn_add.click(add_dict, inputs=[nw, np], outputs=[status]) with gr.Tab("백업/복구"): dl_btn = gr.DownloadButton("📥 다운로드 (백업)") dl_btn.click(get_file, outputs=[dl_btn]) up_file = gr.File(label="업로드 (복구)") up_stat = gr.Textbox(label="상태") up_file.upload(upload_file, inputs=[up_file], outputs=[up_stat]) iface.launch()