Spaces:
Running
Running
Update app.py
Browse files1 unmodified line2345567891011121314151617181920212223242526272829303132331011121314151617181920343536373839402341424344454647484950515253545556575859606162636465666711 unmodified lines7980814647488283848586878889909192939495969798991001011021031041051061075051525354555657585960616263108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155666768697071727374156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258782592602618081262263264265266267842682692702718788892722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023039697304305306307308309991001013103113123133143153163173183193201071081093213223233243253263273283292 unmodified lines3323333341183353363373381203393403413421243433443453463473483491303503513523531 unmodified line35535635735835913813914036036136236314214336436514514636636736814814915015115215315415515615715815916036937037137237337437537637737837916616716838038138238338438538617138738838939039139217639339439539639718118239839940040140218418540340440518718818919019119219319419519619719840640740840941041120120241241320441441541641741841920921042042142242342442542642742842943043121321443243343443543621621721821922022122243743843944044144244322544444544622744744822923023123244945023423523623723845145245345424124245545645745845924546046146224746346446546625025125225346746825525625725825946947047147247326347447547647747818 unmodified lines4974984992882895005015022 unmodified lines5055065072985085095101 unmodified line51251351430651551651719 unmodified lines5375385393325405415425435445455465475485495505515525535545555565575585595603343355615625635643385655665675685695703455715723485735743505755765775785795805813575825835845855865875883625895905915925935945953685965975985993726006013746026033766046056066071 unmodified line6096106113846126136146156163863876176186196206213906226233923936246256266276283976296306316324016336346356366374066386396406414106426436446456464166476486496506516524236531 unmodified lineimport jsonimport osimport difflibimport shutilimport reimport html
# =====================================================================# VoiceScript-AI v147 ์
๊ทธ๋ ์ด๋# - P0: ์ฌ์ ์๊ตฌ ์ ์ฅ (HF Dataset ์๋ ์ปค๋ฐ) + AUTO_MERGE ๋นํ๊ดดํ# - P1: ๋งค์นญ ์์ง ๊ต์ฒด (2๋จ๊ณ: ํ๊ธฐ ์ ๊ทํ -> ๋ฐ์ ์นํ, ๊ณต๋ฐฑ ๋ฌด์ ๋งค์นญ)# - G2P ์๋ ์ ํ: g2pk -> g2pk2 -> g2pkk# - Kiwi: "์ค๊ฐ ๋ง 2.0" ๋ชจ๋(ํ์ฌ ๊ธฐ๋ฐ ๋ณดํธ) + ๋์ด์ฐ๊ธฐ ๊ต์ ๋งค์นญ(์ต์
)# - TTS ๋ฏธ๋ฆฌ๋ฃ๊ธฐ ๋ฒํผ (๋ธ๋ผ์ฐ์ ๋ด์ฅ ์์ฑ)# * ์๋ฌธ์ ์ ๋ ์์ ํ์ง ์๊ณ ๋ณํ ์ถ๋ ฅ๋ง ์์ฑํ๋ค๋ ์์น ์ ์ง# =====================================================================
# ---------- G2P ๋ก๋ฉ (์๋ ์ ํ ์์ ์ฅ์น) ----------g2p = NoneG2P_BACKEND = "์์"for _mod in ("g2pk", "g2pk2", "g2pkk"): try: _m = __import__(_mod, fromlist=["G2p"]) g2p = _m.G2p() G2P_BACKEND = _mod print(f"G2P ๋ก๋ฉ ์๋ฃ: {_mod}") break except Exception as _e: print(f"G2P {_mod} ๋ก๋ฉ ์คํจ: {_e}")
# ---------- Kiwi ๋ก๋ฉ (์ ํ ๊ธฐ๋ฅ: ์ค๊ฐ ๋ง 2.0 / ๋์ด์ฐ๊ธฐ ๊ต์ ) ----------kiwi = Nonetry: from g2pk import G2pexcept ImportError: G2p = None
print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")try: if G2p: g2p = G2p() else: g2p = Noneexcept Exception as e: print(f"G2P ๋ก๋ฉ ์คํจ: {e}") g2p = None from kiwipiepy import Kiwi kiwi = Kiwi() print("Kiwi ๋ก๋ฉ ์๋ฃ")except Exception as _e: print(f"Kiwi ๋ก๋ฉ ์คํจ(์ ํ ๊ธฐ๋ฅ ๋นํ์ฑํ): {_e}")
dict_path = "custom_dict.json"
ADMIN_ID = os.environ.get("ADMIN_ID", "admin")ADMIN_PW = os.environ.get("ADMIN_PW", "admin1234")GUEST_ID = os.environ.get("GUEST_ID", "guest")GUEST_PW = os.environ.get("GUEST_PW", "guest1234")
# ---------- P0: HF Dataset ์๊ตฌ ์ ์ฅ์ ์ฐ๊ฒฐ ----------HF_TOKEN = os.environ.get("HF_TOKEN")DICT_REPO = os.environ.get("DICT_REPO", "").strip()hf_api = Nonedict_repo_id = NoneSYNC_STATE = "๋ก์ปฌ ์ ์ฉ (HF_TOKEN ๋ฏธ์ค์ - Space ์ฌ์์ ์ ๋ฑ๋ก๋ถ ์ ์ค ์ํ!)"try: from huggingface_hub import HfApi, hf_hub_download if HF_TOKEN: hf_api = HfApi(token=HF_TOKEN) dict_repo_id = DICT_REPO or (hf_api.whoami()["name"] + "/voicescript-dict") hf_api.create_repo(dict_repo_id, repo_type="dataset", private=True, exist_ok=True) SYNC_STATE = f"์๊ตฌ ์ ์ฅ์ ์ฐ๊ฒฐ๋จ: {dict_repo_id}"except Exception as _e: hf_api = None SYNC_STATE = f"์๊ตฌ ์ ์ฅ์ ์ฐ๊ฒฐ ์คํจ({_e}) - ๋ก์ปฌ ์ ์ฅ๋ง ๋์"print(f"[์ฌ์ ์ ์ฅ์] {SYNC_STATE}")
# ์ฌ์์ ์์๋ 'ํค๊ฐ ์์ ๋๋ง' ์ถ๊ฐ๋๋ ๊ธฐ๋ณธ ๋จ์ด (์ฌ์ฉ์ ๋ฑ๋ก๊ฐ์ ์ ๋ ๋ฎ์ด์ฐ์ง ์์)AUTO_MERGE_WORDS = [ ["GS๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS ๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS์ต", "์ง์์ค์ต"],["GS ์ต", "์ง์์ค์ต"], ["26SS", "์ด์ญ์ก ์์ค์์ค"],["26 SS", "์ด์ญ์ก ์์ค์์ค"],["26S/S", "์ด์ญ์ก ์์ค์์ค"],["26 S/S", "์ด์ญ์ก ์์ค์์ค"],11 unmodified lines ["๋ฐฐ์ก", "๋ฐฐ์ก"], ["ํ ์ธ", "ํ ์ธ"], ["์ด๋ฒคํธ", "์ด๋ฒคํธ"]]
def perform_auto_recovery_and_merge(): 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 _read_local_dict(): try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) return {i[0].strip(): i[1].strip() for i in data.get("items", []) if len(i) == 2} except Exception: return {}
def _write_local_dict(dict_map): """์์์ ์ ์ฅ: ์์ํ์ผ(.tmp) ์์ฑ -> ์ด์ ๋ฒ์ .bak ๋ฐฑ์
-> os.replace๋ก ๊ต์ฒด. ์ด๋ ๋จ๊ณ์์ ์คํจํด๋ ๊ธฐ์กด ์ปค๋ฐ๋ณธ์ด ์์๋์ง ์์.""" items = [[k, v] for k, v in dict_map.items()] payload = {"items": items, "meta": {"app_title": "VoiceScript AI", "app_version": "147.0", "count": len(items)}} tmp_path = dict_path + ".tmp" with open(tmp_path, "w", encoding="utf-8") as f: json.dump(payload, f, ensure_ascii=False, indent=2) if os.path.exists(dict_path): os.replace(dict_path, dict_path + ".bak") os.replace(tmp_path, dict_path)
def _push_remote(): """์ฌ์ ์ HF Dataset ์ ์ฅ์์ ์ปค๋ฐ (์๊ตฌ ๋ณด์กด). ์คํจํด๋ ๋ก์ปฌ ์ ์ฅ์ ์ ์ง.""" if not hf_api: return "๋ก์ปฌ ์ ์ฅ๋ง ๋จ (์ฌ์์ ์ ์ ์ค ๊ฐ๋ฅ - HF_TOKEN ์ค์ ํ์)" try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f) except: existing_data = {"items":[], "meta": {"count": 0}} dict_map = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2} for w in AUTO_MERGE_WORDS: dict_map[w[0]] = w[1] merged_items = [[k, v] for k, v in dict_map.items()] existing_data["items"] = merged_items existing_data["meta"]["count"] = len(merged_items) with open(dict_path, "w", encoding="utf-8") as f: json.dump(existing_data, f, ensure_ascii=False, indent=2)
perform_auto_recovery_and_merge() hf_api.upload_file(path_or_fileobj=dict_path, path_in_repo="custom_dict.json", repo_id=dict_repo_id, repo_type="dataset", commit_message="update pronunciation dict") return "์๊ตฌ ์ ์ฅ ์๋ฃ" except Exception as e: return f"์๊ฒฉ ์ ์ฅ ์คํจ({e}) - ๋ก์ปฌ๋ง ์ ์ฅ๋จ"
def save_dict(dict_map): """์ ์ฅ + ์๊ฒฉ ์ปค๋ฐ + ์์ง ์บ์ ๋ฌดํจํ. ์ํ ๋ฌธ์์ด ๋ฐํ.""" _write_local_dict(dict_map) _ENGINE_CACHE["sig"] = None msg = _push_remote() return f"{msg} ยท ํ์ฌ ์ฌ์ {len(dict_map)}๊ฐ"
def perform_startup_recovery(): """์์ ์: ๋ก์ปฌ(์ปค๋ฐ๋ณธ) <- ์๊ฒฉ(์ต์ ๋ฑ๋ก๋ถ) ํค ๋จ์ ๋ณํฉ. โ ์๊ฒฉ ํ์ผ์ด ์์ผ๋ฉด ์๊ฒฉ์ด ์ต์ -> ๋ก์ปฌ์ ๋ณํฉ(์ถฉ๋ ์ ์๊ฒฉ ์ฐ์ ). โก ๋ค์ด๋ก๋ ์์ธ(๋คํธ์ํฌ/์ธ์ฆ/์ผ์ ์ฅ์ )๋ฉด ๋ก์ปฌ ๋ณดํธ -> ์๊ฒฉ์ ์๋ฌด๊ฒ๋ pushํ์ง ์์. โข ๊ธฐ๋ณธ ๋จ์ด๋ 'ํค๊ฐ ์์ ๋๋ง' ์ถ๊ฐ (์ฌ์ฉ์ ๋ฑ๋ก๊ฐ ์ ๋ ๋ฎ์ด์ฐ์ง ์์).""" dict_map = _read_local_dict() remote_ok = True if hf_api: try: remote_path = hf_hub_download(repo_id=dict_repo_id, repo_type="dataset", filename="custom_dict.json", token=HF_TOKEN) with open(remote_path, "r", encoding="utf-8") as f: remote = json.load(f) for i in remote.get("items", []): if len(i) == 2: dict_map[i[0].strip()] = i[1].strip() print(f"[์ฌ์ ] ์๊ฒฉ ์ ์ฅ์์์ {len(remote.get('items', []))}๊ฐ ๋ณต์") except Exception as e: is_404 = getattr(getattr(e, "response", None), "status_code", None) == 404 if is_404: print("[์ฌ์ ] ์๊ฒฉ์ ์์ง ์ฌ์ ํ์ผ ์์ - ์ต์ด ๋ถํ
์ผ๋ก ์ฒ๋ฆฌ (๋ก์ปฌ ์ ์ง)") else: remote_ok = False print(f"[์ฌ์ ] ์๊ฒฉ ๋ณต์ ์คํจ({e}) - ๋ก์ปฌ ์ฌ์ ๋ณดํธ, ์๊ฒฉ ๋ฏธ๋ณ๊ฒฝ") # ๋นํ๊ดด ๋ณํฉ: ํค๊ฐ ์์ ๋๋ง ์ถ๊ฐ (์ฌ์ฉ์ ๊ฐ ๋ณด์กด) for w in AUTO_MERGE_WORDS: dict_map.setdefault(w[0], w[1]) _write_local_dict(dict_map) if hf_api and remote_ok: _push_remote()
perform_startup_recovery()
def load_dict(user_temp_dict=None): flat_dict = {} try: if os.path.exists(dict_path): 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].strip()] = item[1].strip() except: pass if user_temp_dict: flat_dict.update(user_temp_dict) flat_dict = _read_local_dict() if user_temp_dict: flat_dict.update({k.strip(): v.strip() for k, v in user_temp_dict.items()}) return flat_dict
# ---------- P1: 2๋จ๊ณ ๋งค์นญ ์์ง (ํ๊ธฐ ์ ๊ทํ -> ๋ฐ์ ์นํ, ๊ณต๋ฐฑ ๋ฌด์) ----------_ENGINE_CACHE = {"sig": None}
def _strip_np(s): return re.sub(r'[\s,\.ยท]', '', s)
def _build_pattern(keys): """ํค์ ๊ณต๋ฐฑ์ \\s* ๋ก ๋ฐ๊พผ ์ ์ฐ ํจํด. (์ปดํ์ผ๋ ํจํด, ๊ทธ๋ฃน๋ฒํธ->ํค ๋ชฉ๋ก) ๋ฐํ.""" ordered = sorted(keys, key=lambda x: len(x.replace(" ", "")), reverse=True) if not ordered: return None, [] pats = [r'\s*'.join(map(re.escape, k.split(" "))) for k in ordered] return re.compile("|".join(f"({p})" for p in pats)), ordered
def get_engine(flat_dict): sig = hash(tuple(sorted(flat_dict.items()))) if _ENGINE_CACHE.get("sig") == sig: return _ENGINE_CACHE phase1, phase2, shields = {}, {}, set() for k, v in flat_dict.items(): if not k: continue if k == v: shields.add(k) # ๋ฑ๋ก๊ฐ ๊ทธ๋๋ก ๋ณดํธ(g2p ์ฐจ๋จ)๋ง elif _strip_np(k) == _strip_np(v): phase1[k] = v # ๋์ด์ฐ๊ธฐยท๋ฌธ์ฅ๋ถํธ๋ง ๋ค๋ฅธ ํ๊ธฐ ์ ๊ทํ shields.add(v) # ์ ๊ทํ ๊ฒฐ๊ณผ๋ g2p๋ก๋ถํฐ ๋ณดํธ else: phase2[k] = v # ๋ฐ์ ์นํ p1_pat, p1_keys = _build_pattern(phase1.keys()) p2_pat, p2_keys = _build_pattern(phase2.keys()) sh_pat, _ = _build_pattern(shields) _ENGINE_CACHE.update({"sig": sig, "phase1": phase1, "phase2": phase2, "p1_pat": p1_pat, "p1_keys": p1_keys, "p2_pat": p2_pat, "p2_keys": p2_keys, "sh_pat": sh_pat}) return _ENGINE_CACHE
def _sub_phase1(text, eng): if not eng["p1_pat"]: return text return eng["p1_pat"].sub(lambda m: eng["phase1"][eng["p1_keys"][m.lastindex - 1]], text)
def _segments_phase2(text, eng): """๋ฐ์ ๊ท์น ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ [(์นํ๋จ?, ํ
์คํธ)] ์ธ๊ทธ๋จผํธ๋ก ๋ฐํ. ์นํ ๊ตฌ๊ฐ์ ์ดํ g2p๋ก๋ถํฐ ๋ณดํธ.""" if not eng["p2_pat"] or not text: return [(False, text)] if text else [] segs, last = [], 0 for m in eng["p2_pat"].finditer(text): s, e = m.span() if s > last: segs.append((False, text[last:s])) segs.append((True, eng["phase2"][eng["p2_keys"][m.lastindex - 1]])) last = e if last < len(text): segs.append((False, text[last:])) return segs
def _g2p_shielded(chunk, mode, eng): """์ฌ์ ์ด ๋ง๋ค์ด๋ธ ํ๊ธฐ(์ ๊ทํ ๊ฒฐ๊ณผยท๋ณดํธ ๋จ์ด)๋ g2p๋ฅผ ๊ฑด๋๋ฐ๊ณ ๋๋จธ์ง๋ง ๋ณํ.""" if not chunk: return chunk if not eng["sh_pat"]: return apply_g2p(chunk, mode) res, last = [], 0 for m in eng["sh_pat"].finditer(chunk): s, e = m.span() if s > last: res.append(apply_g2p(chunk[last:s], mode)) res.append(m.group()) last = e res.append(apply_g2p(chunk[last:], mode)) return "".join(res)
def convert_text(text, mode, flat_dict, spacing_fix=False): """์ ์ฒด ๋ณํ ํ์ดํ๋ผ์ธ: (์ต์
Kiwi ๋์ด์ฐ๊ธฐ ๊ต์ ) -> ํ๊ธฐ ์ ๊ทํ -> ๋ฐ์ ์นํ -> ๋จ์ ๊ตฌ๊ฐ์ ์ซ์ ์ ๊ท์ -> ๋ฐ์ ์นํ 2์ฐจ -> g2p(๋ณดํธ ๊ตฌ๊ฐ ์ ์ธ)""" if spacing_fix and kiwi is not None: try: text = kiwi.space(text) except Exception: pass eng = get_engine(flat_dict) t = _sub_phase1(text, eng) out = [] for fixed, chunk in _segments_phase2(t, eng): if fixed: out.append(chunk) continue chunk = apply_smart_regex(chunk) # ์ซ์ ๋ณํ ๊ฒฐ๊ณผ์ ๋ฐ์ ๊ท์น์ ํ ๋ฒ ๋ (์: 100% -> ๋ฐฑํ๋ก -> ๋ฐฑํผ์ผํธ) for fixed2, c2 in _segments_phase2(chunk, eng): out.append(c2 if fixed2 else _g2p_shielded(c2, mode, eng)) return "".join(out)
# ---------- G2P ๋ชจ๋ ----------_KIWI_PROTECT_TAGS = ("NNP", "SL", "SH", "SN", "SW") # ๊ณ ์ ๋ช
์ฌยท์ธ๋์ดยทํ์ยท์ซ์ยท๊ธฐํธ
def apply_g2p(text, mode): if g2p is None or mode.startswith("๋๊ธฐ"): return text if g2p is None or mode.startswith("๋๊ธฐ"): return text if mode.startswith("๋งค์ด ๋ง"): try: return g2p(text) except: return text try: return g2p(text) except Exception: return text if mode.startswith("์ํ ๋ง"): words = re.split(r'(\s+)', text) res =[] res = [] for w in words: if w.strip(): try: try: res.append(g2p(w)) except: res.append(w) else: res.append(w) res.append(g2p(w)) except Exception: res.append(w) else: res.append(w) return "".join(res) if mode.startswith("์ค๊ฐ ๋ง 2.0"): if kiwi is None: mode = "์ค๊ฐ ๋ง" # Kiwi ์์ผ๋ฉด ๊ธฐ์กด ์ค๊ฐ ๋ง์ผ๋ก ํด๋ฐฑ else: res = [] for w in re.split(r'(\s+)', text): if not w.strip(): res.append(w) continue if not re.match(r'^[๊ฐ-ํฃ]+[.,!?]*$', w): res.append(w) # ์๋ฌธยท์ซ์ ์์ธ ์ด์ ์ ์ํ ๋ณดํธ continue try: toks = kiwi.tokenize(w) if any(tk.tag in _KIWI_PROTECT_TAGS for tk in toks): res.append(w) # ๊ณ ์ ๋ช
์ฌ ๋ฑ ๋ณดํธ else: res.append(g2p(w)) except Exception: res.append(w) return "".join(res) if mode.startswith("์ค๊ฐ ๋ง"): tokens = re.split(r'([๊ฐ-ํฃ]+|[a-zA-Z]+|\d+|[^\w\s]+|\s+)', text) particles = {'์ด', '๊ฐ', '์', '๋ฅผ', '์', '๋', '์', '๋ก', '์ผ๋ก', '๊ณ ', '๋ค', '์ด', '์', '๋ค', '์
๋๋ค', '์ต๋๋ค'} res = [] for t in tokens: if not t: continue if t.isspace() or t in particles or len(t) == 1: res.append(t) if not t: continue if t.isspace() or t in particles or len(t) == 1: res.append(t) elif re.match(r'^[๊ฐ-ํฃ]{2,}$', t): try: try: res.append(g2p(t)) except: res.append(t) else: res.append(t) res.append(g2p(t)) except Exception: res.append(t) else: res.append(t) return "".join(res) return text
# ---------- ์ซ์/๋จ์ ์ค๋งํธ ์ ๊ท์ (๊ธฐ์กด ์ ์ง) ----------def number_to_korean(num_str): num_str = num_str.replace(',', '') try: n = int(num_str) except: return num_str if n == 0: return "์" try: n = int(num_str) except Exception: return num_str if n == 0: return "์" digits, units, large_units = ["", "์ผ", "์ด", "์ผ", "์ฌ", "์ค", "์ก", "์น ", "ํ", "๊ตฌ"], ["", "์ญ", "๋ฐฑ", "์ฒ"], ["", "๋ง", "์ต", "์กฐ"] res, str_n = [], str(n)[::-1] for i in range(0, len(str_n), 4):2 unmodified lines d = int(char) if d > 0: word = digits[d] if d > 1 or j == 0 else "" if d == 1 and j == 0 and len(chunk) == 1 and i == 0: word = "์ผ" if d == 1 and j == 0 and len(chunk) == 1 and i == 0: word = "์ผ" chunk_res.append(word + units[j]) if chunk_res: if chunk_res: res.append("".join(chunk_res[::-1]) + large_units[i//4]) res.append("".join(chunk_res[::-1]) + large_units[i//4]) return "".join(res[::-1]) or num_str
def apply_smart_regex(text): if not text: return text if not text: return text text = re.sub(r'\b1\s*๋ง\s*์\s*๋\b', "๋ง์๋", text) text = re.sub(r'\b1\s*๋ง์\s*๋\b', "๋ง์๋", text) text = re.sub(r'\b1\s*๋ง์๋\b', "๋ง์๋", text) text = re.sub(r'(\d+)\s*๊ฐ์\b', lambda m: f"{number_to_korean(m.group(1))}๊ฐ์", text) text = re.sub(r'(\d+)\s*(%|ํ๋ก)\b', lambda m: f"{number_to_korean(m.group(1))}ํ๋ก", text) size_dict = {"55":"์ค์ค", "66":"์ต์ก", "77":"์น ์น ", "88":"ํํ"} size_dict = {"55": "์ค์ค", "66": "์ต์ก", "77": "์น ์น ", "88": "ํํ"} text = re.sub(r'\b(55|66|77|88)\s*์ฌ์ด์ฆ\b', lambda m: f"{size_dict[m.group(1)]} ์ธ์ด์ฆ", text) text = re.sub(r'(\d{1,3}(?:,\d{3})+|\d+)\s*๋ง\s*์\s*๋\b', lambda m: f"{number_to_korean(m.group(1))}๋ง์๋", text) text = re.sub(r'(\d{1,3}(?:,\d{3})+|\d+)\s*์\b', lambda m: f"{number_to_korean(m.group(1))} ์", text)1 unmodified line text = re.sub(r'(\d+)\s*(mm|cm|m|km|g|kg|ml|L)\b', lambda m: f"{number_to_korean(m.group(1))} {unit_dict[m.group(2).lower() if m.group(2).lower() != 'l' else 'L']}", text, flags=re.IGNORECASE) return text
# ---------- ๋ฉ์ธ ๋ณํ ----------def process_text(input_text, ai_mode, auto_break, smart_pause, spacing_fix, font_size, user_temp_dict):def process_text(input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict): if not input_text or not input_text.strip(): return "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." clean_text = input_text.replace('\r', '') if not input_text or not input_text.strip(): return "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." clean_text = input_text.replace('\r', '') char_count = f"๊ณต๋ฐฑ ํฌํจ: {len(clean_text)}์ / ๊ณต๋ฐฑ ์ ์ธ: {len(clean_text.replace(' ', '').replace(chr(10), ''))}์" text_for_processing = apply_smart_regex(input_text)
flat_dict = load_dict(user_temp_dict) if not flat_dict: final_text = apply_g2p(text_for_processing, ai_mode) if not flat_dict: final_text = apply_g2p(apply_smart_regex(input_text), ai_mode) else: sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True) pattern = re.compile("|".join(map(re.escape, sorted_keys))) parts, last_end = [], 0 for match in pattern.finditer(text_for_processing): start, end = match.span() if chunk := text_for_processing[last_end:start]: parts.append(apply_g2p(chunk, ai_mode)) parts.append(flat_dict[match.group()]) last_end = end if tail := text_for_processing[last_end:]: parts.append(apply_g2p(tail, ai_mode)) final_text = "".join(parts)
if smart_pause: final_text = re.sub(r',\s*', ', / ', final_text) if auto_break: final_text = final_text.replace(". ", ".\n\n").replace("? ", "?\n\n") final_text = convert_text(input_text, ai_mode, flat_dict, spacing_fix)
if smart_pause: final_text = re.sub(r',\s*', ', / ', final_text) if auto_break: final_text = final_text.replace(". ", ".\n\n").replace("? ", "?\n\n")
seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts = [] for tag, i1, i2, j1, j2 in seq.get_opcodes(): chunk = html.escape(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;">{chunk}</span>') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold;">{chunk}</span>')
raw_html = re.sub(r'([^\s<]*@@[^\s<]*)', r'<span style="background-color: #ff4d4d; color: white;">\1</span>', "".join(html_parts)) html_output = f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:{font_size}px;">{raw_html}</div>'
markers = re.findall(r'[^\s]*@@[^\s]*', input_text) unique_markers = list(dict.fromkeys([m.strip('.,!?\n') for m in markers if m.strip('.,!?\n')])) return final_text, html_output, char_count, " / ".join(unique_markers) if unique_markers else "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
def clear_all():def clear_all(): return "", "", '<div id="prompter_output_div" style="font-size:22px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." return "", "", '<div id="prompter_output_div" style="font-size:22px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
def get_file(): return dict_path if os.path.exists(dict_path) else None
def add_and_update(word, pronun, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict): if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dictdef add_and_update(word, pronun, user_role, current_input, ai_mode, auto_break, smart_pause, spacing_fix, font_size, user_temp_dict): if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict word, pronun = word.strip(), pronun.strip() if len(word) == 1: if len(word) == 1: return f"๐ซ [๋ฑ๋ก ์คํจ] 1๊ธ์๋ ๋ฑ๋กํ ์ ์์ต๋๋ค.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict return "๐ซ [๋ฑ๋ก ์คํจ] 1๊ธ์๋ ๋ฑ๋กํ ์ ์์ต๋๋ค.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict
if user_role == "admin": 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].strip() == word: item[1] = pronun; found = True; break if not found: j_struct["items"].append([word, pronun]) try: j_struct["meta"]["count"] = len(j_struct["items"]) with open(dict_path, "w", encoding="utf-8") as f: json.dump(j_struct, f, ensure_ascii=False, indent=2) status_msg = f"๐ [์ด๋๋ฏผ] '{word}' -> '{pronun}' ์๋ฒ ์๊ตฌ ์ ์ฅ ์๋ฃ!" except Exception as e: status_msg = f"โ ์ ์ฅ ์คํจ: {e}" dict_map = _read_local_dict() dict_map[word] = pronun sync_msg = save_dict(dict_map) status_msg = f"๐ [์ด๋๋ฏผ] '{word}' -> '{pronun}' ์ ์ฅ! ({sync_msg})" else: user_temp_dict[word] = pronun status_msg = f"๐ค [๊ฒ์คํธ] '{word}' -> '{pronun}' ์์ ์ ์ฅ ์๋ฃ! (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ)"
status_msg = f"๐ค [๊ฒ์คํธ] '{word}' -> '{pronun}' ์์ ์ ์ฅ ์๋ฃ! (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ - ์๊ตฌ ๋ฑ๋ก์ ์ด๋๋ฏผ์๊ฒ ์์ฒญ)" if current_input.strip(): ft, ho, cc, mr = process_text(current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict) ft, ho, cc, mr = process_text(current_input, ai_mode, auto_break, smart_pause, spacing_fix, font_size, user_temp_dict) return status_msg, ft, ho, cc, mr, user_temp_dict return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict
def load_current_json_to_text(user_temp_dict): try: try: return json.dumps({"items": load_dict(user_temp_dict)}, ensure_ascii=False, indent=4), "โ
์ฌ์ ์ ๋ถ๋ฌ์์ต๋๋ค!" except Exception as e: return "", f"โ ์คํจ: {e}" return json.dumps({"items": load_dict(user_temp_dict)}, ensure_ascii=False, indent=4), "โ
์ฌ์ ์ ๋ถ๋ฌ์์ต๋๋ค!" except Exception as e: return "", f"โ ์คํจ: {e}"
def _parse_items(json_string): new_data = json.loads(json_string) items = new_data.get("items", []) if isinstance(items, dict): return {k.strip(): str(v).strip() for k, v in items.items()} return {i[0].strip(): i[1].strip() for i in items if len(i) == 2}
def overwrite_json_text(json_string, user_role): if user_role != "admin": return "๐ซ [๊ถํ ์์] ์ด๋๋ฏผ๋ง ๋ฎ์ด์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํฉ๋๋ค." if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์." if user_role != "admin": return "๐ซ [๊ถํ ์์] ์ด๋๋ฏผ๋ง ๋ฎ์ด์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํฉ๋๋ค." if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์." try: new_data = json.loads(json_string) new_items = [[k.strip(), v.strip()] for k, v in (new_data.get("items", {}).items() if isinstance(new_data.get("items"), dict) else {i[0]: i[1] for i in new_data.get("items", []) if len(i)==2}.items())] with open(dict_path, "w", encoding="utf-8") as f: json.dump({"items": new_items, "meta": {"count": len(new_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return f"๐ [์ด๋๋ฏผ] ์๋ฒ ์ฌ์ ์ ์๋ฒฝํ ๊ต์ฒดํ์ต๋๋ค!" except Exception as e: return f"โ ์๋ฌ: {e}" dict_map = _parse_items(json_string) sync_msg = save_dict(dict_map) return f"๐ [์ด๋๋ฏผ] ์๋ฒ ์ฌ์ ์ ์์ ํ ๊ต์ฒดํ์ต๋๋ค! ({sync_msg})" except Exception as e: return f"โ ์๋ฌ: {e}"
def apply_json_text(json_string, user_role, user_temp_dict): if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์.", user_temp_dict if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์.", user_temp_dict try: parsed_items = {i[0].strip(): i[1].strip() for i in json.loads(json_string).get("items", []) if len(i)==2} if isinstance(json.loads(json_string).get("items"), list) else json.loads(json_string).get("items", {}) parsed_items = _parse_items(json_string) if user_role == "admin": try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f) except: existing_data = {"items":[], "meta": {"count": 0}} dict_map = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2} dict_map = _read_local_dict() dict_map.update(parsed_items) merged_items = [[k, v] for k, v in dict_map.items()] with open(dict_path, "w", encoding="utf-8") as f: json.dump({"items": merged_items, "meta": {"count": len(merged_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return f"๐ [์ด๋๋ฏผ] ์๋ฒ์ ์๊ตฌ ์ถ๊ฐํ์ต๋๋ค!", user_temp_dict sync_msg = save_dict(dict_map) return f"๐ [์ด๋๋ฏผ] ์๋ฒ์ ์ถ๊ฐํ์ต๋๋ค! ({sync_msg})", user_temp_dict else: user_temp_dict.update(parsed_items) return f"๐ค [๊ฒ์คํธ] ์์ ๋จ์ด์ฅ์ ์ถ๊ฐ๋์์ต๋๋ค!", user_temp_dict except Exception as e: return f"โ ์๋ฌ: {e}", user_temp_dict return "๐ค [๊ฒ์คํธ] ์์ ๋จ์ด์ฅ์ ์ถ๊ฐ๋์์ต๋๋ค!", user_temp_dict except Exception as e: return f"โ ์๋ฌ: {e}", user_temp_dict
def upload_file(file_path, user_role, user_temp_dict): if file_path is None: return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค.", user_temp_dict if file_path is None: return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค.", user_temp_dict try: with open(file_path, "r", encoding="utf-8") as f: uploaded_data = json.load(f) with open(file_path, "r", encoding="utf-8") as f: uploaded_data = json.load(f) parsed_items = {item[0].strip(): item[1].strip() for item in uploaded_data.get("items", []) if len(item) == 2} if user_role == "admin": try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f) except: existing_data = {"items":[], "meta": {"count": 0}} dict_map = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2} dict_map = _read_local_dict() dict_map.update(parsed_items) merged_items = [[k, v] for k, v in dict_map.items()] with open(dict_path, "w", encoding="utf-8") as f: json.dump({"items": merged_items, "meta": {"count": len(merged_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return "๐ [์ด๋๋ฏผ] ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์๊ตฌ ๋ณํฉํ์ต๋๋ค!", user_temp_dict sync_msg = save_dict(dict_map) return f"๐ [์ด๋๋ฏผ] ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ๋ณํฉํ์ต๋๋ค! ({sync_msg})", user_temp_dict else: user_temp_dict.update(parsed_items) return "๐ค [๊ฒ์คํธ] ๋ด ์์ ๋จ์ด์ฅ์ ๋ณํฉ๋์์ต๋๋ค!", user_temp_dict except Exception as e: return f"โ ์๋ฌ: {e}", user_temp_dict except Exception as e: return f"โ ์๋ฌ: {e}", user_temp_dict
def verify_login(uid, upw): if uid == ADMIN_ID and upw == ADMIN_PW:18 unmodified lines.login-box { max-width: 400px; margin: 100px auto; padding: 30px; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }.header-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }"""
# โ
๋๋ฝ๋์๋ ์คํฌ๋กค ๋๊ธฐํ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋ ์๋ฒฝ ๋ณต๊ตฌscript_js = """function() { setTimeout(function() {2 unmodified lines let ta2 = document.querySelector('#output_text_area textarea'); if(ta2) { ta2.style.fontSize = '22px'; ta2.style.lineHeight = '1.8'; } }, 500);
let isSyncing1 = false; let isSyncing2 = false; let isSyncing3 = false; document.addEventListener('scroll', function(e) { let target = e.target;1 unmodified line let ta2 = document.querySelector('#output_text_area textarea'); let div3 = document.getElementById('prompter_output_div'); if (!ta1 || !div3) return;
if (target === ta1) { if (isSyncing1) { isSyncing1 = false; return; } if (ta1.scrollHeight - ta1.clientHeight <= 0) return;19 unmodified lines }, true);}"""
insert_marker_js = "(t) => { let ta = document.querySelector('#input_text_area textarea'); if (!ta) return t; let s = ta.selectionStart, e = ta.selectionEnd, v = ta.value; ta.value = v.substring(0, s) + '@@' + v.substring(e); ta.dispatchEvent(new Event('input', { bubbles: true })); ta.selectionStart = ta.selectionEnd = s + 2; ta.focus(); return ta.value; }"tts_js = """(t) => { if (!t || !t.trim()) { alert('๋จผ์ [๋ณํํ๊ธฐ]๋ฅผ ๋๋ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์.'); return; } const synth = window.speechSynthesis; if (synth.speaking) { synth.cancel(); return; } const clean = t.replace(/\\//g, ' ').replace(/@@/g, ''); const u = new SpeechSynthesisUtterance(clean); u.lang = 'ko-KR'; u.rate = 1.0; const ko = synth.getVoices().find(v => v.lang && v.lang.startsWith('ko')); if (ko) u.voice = ko; synth.speak(u);}"""
_mode_choices = ["๋๊ธฐ (์๋ ์ฌ์ ๋ง ์ ์ฉ)", "์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)"]if kiwi is not None: _mode_choices.append("์ค๊ฐ ๋ง 2.0 (Kiwi ํ์ฌ ๊ธฐ๋ฐ - ๊ณ ์ ๋ช
์ฌ ๋ณดํธ)")_mode_choices += ["์ํ ๋ง (๋จ์ด๋ณ ๋๊ธฐ - ๊ณผ๋ํ ์ฐ์ ๋ฐฉ์ง)", "๋งค์ด ๋ง (๋ฌธ์ฅ ํต์งธ๋ก - ์์ฐ์ค๋ฌ์ด ์ฐ์)"]
_GRADIO_MAJOR = int(gr.__version__.split(".")[0])_BLOCKS_KW = {"theme": "default", "css": custom_css} if _GRADIO_MAJOR < 6 else {}_LAUNCH_KW = {"theme": "default", "css": custom_css} if _GRADIO_MAJOR >= 6 else {}
with gr.Blocks(theme="default", css=custom_css) as iface:
with gr.Blocks(**_BLOCKS_KW) as iface: user_role = gr.State("none") user_temp_dict = gr.State(lambda: {})
with gr.Column(visible=True, elem_classes="login-box") as login_screen: gr.Markdown("### ๐ ์์คํ
๋ก๊ทธ์ธ") login_id = gr.Textbox(label="์์ด๋") login_pw = gr.Textbox(label="๋น๋ฐ๋ฒํธ", type="password") login_btn = gr.Button("๋ก๊ทธ์ธ", variant="primary") login_msg = gr.Markdown("")
with gr.Column(visible=False) as main_app: with gr.Row(elem_classes="header-row"): gr.Markdown("## ๐๏ธ AI ์คํธ๋ฆฝํฐ ๋ณํ๊ธฐ") gr.Markdown("## ๐๏ธ AI ์คํฌ๋ฆฝํฐ ๋ณํ๊ธฐ") btn_logout = gr.Button("๐ช ๋ก๊ทธ์์", variant="secondary", size="sm", scale=0, min_width=100)
with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"): with gr.Row(): with gr.Column(): input_text = gr.Textbox(label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ", elem_id="input_text_area") with gr.Group(): ai_mode = gr.Radio(choices=["๋๊ธฐ (์๋ ์ฌ์ ๋ง ์ ์ฉ)", "์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", "์ํ ๋ง (๋จ์ด๋ณ ๋๊ธฐ - ๊ณผ๋ํ ์ฐ์ ๋ฐฉ์ง)", "๋งค์ด ๋ง (๋ฌธ์ฅ ํต์งธ๋ก - ์์ฐ์ค๋ฌ์ด ์ฐ์)"], value="์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", label="๐ค AI ๋ฐ์ ๋ณํ ๊ฐ๋") ai_mode = gr.Radio(choices=_mode_choices, value="์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", label="๐ค AI ๋ฐ์ ๋ณํ ๊ฐ๋") auto_break = gr.Checkbox(label="๐ ๋ฌธ์ฅ ๋ ์๋ ์ค๋ฐ๊ฟ", value=False) smart_pause = gr.Checkbox(label="โฑ๏ธ ์ผํ ๋ค ํธํก์ ( / ) ์ถ๊ฐ", value=False) spacing_fix = gr.Checkbox(label="๐ ๋์ด์ฐ๊ธฐ ๊ต์ ํ ๋งค์นญ (Kiwi, ์คํ์ )", value=False, interactive=(kiwi is not None)) btn_insert_marker = gr.Button("๐จ ๋ง์ปค(@@) ์ฝ์
", variant="secondary", size="sm") font_size = gr.Slider(minimum=16, maximum=40, value=22, step=2, label="๐ ๊ธ์ ํฌ๊ธฐ")
char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="๊ณต๋ฐฑ ํฌํจ: 0์", interactive=False) with gr.Row(): btn_conv = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary") btn_tts = gr.Button("๐ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ", variant="secondary") btn_copy = gr.Button("๐ ๋ณต์ฌ", variant="secondary") btn_clr = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop")
with gr.Column(): output_html = gr.HTML(value='<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:22px;"></div>', elem_id="prompter_html_wrap") with gr.Accordion("๐ ํ
์คํธ ๊ฒฐ๊ณผ (ํด๋ฆญํ์ฌ ํผ์น๊ธฐ)", open=False): output_text = gr.Textbox(lines=8, show_label=False, show_copy_button=True, elem_id="output_text_area") output_text = gr.Textbox(lines=8, show_label=False, elem_id="output_text_area") marker_collection = gr.Textbox(label="๐จ ์์ ํ์ ๋จ์ด (@@)", interactive=False, lines=2)
gr.Markdown("---") gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ**") gr.Markdown(f"**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ** <small>์ ์ฅ์ ์ํ: {SYNC_STATE} ยท G2P ์์ง: {G2P_BACKEND}</small>") with gr.Row(): nw = gr.Textbox(label="๋จ์ด (2๊ธ์ ์ด์)") np = gr.Textbox(label="์ํ๋ ๋ฐ์")1 unmodified line btn_add = gr.Button("๐พ ์ ์ฉ ๋ฐ ๋ณํ", variant="primary") btn_clear_dict_inputs = gr.Button("๐งน ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ", variant="secondary") status = gr.Textbox(label="์ ์ฅ ์ํ", interactive=False)
proc_inputs = [input_text, ai_mode, auto_break, smart_pause, spacing_fix, font_size, user_temp_dict] proc_outputs = [output_text, output_html, char_count, marker_collection]
btn_insert_marker.click(fn=None, inputs=[input_text], outputs=[input_text], js=insert_marker_js, api_name=False) btn_insert_marker.click(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) btn_conv.click(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) btn_insert_marker.click(fn=process_text, inputs=proc_inputs, outputs=proc_outputs, api_name=False) btn_conv.click(fn=process_text, inputs=proc_inputs, outputs=proc_outputs, api_name=False) btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count, marker_collection], api_name=False) btn_copy.click(fn=None, inputs=[output_text], outputs=None, js="(t) => { if(t) { navigator.clipboard.writeText(t); alert('โ
๋ณต์ฌ ์๋ฃ!'); } }", api_name=False) btn_tts.click(fn=None, inputs=[output_text], outputs=None, js=tts_js, api_name=False) btn_add.click(fn=add_and_update, inputs=[nw, np, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[status, output_text, output_html, char_count, marker_collection, user_temp_dict], api_name=False) btn_add.click(fn=add_and_update, inputs=[nw, np, user_role, input_text, ai_mode, auto_break, smart_pause, spacing_fix, font_size, user_temp_dict], outputs=[status, output_text, output_html, char_count, marker_collection, user_temp_dict], api_name=False) btn_clear_dict_inputs.click(fn=lambda: ("", ""), outputs=[nw, np], api_name=False) for evt in [font_size.change, ai_mode.change, auto_break.change, smart_pause.change]: evt(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[output_text, output_html, char_count, marker_collection], api_name=False)
for evt in [font_size.change, ai_mode.change, auto_break.change, smart_pause.change, spacing_fix.change]: evt(fn=process_text, inputs=proc_inputs, outputs=proc_outputs, 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("---") btn_load_json = gr.Button("๐ 2. ๋ฑ๋ก๋ ๋จ์ด์ฅ ๋ถ๋ฌ์ค๊ธฐ", variant="secondary") json_input = gr.Textbox(lines=10, label="ํธ์ง์ฐฝ")
with gr.Row(): btn_apply_json = gr.Button("๐ ํ๋ฌ์ค(+) ํ๊ธฐ", variant="primary") btn_overwrite_json = gr.Button("๐จ ์์ ํ ๋ฎ์ด์ฐ๊ธฐ (์ด๋๋ฏผ ์ ์ฉ)", variant="stop") json_stat = gr.Textbox(label="์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)
btn_load_json.click(fn=load_current_json_to_text, inputs=[user_temp_dict], outputs=[json_input, json_stat], api_name=False) btn_apply_json.click(fn=apply_json_text, inputs=[json_input, user_role, user_temp_dict], outputs=[json_stat, user_temp_dict], api_name=False) btn_overwrite_json.click(fn=overwrite_json_text, inputs=[json_input, user_role], outputs=[json_stat], api_name=False)
gr.Markdown("---") with gr.Accordion("๐ค 3. ๋ฐฑ์
ํ์ผ๋ก ์ฌ์ ์ ๋ณํฉํ๊ธฐ", open=False): up_file = gr.File(label="๋ฐฑ์
ํ์ผ ์
๋ก๋", type="filepath") up_stat = gr.Textbox(label="์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file, user_role, user_temp_dict], outputs=[up_stat, user_temp_dict], api_name=False)
# ์ด๋ฒคํธ ์ฐ๊ฒฐ login_btn.click(fn=verify_login, inputs=[login_id, login_pw], outputs=[login_screen, main_app, user_role, login_msg, login_pw], api_name=False) btn_logout.click(fn=perform_logout, inputs=[], outputs=[login_screen, main_app, user_role, login_msg, login_id, login_pw, user_temp_dict], api_name=False) iface.load(fn=handle_page_load, inputs=[user_role], outputs=[login_screen, main_app, user_role, login_msg, login_id, login_pw], js=script_js)
if __name__ == "__main__": iface.launch() iface.launch(**_LAUNCH_KW)app.py+397-167