Spaces:
Running
Running
Update app.py
Browse files21 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243252627282930313233343536373839404121 unmodified lines
dict_path = "custom_dict.json"
if not os.path.exists(dict_path): with open(dict_path, "w", encoding="utf-8") as f: json.dump({"items":[], "meta": {"count": 0}}, f)
def load_dict(): flat_dict = {} try: if os.path.exists(dict_path): with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) for item in data.get("items",[]): if len(item) == 2: flat_dict[item[0].strip()] = item[1].strip() except: pass return flat_dict
def process_text(input_text): if not input_text or not input_text.strip(): return "", "", "์
๋ ฅ ๊ธ์ ์: 0์" char_count = f"์
๋ ฅ ๊ธ์ ์: {len(input_text)}์" flat_dict = load_dict() if not flat_dict: final_text = input_text if g2p is not None: try: final_text = g2p(input_text) except: pass else: sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True) pattern = re.compile("|".join(map(re.escape, sorted_keys))) parts =[] last_end = 0 for match in pattern.finditer(input_text): start, end = match.span() chunk = input_text[last_end:start] if chunk: if g2p is not None: try: parts.append(g2p(chunk)) except: parts.append(chunk) else: parts.append(chunk) matched_word = match.group() parts.append(flat_dict[matched_word]) last_end = end tail = input_text[last_end:] if tail: if g2p is not None: try: parts.append(g2p(tail)) except: parts.append(tail) else: parts.append(tail) final_text = "".join(parts)
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:5px; background-color:#f9f9f9;">{"".join(html_parts)}</div>' return final_text, html_output, char_count
def clear_all(): return "", "", "", "์
๋ ฅ ๊ธ์ ์: 0์"
def add_and_update(word, pronun, current_input): if not word or not pronun: return "โ ๏ธ ๊ธฐ์กด ๋จ์ด์ ์ํ๋ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update() word = word.strip() pronun = pronun.strip() try: with open(dict_path, "r", encoding="utf-8") as f: j_struct = json.load(f) except: j_struct = {"items":[], "meta": {"count": 0}} found = False for item in j_struct.get("items",[]): if item[0].strip() == word: item[1] = pronun found = True break if not found: j_struct["items"].append([word, pronun]) try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(j_struct, f, ensure_ascii=False, indent=2) if current_input and word not in current_input: status_msg = f"โ ๏ธ ์ฌ์ ์ ์ฅ ์๋ฃ! ํ์ง๋ง ์๋ณธ ํ
์คํธ์ '{word}'(์ด)๊ฐ ์์ด ํ๋ฉด์ ๋ณํ๊ฐ ์์ต๋๋ค. (1๋ฒ ์ฐฝ์์ ๊ทธ๋๋ก ๋ณต์ฌํด์ค์ธ์!)" else: status_msg = f"โ
'{word}' -> '{pronun}' ์ ์ฅ ๋ฐ ์ฆ์ ์ฌ๋ณํ ์ ์ฉ ์๋ฃ!" except Exception as e: status_msg = f"โ ์ ์ฅ ์คํจ: {e}"
if current_input and current_input.strip(): final_text, html_output, char_count = process_text(current_input) return status_msg, final_text, html_output, char_count else: return status_msg, "", "", "์
๋ ฅ ๊ธ์ ์: 0์"
def get_file(): return dict_path if os.path.exists(dict_path) else None
def upload_file(file_path): if file_path is not None: shutil.copy(file_path, dict_path) return "โ
ํ์ผ ๋ณต๊ตฌ ์๋ฃ!" return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."
# โ
์๋ก ์ถ๊ฐ๋ ๋ถ์ฌ๋ฃ๊ธฐ ์ ์ฉ ํจ์ โ
def apply_json_text(json_string): if not json_string or not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์." try: # ํ
์คํธ๊ฐ ์ ์์ ์ธ JSON์ธ์ง ํ์ธ data = json.loads(json_string) if "items" not in data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค. ('items' ํค๊ฐ ์์ต๋๋ค)" # ์ ์์ ์ด๋ฉด ํ์ผ๋ก ์ ์ฅ with open(dict_path, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) return "โ
๋ณต์ฌ/๋ถ์ฌ๋ฃ๊ธฐ๋ก ๋๋ ๋จ์ด ์ฌ์ ์ ์ฉ ์๋ฃ!" except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ (๊ดํธ๋ ์ผํ๊ฐ ๋น ์ก๋์ง ํ์ธํด์ฃผ์ธ์): {e}"
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๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ") char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="์
๋ ฅ ๊ธ์ ์: 0์", interactive=False) with gr.Row(): btn_conv = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary") btn_copy = gr.Button("๐ ๋ณํ๋ณต์ฌ", variant="secondary") btn_clr = gr.Button("๐๏ธ ๋๋ณธ ์ด๊ธฐํ", variant="stop") with gr.Column(): output_text = gr.Textbox(lines=8, label="2๏ธโฃ ๋ณํ๋ ํ
์คํธ", show_copy_button=True) output_html = gr.HTML(label="๐ ๋ณ๊ฒฝ์ ํ์ธ") btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count], api_name=False) btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], api_name=False) btn_copy.click( fn=None, inputs=[output_text], outputs=None, js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('โ
๋ณต์ฌ ์๋ฃ!'); } }", api_name=False ) gr.Markdown("---") with gr.Row(): nw = gr.Textbox(label="๊ธฐ์กด ๋จ์ด ๋๋ ๋ฌธ์ฅ (โ
๋ฐ๋์ 1๋ฒ ์ฐฝ์์ ๋ณต์ฌํด์ค์ธ์โ
)") np = gr.Textbox(label="์ํ๋ ๋ฐ์") with gr.Column(min_width=200): btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐํ๊ณ ์ฆ์ ๋๋ณธ ๋ณํํ๊ธฐ", variant="primary") btn_clear_dict_inputs = gr.Button("๐งน ๋จ์ด ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ", variant="secondary") status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) btn_add.click( fn=add_and_update, inputs=[nw, np, input_text], outputs=[status, output_text, output_html, char_count], api_name=False ) btn_clear_dict_inputs.click( fn=lambda: ("", ""), outputs=[nw, np], api_name=False )
with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/๋ณต๊ตฌ (๋๋ ๋ฑ๋ก)"): dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์์ฑ๋ ์ฌ์ ๋ฐฑ์
ํ๊ธฐ (PC ๋ค์ด๋ก๋)") dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) gr.Markdown("---") gr.Markdown("### ๐ 2. ์ฝ๋๋ฅผ ๋ณต์ฌํด์ ์ง์ ๋ถ์ฌ๋ฃ๊ธฐ (์ถ์ฒ)") json_input = gr.Textbox(lines=10, label="์ฌ๊ธฐ์ JSON ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ์ผ์ธ์") btn_apply_json = gr.Button("๐ ๋ถ์ฌ๋ฃ์ ์ฝ๋๋ก ์ฌ์ ๋ฎ์ด์ฐ๊ธฐ", variant="primary") json_stat = gr.Textbox(label="๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) btn_apply_json.click(fn=apply_json_text, inputs=[json_input], outputs=[json_stat], api_name=False)
gr.Markdown("---") gr.Markdown("### ๐ค 3. ํ์ผ๋ก ์
๋ก๋ํ๊ธฐ (๊ธฐ์กด ๋ฐฉ์)") up_file = gr.File(label="๋ฐฑ์
ํ์ผ ๋ณต๊ตฌํ๊ธฐ (์
๋ก๋)", type="filepath") up_stat = gr.Textbox(label="ํ์ผ ๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat], api_name=False)
if __name__ == "__main__": iface.launch()# =====================================================================# โ
AI๊ฐ ์ง ๊ธฐ์ ์ ์๋ ๋ณต๊ตฌ & ๋ณํฉ ๋ก์ง (์ฌ์ฉ์๋ ์๋ฌด๊ฒ๋ ์ ํด๋ ๋จ) โ
# =====================================================================AUTO_MERGE_WORDS = [["GS๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"], ["์ง์์ค ๋ง์ด์ต", "์ง์์ค ๋ง์ด์ต"],["1+1", "์ํ๋ฌ์ค์"], ["2+1", "ํฌํ๋ฌ์ค์"],["F/W", "์ํ ๋๋ธ์ "], ["S/S", "์์ค ์์ค"],["VIP", "๋ธ์ด์์ดํผ"], ["ํ ์ธ๊ฐ", "ํ๋ฆฐ๊น"], ["ํน๊ฐ", "ํน๊น"],["์ต์ ๊ฐ", "์ต์ ๊น"], ["๋จ๋
๊ตฌ์ฑ", "๋จ๋
๊พธ์ฑ"],["์ญ๋๊ธ", "์ญ๋๋"], ["์ฑ๊ฒฐ์ ", "์ฑ๊ปผ์ "],["์ผ์๋ถ", "์ผ์จ๋ถ"], ["๋ฌด์ด์", "๋ฌด์ด์"],["๋งค์ง์๋ฐ", "๋งค์ง๋๋ฐ"], ["์๋์ฃผ๋ฌธ", "์๋์ฃผ๋ฌธ"],["์ฌ์ํ", "์ฌ์ํ"], ["ํ๋ฆฌํฐ๋ฅผ", "ํ๋ฆฌํฐ๋ฅผ"],["๋ํ
์ผ์ด", "๋ํ
์ด๋ฆฌ"], ["์ค๋ฃจ์ฃ์ด", "์ค๋ฃจ์์"],["์ปคํฐ์ค๋ฒ
", "์ปคํฐ์ค๋ฒ
"], ["์ธ๋ฅด์ฆ๋ธ๋์ฝ", "์ธ๋ฅด์ฆ๋ธ๋์ฝ"],["ํฌ๋จธํฐ๋ธ", "ํฌ๋จธํฐ๋ธ"], ["๋นํธ๋ฆด", "๋นํธ๋ฆด"],["๋ ๋ฏธ์ค๋ฅด๋ฃจ", "๋ ๋ฏธ์ค๋ฅด๋ฃจ"], ["์ญ๋ํด๋ผ์ฐ์ค", "์๋ํด๋ผ์ฐ์ค"],["ํธ๋ง๊ณจํ", "ํธ๋ง๊ณจํ"], ["์ปฌ๋ผ๋น์", "์ปฌ๋ผ๋น์"],["์์ค๊น๋ค์คํฌ์ธ ", "์์ค๊น๋ค์คํฌ์ธ "],["์์ค์นด๋ค์คํฌ์ธ ", "์์ค๊น๋ค์คํฌ์ธ "], ["์์ธ", "์์ธ"],["์๋ํ๋", "์๋ํ๋"], ["๋ผ์์ํ", "๋ผ์์ํ"], ["๋ชจ๋ฅด๊ฐ", "๋ชจ๋ฅด๊ฐ"], ["๋ง์ค์คํ๋์ค", "๋ง์ฐ์คํ๋์ค"], ["๋ฐ์ผ๋ฆฌ๋ธ๋ฃจ", "๋ฐ์ผ๋ฆฌ๋ธ๋ฃจ"],["ํธ๋ผ์ด์ํ", "ํธ๋ผ์ด์ํ"],["์์จ๋น ํด๋กํด๋ฝ", "์์จ๋น ํด๋กํด๋ฝ"], ["๊ธฐํ ", "๊ธฐํ "],["์ค๋ก ๋", "์ค๋ก ๋"], ["SJ์๋", "์์ค์ ์ด์๋"],["์ฝ์ด ์ด์ผํฑ", "์ฝ์ด ์ด์ผํฑ"], ["๋ฅด๋คํฌ๋ฃจ", "๋ฅด๋คํฌ๋ฃจ"],["๋ํ", "๋ํ"], ["1์ข
", "์ผ์ข
"], ["2์ข
", "์ด์ข
"],["3์ข
", "์ผ์ข
"], ["4์ข
", "์ฌ์ข
"], ["5์ข
", "์ค์ข
"],["10์ข
", "์ญ์ข
"], ["4in1", "ํฌ์ธ์"],["25FW", "์ด์ค ์ํ ๋๋ธ์ "],["25 F/W", "์ด์ค ์ํ ๋๋ธ์ "], ["์ด์ค ์ํ๋ ๋ธ์ ", "์ด์ค ์ํ ๋๋ธ์ "],["26SS", "์ด์ญ๋ต์์ค์์ค"],["2026 SS", "์ด์ฒ์ด์ญ๋ต ์์ค์์ค"], ["SS์์ฆ", "์์ค์์ค ์์ฆ"],["์
์
", "์ธ๋ฅ"], ["๋ฆฌ๋ฒ์๋ธ", "๋ฆฌ๋ฒ์๋ธ"], ["๋งจํฌ๋งจ", "๋งจํฌ๋งจ"], ["์ํฐํฌ์ธ ", "์ํฐํฌ์ธ "],["๋ณธ๋ฉํฌ์ธ ", "๋ณธ๋ฉํฌ์ธ "], ["์ค์์์ผ", "์ค์์์ผ"],["ํ์ด๋ธ๋ฆฌ๋", "ํ์ด๋ธ๋ฆฌ๋"], ["๋ ์ด์ด๋ง", "๋ ์ด์ด๋ง"], ["๋ ์ด์ด๋", "๋ ์ด์ด๋"], ["์์นด๋ผ", "์์นด๋ผ"], ["์ธํ์ค", "์ธํ์ค"],["๋๋ค์ดํจ๋ฉ", "๋๋ฐ์ดํจ๋ฉ"],["๋๋ค์ด์ค์ปคํธ", "๋๋ฐ์ด์ค์ปคํธ"], ["๊ทธ๋ ์ด์ง", "๊ทธ๋ ์ด์ง"], ["ํธ์", "ํธ์"],["ํ๊ฐ๋จผํธ", "ํ๊ฐ๋จผํธ"], ["ํ๋ฌํผ", "ํ๋ฌํผ"],["๋
ธ๋ฅด๋", "๋
ธ๋ฅด๋"], ["์ธ๋ธ๋ ๋", "์ธ๋ธ๋ ๋"],["๋ผ์ฟค ๋ธ๋ ๋", "๋ผ์ฟค ๋ธ๋ ๋"], ["์ธ์ด์ง๊ทธ๋ฆฐ", "์ธ์ด์ง๊ทธ๋ฆฐ"], ["๋ชจ๋ธ์์ธ", "๋ชจ๋ธ์์ธ"], ["ํฌ๋ก์", "ํฌ๋ก์
ฐ"],["๋ ์ด์ค ํจ์ง", "๋ ์ด์ค ํจ์ง"], ["์์ด์์ปค", "์์ด์์ปค"], ["ํธ๋ ์คํ์ผ๋ฌ", "ํธ๋ ์คํ์ผ๋ฌ"], ["์์ผ๋ ", "์์ผ๋ "],app.py+17-219