Spaces:
Running
Running
Update app.py
Browse files22 unmodified lines23242526272829303132333435333435363738393637383940414243444546474814 unmodified lines636465626364666768697071727374757673777879807881828381848385868788898990919293949495963 unmodified lines1001011021041031041051061071101081091106 unmodified lines11711811912312012112210 unmodified lines13313413513613713813917 unmodified lines15715815916016116216316416716516616916716816917017117217317417511 unmodified lines18718818919119019119219332 unmodified lines22622722823022923023123223322 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]] = item[1] except: pass 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 lines return False return 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_text if g2p is not None: try: final_text = g2p(input_text) except Exception: except: pass else: # 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 lines else: 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 lines final_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 lines 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 += " (โ๏ธ ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์
์๋ฃ!)" status_msg += " (โ๏ธ ํด๋ผ์ฐ๋ ์๊ตฌ ์ ์ฅ ์๋ฃ! ๋ฆฌ์คํํธํด๋ ์ ์ง๋ฉ๋๋ค)" else: status_msg += " (โป HF_TOKEN ๋ฏธ์ค์ ์ผ๋ก ์๋ ๋ฐฑ์
์ ๋ณด๋ฅ๋จ)" 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 ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ๋ฌด๊ฒฐ์ฑ ๋ฒ์ )") gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (โ๏ธ ํด๋ผ์ฐ๋ ์๋ ๋ฐฑ์
ํ์ฑํ)") with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):32 unmodified lines api_name=False )
with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/๋ณต๊ตฌ"): 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