Spaces:
Running
Running
Update app.py
Browse files4 unmodified lines56789891011131215131415167 unmodified lines242526293027282930313432333435394036373842433940414246434448454647482 unmodified lines515253575455595657586259606166676862636970646566676869707771727374828375767785868778798990919293948081828384858696979899100101102103104105106107108878889901101119111211392115116939495119120121969798123124125126991001011021281291031041051311321331061071081091351361101381391401411421111121131141151161441451461471481491501511521531541171181191201211221231561571244 unmodified linesimport shutilfrom g2pk import G2p
# 1. G2P λͺ¨λΈ λ‘λ©print("G2P λͺ¨λΈ λ‘λ© μ€...")print("G2P λ‘λ© μ€...")g2p = G2p()print("λ‘λ© μλ£")
# 2. μ¬μ κ΄λ¦¬ λ° λ‘λdict_path = "custom_dict.json"json_structure = {"items":[], "meta": {"app_title": "VoiceScript AI", "app_version": "146.0", "count": 0}}json_structure = {"items":[], "meta": {"count": 0}}flat_dict = {}
def load_dict():7 unmodified lines for item in data["items"]: if len(item) == 2: flat_dict[item[0]] = item[1] except Exception as e: print(f"μ¬μ μ½κΈ° μλ¬: {e}") except: pass
load_dict()
# 3. ν
μ€νΈ λ³νdef process_text(input_text): if not input_text or not input_text.strip(): return "", "", "μ
λ ₯ κΈμ μ: 0μ" char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ (곡백 ν¬ν¨)" processed_text = input_text char_count = f"μ
λ ₯ κΈμ μ: {len(input_text)}μ" processed = input_text for word, pronun in flat_dict.items(): processed_text = processed_text.replace(word, pronun) for w, p in flat_dict.items(): processed = processed.replace(w, p) try: final_text = g2p(processed_text) final_text = g2p(processed) except Exception as e: return f"λ³ν μ€ν¨: {e}", "μλ¬ λ°μ", char_count return f"μλ¬: {e}", "μλ¬", char_count
seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts =[]2 unmodified lines 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;">{chunk}</span>') html_parts.append(f'<span style="background-color: yellow;">{chunk}</span>') html_output = f'<div style="line-height: 1.6; font-size: 16px; padding: 10px; border: 1px solid #ddd; border-radius: 8px;">{"".join(html_parts)}</div>' 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
# 4. μ΄κΈ°νdef clear_all(): return "", "", "", "μ
λ ₯ κΈμ μ: 0μ"
# 5. μ¬μ μΆκ° (ν νλ©΄μμ)def add_to_dict(word, pronun): if not word or not pronun:def add_dict(word, pronun): if not word or not pronun: return "μ
λ ₯ μ€λ₯" return "β οΈ λ¨μ΄μ λ°μμ λͺ¨λ μ
λ ₯νμΈμ." flat_dict[word] = pronun 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]) with open(dict_path, "w", encoding="utf-8") as f: json.dump(json_structure, f, ensure_ascii=False, indent=2) return f"β
'{word}' -> '{pronun}' μ μ₯ μλ£!" json.dump(json_structure, f, ensure_ascii=False) return f"β
'{word}' μ μ₯ μλ£!"
# 6. λ°±μ
λ° λ³΅κ΅¬ κΈ°λ₯def get_dict_file(): return dict_path if os.path.exists(dict_path) else Nonedef get_file(): return dict_path if os.path.exists(dict_path) else None
def upload_dict_file(file_obj): if file_obj is None: return "νμΌμ΄ μμ΅λλ€." shutil.copy(file_obj.name, dict_path) load_dict() return "β
λ°±μ
νμΌ λ³΅κ΅¬(μ
λ‘λ)κ° μλ£λμμ΅λλ€! μ΄μ μ¬μ μ μ μμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€."def upload_file(f): if f: shutil.copy(f.name, dict_path) load_dict() return "볡ꡬ μλ£!" return "νμΌ μμ"
# μμ ν λ³΅μ¬ μλ°μ€ν¬λ¦½νΈcopy_js = """async (text) => { if (!text) { alert("볡μ¬ν λ΄μ©μ΄ μμ΅λλ€."); return text; } try { await navigator.clipboard.writeText(text); alert("β
ν
μ€νΈκ° 볡μ¬λμμ΅λλ€!"); } catch (err) { alert("λ³΅μ¬ μ€ν¨!"); } return text;}"""# μμ ν λ³΅μ¬ ν¨μdef dummy_copy(text): return text
# UI ꡬμ±with gr.Blocks(theme="default") as iface:with gr.Blocks() as iface: gr.Markdown("### ποΈ λλ§μ TTS μ μ²λ¦¬ λ³νκΈ°") with gr.Tabs(): # 첫 λ²μ§Έ ν: λ©μΈ μμ
κ³΅κ° with gr.Tab("λ©μΈ νλ©΄ (λ³ν λ° μ¬μ λ±λ‘)"): 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) input_text = gr.Textbox(lines=7, label="1οΈβ£ μλ³Έ ν
μ€νΈ") char_count = gr.Textbox(label="κΈμ μ", value="0μ", interactive=False) with gr.Row(): convert_btn = gr.Button("β¨ λ³ννκΈ°", variant="primary") copy_btn = gr.Button("π λ³ν볡μ¬", variant="secondary") clear_btn = gr.Button("ποΈ λλ³Έ μ΄κΈ°ν", variant="stop") 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="π λ³κ²½μ νμΈ") output_text = gr.Textbox(lines=7, 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]) copy_btn.click(fn=None, inputs=[output_text], outputs=[output_text], js=copy_js) 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;}") gr.Markdown("---") gr.Markdown("#### π οΈ μ¬μ μ λ¨μ΄ μ¦μ λ±λ‘") 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]) 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("λ°±μ
λ° λ³΅κ΅¬ (μλ² λ¦¬μ€ννΈ λλΉ)"): gr.Markdown("νκΉ
νμ΄μ€ μλ²κ° λ©μ·λ€κ° μΌμ Έμ λ°μ΄ν°κ° λ μκ°μ λ, 보κ΄ν΄λ νμΌμ μ¬λ €μ 볡ꡬνμΈμ.") dl_btn = gr.DownloadButton("π₯ 1. νμ¬ μμ±λ μ¬μ PCλ‘ λ€μ΄λ‘λ (λ°±μ
νκΈ°)") dl_btn.click(fn=get_dict_file, outputs=[dl_btn]) gr.Markdown("---") gr.Markdown("π€ **2. λ°±μ
ν΄λ νμΌ μ
λ‘λνμ¬ μ¬μ μ 볡ꡬνκΈ°**") upload_file = gr.File(label="custom_dict.json νμΌ μ
λ‘λ") upload_status = gr.Textbox(label="볡ꡬ μν", interactive=False) upload_file.upload(fn=upload_dict_file, inputs=[upload_file], outputs=[upload_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])
if __name__ == "__main__": iface.launch()iface.launch()app.py+51-84