easyadaptor's picture
Update app.py
d1a8335 verified
Raw History Blame
4.43 kB
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', '<br>')
if tag == 'equal':
html_parts.append(chunk)
elif tag in ['replace', 'insert']:
html_parts.append(f'<span style="background-color: yellow;">{chunk}</span>')
html_output = f'<div style="line-height:1.6; padding:10px; border:1px solid #ddd;">{"".join(html_parts)}</div>'
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()