Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567828 unmodified lines37383940414243444546474849505152535455565758596061626364656667686916 unmodified lines8687888990919212 unmodified lines1051061071081091101111121131141156 unmodified lines12212312412512612712812913013113213325 unmodified lines1591601611621631641651 unmodified line1671681691701711721732 unmodified linesimport osimport difflibimport shutiltry:from g2pk import G2p28 unmodified linespassreturn flat_dictdef process_text(input_text):if not input_text or not input_text.strip():return "", "", "์ ๋ ฅ ๊ธ์ ์: 0์"char_count = f"์ ๋ ฅ ๊ธ์ ์: {len(input_text)}์"processed = input_textflat_dict = load_dict()placeholders = {}# ๋ด ๋จ์ด ๋ณดํธfor i, (word, pronun) in enumerate(flat_dict.items()):ph = f"__PH_{i}__"placeholders[ph] = pronunprocessed = processed.replace(word, ph)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)final_text = processed# ํ๊ดํ (HTML)seq = difflib.SequenceMatcher(None, input_text, final_text)16 unmodified linesif 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 linestry:with open(dict_path, "w", encoding="utf-8") as f:json.dump(j_struct, f, ensure_ascii=False, indent=2)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_count6 unmodified linesdef upload_file(file_path):if file_path is not None:shutil.copy(file_path, dict_path)return "โ ๋ฐฑ์ ๋ณต๊ตฌ ์๋ฃ!"return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):25 unmodified linesbtn_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 lineapi_name=False)with gr.Tab("๐ฅ ๋ฐฑ์ /๋ณต๊ตฌ"):dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์ฌ์ ๋ฐฑ์ ํ๊ธฐ (PC ๋ค์ด๋ก๋)")dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False)2 unmodified lines34567891028 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312416 unmodified lines14114214314414514612 unmodified lines1591601611621631641651661671681691701711721731741751766 unmodified lines18318418518618718818919019119219319425 unmodified lines2202212222232242251 unmodified line2272282292302312322332 unmodified linesimport osimport difflibimport shutilimport refrom huggingface_hub import HfApitry:from g2pk import G2p28 unmodified linespassreturn flat_dictdef 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 Trueexcept Exception as e:print(f"์๋ ๋ฐฑ์ ์๋ฌ: {e}")return 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: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 linesif 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 linestry: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}' ์ ์ฅ ์๋ฃ"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 "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."# UI ํ๋ฉดwith gr.Blocks(theme="default") as iface:gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (์์ ๋ฌด๊ฒฐ์ฑ ๋ฒ์ )")with gr.Tabs():with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):25 unmodified linesbtn_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 lineapi_name=False)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