Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567889101112121314151617 unmodified lines3435363637383839404142434445464748394041424344454647484950515152525455565758596061626364656653545556575859606162636465666768696870717273727374757675777879806 unmodified lines87888988899091929394909192939495969798991001001011021031041041051061071081051061071081101111121131141151161091101111121131141151161171181191201211221181191201211221231241251261271281291301311321331231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521532 unmodified linesimport osimport difflibfrom g2pk import G2pimport traceback
# 1. G2P ๋ชจ๋ธ ๋ก๋ฉprint("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์์...")g2p = G2p()print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
# 2. ์ฌ์ ํ์ผ ๋ก๋ฉ# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌ (์๊ตฌ ๋ณด์กด์ ์ํ ๊ตฌ์กฐ)dict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"app_title": "VoiceScript AI", "app_version": "146.0", "count": 0}}flat_dict = {}17 unmodified lines
load_dict()
# 3. ๋ณํ ๋ฐ ๊ธ์ ์ ์ธ๊ธฐ (๋ฒํผ ๋๋ฅผ ๋๋ง ์๋)# 3. ํ
์คํธ ๋ณํ ๋ก์งdef process_text(input_text): if not input_text.strip(): return "", "", "์
๋ ฅ ๊ธ์ ์: 0์" # ๊ธ์ ์ ์ธก์ char_count = f"์
๋ ฅ ๊ธ์ ์: {len(input_text)}์ (๊ณต๋ฐฑ ํฌํจ)" processed_text = input_text # ์ฌ์ ๋จ์ด ๋ณํ for word, pronun in flat_dict.items(): processed_text = processed_text.replace(word, pronun) try: if not input_text or not input_text.strip(): return "", "", "์
๋ ฅ ๊ธ์ ์: 0์" char_count = f"์
๋ ฅ ๊ธ์ ์: {len(input_text)}์ (๊ณต๋ฐฑ ํฌํจ)" processed_text = input_text # 1์์: ๋ด ์ฌ์ ๋ณํ for word, pronun in flat_dict.items(): processed_text = processed_text.replace(word, pronun) # 2์์: G2P ์๋ ๋ณํ final_text = g2p(processed_text) except Exception as e: return f"๋ณํ ์๋ฌ: {e}", "<div style='color:red;'>์๋ฌ ๋ฐ์</div>", char_count
# HTML ํ๊ดํ ์ฒ๋ฆฌ seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[] for tag, i1, i2, j1, j2 in seq.get_opcodes(): chunk = final_text[j1:j2].replace('\n', '<br>') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold; padding: 2px; border-radius: 3px;">{chunk}</span>') html_output = f'<div style="line-height: 1.6; font-size: 16px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;">{"".join(html_parts)}</div>' return final_text, html_output, char_count # ํ๊ดํ ํจ๊ณผ seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[] for tag, i1, i2, j1, j2 in seq.get_opcodes(): chunk = final_text[j1:j2].replace('\n', '<br>') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold; padding: 2px; border-radius: 3px;">{chunk}</span>') html_output = f'<div style="line-height: 1.6; font-size: 16px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;">{"".join(html_parts)}</div>' return final_text, html_output, char_count except Exception as e: error_msg = traceback.format_exc() return f"๋ณํ ์คํจ: {e}", f"<div style='color:red;'>์๋ฌ ๋ฐ์: {e}</div>", "์๋ฌ ๋ฐ์"
# 4. ์ด๊ธฐํ# 4. ํ๋ฉด ์ด๊ธฐํ (์ฌ์ ์ ๋ณด์กด๋จ)def clear_all(): return "", "", "", "์
๋ ฅ ๊ธ์ ์: 0์"
# 5. ์ฌ์ ์ ์ถ๊ฐdef add_to_dict(word, pronun, current_input):# 5. ์ฌ์ ์ ๋จ์ด ์ถ๊ฐdef add_to_dict(word, pronun): if not word or not pronun: return "๋จ์ด์ ๋ฐ์์ ์
๋ ฅํ์ธ์.", current_input, "" return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํด์ฃผ์ธ์." flat_dict[word] = pronun 6 unmodified lines if not found: json_structure["items"].append([word, pronun]) with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False, indent=2) if current_input.strip(): new_final, new_html, _ = process_text(current_input) return f"โ
'{word}' ์ ์ฅ๋จ!", new_final, new_html return f"โ
'{word}' ์ ์ฅ๋จ!", "", "" try: with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False, indent=2) return f"โ
์ฑ๊ณต์ ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค: '{word}' -> '{pronun}'" except Exception as e: return f"โ ์ ์ฅ ์คํจ: {e}"
# 6. ํ์ผ ๋ค์ด๋ก๋def get_dict_file(): return dict_path if os.path.exists(dict_path) else None
# UI ๊ตฌ์ฑ# 7. UI ๊ตฌ์ฑ (ํญ ๋ถ๋ฆฌ ๋ฐ 3๋ฒํผ ๋ณต๊ตฌ)with gr.Blocks(theme="default") as iface: gr.Markdown("### ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ") with gr.Row(): with gr.Column(): input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ") char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ (๋ณํ๏ฟฝ๏ฟฝ๊ธฐ๋ฅผ ๋๋ฅด๋ฉด ๊ณ์ฐ๋ฉ๋๋ค)", value="์
๋ ฅ ๊ธ์ ์: 0์", interactive=False) with gr.Tabs(): # ์ฒซ ๋ฒ์งธ ํญ: ๋ณํ๊ธฐ with gr.Tab("๐๏ธ ๋ณํ๊ธฐ (๋๋ณธ ์์
)"): with gr.Row(): convert_btn = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary") clear_btn = gr.Button("๐๏ธ ๋๋ณธ ์ด๊ธฐํ", variant="stop") with gr.Column(): # show_copy_button=True ๋ฅผ ํตํด ์์ ํ๊ฒ ๊ธฐ๋ณธ ๋ณต์ฌ ๋ฒํผ ์ ๊ณต output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ
์คํธ (์ฐ์ธก ์๋จ ์์ด์ฝ ํด๋ฆญํ์ฌ ๋ณต์ฌ)", show_copy_button=True) output_html = gr.HTML(label="๐ ๋ณ๊ฒฝ์ ํ์ธ") with gr.Column(): input_text = gr.Textbox(lines=8, label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ", placeholder="์ด๊ณณ์ ์๋ณธ ๋๋ณธ์ ๋ถ์ฌ๋ฃ์ผ์ธ์.") char_count = gr.Textbox(label="๊ธ์ ์ (๋ณํํ๊ธฐ ํด๋ฆญ ์ ๊ณ์ฐ๋จ)", value="์
๋ ฅ ๊ธ์ ์: 0์", interactive=False) # ์์ฒญํ์ 3๊ฐ์ ๋ฒํผ ์๋ฒฝ ๋ณต๊ตฌ with gr.Row(): convert_btn = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary") copy_btn = gr.Button("๐ ๋ณํ๋ณต์ฌ", variant="secondary") clear_btn = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop") with gr.Column(): output_text = gr.Textbox(lines=8, label="2๏ธโฃ ๋ณํ๋ ํ
์คํธ ๊ฒฐ๊ณผ์ฐฝ") output_html = gr.HTML(label="๐ ๋ณ๊ฒฝ์ ํ์ธ (ํ๊ดํ)")
# ๋ฒํผ ํด๋ฆญ ์์๋ง ํจ์ ์คํ (์๋ฒ ๋ค์ด ๋ฐฉ์ง) convert_btn.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count]) clear_btn.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count]) gr.Markdown("---") gr.Markdown("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ (์ฌ์ ์ ์ด๊ธฐํํด๋ ์ง์์ง์ง ์์ต๋๋ค)") with gr.Row(): new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด") new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์") add_btn = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐ") dict_status = gr.Markdown("") add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_html]) dl_btn = gr.DownloadButton("๐ฅ JSON ์ฌ์ ๋ฐฑ์
(PC๋ก ๋ค์ด๋ก๋)") dl_btn.click(fn=get_dict_file, outputs=[dl_btn]) # ๋ณํ ์ด๋ฒคํธ convert_btn.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count]) # ์ด๊ธฐํ ์ด๋ฒคํธ clear_btn.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count]) # ํด๋ฆฝ๋ณด๋ ๋ณต์ฌ ์ด๋ฒคํธ (์์ ํ ์๋ฐ์คํฌ๋ฆฝํธ ์ฌ์ฉ) copy_btn.click( fn=lambda x: x, inputs=[output_text], outputs=[output_text], js="(text) => { if(text) { navigator.clipboard.writeText(text); alert('โ
๋ณํ๋ ํ
์คํธ๊ฐ ํด๋ฆฝ๋ณด๋์ ๋ณต์ฌ๋์์ต๋๋ค.'); } else { alert('๋ณต์ฌํ ๋ด์ฉ์ด ์์ต๋๋ค.'); } return text; }" )
# ๋ ๋ฒ์งธ ํญ: ์ฌ์ ๊ด๋ฆฌ with gr.Tab("๐ ์ฌ์ ๊ด๋ฆฌ (๋จ์ด ๋ฑ๋ก)"): gr.Markdown("์๋ฒ ์ฌ์์(Sleep) ์ ๋ฐ์ดํฐ๊ฐ ์ด๊ธฐํ๋ ์ ์์ผ๋ ๋ฑ๋ก ํ ํ๋จ์ ๋ฐฑ์
๋ค์ด๋ก๋๋ฅผ ๊ถ์ฅํฉ๋๋ค.") with gr.Row(): new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด (์: ์ฑ์งํผํฐ)", scale=1) new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์ (์: ์ฑ์ฐํผํฐ)", scale=1) add_btn = gr.Button("๐พ ์ฌ์ ์ ๋จ์ด ์ถ๊ฐ", variant="primary", scale=1) dict_status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun], outputs=[dict_status]) gr.Markdown("---") dl_btn = gr.DownloadButton("๐ฅ ์ต์ JSON ์ฌ์ ๋ฐฑ์
(PC๋ก ๋ค์ด๋ก๋)") dl_btn.click(fn=get_dict_file, outputs=[dl_btn])
if __name__ == "__main__": iface.launch()app.py+86-69