Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567891028 unmodified lines3940414243444546474849505152535455565758596061626364656667686945467048715051525354727374757677787980818283848556575859606162636486878866899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312416 unmodified lines1411421438914414514612 unmodified lines1591601611621631641081651661671681691701711721731121741751766 unmodified lines18318418512518618718818919013019119219319425 unmodified lines2202212221622232242251 unmodified line2272282291702302312322332 unmodified linesimport osimport difflibimport shutilimport refrom huggingface_hub import HfApi
try: from g2pk import G2p28 unmodified lines pass return flat_dict
def auto_backup_to_space(): space_id = os.environ.get("SPACE_ID") hf_token = os.environ.get("HF_TOKEN") if space_id and hf_token: try: api = HfApi() api.upload_file( path_or_fileobj=dict_path, path_in_repo=dict_path, repo_id=space_id, repo_type="space", token=hf_token ) return True except Exception as e: print(f"์๋ ๋ฐฑ์
์๋ฌ: {e}") 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)}์" processed = input_text flat_dict = load_dict() placeholders = {} # ๋ด ๋จ์ด ๋ณดํธ for i, (word, pronun) in enumerate(flat_dict.items()): ph = f"__PH_{i}__" placeholders[ph] = pronun processed = processed.replace(word, ph) if not flat_dict: # ์ฌ์ ์ด ๋น์ด์์ผ๋ฉด ์ ์ฒด G2P ๋ณํ final_text = input_text if g2p is not None: try: final_text = g2p(input_text) except Exception: pass else: # 1. ์ฌ์ ๋จ์ด๊ฐ ๊ธด ์์๋๋ก ์ ๋ ฌ (๊ฒน์น๋ ๋จ์ด ๊ผฌ์ ๋ฐฉ์ง) sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True) # 2. ๋ฑ๋ก๋ ๋จ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋ณธ์ ์๋ฅด๊ธฐ ์ํ ํจํด ์์ฑ pattern = re.compile("|".join(map(re.escape, sorted_keys))) if g2p is not None: try: processed = g2p(processed) except Exception as e: pass
# ๋ด ๋จ์ด ๋ณต๊ตฌ for ph, pronun in placeholders.items(): processed = processed.replace(ph, pronun) parts =[] last_end = 0 final_text = processed # 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: parts.append(g2p(chunk)) except: parts.append(chunk) 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: try: parts.append(g2p(tail)) except: parts.append(tail) else: parts.append(tail) final_text = "".join(parts)
# ํ๊ดํ (HTML) seq = difflib.SequenceMatcher(None, input_text, final_text)16 unmodified lines if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update() # ์ฌ์ ์ ์ฅ try: with open(dict_path, "r", encoding="utf-8") as f: j_struct = json.load(f)12 unmodified lines try: with 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}' ์ ์ฅ ๋ฐ ์ฆ์ ๋ฐ์ ์๋ฃ!" 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_count6 unmodified linesdef upload_file(file_path): if file_path is not None: shutil.copy(file_path, dict_path) return "โ
๋ฐฑ์
๋ณต๊ตฌ ์๋ฃ!" return "โ
์๋ ๋ฐฑ์
๋ณต๊ตฌ ์๋ฃ!" return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."
# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface: gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ") gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ๋ฌด๊ฒฐ์ฑ ๋ฒ์ )") with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):25 unmodified lines btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ", variant="primary") status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) # ์ฌ์ ์ถ๊ฐ์ ๋์์ ๋ณํ ๊ฒฐ๊ณผ ์ฆ์ ์
๋ฐ์ดํธ btn_add.click( fn=add_and_update, inputs=[nw, np, input_text], 1 unmodified line api_name=False )
with gr.Tab("๐ฅ ๋ฐฑ์
/๋ณต๊ตฌ"): with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/๋ณต๊ตฌ"): dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์
ํ๊ธฐ (PC ๋ค์ด๋ก๋)") dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) app.py+85-25