Spaces:
Running
Running
Update app.py
Browse files17 unmodified lines18192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858 unmodified lines949596979899100166 unmodified lines26726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935019 unmodified lines37037137237337437537637737837938038138238338438538638738838939039139210 unmodified lines40340440524824940640740840941041125225341241341441541641741841942042142242342442542642726542842943043110 unmodified lines44244344428228328444544644744844928745045145245329045445545629345745845946046146246346429546546646746846947047147247347447547630347747847948014 unmodified lines49549649732449849932650050150250350432850550650733050850951051151251351451533551651733751851952052196 unmodified lines6186196204406216226236241 unmodified line62662762862963044963163263363445163563663763822 unmodified lines6616626634806646656666678 unmodified lines67667767849567968068168238 unmodified lines72172272354072472572672717 unmodified lines74574674756474874975075156875275375475512 unmodified lines76876977058777177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682726 unmodified lines85485585662385785885986066 unmodified lines92792892993093193293355 unmodified lines9899909919929939949959969979989991000100110021003100410051006100775710081009101010113 unmodified lines10151016101776710181019102010215 unmodified lines1027102810297791030103110321033103478410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106115 unmodified lines107710781079812108010811082108317 unmodified lines# - ๋ฒํผ ํ์ ๊ฐ์ scaleยทsize๋ก ํต์ผ. ์ด๊ธฐํ ๋ฒํผ์ ๋ง์ง๋ง ์๋ฆฌ๋ก ๋ณต์.# ============================================================================
# ---------- [v154] ํํ์ ๊ฒฝ๊ณ ์ธ์ (A+B ํ์ด๋ธ๋ฆฌ๋) ----------# B: Kiwi(๊ฒฝ๋ ํํ์ ๋ถ์๊ธฐ, ์์ ํ ) โ ์์ผ๋ฉด ์กฐ์ฌ/์ด๋ฏธ ๊ฒฝ๊ณ๋ฅผ ์ ๋ฐ ํ๋ณ# A: Kiwi๊ฐ ์๊ฑฐ๋ ๋ก๋ ์คํจ ์ ์กฐ์ฌ ์ ๋ฏธ์ฌ ๋ฆฌ์คํธ๋ก ํด๋ฐฑ (์์กด์ฑ 0, ์ฑ์ ์ ๋ ์ ์ฃฝ์)_kiwi = None_KIWI_OK = None # None=์์ง ์๋ ์ ํจ / True / False
def _load_kiwi(): global _kiwi, _KIWI_OK if _KIWI_OK is None: try: from kiwipiepy import Kiwi _kiwi = Kiwi() _KIWI_OK = True print("[ํํ์ ๋ถ์๊ธฐ] Kiwi ๋ก๋ ์๋ฃ โ ์กฐ์ฌ/์ด๋ฏธ ๊ฒฝ๊ณ ํ๋ณ ์ฌ์ฉ") except Exception as e: _KIWI_OK = False print(f"[ํํ์ ๋ถ์๊ธฐ] Kiwi ๋ก๋ ์คํจ({e}) โ ์กฐ์ฌ ๋ฆฌ์คํธ ๊ท์น์ผ๋ก ํด๋ฐฑ") return _kiwi
# A ํด๋ฐฑ์ฉ: ์ด์ ๋ค์ ๋ถ๋ ํ๊ตญ์ด ์กฐ์ฌยท๋ณด์กฐ์ฌยท์ด๋ฏธ (๊ธธ์ด ๋ด๋ฆผ์ฐจ์ โ ๊ธด ๊ฒ ๋จผ์ )_PARTICLE_SUFFIXES = sorted(set([ "์ผ๋ก๋ถํฐ", "๋ก๋ถํฐ", "์์", "์๊ฒ", "ํํ
", "๊น์ง", "๋ณด๋ค", "์ฒ๋ผ", "๊ฐ์ด", "๋งํผ", "์กฐ์ฐจ", "๋ง์ ", "ํ๊ณ ", "์ด๋", "์ด๋ผ", "์
๋๋ค", "์ด์ง๋ง", "๋๊น", "๋๋ฐ", "๋", "์", "์ด", "๊ฐ", "์", "๋ฅผ", "์", "์", "๋", "๋ง", "๊ณผ", "์", "๋ก", "์ผ", "์ผ", "์", "๊ป", "๋"]), key=lambda x: -len(x))
class _MorphBoundary: """ํ์ฌ ํ
์คํธ ๊ธฐ์ค ํํ์ ๊ฒฝ๊ณ โ ํค ๋งค์นญ ์์น ๋ค๊ฐ '์กฐ์ฌ/์ด๋ฏธ์ ์์'์ธ์ง ํ๋ณ.""" def __init__(self, text): self.text = text or "" self._kiwi = _load_kiwi() self._starts = {} # position -> tag (Kiwi ํ ํฐ ์์์ ) if self._kiwi is not None: try: for tok in self._kiwi.tokenize(self.text): st = int(tok.start) if tok.start is not None else None if st is not None and st not in self._starts: self._starts[st] = tok.tag except Exception: self._kiwi = None
@staticmethod def _is_particle_tag(tag): # J*: ์กฐ์ฌ / E*: ์ด๋ฏธ / VCPยทVCN: '์ด๋คยท์๋๋ค' ๋ฅ (์: ์์ค์
๋๋ค) return tag.startswith("J") or tag.startswith("E") or tag in ("VCP", "VCN")
def allow_after(self, e): """ํค ๋งค์นญ์ด ๋๋ ์์น e ๋ค์ ์กฐ์ฌ/์ด๋ฏธ(์ ํํ์)๊ฐ ์ด์ด์ง๋ฉด True.""" if e is None or e >= len(self.text): return True nxt = self.text[e] if not ('๊ฐ' <= nxt <= 'ํฃ'): # ๊ณต๋ฐฑยท๋ฌธ์ฅ๋ถํธยท๋ โ ๋
๋ฆฝ ๋จ์ด return True if self._kiwi is not None and self._starts: tag = self._starts.get(e) if tag is not None: return self._is_particle_tag(tag) return False # ํ๊ธ ์ฐ์์ธ๋ฐ ํํ์ ๊ฒฝ๊ณ ์์ โ ํฉ์ฑ์ด # ํด๋ฐฑ A: ์กฐ์ฌ ์ ๋ฏธ์ฌ๋ก ์์ํ๋์ง (Kiwi ์์ ๋๋ง) return any(self.text[e:].startswith(p) for p in _PARTICLE_SUFFIXES)
# ---------- G2P ์๋ ์ ํ (g2pk โ g2pk2 โ g2pkk) ----------g2p = NoneG2P_BACKEND = "์์"8 unmodified lines print(f"G2P {_mod} ๋ก๋ฉ ์คํจ: {_e}")
dict_path = "custom_dict.json"whitelist_path = "custom_whitelist.json" # [v153] ์์ธ ๋จ์ด(์นํ ๊ธ์ง) ์ ์ฅ์ADMIN_ID = os.environ.get("ADMIN_ID", "admin")ADMIN_PW = os.environ.get("ADMIN_PW", "admin1234")GUEST_ID = os.environ.get("GUEST_ID", "guest")166 unmodified lines msg = "๋ก์ปฌ ์ ์ฅ ์๋ฃ (HF_TOKEN ์์ โ ์ฌ์์ ์ ์ ์ค ๊ฐ๋ฅ)" return f"{msg} ยท ํ์ฌ ์ฌ์ {len(dict_map)}๊ฐ"
# ---------- [v153] ํ์ดํธ๋ฆฌ์คํธ I/O + ๋ง์คํน ----------def _read_local_whitelist(): try: with open(whitelist_path, "r", encoding="utf-8") as f: data = json.load(f) return sorted({str(x).strip() for x in data.get("items", []) if str(x).strip()}, key=lambda x: -len(x)) except Exception: return []
def _write_local_whitelist(wl_list): items = sorted({s.strip() for s in wl_list if s and s.strip()}, key=lambda x: -len(x)) payload = {"items": items, "meta": {"app_title": "VoiceScript AI whitelist", "app_version": "153.0", "count": len(items)}} tmp_path = whitelist_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(whitelist_path): os.replace(whitelist_path, whitelist_path + ".bak") os.replace(tmp_path, whitelist_path)
def _push_remote_whitelist(): if not hf_api: return "๋ก์ปฌ ์ ์ฅ๋ง ๋จ (์ฌ์์ ์ ์ ์ค ๊ฐ๋ฅ - HF_TOKEN ์ค์ ํ์)" try: hf_api.upload_file(path_or_fileobj=whitelist_path, path_in_repo="custom_whitelist.json", repo_id=dict_repo_id, repo_type="dataset", commit_message="update pronunciation whitelist") return "์๊ตฌ ์ ์ฅ ์๋ฃ" except Exception as e: return f"์๊ฒฉ ์ ์ฅ ์คํจ({e}) - ๋ก์ปฌ๋ง ์ ์ฅ๋จ"
def save_whitelist(wl_list): _write_local_whitelist(wl_list) if hf_api: def _bg_push(): try: msg = _push_remote_whitelist() print(f"[ํ์ดํธ๋ฆฌ์คํธ ์๊ฒฉ ์ ์ฅ] {msg}") except Exception as e: print(f"[ํ์ดํธ๋ฆฌ์คํธ ์๊ฒฉ ์ ์ฅ ์คํจ] {e}") threading.Thread(target=_bg_push, daemon=True).start() msg = "์ ์ฅ ์๋ฃ (์๊ฒฉ ๋๊ธฐํ๋ ๋ฐฑ๊ทธ๋ผ์ด๋ ์งํ ์ค)" else: msg = "๋ก์ปฌ ์ ์ฅ ์๋ฃ (HF_TOKEN ์์)" items = _read_local_whitelist() return f"{msg} ยท ํ์ฌ ์์ธ ๋จ์ด {len(items)}๊ฐ", items
_WL_TOKEN_FMT = "\x00WL{}\x00"
def _mask_whitelist(text, wl_terms): """ํ์ดํธ๋ฆฌ์คํธ ๋จ์ด๋ค์ ์์ ํ ํฐ์ผ๋ก ์นํ. (masked_text, token_map) ๋ฐํ.""" if not text or not wl_terms: return text, {} # ๊ธด ๊ฒ๋ถํฐ ๋งค์นญ (๊ฒน์นจ ๋ฐฉ์ง) ordered = sorted({w for w in wl_terms if w}, key=lambda x: -len(x)) token_map = {} for idx, term in enumerate(ordered): token = _WL_TOKEN_FMT.format(idx) if term in text: text = text.replace(term, token) token_map[token] = term return text, token_map
def _unmask_whitelist(text, token_map): if not token_map: return text for token, term in token_map.items(): text = text.replace(token, term) return text
def load_whitelist(user_temp_wl=None): base = list(_read_local_whitelist()) if user_temp_wl: base.extend([str(x).strip() for x in user_temp_wl if str(x).strip()]) # ์ค๋ณต ์ ๊ฑฐ + ๊ธด ์ return sorted(set(base), key=lambda x: -len(x))
def perform_startup_recovery(): dict_map = _read_local_dict() remote_ok = True19 unmodified lines if hf_api and remote_ok: _push_remote()
# [v153] ํ์ดํธ๋ฆฌ์คํธ๋ ์๊ฒฉ์์ ๋ณต์ ์๋ (์์ผ๋ฉด ๋ฌด์) wl_local = _read_local_whitelist() if hf_api: try: remote_wl_path = hf_hub_download(repo_id=dict_repo_id, repo_type="dataset", filename="custom_whitelist.json", token=HF_TOKEN) with open(remote_wl_path, "r", encoding="utf-8") as f: remote_wl = json.load(f) merged = set(wl_local) | {str(x).strip() for x in remote_wl.get("items", []) if str(x).strip()} _write_local_whitelist(list(merged)) except Exception as e: is_404 = getattr(getattr(e, "response", None), "status_code", None) == 404 if is_404: print("[ํ์ดํธ๋ฆฌ์คํธ] ์๊ฒฉ์ ํ์ผ ์์ โ ์ต์ด ๋ถํ
") else: print(f"[ํ์ดํธ๋ฆฌ์คํธ] ์๊ฒฉ ๋ณต์ ์คํจ({e}) - ๋ก์ปฌ ๋ณดํธ")
perform_startup_recovery()
def load_dict(user_temp_dict=None):10 unmodified lines [v150] 1๊ธ์ ์์ซ์ ํค๋ ๋จ์ด ๊ฒฝ๊ณ(boundary)๋ฅผ ๊ฐ์ ํด์ 'CLASS' ๊ฐ์ ๋จ์ด ์ค๊ฐ์์ ์คํ๋์ง ์๊ฒ ํ๋ค. [v152] ์์ ํ๊ธ ํค๋ ์ข์ธก์ ํ๊ธ์ด ์ค๋ฉด ๋งค์นญ ๊ธ์ง โ '์์ค'๊ฐ 'ํด๋ฆฌ์์คํฐ' ๊ฐ์ ํฉ์ฑ์ด ์ค๊ฐ์์ ์คํ๋์ง ์๋๋ก. ์ฐ์ธก ๊ฒฝ๊ณ๋ ๋์ง ์๋๋ค (์กฐ์ฌ '์์ค๋/์์ค๊ฐ' ๋ฑ์ ๊ทธ๋๋ก ์กํ์ผ ํ๊ธฐ ๋๋ฌธ).""" 'ํด๋ฆฌ์์คํฐ' ๊ฐ์ ํฉ์ฑ์ด ์ค๊ฐ์์ ์คํ๋์ง ์๋๋ก. [v154] ์์ ํ๊ธ ํค๋ guarded=True โ ์ค์ ๋งค์นญ ์ ํํ์ ๊ฒฝ๊ณ (์กฐ์ฌ/์ด๋ฏธ ์ฌ๋ถ, Kiwi ๋๋ ์กฐ์ฌ ๋ฆฌ์คํธ ํด๋ฐฑ)๋ฅผ ํ์ธํด '์์ค์ปฌ๋ ์ดํฐ/์์คํ๋ ์/์ ์ด์จ์ฐ' ๊ฐ์ ํฉ์ฑ์ด ์คํ์ ๋ง๋๋ค.""" ordered = sorted(keys, key=lambda x: -len(x.replace(" ", ""))) if not ordered: return None, [] pats = [] return None, [], [] pats, guarded = [], [] for k in ordered: if len(k) == 1 and re.match(r'^[A-Za-z0-9_]$', k): # 1๊ธ์ ์์ซ์: ์ข์ฐ alphanumeric/underscore๊ฐ ์ค๋ฉด ๋งค์นญ ์ ํจ pats.append(rf'(?<![A-Za-z0-9_]){re.escape(k)}(?![A-Za-z0-9_])') guarded.append(False) else: body = r'\s*'.join(map(re.escape, k.split(" "))) # [v152] ๊ณต๋ฐฑ ์ ๊ฑฐ ํ ์ ๋ถ ํ๊ธ์ด๋ฉด ์ข์ธก์ ํ๊ธ์ด ์ค๋ฉด ์ ๋จ if re.match(r'^[๊ฐ-ํฃ]+$', k.replace(" ", "")): pats.append(rf'(?<![๊ฐ-ํฃ]){body}') guarded.append(True) else: pats.append(body) guarded.append(False) return re.compile("|".join(f"({p})" for p in pats)), ordered return re.compile("|".join(f"({p})" for p in pats)), ordered, guarded
def get_engine(flat_dict): sig = hash(tuple(sorted(flat_dict.items())))10 unmodified lines shields.add(v) 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) p1_pat, p1_keys, p1_guard = _build_pattern(phase1.keys()) p2_pat, p2_keys, p2_guard = _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}) "p2_pat": p2_pat, "p2_keys": p2_keys, "sh_pat": sh_pat, "p1_guard": p1_guard, "p2_guard": p2_guard}) return _ENGINE_CACHE
def _sub_phase1(text, eng):def _sub_phase1(text, eng, boundary=None): if not eng["p1_pat"]: return text return eng["p1_pat"].sub(lambda m: eng["phase1"][eng["p1_keys"][m.lastindex - 1]], text) guard = eng.get("p1_guard", []) or [] def _repl(m): gi = m.lastindex - 1 if guard[gi] and boundary is not None and not boundary.allow_after(m.end()): return m.group() # ํฉ์ฑ์ด ๋ด๋ถ โ ์ ์ง return eng["phase1"][eng["p1_keys"][gi]] return eng["p1_pat"].sub(_repl, text)
def _segments_phase2(text, eng):def _segments_phase2(text, eng, boundary=None): if not eng["p2_pat"] or not text: return [(False, text)] if text else [] guard = eng.get("p2_guard", []) or [] segs, last = [], 0 for m in eng["p2_pat"].finditer(text): s, e = m.span() gi = m.lastindex - 1 if guard[gi] and boundary is not None and not boundary.allow_after(e): continue # ํฉ์ฑ์ด ๋ด๋ถ โ ์ด ๋งค์นญ ๊ฑด๋๋ (์๋ฌธ ์ ์ง) if s > last: segs.append((False, text[last:s])) segs.append((True, eng["phase2"][eng["p2_keys"][m.lastindex - 1]])) segs.append((True, eng["phase2"][eng["p2_keys"][gi]])) last = e if last < len(text): segs.append((False, text[last:]))14 unmodified lines res.append(apply_g2p(chunk[last:], mode)) return "".join(res)
def convert_text(text, mode, flat_dict):def convert_text(text, mode, flat_dict, whitelist=None): """์๋ฌธ ๋น๊ณต๋ฐฑ ๋ฌธ์์ด ๋ณํ ์ ํ ๋์ผ์ฑ ์ ์ง. ์ธํ ๋ณํ๋ ์ฌ์ ยท์ซ์ยท๋ฌธ์ฅ๋ถํธ ํธํก ๋ง์ปค๋ก๋ง ๋ง๋ค์ด์ง๋ค.""" ์ธํ ๋ณํ๋ ์ฌ์ ยท์ซ์ยท๋ฌธ์ฅ๋ถํธ ํธํก ๋ง์ปค๋ก๋ง ๋ง๋ค์ด์ง๋ค. [v153] whitelist์ ํฌํจ๋ ๋จ์ด๋ ์ด๋ค ์ฌ์ ํค๊ฐ ์์ ๋ค์ด์์ด๋ ์นํ ๋์์์ ์์ ํ ์ ์ธ๋๋ค.""" text, wl_map = _mask_whitelist(text, whitelist or []) eng = get_engine(flat_dict) t = _sub_phase1(text, eng) t = _sub_phase1(text, eng, _MorphBoundary(text)) out = [] b2 = _MorphBoundary(t) for fixed, chunk in _segments_phase2(t, eng): for fixed, chunk in _segments_phase2(t, eng, b2): if fixed: out.append(chunk) continue chunk = apply_smart_regex(chunk) # [v154] 2์ฐจ ๋งค์นญ๋ ์กฐ๊ฐ ๋จ์ ํํ์ ๊ฐ๋ ์ ์ฉ โ 1์ฐจ์์ ๊ฑฐ์ ๋ # ํฉ์ฑ์ด(์์ค์ปฌ๋ ์ดํฐ ๋ฑ)๊ฐ ์ด ๋ฃจํ์์ ์ฌ์คํ๋๋ ๊ฒ ๋ฐฉ์ง. b_chunk = _MorphBoundary(chunk) for fixed2, c2 in _segments_phase2(chunk, eng): for fixed2, c2 in _segments_phase2(chunk, eng, b_chunk): out.append(c2 if fixed2 else _g2p_shielded(c2, mode, eng)) return "".join(out) return _unmask_whitelist("".join(out), wl_map)
# ---------- G2P ๋ชจ๋ ----------def apply_g2p(text, mode):96 unmodified lines text = re.sub(r'โฆ\s*', 'โฆ / ', text) return text
def process_text(input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict):def process_text(input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl=None): 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์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."1 unmodified line char_count = f"๊ณต๋ฐฑ ํฌํจ: {len(input_text)}์ / ๊ณต๋ฐฑ ์ ์ธ: {len(input_text.replace(' ', '').replace(chr(10), ''))}์"
flat_dict = load_dict(user_temp_dict) whitelist = load_whitelist(user_temp_wl) if not flat_dict: final_text = apply_g2p(apply_smart_regex(input_text), ai_mode) # ์ฌ์ ์ ์๋๋ผ๋ ํ์ดํธ๋ฆฌ์คํธ๋ ์ค๋งํธ ์ ๊ท์/G2P์์ ๋ณดํธ๋์ด์ผ ํจ masked, wl_map = _mask_whitelist(input_text, whitelist) final_text = _unmask_whitelist(apply_g2p(apply_smart_regex(masked), ai_mode), wl_map) else: final_text = convert_text(input_text, ai_mode, flat_dict) final_text = convert_text(input_text, ai_mode, flat_dict, whitelist)
if smart_pause: final_text = _join_breath_marks(final_text)22 unmodified linesdef 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):def add_and_update(word, pronun, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl=None): if not word or not pronun: return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict word, pronun = word.strip(), pronun.strip()8 unmodified lines user_temp_dict[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, font_size, user_temp_dict, user_temp_wl) return status_msg, ft, ho, cc, mr, user_temp_dict return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict
38 unmodified lines except Exception as e: return f"โ ์๋ฌ: {e}", user_temp_dict
def update_dict_entry(old_word, new_pronun, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict):def update_dict_entry(old_word, new_pronun, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl=None): """๋จ์ด์ฅ ํญ๋ชฉ ์์ โ key(๋จ์ด) ์๋ณ์๊ฐ ๊ทธ๋๋ก์ธ ๋์ value(๋ฐ์)๋ง ๊ต์ฒด.""" if not old_word: return "โ ๏ธ ๋จผ์ ๋จ์ด๋ฅผ ์ ํํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), update_choices(), user_temp_dict17 unmodified lines user_temp_dict[old_word] = new_pronun status_msg = f"๐ค [๊ฒ์คํธ ์์ ] '{old_word}' : '{old_value}' โ '{new_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, font_size, user_temp_dict, user_temp_wl) return status_msg, ft, ho, cc, mr, update_choices(), user_temp_dict return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), update_choices(), user_temp_dict
def delete_dict_entry(old_word, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict):def delete_dict_entry(old_word, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl=None): """๋จ์ด์ฅ ํญ๋ชฉ์์ ํ ์ค๋ง ์ญ์ . ADMIN_IDยท์ด๋๋ฏผ์ด๋ฉด ์๋ฒ ์ ์ฅ์, ๊ฒ์คํธ๋ฉด ์์ ์ฌ์ .""" if not old_word: return "โ ๏ธ ๋จผ์ ๋จ์ด๋ฅผ ์ ํํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), update_choices(), user_temp_dict12 unmodified lines old_value = user_temp_dict.pop(old_word) status_msg = f"๐๏ธ [๊ฒ์คํธ ์ญ์ ] '{old_word}' : '{old_value}' (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ)" 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, font_size, user_temp_dict, user_temp_wl) return status_msg, ft, ho, cc, mr, update_choices(), user_temp_dict return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), update_choices(), user_temp_dict
# ---------- [v153] ํ์ดํธ๋ฆฌ์คํธ UI ํธ๋ค๋ฌ ----------def add_whitelist_entry(term, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl): term = (term or "").strip() if not term: return "โ ๏ธ ์์ธ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), user_temp_wl if user_role == "admin": wl = _read_local_whitelist() if term in wl: status_msg = f"โน๏ธ ์ด๋ฏธ ์์ธ ๋ชฉ๋ก์ ์์: '{term}'" else: wl.append(term) msg, _ = save_whitelist(wl) status_msg = f"๐ [์ด๋๋ฏผ] ์์ธ ๋จ์ด ์ถ๊ฐ: '{term}' ({msg})" else: if term in user_temp_wl: status_msg = f"โน๏ธ ์ด๋ฏธ ์์ ์์ธ ๋ชฉ๋ก์ ์์: '{term}'" else: user_temp_wl.append(term) status_msg = f"๐ค [๊ฒ์คํธ] ์์ ์์ธ ์ถ๊ฐ: '{term}' (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ)" dd_update = gr.update(choices=list(_read_local_whitelist())) if current_input.strip(): ft, ho, cc, mr = process_text(current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl) return status_msg, ft, ho, cc, mr, dd_update, user_temp_wl return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), dd_update, user_temp_wl
def delete_whitelist_entry(term, user_role, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl): term = (term or "").strip() if not term: return "โ ๏ธ ์ญ์ ํ ์์ธ ๋จ์ด๋ฅผ ์ ํํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), user_temp_wl if user_role == "admin": wl = _read_local_whitelist() if term not in wl: return f"๐ซ ์์ธ ๋ชฉ๋ก์ '{term}' ๊ฐ ์์ต๋๋ค.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), user_temp_wl wl = [x for x in wl if x != term] msg, _ = save_whitelist(wl) status_msg = f"๐๏ธ [์์ธ ์ญ์ ] '{term}' ({msg})" else: if term not in user_temp_wl: return f"๐ซ ์์ ์์ธ ๋ชฉ๋ก์ '{term}' ๊ฐ ์์ต๋๋ค.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), user_temp_wl user_temp_wl = [x for x in user_temp_wl if x != term] status_msg = f"๐๏ธ [๊ฒ์คํธ ์์ธ ์ญ์ ] '{term}' (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ)" dd_update = gr.update(choices=list(_read_local_whitelist()), value=None) if current_input.strip(): ft, ho, cc, mr = process_text(current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl) return status_msg, ft, ho, cc, mr, dd_update, user_temp_wl return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), dd_update, user_temp_wl
def refresh_whitelist_choices(): return gr.update(choices=list(_read_local_whitelist()))
def update_choices(): """๋๋กญ๋ค์ด์ ํ์ฌ ์๋ฒ ์ฌ์ ์ ํค๋ค์ ์ํ๋ฒณ์์ผ๋ก ๋
ธ์ถ.""" d = _read_local_dict()26 unmodified lines return gr.update(visible=True), gr.update(visible=False), "none", "โ ์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค.", ""
def perform_logout(): return gr.update(visible=True), gr.update(visible=False), "none", "", "", "", {} return gr.update(visible=True), gr.update(visible=False), "none", "", "", "", {}, []
def handle_page_load(current_role): if current_role in ["admin", "guest"]:66 unmodified lineswith gr.Blocks(**_BLOCKS_KW) as iface: user_role = gr.State("none") user_temp_dict = gr.State(lambda: {}) user_temp_wl = gr.State(lambda: []) # [v153] ๊ฒ์คํธ ์์ ํ์ดํธ๋ฆฌ์คํธ with gr.Column(visible=True, elem_classes="login-box") as login_screen: gr.Markdown("### ๐ ์์คํ
๋ก๊ทธ์ธ") login_id = gr.Textbox(label="์์ด๋")55 unmodified lines btn_delete_entry = gr.Button("๐๏ธ ์ ํ ๋จ์ด ์ญ์ ", variant="stop") btn_refresh_choices = gr.Button("๐ ๋ชฉ๋ก ์๋ก๊ณ ์นจ", variant="secondary")
# [v153] ์์ธ ๋จ์ด(ํ์ดํธ๋ฆฌ์คํธ) โ ์ฌ์ ์นํ์์ ์ ์ธํ ์ด์ /ํฉ์ฑ์ด with gr.Accordion("๐ก๏ธ ์์ธ ๋จ์ด(ํ์ดํธ๋ฆฌ์คํธ) (v153)", open=False): gr.Markdown( "**๋จ์ด์ฅ ์นํ์์ ์์ ํ ์ ์ธํ ๋จ์ด**๋ฅผ ๋ฑ๋กํฉ๋๋ค. " "์: `์์ค์ปฌ๋ ์ดํฐ`, `์์คํ๋ ์`, `ํด๋ฆฌ์์คํฐ` ์ฒ๋ผ ์ฌ์ ์ `์์คโ์ค๋ชฐ`์ด ์์ด๋ " "์ด ๋จ์ด๋ค์ ์๋ฌธ ๊ทธ๋๋ก ์ ์ง๋ฉ๋๋ค. **์ ํํ ์ผ์นํ๋ ๋ฌธ์์ด**๋ก ๋ฑ๋กํ์ธ์ " "(๋ฌธ์์ด ๋ถ๋ถ์ผ์น ๋ฐฉ์์ด๋ผ `์์ค์ปฌ๋ ์ดํฐ`๋ก ๋ฑ๋กํ๋ฉด `์์ค์ปฌ๋ ์ดํฐ๋/์์ค์ปฌ๋ ์ดํฐ๋ฅผ`๋ ํจ๊ป ๋ณดํธ๋ฉ๋๋ค)." ) with gr.Row(): wl_input = gr.Textbox(label="์์ธ๋ก ์ง์ ํ ๋จ์ด(์ด์ )", placeholder="์: ์์ค์ปฌ๋ ์ดํฐ") wl_keys_dd = gr.Dropdown(choices=[], label="๋ฑ๋ก๋ ์์ธ ๋จ์ด ์ ํ", interactive=True) with gr.Row(): btn_add_wl = gr.Button("โ ์์ธ ์ถ๊ฐ", variant="primary") btn_del_wl = gr.Button("๐๏ธ ์ ํ ์์ธ ์ญ์ ", variant="stop") btn_refresh_wl = gr.Button("๐ ์์ธ ๋ชฉ๋ก ์๋ก๊ณ ์นจ", variant="secondary")
proc_inputs = [input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict] proc_inputs = [input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl] 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)3 unmodified lines btn_copy.click(fn=None, inputs=[output_text], outputs=None, js="(t) => { if(t) { navigator.clipboard.writeText(t); alert('โ
๋ณต์ฌ ์๋ฃ!'); } }", 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], inputs=[nw, np, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl], outputs=[status, output_text, output_html, char_count, marker_collection, user_temp_dict], api_name=False) btn_add.click(fn=lambda: gr.update(choices=sorted(_read_local_dict().keys())), outputs=[dict_keys_dd], api_name=False) btn_clear_dict_inputs.click(fn=lambda: ("", ""), outputs=[nw, np], api_name=False)5 unmodified lines ) btn_update_entry.click( fn=update_dict_entry, inputs=[dict_keys_dd, edit_pron, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], inputs=[dict_keys_dd, edit_pron, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl], outputs=[status, output_text, output_html, char_count, marker_collection, dict_keys_dd, user_temp_dict], api_name=False, ) btn_delete_entry.click( fn=delete_dict_entry, inputs=[dict_keys_dd, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], inputs=[dict_keys_dd, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl], outputs=[status, output_text, output_html, char_count, marker_collection, dict_keys_dd, user_temp_dict], api_name=False, ) btn_refresh_choices.click( fn=update_choices, outputs=[dict_keys_dd], api_name=False, ) iface.load(fn=update_choices, outputs=[dict_keys_dd], api_name=False)
# [v153] ํ์ดํธ๋ฆฌ์คํธ ํธ๋ค๋ฌ ๋ฐ์ธ๋ฉ btn_add_wl.click( fn=add_whitelist_entry, inputs=[wl_input, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl], outputs=[status, output_text, output_html, char_count, marker_collection, wl_keys_dd, user_temp_wl], api_name=False, ) btn_add_wl.click(fn=lambda: "", outputs=[wl_input], api_name=False) # ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ btn_del_wl.click( fn=delete_whitelist_entry, inputs=[wl_keys_dd, user_role, input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, user_temp_wl], outputs=[status, output_text, output_html, char_count, marker_collection, wl_keys_dd, user_temp_wl], api_name=False, ) btn_refresh_wl.click(fn=refresh_whitelist_choices, outputs=[wl_keys_dd], api_name=False) iface.load(fn=refresh_whitelist_choices, outputs=[wl_keys_dd], api_name=False) for evt in [font_size.change, ai_mode.change, auto_break.change, smart_pause.change]: evt(fn=process_text, inputs=proc_inputs, outputs=proc_outputs, api_name=False) with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/์กฐํ/์์ "):15 unmodified lines 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) btn_logout.click(fn=perform_logout, inputs=[], outputs=[login_screen, main_app, user_role, login_msg, login_id, login_pw, user_temp_dict, user_temp_wl], 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__":app.py+302-34