Spaces:
Running
Running
Update app.py
Browse files22 unmodified lines2324252627282930313233343536373839404142434414 unmodified lines5960616263646566676869707172737475767778798081828384858687888990919293949596973 unmodified lines1011021031041051061071081091101111121136 unmodified lines12012112212312412512610 unmodified lines13713813914014114217 unmodified lines16016116216316416516616716816917017117217317417517611 unmodified lines18818919019119219319432 unmodified lines22722822923023123223322 unmodified linesdict_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: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]] = item[1]except:passreturn flat_dictdef auto_backup_to_space():space_id = os.environ.get("SPACE_ID")hf_token = os.environ.get("HF_TOKEN")14 unmodified linesreturn Falsereturn False# ---------------------------------------------------------# [ํต์ฌ ๋ก์ง ์์ ] G2P๊ฐ ๋ฑ๋ก ๋จ์ด๋ฅผ ๊ฑด๋๋ฆฌ์ง ๋ชปํ๊ฒ ์๋ฒฝ ๋ถ๋ฆฌ# ---------------------------------------------------------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:# ์ฌ์ ์ด ๋น์ด์์ผ๋ฉด ์ ์ฒด G2P ๋ณํfinal_text = input_textif g2p is not None:try:final_text = g2p(input_text)except Exception:passelse:# 1. ์ฌ์ ๋จ์ด๊ฐ ๊ธด ์์๋๋ก ์ ๋ ฌ (๊ฒน์น๋ ๋จ์ด ๊ผฌ์ ๋ฐฉ์ง)sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True)# 2. ๋ฑ๋ก๋ ๋จ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋ณธ์ ์๋ฅด๊ธฐ ์ํ ํจํด ์์ฑpattern = re.compile("|".join(map(re.escape, sorted_keys)))parts =[]last_end = 0# 3. ๋จ์ด๋ฅผ ๋๋ ค๋ด๊ณ , ๋๋จธ์ง ์ผ๋ฐ ๋ฌธ์ฅ๋ง G2P์ ํต๊ณผ์ํดfor match in pattern.finditer(input_text):start, end = match.span()chunk = input_text[last_end:start]# (1) ์ผ๋ฐ ๋ฌธ์ฅ์ G2P ๋๋ ค์ ๋ถ์if chunk:if g2p is not None:try:3 unmodified lineselse:parts.append(chunk)# (2) ์ฌ์ฉ์๊ฐ ๋ฑ๋กํ ๋จ์ด๋ G2P๋ฅผ ๊ฑฐ์น์ง ์๊ณ '์ํ๋ ๋ฐ์'์ ๊ทธ๋๋ก ๋ถ์matched_word = match.group()parts.append(flat_dict[matched_word])last_end = end# 4. ๋ง์ง๋ง ๋จ์ ๊ผฌ๋ฆฌ ๋ฌธ์ฅ ์ฒ๋ฆฌtail = input_text[last_end:]if tail:if g2p is not None:6 unmodified linesfinal_text = "".join(parts)# ํ๊ดํ (HTML)seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():10 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()17 unmodified lineswith open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)is_synced = auto_backup_to_space()status_msg = f"โ '{word}' -> '{pronun}' ์ ์ฅ ์๋ฃ"if is_synced:status_msg += " (โ๏ธ ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์ ์๋ฃ!)"else:status_msg += " (โป HF_TOKEN ๋ฏธ์ค์ ์ผ๋ก ์๋ ๋ฐฑ์ ์ ๋ณด๋ฅ๋จ)"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_count11 unmodified lines# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ๋ฌด๊ฒฐ์ฑ ๋ฒ์ )")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):32 unmodified linesapi_name=False)with gr.Tab("๐ฅ ์๋ ๋ฐฑ์ /๋ณต๊ตฌ"):dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์ ํ๊ธฐ (PC ๋ค์ด๋ก๋)")dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False)22 unmodified lines232425262728293031323334353637383940414243444546474814 unmodified lines636465666768697071727374757677787980818283848586878889909192939495963 unmodified lines1001011021031041051061071081091106 unmodified lines11711811912012112210 unmodified lines13313413513613713813917 unmodified lines15715815916016116216316416516616716816917017117217317417511 unmodified lines18718818919019119219332 unmodified lines22622722822923023123223322 unmodified linesdict_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]] = item[1]except Exception as e:print(f"์ฌ์ ๋ก๋ฉ ์๋ฌ: {e}")return flat_dict# โ๏ธ ํ๊น ํ์ด์ค ํด๋ผ์ฐ๋์ ์ฌ์ ์ ์๊ตฌ์ ์ผ๋ก ๋ฎ์ด์ฐ๋(๋ฐฑ์ ) ํจ์def auto_backup_to_space():space_id = os.environ.get("SPACE_ID")hf_token = os.environ.get("HF_TOKEN")14 unmodified linesreturn Falsereturn False# ํ ์คํธ ๋ณํ ํต์ฌ ๋ก์ง (์ฌ์ฉ์ ๋จ์ด ์ ๋ ๋ณดํธ)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_textif g2p is not None:try:final_text = g2p(input_text)except:passelse:sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True)pattern = re.compile("|".join(map(re.escape, sorted_keys)))parts =[]last_end = 0for match in pattern.finditer(input_text):start, end = match.span()chunk = input_text[last_end:start]if chunk:if g2p is not None:try:3 unmodified lineselse:parts.append(chunk)matched_word = match.group()parts.append(flat_dict[matched_word])last_end = endtail = input_text[last_end:]if tail:if g2p is not None:6 unmodified linesfinal_text = "".join(parts)seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts =[]for tag, i1, i2, j1, j2 in seq.get_opcodes():10 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()17 unmodified lineswith 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 += " (โป ๋ฐฑ์ ์คํจ: ํ ํฐ ์ค์ ์ ๋ค์ ํ์ธํด์ฃผ์ธ์)"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_count11 unmodified lines# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (โ๏ธ ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์ ํ์ฑํ)")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):32 unmodified linesapi_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+23-23