Spaces:
Running
Running
Download app.py from easyadaptor/VoiceScript-AI-II: direct link, hf CLI and curl.
- Browser
- Download file 5.89 kB
-
https://huggingface.co/spaces/easyadaptor/VoiceScript-AI-II/resolve/fcfdebdb0546999899208b4eb23fd3fde9e7cce8/app.py
- Command line
-
hf download hf://spaces/easyadaptor/VoiceScript-AI-II@fcfdebdb0546999899208b4eb23fd3fde9e7cce8/app.py
-
curl -L -o app.py https://huggingface.co/spaces/easyadaptor/VoiceScript-AI-II/resolve/fcfdebdb0546999899208b4eb23fd3fde9e7cce8/app.py
5.89 kB
| import gradio as gr | |
| import json | |
| import os | |
| import difflib | |
| import shutil | |
| try: | |
| from g2pk import G2p | |
| except ImportError: | |
| G2p = None | |
| print("G2P λͺ¨λΈ λ‘λ© μ€...") | |
| try: | |
| if G2p: | |
| g2p = G2p() | |
| else: | |
| g2p = None | |
| except Exception as e: | |
| print(f"G2P λ‘λ© μ€ν¨: {e}") | |
| g2p = None | |
| 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: | |
| 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 process_text(input_text): | |
| if not input_text or not input_text.strip(): | |
| return "", "", "μ λ ₯ κΈμ μ: 0μ" | |
| char_count = f"μ λ ₯ κΈμ μ: {len(input_text)}μ" | |
| processed = input_text | |
| # 1. μ¬μ©μ μ§μ μ¬μ μΉν | |
| flat_dict = load_dict() | |
| for word, pronun in flat_dict.items(): | |
| processed = processed.replace(word, pronun) | |
| final_text = processed | |
| # 2. G2P μλ λ°μ λ³ν μ μ© | |
| if g2p is not None: | |
| try: | |
| final_text = g2p(processed) | |
| except Exception as e: | |
| print(f"G2P μλ¬: {e}") | |
| # 3. νκ΄ν ν¨κ³Ό (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:5px; background-color:#f9f9f9;">{"".join(html_parts)}</div>' | |
| return final_text, html_output, char_count | |
| def clear_all(): | |
| return "", "", "", "μ λ ₯ κΈμ μ: 0μ" | |
| def add_dict(word, pronun): | |
| if not word or not pronun: | |
| return "β οΈ λ¨μ΄/λ¬Έμ₯κ³Ό λ°μμ λͺ¨λ μ λ ₯νμΈμ." | |
| try: | |
| with open(dict_path, "r", encoding="utf-8") as f: | |
| j_struct = json.load(f) | |
| except: | |
| j_struct = {"items":[], "meta": {"count": 0}} | |
| found = False | |
| for item in j_struct.get("items",[]): | |
| if item[0] == word: | |
| item[1] = pronun | |
| found = True | |
| break | |
| if not found: | |
| j_struct["items"].append([word, pronun]) | |
| try: | |
| with open(dict_path, "w", encoding="utf-8") as f: | |
| json.dump(j_struct, f, ensure_ascii=False, indent=2) | |
| return f"β '{word}' -> '{pronun}' μ μ₯ μλ£!" | |
| except Exception as e: | |
| return f"β μ μ₯ μ€ν¨: {e}" | |
| def get_file(): | |
| return dict_path if os.path.exists(dict_path) else None | |
| def 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("ποΈ λ©μΈ νλ©΄"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox(lines=8, label="1οΈβ£ μλ³Έ ν μ€νΈ μ λ ₯") | |
| char_count = gr.Textbox(label="κΈμ μ νμΈ", value="μ λ ₯ κΈμ μ: 0μ", interactive=False) | |
| with gr.Row(): | |
| btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary") | |
| btn_clr = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") | |
| with gr.Column(): | |
| # μ°μΈ‘ μλ¨ λ³΅μ¬ λ²νΌ νμ±ν | |
| output_text = gr.Textbox(lines=8, label="2οΈβ£ λ³νλ ν μ€νΈ (μ°μΈ‘ μλ¨ π μμ΄μ½ ν΄λ¦ μ 볡μ¬)", show_copy_button=True) | |
| output_html = gr.HTML(label="π λ³κ²½μ νμΈ") | |
| # api_name=False μμ± μΆκ°λ‘ λ¨Ήν΅ λ²κ·Έ μλ²½ μ°¨λ¨! | |
| btn_conv.click(fn=process_text, inputs=[input_text], outputs=[output_text, output_html, char_count], api_name=False) | |
| btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], api_name=False) | |
| gr.Markdown("---") | |
| with gr.Row(): | |
| nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄ λλ λ¬Έμ₯") | |
| np = gr.Textbox(label="μνλ λ°μ") | |
| btn_add = gr.Button("πΎ μ¬μ μ μΆκ°", variant="primary") | |
| status = gr.Textbox(label="μ μ₯ μν κ²°κ³Όμ°½", interactive=False) | |
| btn_add.click(fn=add_dict, inputs=[nw, np], outputs=[status], api_name=False) | |
| with gr.Tab("π₯ λ°±μ /볡ꡬ"): | |
| dl_btn = gr.DownloadButton("π₯ 1. νμ¬ μ¬μ λ°±μ νκΈ° (PC λ€μ΄λ‘λ)") | |
| dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) | |
| gr.Markdown("---") | |
| up_file = gr.File(label="π€ 2. λ°±μ νμΌ λ³΅κ΅¬νκΈ° (μ λ‘λ)", type="filepath") | |
| up_stat = gr.Textbox(label="볡ꡬ μν κ²°κ³Όμ°½", interactive=False) | |
| up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat], api_name=False) | |
| if __name__ == "__main__": | |
| iface.launch() | |