Spaces:
Running
Running
Update app.py
Browse files22 unmodified lines2324252626272829313031323 unmodified lines363738414239404142454643444546477 unmodified lines555657605859626364606162666364656667727368697025 unmodified lines9697981059910010124 unmodified lines12612712813612913013113216 unmodified lines1491501511591601611521631641651661671531541551561571581721591601619 unmodified lines17117217318817419017517617717822 unmodified lines20120220321920420520620720820921021121221321421521621721821922023022122222322 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:3 unmodified lines for item in data.get("items",[]): if len(item) == 2: flat_dict[item[0]] = item[1] except Exception as e: print(f"์ฌ์ ๋ก๋ฉ ์๋ฌ: {e}") except: pass return flat_dict
# โ๏ธ ํ๊น
ํ์ด์ค ํด๋ผ์ฐ๋์ ์ฌ์ ์ ์๊ตฌ์ ์ผ๋ก ๋ฎ์ด์ฐ๋(๋ฐฑ์
) ํจ์def auto_backup_to_space():# ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์
ํจ์ (๋ฒํผ ๋๋ฅผ ๋๋ง ์๋)def backup_to_space(): space_id = os.environ.get("SPACE_ID") hf_token = os.environ.get("HF_TOKEN") 7 unmodified lines repo_type="space", token=hf_token ) return True return "โ๏ธ ํด๋ผ์ฐ๋ ์๊ตฌ ์ ์ฅ ์๋ฃ! (โป ์ ์ ํ ์๋ฒ๊ฐ ๋ฐฑ์
์ ์ํด ์๋์ผ๋ก ์๋ก๊ณ ์นจ ๋ ์ ์์ต๋๋ค)" except Exception as e: print(f"์๋ ๋ฐฑ์
์๋ฌ: {e}") return False return False return f"โ ๋ฐฑ์
์คํจ: {e}" return "โ ๏ธ HF_TOKEN์ด ์ค์ ๋์ง ์์ ํด๋ผ์ฐ๋ ์ ์ฅ์ ํ ์ ์์ต๋๋ค."
# ํ
์คํธ ๋ณํ ํต์ฌ ๋ก์ง (์ฌ์ฉ์ ๋จ์ด ์ ๋ ๋ณดํธ)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:25 unmodified lines matched_word = match.group() parts.append(flat_dict[matched_word]) last_end = end tail = input_text[last_end:]24 unmodified linesdef 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()16 unmodified lines try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(j_struct, f, ensure_ascii=False, indent=2) # โ
๋ฑ๋กํ์ HF_TOKEN์ ์ด์ฉํด ํด๋ผ์ฐ๋์ ์๊ตฌ ๋ฐฑ์
์คํ โ
is_synced = auto_backup_to_space() status_msg = f"โ
'{word}' -> '{pronun}' ์ ์ฅ ์๋ฃ" if is_synced: status_msg += " (โ๏ธ ํด๋ผ์ฐ๋ ์๊ตฌ ์ ์ฅ ์๋ฃ! ๋ฆฌ์คํํธํด๋ ์ ์ง๋ฉ๋๋ค)" else: status_msg += " (โป ๋ฐฑ์
์คํจ: ํ ํฐ ์ค์ ์ ๋ค์ ํ์ธํด์ฃผ์ธ์)" # ํด๋ผ์ฐ๋ ์๋ ์ ์ฅ์ ๋นผ๋ฒ๋ฆผ (๋๋ณธ ๋ ์๊ฐ ๋ฐฉ์ง) 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_count9 unmodified lines return "โ
์๋ ๋ฐฑ์
๋ณต๊ตฌ ์๋ฃ!" return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."
# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface: gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (โ๏ธ ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์
ํ์ฑํ)") gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์ฌ์์ ๋ฐฉ์ง ์ต์ ํ)") with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):22 unmodified lines with gr.Row(): nw = gr.Textbox(label="๊ธฐ์กด ๋จ์ด ๋๋ ๋ฌธ์ฅ") np = gr.Textbox(label="์ํ๋ ๋ฐ์") btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ", variant="primary") btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ (๋๋ณธ ์ ์ง๋จ)", variant="primary") 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 ) gr.Markdown("---") btn_sync = gr.Button("โ๏ธ ์์
์ด ๋๋๋ฉด ์ฌ๊ธฐ๋ฅผ ๋๋ฌ ํด๋ผ์ฐ๋์ ์๊ตฌ ๋ฐฑ์
ํ์ธ์ (์๋ฒ ์๋ก๊ณ ์นจ ๋จ)") btn_sync.click(fn=backup_to_space, outputs=[status], api_name=False)
with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/๋ณต๊ตฌ (๋น์์ฉ)"): gr.Markdown("๐ก ํด๋ผ์ฐ๋ ์๋ ์ ์ฅ์ด ์ผ์ ธ ์์ผ๋ฏ๋ก ์ด ํญ์ ๋น์์์๋ง ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.") dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์
ํ๊ธฐ (PC ๋ค์ด๋ก๋)") dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) app.py+18-28