Spaces:
Running
Running
Download app.py from easyadaptor/VoiceScript-AI-II: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/easyadaptor/VoiceScript-AI-II/resolve/d1a8335a1d63a8b084e50cdeb16da8e2d45a200e/app.py
- Command line
-
hf download hf://spaces/easyadaptor/VoiceScript-AI-II@d1a8335a1d63a8b084e50cdeb16da8e2d45a200e/app.py
-
curl -L -o app.py https://huggingface.co/spaces/easyadaptor/VoiceScript-AI-II/resolve/d1a8335a1d63a8b084e50cdeb16da8e2d45a200e/app.py
4.43 kB
| import gradio as gr | |
| import json | |
| import os | |
| import difflib | |
| import shutil | |
| from g2pk import G2p | |
| print("G2P λ‘λ© μ€...") | |
| g2p = G2p() | |
| print("λ‘λ© μλ£") | |
| dict_path = "custom_dict.json" | |
| json_structure = {"items":[], "meta": {"count": 0}} | |
| flat_dict = {} | |
| 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] | |
| except: | |
| pass | |
| load_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 | |
| for w, p in flat_dict.items(): | |
| processed = processed.replace(w, p) | |
| try: | |
| final_text = g2p(processed) | |
| except Exception as e: | |
| return f"μλ¬: {e}", "μλ¬", 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: yellow;">{chunk}</span>') | |
| html_output = f'<div style="line-height:1.6; padding:10px; border:1px solid #ddd;">{"".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 "μ λ ₯ μ€λ₯" | |
| flat_dict[word] = pronun | |
| found = False | |
| for item in json_structure["items"]: | |
| if item[0] == word: | |
| item[1] = pronun | |
| found = True | |
| 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) | |
| return f"β '{word}' μ μ₯ μλ£!" | |
| def get_file(): return dict_path if os.path.exists(dict_path) else None | |
| def upload_file(f): | |
| if f: | |
| shutil.copy(f.name, dict_path) | |
| load_dict() | |
| return "볡ꡬ μλ£!" | |
| return "νμΌ μμ" | |
| # μμ ν λ³΅μ¬ ν¨μ | |
| def dummy_copy(text): | |
| return text | |
| with gr.Blocks() as iface: | |
| with gr.Tabs(): | |
| with gr.Tab("λ©μΈ νλ©΄"): | |
| 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.Row(): | |
| btn_conv = gr.Button("β¨ λ³ννκΈ°", variant="primary") | |
| btn_copy = gr.Button("π λ³ν볡μ¬") | |
| btn_clr = gr.Button("ποΈ μ΄κΈ°ν", variant="stop") | |
| with gr.Column(): | |
| output_text = gr.Textbox(lines=7, label="2οΈβ£ λ³νλ ν μ€νΈ") | |
| output_html = gr.HTML(label="π λ³κ²½μ ") | |
| btn_conv.click(process_text, inputs=[input_text], outputs=[output_text, output_html, char_count]) | |
| btn_clr.click(clear_all, outputs=[input_text, output_text, output_html, char_count]) | |
| btn_copy.click(dummy_copy, inputs=[output_text], outputs=[output_text], js="(t) => {navigator.clipboard.writeText(t); alert('λ³΅μ¬ μλ£!'); return t;}") | |
| with gr.Row(): | |
| nw = gr.Textbox(label="κΈ°μ‘΄ λ¨μ΄") | |
| np = gr.Textbox(label="μνλ λ°μ") | |
| btn_add = gr.Button("πΎ μΆκ°") | |
| status = gr.Textbox(label="μν") | |
| btn_add.click(add_dict, inputs=[nw, np], outputs=[status]) | |
| with gr.Tab("λ°±μ /볡ꡬ"): | |
| dl_btn = gr.DownloadButton("π₯ λ€μ΄λ‘λ (λ°±μ )") | |
| dl_btn.click(get_file, outputs=[dl_btn]) | |
| up_file = gr.File(label="μ λ‘λ (볡ꡬ)") | |
| up_stat = gr.Textbox(label="μν") | |
| up_file.upload(upload_file, inputs=[up_file], outputs=[up_stat]) | |
| iface.launch() |