Spaces:
Running
Running
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import gradio as grimport jsonimport osimport difflibfrom g2pk import G2p
# 1. G2p ์์ง ์ด๊ธฐํg2p = G2p()
# 2. ์ฌ์ ํ์ผ ๊ด๋ฆฌ ๋ฐ ๊ตฌ์กฐ ์ ์งdict_path = "custom_dict.json"
# ์๋ณธ JSON์ ๋ผ๋๋ฅผ ์ ์งํ๊ธฐ ์ํ ๋ณ์json_structure = { "items":[], "meta": { "app_title": "VoiceScript AI", "app_version": "146.0", "count": 0 }}flat_dict = {} # ์ค์ ๋ณํ์ ์ฌ์ฉํ 1:1 ๋งคํ ๋์
๋๋ฆฌ
def load_dict(): global json_structure, flat_dict if os.path.exists(dict_path): try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) if "items" in data: json_structure = data for item in data["items"]: if len(item) == 2: flat_dict[item[0]] = item[1] else: # ์์ ๋ฐฉ์์ ๋จ์ ๋์
๋๋ฆฌ์ผ ๊ฒฝ์ฐ ํธํ flat_dict = data json_structure["items"] = [[k, v] for k, v in data.items()] json_structure["meta"]["count"] = len(flat_dict) except Exception as e: print(f"โ ๏ธ JSON ์ฝ๊ธฐ ์๋ฌ: {e}")
load_dict()
# 3. ํ
์คํธ ๋ณํ ๋ฐ ํ์ด๋ผ์ดํธ ํจ์def process_text(input_text): if not input_text: return "",[], "๊ธ์ ์: 0์" char_count = f"์
๋ ฅ ๊ธ์ ์: {len(input_text)}์ (๊ณต๋ฐฑ ํฌํจ)" processed_text = input_text # 1์์: ์ฌ์ฉ์ ์ฌ์ ์ ์๋ ๋จ์ด ์ฐ์ ์นํ for word, pronunciation in flat_dict.items(): processed_text = processed_text.replace(word, pronunciation) # 2์์: G2P ์์ง์ผ๋ก ๋๋จธ์ง ๋ณํ try: final_text = g2p(processed_text) except Exception as e: final_text = f"๋ณํ ์ค ์ค๋ฅ ๋ฐ์: {e}"
# ๋ณ๊ฒฝ๋ ๋ถ๋ถ ํ๊ดํ ์ฒ๋ฆฌ seq = difflib.SequenceMatcher(None, input_text, final_text) highlights =[] for tag, i1, i2, j1, j2 in seq.get_opcodes(): if tag == 'equal': highlights.append((final_text[j1:j2], None)) elif tag in['replace', 'insert']: highlights.append((final_text[j1:j2], "๋ณ๊ฒฝ๋จ")) return final_text, highlights, char_count
# 4. ์ค์๊ฐ ์ฌ์ ์ถ๊ฐ ๋ฐ JSON ์๋ณธ ๊ตฌ์กฐ ์ ์ง ์ ์ฅdef add_to_dict(word, pronun, current_input): if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์.", gr.update(), gr.update() # 1. 1:1 ๋งคํ ๋์
๋๋ฆฌ ์
๋ฐ์ดํธ flat_dict[word] = pronun # 2. ์๋ณธ JSON ๊ตฌ์กฐ(items ๋ฐฐ์ด)์ ์ถ๊ฐ # ์ด๋ฏธ ์กด์ฌํ๋ ๋จ์ด์ธ์ง ํ์ธ ํ ๋ฎ์ด์ฐ๊ธฐ ๋๋ ์๋ก ์ถ๊ฐ found = False for item in json_structure["items"]: if item[0] == word: item[1] = pronun found = True break if not found: json_structure["items"].append([word, pronun]) json_structure["meta"]["count"] = len(json_structure["items"]) # 3. ํ์ผ๋ก ์ ์ฅ with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False, indent=2) # 4. ๋ฑ๋ก ํ ํ์ฌ ํ
์คํธ ์ฆ์ ์ฌ๋ณํ new_final_text, new_highlights, _ = process_text(current_input) return f"โ
'{word}' -> '{pronun}' ์ ์ฅ ์๋ฃ!", new_final_text, new_highlights
# ๋ค์ด๋ก๋ ๋ฒํผ์ฉ ํจ์def get_dict_file(): return dict_path if os.path.exists(dict_path) else None
# 5. UI ํ๋ฉด ๊ตฌ์ฑwith gr.Blocks(theme="default", title="๋๋ง์ TTS ๋ณํ๊ธฐ") as iface: gr.Markdown("# ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (PRO ๋ฒ์ )") gr.Markdown("์๋ณธ ๋๋ณธ์ ์์ ํ์ง ์๊ณ ์๋ฆฌ ๋๋ ๋๋ก๋ง ๋ณํํฉ๋๋ค. ์ฐ์ธก ์๋จ ์์ด์ฝ์ ๋๋ฌ ๋ณํ๋ ํ
์คํธ๋ฅผ ๋ณต์ฌํ์ธ์.") with gr.Row(): with gr.Column(): input_text = gr.Textbox(lines=7, label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ", placeholder="์ฌ๊ธฐ์ ์๋ณธ ๋๋ณธ์ ์
๋ ฅํ์ธ์...") char_count_display = gr.Markdown("๊ธ์ ์: 0์") convert_btn = gr.Button("โจ ์๋ฆฌ๋๋ ๋๋ก ๋ณํํ๊ธฐ", variant="primary") with gr.Column(): output_text = gr.Textbox(lines=7, label="2๏ธโฃ ๋ณํ๋ ํ
์คํธ (๋ณต์ฌ์ฉ)", show_copy_button=True) output_highlight = gr.HighlightedText(label="๐ ๋ณ๊ฒฝ์ ํ์ธ (๋
ธ๋์ ํ๊ดํ)", color_map={"๋ณ๊ฒฝ๋จ": "yellow"})
input_text.change(fn=lambda x: f"์
๋ ฅ ๊ธ์ ์: {len(x)}์ (๊ณต๋ฐฑ ํฌํจ)", inputs=input_text, outputs=char_count_display) convert_btn.click(fn=process_text, inputs=input_text, outputs=[output_text, output_highlight, char_count_display]) gr.Markdown("---") gr.Markdown("### ๐ ๏ธ ๋จ์ด/๋ฌธ์ฅ ์ฆ์ ๋ฑ๋ก ๋ฐ ์ฌ๋ณํ (์๋ณธ JSON ๊ตฌ์กฐ ์ ์ง)") with gr.Row(): new_word = gr.Textbox(label="๊ธฐ์กด ๋จ์ด (์: ์ฑ์งํผํฐ)", scale=1) new_pronun = gr.Textbox(label="์ํ๋ ๋ฐ์ (์: ์ฑ์ฐํผํฐ)", scale=1) add_btn = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐํ๊ณ ์ฆ์ ์ฌ๋ณํ", variant="secondary", scale=1) dict_status = gr.Markdown("") add_btn.click(fn=add_to_dict, inputs=[new_word, new_pronun, input_text], outputs=[dict_status, output_text, output_highlight])
gr.Markdown("---") download_btn = gr.DownloadButton("๐ฅ ํ์ฌ JSON ์ฌ์ ์์ ํ๊ฒ PC๋ก ๋ค์ด๋ก๋") download_btn.click(fn=get_dict_file, outputs=download_btn)
if __name__ == "__main__": iface.launch()app.pyAdded+138