Spaces:
Running
Running
Update app.py
Browse files4 unmodified lines567891011121314151617181920216 unmodified lines2829303132333435363738394041424344454647484950515253546 unmodified lines616263646566676869707172737475767778798081828384853 unmodified lines899091929394959697982 unmodified lines1011021031041051061075 unmodified lines1131141151161171181191201211221231241251261271281295 unmodified lines1351361371381391401411421431441451461471481491501516 unmodified lines1581591601611621631641651661671681691701711728 unmodified lines1811821831841851861871881891901911928 unmodified lines2012022032042052062078 unmodified lines2162172182192202212229 unmodified lines2322332342352362372382392402412422436 unmodified lines25025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443514 unmodified lines45045145245345445545645745845946046146246346446546646746846947047147247347447547647747847910 unmodified lines4904914924934944954962 unmodified lines49950050150250350450516 unmodified lines5225235245255265275285295305315325335345355365375385395405411 unmodified line54354454554654754854955055155255355417 unmodified lines5725735745755765775789 unmodified lines58858959059159259359472 unmodified lines66766866967067167267367467567667767836 unmodified lines71571671771871972072172272372472572672772872973073173273373473573619 unmodified lines7567577587597607617627637647657667677687697707717727737747757767777787792 unmodified lines7827837847857867877887897907917927937947957967977987998008011 unmodified line8038048058068078088098108118128138148158168178188198208218228238248258268272 unmodified lines8308318328338348358364 unmodified linesimport reimport html# =====================================================================# VoiceScript-AI v147 ์ ๊ทธ๋ ์ด๋# - P0: ์ฌ์ ์๊ตฌ ์ ์ฅ (HF Dataset ์๋ ์ปค๋ฐ) + AUTO_MERGE ๋นํ๊ดดํ# - P1: ๋งค์นญ ์์ง ๊ต์ฒด (2๋จ๊ณ: ํ๊ธฐ ์ ๊ทํ -> ๋ฐ์ ์นํ, ๊ณต๋ฐฑ ๋ฌด์ ๋งค์นญ)# - G2P ์๋ ์ ํ: g2pk -> g2pk2 -> g2pkk# - Kiwi: "์ค๊ฐ ๋ง 2.0" ๋ชจ๋(ํ์ฌ ๊ธฐ๋ฐ ๋ณดํธ) + ํธํก๊ตฌ ๋ฆฌ์คํ์ด์ฑ(์ต์ )# - TTS ๋ฏธ๋ฆฌ๋ฃ๊ธฐ: ๐ ๋ธ๋ผ์ฐ์ ๋ด์ฅ ์์ฑ + ๐ ๋ฌด๋ฃ edge-tts/gTTS (ํคยท๊ฒฐ์ ์์)# * ์๋ฌธ์ ์ ๋ ์์ ํ์ง ์๊ณ ๋ณํ ์ถ๋ ฅ๋ง ์์ฑํ๋ค๋ ์์น ์ ์ง# =====================================================================# ---------- G2P ๋ก๋ฉ (์๋ ์ ํ ์์ ์ฅ์น) ----------g2p = NoneG2P_BACKEND = "์์"for _mod in ("g2pk", "g2pk2", "g2pkk"):6 unmodified linesexcept Exception as _e:print(f"G2P {_mod} ๋ก๋ฉ ์คํจ: {_e}")# ---------- Kiwi ๋ก๋ฉ (์ ํ ๊ธฐ๋ฅ: ์ค๊ฐ ๋ง 2.0 / ํธํก๊ตฌ ๋ฆฌ์คํ์ด์ฑ) ----------kiwi = Nonetry:from kiwipiepy import Kiwikiwi = 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_downloadif HF_TOKEN:6 unmodified linesSYNC_STATE = f"์๊ตฌ ์ ์ฅ์ ์ฐ๊ฒฐ ์คํจ({_e}) - ๋ก์ปฌ ์ ์ฅ๋ง ๋์"print(f"[์ฌ์ ์ ์ฅ์] {SYNC_STATE}")# ์ฌ์์ ์์๋ 'ํค๊ฐ ์์ ๋๋ง' ์ถ๊ฐ๋๋ ๊ธฐ๋ณธ ๋จ์ด (์ฌ์ฉ์ ๋ฑ๋ก๊ฐ์ ์ ๋ ๋ฎ์ด์ฐ์ง ์์)AUTO_MERGE_WORDS = [["GS๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS ๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS์ต", "์ง์์ค์ต"],["GS ์ต", "์ง์์ค์ต"],["26SS", "์ด์ญ์ก ์์ค์์ค"],["26 SS", "์ด์ญ์ก ์์ค์์ค"],["26S/S", "์ด์ญ์ก ์์ค์์ค"],["26 S/S", "์ด์ญ์ก ์์ค์์ค"],["55", "์ค์ค"],["55์ฌ์ด์ฆ", "์ค์ค ์ธ์ด์ฆ"],["55 ์ฌ์ด์ฆ", "์ค์ค ์ธ์ด์ฆ"],["66", "์ต์ก"],["66์ฌ์ด์ฆ", "์ต์ก ์ธ์ด์ฆ"],["66 ์ฌ์ด์ฆ", "์ต์ก ์ธ์ด์ฆ"],["77", "์น ์น "],["77์ฌ์ด์ฆ", "์น ์น ์ธ์ด์ฆ"],["77 ์ฌ์ด์ฆ", "์น ์น ์ธ์ด์ฆ"],["88", "ํํ"],["88์ฌ์ด์ฆ", "ํํ ์ธ์ด์ฆ"],["88 ์ฌ์ด์ฆ", "ํํ ์ธ์ด์ฆ"],["100%", "๋ฐฑํ๋ก"],["100 %", "๋ฐฑํ๋ก"],["100ํ๋ก", "๋ฐฑํ๋ก"],["59,900์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59,900 ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59900์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59900 ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["5๋ง 9์ฒ 9๋ฐฑ ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["3๊ฐ์", "์ผ๊ฐ์"],["3 ๊ฐ์", "์ผ๊ฐ์"],["1๋ง์๋", "๋ง์๋"],["1๋ง ์๋", "๋ง์๋"],["1๋ง ์ ๋", "๋ง์๋"],["1๋ง์ ๋", "๋ง์๋"],["~์ ๋๋ค", "~์ต๋๋ค"], ["์์ ์ ๋๋ค", "์์ ์ ๋๋ค"], ["๊ฒฐ๊ณผ์ ์ผ๋ก", "๊ฒฐ๊ณผ์ ์ผ๋ก"],["์ผ์ฑ", "์ผ์ฑ"], ["LG", "์์ง"], ["SK", "์์ค์ผ์ด"],["์ํ", "์ํ"], ["์ ํ", "์ ํ"], ["๊ฐ๊ฒฉ", "๊ฐ๊ฒฉ"],["๋ฐฐ์ก", "๋ฐฐ์ก"], ["ํ ์ธ", "ํ ์ธ"], ["์ด๋ฒคํธ", "์ด๋ฒคํธ"]]# ---------- ์ฌ์ ์ฝ๊ธฐ/์ฐ๊ธฐ ๊ณตํต ํฌํผ ----------def _read_local_dict():try:with open(dict_path, "r", encoding="utf-8") as f:3 unmodified linesreturn {}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)2 unmodified linesos.replace(tmp_path, dict_path)def _push_remote():"""์ฌ์ ์ HF Dataset ์ ์ฅ์์ ์ปค๋ฐ (์๊ตฌ ๋ณด์กด). ์คํจํด๋ ๋ก์ปฌ ์ ์ฅ์ ์ ์ง."""if not hf_api:return "๋ก์ปฌ ์ ์ฅ๋ง ๋จ (์ฌ์์ ์ ์ ์ค ๊ฐ๋ฅ - HF_TOKEN ์ค์ ํ์)"try:5 unmodified linesreturn f"์๊ฒฉ ์ ์ฅ ์คํจ({e}) - ๋ก์ปฌ๋ง ์ ์ฅ๋จ"def save_dict(dict_map):"""์ ์ฅ + ์๊ฒฉ ์ปค๋ฐ + ์์ง ์บ์ ๋ฌดํจํ. ์ํ ๋ฌธ์์ด ๋ฐํ."""_write_local_dict(dict_map)_ENGINE_CACHE["sig"] = Nonemsg = _push_remote()return f"{msg} ยท ํ์ฌ ์ฌ์ {len(dict_map)}๊ฐ"def perform_startup_recovery():"""์์ ์: ๋ก์ปฌ(์ปค๋ฐ๋ณธ) <- ์๊ฒฉ(์ต์ ๋ฑ๋ก๋ถ) ํค ๋จ์ ๋ณํฉ.โ ์๊ฒฉ ํ์ผ์ด ์์ผ๋ฉด ์๊ฒฉ์ด ์ต์ -> ๋ก์ปฌ์ ๋ณํฉ(์ถฉ๋ ์ ์๊ฒฉ ์ฐ์ ).โก ๋ค์ด๋ก๋ ์์ธ(๋คํธ์ํฌ/์ธ์ฆ/์ผ์ ์ฅ์ )๋ฉด ๋ก์ปฌ ๋ณดํธ -> ์๊ฒฉ์ ์๋ฌด๊ฒ๋ pushํ์ง ์์.โข ๊ธฐ๋ณธ ๋จ์ด๋ 'ํค๊ฐ ์์ ๋๋ง' ์ถ๊ฐ (์ฌ์ฉ์ ๋ฑ๋ก๊ฐ ์ ๋ ๋ฎ์ด์ฐ์ง ์์)."""dict_map = _read_local_dict()remote_ok = Trueif hf_api:5 unmodified linesfor 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) == 404if is_404:print("[์ฌ์ ] ์๊ฒฉ์ ์์ง ์ฌ์ ํ์ผ ์์ - ์ต์ด ๋ถํ ์ผ๋ก ์ฒ๋ฆฌ (๋ก์ปฌ ์ ์ง)")else:remote_ok = Falseprint(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()6 unmodified linesflat_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]8 unmodified linesif not k:continueif 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)8 unmodified linesreturn 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 = [], 08 unmodified linesreturn segsdef _g2p_shielded(chunk, mode, eng):"""์ฌ์ ์ด ๋ง๋ค์ด๋ธ ํ๊ธฐ(์ ๊ทํ ๊ฒฐ๊ณผยท๋ณดํธ ๋จ์ด)๋ g2p๋ฅผ ๊ฑด๋๋ฐ๊ณ ๋๋จธ์ง๋ง ๋ณํ."""if not chunk:return chunkif not eng["sh_pat"]:9 unmodified linesreturn "".join(res)def convert_text(text, mode, flat_dict):"""์ ์ฒด ๋ณํ ํ์ดํ๋ผ์ธ: ํ๊ธฐ ์ ๊ทํ -> ๋ฐ์ ์นํ -> ๋จ์ ๊ตฌ๊ฐ์ ์ซ์ ์ ๊ท์-> ๋ฐ์ ์นํ 2์ฐจ -> g2p(๋ณดํธ ๊ตฌ๊ฐ ์ ์ธ).(2026-08-29 ํ์ : kiwi.space ๊ธฐ๋ฐ '๋์ด์ฐ๊ธฐ ๊ต์ ํ ๋งค์นญ'์ ์ฒดํฌ๋ฐ์คยท์ด๋ฒคํธยท๋ฐ์ธ๋ฉ๊น์ง์์ ์ ๊ฑฐ๋จ. ์๋ฌธ ๋น๊ณต๋ฐฑ ๋ฌธ์์ด์ 1๋ฐ์ดํธ๋ ๋ฐ๊พธ์ง ์๋ ๋ถ๋ณ์ ์๋, ์ธํ ๋ณํ๋ํธํก๊ตฌ ๋ฆฌ์คํ์ด์ฑ(add_breath_groups) ํ๋๋ฟ์ด๋ค.)๋ถ๋ณ์: re.sub(r'\s+', '', input) == re.sub(r'\s+', '', output)"""eng = get_engine(flat_dict)t = _sub_phase1(text, eng)out = []6 unmodified linesout.append(c2 if fixed2 else _g2p_shielded(c2, mode, eng))return "".join(out)# ===== BREATH-BLOCK-BEGIN =====# ---------- ํธํก๊ตฌ ๋ฆฌ์คํ์ด์ฑ (์๋ฌธ ๋ถ๋ณ + ์์ ๋ง ํฌํจ) ----------# [2026-08-29 v2] ์ ๊ฒฝ๊ณ ๋ถํ + ๋ฌธ๋์ฃผ์ ๋ถ๋ฆฌ + ๊ทธ๋ฃน ๋ด ๋ถ์ฌ์ฝ๊ธฐ + MAX9# ๋ถ๋ณ์: re.sub(r'[\s/]', '', ์ ๋ ฅ) == re.sub(r'[\s/]', '', ์ถ๋ ฅ)# - ํ๋ฝ: ๊ณต๋ฐฑ ์ฝ์ ยท์ญ์ , ํธํก๊ตฌ ์ฌ์ด ' / ' ๋ง์ปค ๋ถ์ฐฉ(=๊ณต๋ฐฑ ๋ณํ)# - ๊ธ์ง: ์กฐ์ฌ/์ด๋ฏธ ๋ถ๋ฆฌ, ๊ธ์ ๋จ์ ๋ณํ, ์ด์ ๋ณํฉยท๋ถํ , ์คํ์ ๊ต์ # โ๏ธ ๋ณํ์ด ์ด์ํด์ก์ ๋: ์ด ํ ์ค๋ง False ๋ก ๋ฐ๊พธ๋ฉด ๊ธฐ์กด(v1) ๋์์ผ๋ก ์ฆ์ ๋ณต๊ทUSE_NEW_BREATH = True_BREATH_TAGS = {"EC", "EF", "ETN", "ETM", "SF", "SP", "SE", "SY"} # ์ /๋ฌธ ๊ฒฝ๊ณ๋ง (๊ฒฉ์กฐ์ฌ X)_BREATH_MIN = 6 # ํ ํธํก ์ต์ ๊ธ์ (6 ๋ฏธ๋ง์ด๋ฉด ๋ถํ ์ ํจ)_BREATH_MAX = 15 # ํ ํธํก ์ํ ๊ธ์ (๋ฌด๋ฃ ์ ์ฉ: 12โ15 โ ๋ ๊ธธ๊ฒ, ๊ณผ๏ฟฝ๏ฟฝ๏ฟฝํ ๋ฐฉ์ง)_BREATH_JOIN = True # True: ๊ฐ์ ํธํก๊ตฌ ์ ๊ณต๋ฐฑ ์ ๊ฑฐ(๋ถ์ฌ ์ฝ๊ธฐ) / False: 1์นธ ์ ์ง_CODE_TAGS = ("SL", "SN", "SW") # ์๋ฌธยท์ซ์ยท๊ธฐํธ (์ฝ๋ ํด๋ฌ์คํฐ ๋ด๋ถ ๋ถํ ๊ธ์ง)_BREATH_TAGS_LEGACY = {"JKS", "JKC", "JKG", "JKO", "JKB", "JKV", "JX", "JC","EF", "EC", "ETN", "ETM", "SF", "SP", "SE", "SY"}_BREATH_MIN_LEGACY = 5_BREATH_MAX_LEGACY = 14def _breath_len(e):return len(re.sub(r"[^๊ฐ-ํฃa-zA-Z0-9]", "", e)) or len(e)def _breath_last_tag(e):try:toks = kiwi.tokenize(e)return toks[-1].tag if toks else Noneexcept Exception:return Nonedef add_breath_groups_legacy(text):"""[๋กค๋ฐฑ์ฉ] ๊ธฐ์กด v1 ํจ์ (๊ฒฉ์กฐ์ฌ ํฌํจ ํ๊ทธ + MIN5/MAX14 + ๊ทธ๋ฃน ๋ด 1์นธ ๊ณต๋ฐฑ)"""if kiwi is None or not text or not text.strip():return textorig_eojeols = text.split()if len(orig_eojeols) <= 1:return textgroups, cur, cur_len = [], [], 0for e in orig_eojeols:cur.append(e)cur_len += _breath_len(e)tag = _breath_last_tag(e)if (tag in _BREATH_TAGS_LEGACY and cur_len >= _BREATH_MIN_LEGACY) or cur_len >= _BREATH_MAX_LEGACY:groups.append(cur)cur, cur_len = [], 0if cur:groups.append(cur)return " / ".join(" ".join(g) for g in groups)def add_breath_groups_new(text):"""[v2] ์ ๊ฒฝ๊ณ ๋ถํ + ๋ฌธ๋์ฃผ์ ๋ถ๋ฆฌ + ๊ทธ๋ฃน ๋ด ๋ถ์ฌ์ฝ๊ธฐ + MAX9"""if kiwi is None or not text or not text.strip():return textes = text.split()n = len(es)if n <= 1:return textgroups, cur, cur_len = [], [], 0for i, e in enumerate(es):tag = _breath_last_tag(e)# โ ๋ฌธ๋ ์ฃผ์ (์/๋) = ๋ ๋ฆฝ ํธํก๊ตฌ โ "์ค๋์ / ..."if i == 0 and tag == "JX" and n > 1:groups.append([e])continue# โก ์๋ฌธยท์ซ์ยท๊ธฐํธ ํด๋ฌ์คํฐ๋ ๋ด๋ถ ๋ถํ ๊ธ์ง โ "GS๋ง์ด์ต 26SS" ์ ์งif cur and tag in _CODE_TAGS and _breath_last_tag(cur[-1]) in _CODE_TAGS:cur.append(e); cur_len += _breath_len(e)continuecur.append(e); cur_len += _breath_len(e)# โข ์ /๋ฌธ ๊ฒฝ๊ณ + ์ต์ยท์ต๋ ์ถฉ์กฑ ์ ๋ถํif tag in _BREATH_TAGS and _BREATH_MIN <= cur_len <= _BREATH_MAX:groups.append(cur); cur, cur_len = [], 0continue# โฃ ์ํ ์ด๊ณผ โ ๊ฐ์ ๋ถํ (๋ฐฉ๊ธ ๋ถ์ธ ์ด์ ์ด ์ฝ๋๋ฉด ์์ธ)if cur_len > _BREATH_MAX and tag not in _CODE_TAGS:if len(cur) >= 2 and cur_len - _breath_len(e) >= _BREATH_MIN:groups.append(cur[:-1]); cur = [cur[-1]]; cur_len = _breath_len(e)else:groups.append(cur); cur, cur_len = [], 0if cur:groups.append(cur)sep = "" if _BREATH_JOIN else " "return " / ".join(sep.join(g) for g in groups)def _invariant_ok(out, src):"""์๋ฌธ ๋น๊ณต๋ฐฑ(๊ณต๋ฐฑยท'/' ์ ๊ฑฐ) ๋ฌธ์์ด์ด ๊ทธ๋๋ก์ธ์ง ํ์ธ"""try:return re.sub(r'[\s/]', '', src) == re.sub(r'[\s/]', '', out)except Exception:return Falsedef _max_ok(out):"""ํธํก๊ตฌ๋ณ ๊ธ์ ์(๊ณต๋ฐฑยท'/' ์ ์ธ)๊ฐ _BREATH_MAX ์ดํ์ธ์ง ํ์ธ"""try:for g in out.split(" / "):if len(re.sub(r'[\s/]', '', g)) > _BREATH_MAX:return Falsereturn Trueexcept Exception:return Falsedef add_breath_groups(text):"""๋ฉ์ธ ์ํธ๋ฆฌ: ์ ํจ์ ์คํ โ ๋ถ๋ณ์ ๊ฒ์ฆ โ ์คํจ ์ ์๋์ผ๋ก ๊ธฐ์กด(v1) ๊ฒฐ๊ณผ ๋ฐํ.USE_NEW_BREATH = False ๋ก ๋ฐ๊พธ๋ฉด ์ฒ์๋ถํฐ v1(legacy)๋ฅผ ์ฌ์ฉํฉ๋๋ค."""if not USE_NEW_BREATH:return add_breath_groups_legacy(text)out = add_breath_groups_new(text)if _invariant_ok(out, text):return out# ์์ ๋ง: ์ ํจ์ ์ถ๋ ฅ์ด ๋ถ๋ณ์(๊ธ์ ์ ์คยท๋ณํ)์ ๊นจ๋ฉด ๊ธฐ์กด ํจ์๋ก ์๋ ๋ณต๊ท# (MAX ๊ธธ์ด ์ด๊ณผ๋ "๋ฌธ์ฒด" ๋ฌธ์ ์ด๋ฏ๋ก ํด๋ฐฑ ๋์์ด ์๋ โ _BREATH_MIN/MAX๋ก ํ๋)return add_breath_groups_legacy(text)# ===== BREATH-BLOCK-END =====# ---------- [๋ฌด๋ฃ ์ ์ฉ] TTS: edge-tts โ gTTS โ ๐ ๋ธ๋ผ์ฐ์ ํด๋ฐฑ (API ํคยท๊ฒฐ์ ์์) ----------# ๐ฐ ์ ๋ฃ ํด๋ผ์ฐ๋ TTS(Google/Azure/ElevenLabs)๋ ์ผ์ ์ฌ์ฉํ์ง ์์.# - edge-tts: Microsoft Edge ์ ๊ฒฝ๋ง ์์ฑ(ko-KR-SunHiNeural ๋ฑ) โ ํค ๋ถํ์, ๋ฌด๋ฃ# - gTTS: Google Translate TTS โ ํค ๋ถํ์, ๋ฌด๋ฃ (ํด๋ฐฑ)# - ๐ ๋ธ๋ผ์ฐ์ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ: ์ต์ข ํด๋ฐฑ (๊ธฐ์กด ๊ทธ๋๋ก)# requirements.txt์ "edge-tts>=7.0.0"์ "gtts>=2.5.0" ์ถ๊ฐ.FREE_TTS_STATUS = "๐ ๋ฌด๋ฃ TTS ์ค๋น๋จ (edge-tts โ gTTS โ ๋ธ๋ผ์ฐ์ ํด๋ฐฑ, ํคยท๊ฒฐ์ ์์)"_EDGE_VOICE = os.environ.get("FREE_TTS_VOICE", "ko-KR-SunHiNeural")def _tts_clean(text):"""ํธํก๊ตฌ ' / ' โ ์ผํ(์ ๊ฒฝ๋ง ์์ฑ์ด ์ผํ์์ ์์ฐ์ค๋ฝ๊ฒ ๋ฉ์ถค), ๋ง์ปค(@@) ์ ๊ฑฐ"""return text.replace("@@", "").replace(" / ", ", ")def synth_free_edge(text, voice=_EDGE_VOICE):"""edge-tts: ํค ์๋ ๋ฌด๋ฃ ์์ฑ ํฉ์ฑ. ์คํจ ์ None ๋ฐํ."""try:import asyncio, edge_ttsout = "tts_edge.mp3"async def _go():c = edge_tts.Communicate(text, voice)await c.save(out)asyncio.run(_go())if os.path.exists(out) and os.path.getsize(out) > 100:return outexcept Exception as e:print(f"[FREE-TTS] edge-tts ์คํจ: {e}")return Nonedef synth_free_gtts(text):"""gTTS: ํค ์๋ ๋ฌด๋ฃ ์์ฑ ํฉ์ฑ(ํด๋ฐฑ). ์คํจ ์ None ๋ฐํ."""try:from gtts import gTTSout = "tts_gtts.mp3"gTTS(text, lang="ko").save(out)if os.path.exists(out) and os.path.getsize(out) > 100:return outexcept Exception as e:print(f"[FREE-TTS] gTTS ์คํจ: {e}")return Nonedef tts_free_click(text, user_temp_dict):"""๐ ๋ฌด๋ฃ TTS ๋ฃ๊ธฐ ๋ฒํผ ํธ๋ค๋ฌ: edge-tts โ gTTS ์์, ์ ๋ถ ์คํจ ์ ๋ธ๋ผ์ฐ์ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ ์๋ด."""if not text or not text.strip():return None, "โ ๏ธ ๋จผ์ [๋ณํํ๊ธฐ]๋ฅผ ๋๋ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์."try:t = _tts_clean(text)path = synth_free_edge(t) or synth_free_gtts(t)if path:engine = "edge-tts" if "edge" in path else "gTTS"return path, f"โ ๋ฌด๋ฃ TTS ์์ฑ ์๋ฃ ({engine}, ํคยท๊ฒฐ์ ์์ โ {_EDGE_VOICE})"return None, "โ ๋ฌด๋ฃ TTS ์์ฑ ์คํจ โ ์๋ ๐ ๋ธ๋ผ์ฐ์ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ๋ฅผ ์ฌ์ฉํ์ธ์."except Exception as e:return None, f"โ ์ค๋ฅ: {e}"# ---------- G2P ๋ชจ๋ ----------_KIWI_PROTECT_TAGS = ("NNP", "SL", "SH", "SN", "SW") # ๊ณ ์ ๋ช ์ฌยท์ธ๋์ดยทํ์ยท์ซ์ยท๊ธฐํธdef apply_g2p(text, mode):if g2p is None or mode.startswith("๋๊ธฐ"):return text14 unmodified lineselse: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)continueif not re.match(r'^[๊ฐ-ํฃ]+[.,!?]*$', w):res.append(w) # ์๋ฌธยท์ซ์ ์์ธ ์ด์ ์ ์ํ ๋ณดํธcontinuetry: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:10 unmodified linesreturn "".join(res)return text# ---------- ์ซ์/๋จ์ ์ค๋งํธ ์ ๊ท์ (๊ธฐ์กด ์ ์ง) ----------def number_to_korean(num_str):num_str = num_str.replace(',', '')try:2 unmodified linesreturn num_strif n == 0:return "์"digits, units, large_units = ["", "์ผ", "์ด", "์ผ", "์ฌ", "์ค", "์ก", "์น ", "ํ", "๊ตฌ"], ["", "์ญ", "๋ฐฑ", "์ฒ"], ["", "๋ง", "์ต", "์กฐ"]res, str_n = [], str(n)[::-1]for i in range(0, len(str_n), 4):chunk, chunk_res = str_n[i:i+4], []16 unmodified linestext = 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": "ํํ"}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)unit_dict = {"mm": "๋ฐ๋ฆฌ๋ฏธํฐ", "cm": "์ผํฐ๋ฏธํฐ", "m": "๋ฏธํฐ", "km": "ํฌ๋ก๋ฏธํฐ", "g": "๊ทธ๋จ", "kg": "ํฌ๋ก๊ทธ๋จ", "ml": "๋ฐ๋ฆฌ๋ฆฌํฐ", "L": "๋ฆฌํฐ"}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, breath_groups, 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', '')char_count = f"๊ณต๋ฐฑ ํฌํจ: {len(clean_text)}์ / ๊ณต๋ฐฑ ์ ์ธ: {len(clean_text.replace(' ', '').replace(chr(10), ''))}์"flat_dict = load_dict(user_temp_dict)if not flat_dict:1 unmodified lineelse:final_text = convert_text(input_text, ai_mode, flat_dict)if breath_groups and kiwi is not None:final_text = add_breath_groups(final_text)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 = []17 unmodified linesdef get_file():return dict_path if os.path.exists(dict_path) else Nonedef add_and_update(word, pronun, user_role, current_input, ai_mode, auto_break, smart_pause, breath_groups, font_size, user_temp_dict):if not word or not pronun:return "โ ๏ธ ๋จ์ด์ ๋ฐ์์ ์ ๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dictword, pronun = word.strip(), pronun.strip()9 unmodified linesuser_temp_dict[word] = pronunstatus_msg = f"๐ค [๊ฒ์คํธ] '{word}' -> '{pronun}' ์์ ์ ์ฅ ์๋ฃ! (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ - ์๊ตฌ ๋ฑ๋ก์ ์ด๋๋ฏผ์๊ฒ ์์ฒญ)"if current_input.strip():ft, ho, cc, mr = process_text(current_input, ai_mode, auto_break, smart_pause, breath_groups, font_size, user_temp_dict)return status_msg, ft, ho, cc, mr, user_temp_dictreturn status_msg, gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict72 unmodified linesreturn gr.update(visible=False), gr.update(visible=True), current_role, "", gr.update(), gr.update()return gr.update(visible=True), gr.update(visible=False), "none", "", "", ""custom_css = """#prompter_html_wrap, #prompter_html_wrap .prose { width: 100% !important; text-align: left !important; }#input_text_area textarea, #output_text_area textarea, #prompter_output_div { font-family: sans-serif !important; height: 500px !important; overflow-y: auto !important; padding: 16px !important; line-height: 1.8 !important; }#prompter_output_div { background-color: #333 !important; color: #fff !important; border-radius: 8px !important; white-space: pre-wrap !important; }.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() {36 unmodified lines}"""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 {}19 unmodified lineswith gr.Column():input_text = gr.Textbox(label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ", elem_id="input_text_area")with gr.Group():ai_mode = gr.Radio(choices=_mode_choices, value="์ค๊ฐ ๋ง (๋ช ์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", label="๐ค AI ๋ฐ์ ๋ณํ ๊ฐ๋")auto_break = gr.Checkbox(label="๐ ๋ฌธ์ฅ ๋ ์๋ ์ค๋ฐ๊ฟ", value=False)smart_pause = gr.Checkbox(label="โฑ๏ธ ์ผํ ๋ค ํธํก์ ( / ) ์ถ๊ฐ", value=False)# [์ ๊ฑฐ๋จ 2026-08-29] "๐ ๋์ด์ฐ๊ธฐ ๊ต์ ํ ๋งค์นญ" ์ฒดํฌ๋ฐ์ค ์ ๊ฑฐ โ kiwi.space()๊ฐ ์กฐ์ฌ/์ด๋ฏธ๋ฅผ ๋ถ๋ฆฌํด ์๋ฌธ ๊ธ์๋ฅผ ํผ์ํ์breath_groups = gr.Checkbox(label=("๐ฌ๏ธ ์๋ฏธ ๋์ด์ฝ๊ธฐ (Kiwi ํธํก๊ตฌ, ์คํ์ ) โ โ Kiwi ๋ก๋๋จ"if kiwi is not Noneelse "๐ฌ๏ธ ์๋ฏธ ๋์ด์ฝ๊ธฐ (Kiwi ํธํก๊ตฌ, ์คํ์ ) โ โ ๏ธ Kiwi ๋ฏธ์ค์น (requirements.txt์ kiwipiepy ์ถ๊ฐ ํ์)"),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_tts_free = gr.Button("๐ ๋ฌด๋ฃ TTS ๋ฃ๊ธฐ (edge-tts)", variant="secondary")btn_copy = gr.Button("๐ ๋ณต์ฌ", variant="secondary")btn_clr = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop")2 unmodified lineswith gr.Accordion("๐ ํ ์คํธ ๊ฒฐ๊ณผ (ํด๋ฆญํ์ฌ ํผ์น๊ธฐ)", open=False):output_text = gr.Textbox(lines=8, show_label=False, elem_id="output_text_area")marker_collection = gr.Textbox(label="๐จ ์์ ํ์ ๋จ์ด (@@)", interactive=False, lines=2)tts_status = gr.Textbox(label="๐ ๋ฌด๋ฃ TTS ์ํ", value=FREE_TTS_STATUS, interactive=False)tts_audio = gr.Audio(label="๐ ๋ฌด๋ฃ TTS ์ค๋์ค (ํคยท๊ฒฐ์ ์์)")gr.Markdown("---")gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ**")with gr.Row():nw = gr.Textbox(label="๋จ์ด (2๊ธ์ ์ด์)")np = gr.Textbox(label="์ํ๋ ๋ฐ์")with gr.Column():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, breath_groups, 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)1 unmodified linebtn_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_tts_free.click(fn=tts_free_click, inputs=[output_text, user_temp_dict], outputs=[tts_audio, tts_status], api_name=False)btn_add.click(fn=add_and_update, inputs=[nw, np, user_role, input_text, ai_mode, auto_break, smart_pause, breath_groups, 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, breath_groups.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)2 unmodified linesup_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)4 unmodified lines5678910111213141516171819202122236 unmodified lines3031323334353637383940414243444546476 unmodified lines545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601613 unmodified lines1651661671681691701711722 unmodified lines1751761771781791805 unmodified lines1861871881891901911921931941951961975 unmodified lines2032042052062072082092102112122132142152162176 unmodified lines2242252262272282292302312322332342358 unmodified lines2442452462472482492502512522532542558 unmodified lines2642652662672682698 unmodified lines2782792802812822839 unmodified lines2932942952962972982993006 unmodified lines30730830931031131231314 unmodified lines32832933033133233333433533633733810 unmodified lines3493503513523533543552 unmodified lines35835936036136236336436516 unmodified lines3823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184191 unmodified line42142242342442542642742842943017 unmodified lines4484494504514524534549 unmodified lines46446546646746846947072 unmodified lines54354454554654754854955055155255355455555655755855956056136 unmodified lines59859960060160260360460560619 unmodified lines6266276286296306316326336346356366376386396406416426436442 unmodified lines6476486496506516526536546556566576586596606616626636646651 unmodified line6676686696706716726736746756766776786796806816826836846856866876886892 unmodified lines6926936946956966974 unmodified linesimport reimport html# ============================================================================# VoiceScript-AI v149 โ ๋จ์ด์ฅ ์ต๊ฐํ (์ธ๋ถ ํ ํฌ๋์ด์ ๋ฏธ์ฌ์ฉ)# ============================================================================# [2026-08-29]# - ์ธ๋ถ ํ์ฌ ํ ํฌ๋์ด์ ์์กด์ฑ ์์ ์ ๊ฑฐ. ๋์ด์ฝ๊ธฐ๋ ๋จ์ด์ฅ ๊ธฐ๋ฐ ํ๊ธฐ ์ ๊ทํ# + ๋ฌธ์ฅ๋ถํธ ํธํก์ (`, ` ๋ค์ `/`, ๋ฌธ์ฅ ๋์์ `\n\n`)์ผ๋ก๋ง ๋ง๋ ๋ค.# - ์๋ ๋ฐ์ ์ฌ์ 172๊ฐ (์ฌ์ฉ์ ํค ๋ฑ๋ก๊ฐ ์ฐ์ โ ๋นํ๊ดด ๋ณํฉ).# - ์ค๋์ค ์์ฑ ์ฝ๋(edge-ttsยทgTTSยทAudio ์์ ฏ) ์์ ์ ๊ฑฐ โ ๋๋ณธ ๋ณํ ์ ์ฉ ์ฑ.# - ๋จ์ด์ฅ ์ ๊ท 60๊ฐ ์ถ๊ฐ(์๋ฌธ ์ฝ์ดยท์นดํยท์ฑยท๋จ์ยท์ฌ์ด์ฆยท๊ธฐ๊ฐยท์์ผ).# - ๋ฒํผ ํ์ ๊ฐ์ scaleยทsize๋ก ํต์ผ. ์ด๊ธฐํ ๋ฒํผ์ ๋ง์ง๋ง ์๋ฆฌ๋ก ๋ณต์.# ============================================================================# ---------- G2P ์๋ ์ ํ (g2pk โ g2pk2 โ g2pkk) ----------g2p = NoneG2P_BACKEND = "์์"for _mod in ("g2pk", "g2pk2", "g2pkk"):6 unmodified linesexcept Exception as _e:print(f"G2P {_mod} ๋ก๋ฉ ์คํจ: {_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")# ---------- 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_downloadif HF_TOKEN:6 unmodified linesSYNC_STATE = f"์๊ตฌ ์ ์ฅ์ ์ฐ๊ฒฐ ์คํจ({_e}) - ๋ก์ปฌ ์ ์ฅ๋ง ๋์"print(f"[์ฌ์ ์ ์ฅ์] {SYNC_STATE}")# ============================================================================# AUTO_MERGE_WORDS โ ์๋ ๋ฐ์ ์ฌ์ # ์ฌ์ฉ์ ์ฌ์ ์ ๊ฐ์ ํค๊ฐ ์ด๋ฏธ ์์ผ๋ฉด setdefault ๋๋ถ์ ๋ฎ์ด์ฐ์ง ์์.# ============================================================================AUTO_MERGE_WORDS = [# โโ ์๋ฌธ ์ฝ์ดยท๋ธ๋๋ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["H&M", "์์ด์น ์ค ์ "],["ZARA", "์๋ผ"],["UNIQLO", "์ ๋ํด๋ก"],["YouTube", "์ ํ๋ธ"],["Instagram", "์ธ์คํ๊ทธ๋จ"],["iPhone", "์์ดํฐ"], ["iOS", "์์ด์ค์์ค"], ["IOS", "์์ด์ค์์ค"],["Android", "์๋๋ก์ด๋"],["Mac", "๋งฅ"], ["MacBook", "๋งฅ๋ถ"],["Windows", "์๋์ฐ"],["NAVER", "๋ค์ด๋ฒ"], ["DAUM", "๋ค์"], ["KAKAO", "์นด์นด์ค"],["CJ", "์จ์ ์ด"], ["HD", "์์ด์น๋"], ["LS", "์์์ค"],["LG", "์์ง"], ["SK", "์์ค์ผ์ด"], ["KT", "์ผ์ดํฐ"],["26SS", "์ด์ญ์ก ์์ค์์ค"], ["26 SS", "์ด์ญ์ก ์์ค์์ค"],["26S/S", "์ด์ญ์ก ์์ค์์ค"], ["26 S/S", "์ด์ญ์ก ์์ค์์ค"],["25SS", "์ด์ญ์ค ์์ค์์ค"], ["25 SS", "์ด์ญ์ค ์์ค์์ค"],["FW", "์ํ๋๋ธ์ "], ["SSFW", "์์ค์์ค ์ํ๋๋ธ์ "],["GS๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"], ["GS ๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS์ต", "์ง์์ค์ต"], ["GS ์ต", "์ง์์ค์ต"],# โโ ์นดํยท์ฑยท์๋น์ค โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["์นด์นด์คํก", "์นด์นด์ค ํก"],["๋ฐฐ๋ฌ์๋ฏผ์กฑ", "๋ฐฐ๋ฌ์ ๋ฏผ์กฑ"],["๋น๊ทผ๋ง์ผ", "๋น๊ทผ ๋ง์ผ"],["ํ ์ค", "ํ ์ค"], ["์ฟ ํก", "์ฟ ํก"],["๋ค์ด๋ฒ", "๋ค์ด๋ฒ"], ["๋ค์", "๋ค์"], ["์ค", "์ค"],# โโ ์ซ์+๋จ์ ๊ฒฐํฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["14์ธ์น", "์ญ์ฌ ์ธ์น"],["13.3์ธ์น", "์ญ์ผ ์ ์ผ ์ธ์น"],["15์ธ์น", "์ญ์ค ์ธ์น"], ["15.6์ธ์น", "์ญ์ค ์ ์ก ์ธ์น"],["32GB", "์ผ์ญ์ด ๊ธฐ๊ฐ"], ["64GB", "์ก์ญ์ฌ ๊ธฐ๊ฐ"], ["128GB", "๋ฐฑ์ด์ญํ ๊ธฐ๊ฐ"],["256GB", "์ด๋ฐฑ์ค์ญ์ก ๊ธฐ๊ฐ"], ["512GB", "์ค๋ฐฑ์ญ์ด ๊ธฐ๊ฐ"],["1TB", "์ผ ํ ๋ผ๋ฐ์ดํธ"],["1080p", "์ฒํ์ญ ํผ"], ["4K", "ํฌ ์ผ์ด"], ["8K", "์์ ์ผ์ด"],# โโ ๋จ์ ๋ณด์ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["์ธ์น", "์ธ์น"], ["์ผ๋", "์ผ๋"], ["ํ์ด๋", "ํ์ด๋"],["์จ์ค", "์จ์ค"], ["๊ฐค๋ฐ", "๊ฐค๋ฐ"], ["์จ๋", "์จ๋"],["ํฅํ๋ฅด", "ํฅํ๋ฅด"], ["๋ฐ์๋ฆฌํฐ", "๋ฐ์๋ฆฌํฐ"], ["๋ง์ผ", "๋ง์ผ"],["๋ ธํธ", "๋ ธํธ"],["mm", "๋ฐ๋ฆฌ๋ฏธํฐ"], ["cm", "์ผํฐ๋ฏธํฐ"], ["km", "ํฌ๋ก๋ฏธํฐ"],["kg", "ํฌ๋ก๊ทธ๋จ"], ["mg", "๋ฐ๋ฆฌ๊ทธ๋จ"], ["ml", "๋ฐ๋ฆฌ๋ฆฌํฐ"],# โโ ์๋ฌธ ์๋ฅ ์ฌ์ด์ฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["XS", "์์ค์ค"], ["S", "์์ค"], ["M", "์ "], ["L", "์"],["XL", "์์ค์"], ["XXL", "๋๋ธ์์ค์"],["FREE", "ํ๋ฆฌ"], ["ONE SIZE", "์ ์ฌ์ด์ฆ"],# โโ ํ์ซ์ ์ฌ์ด์ฆ(ํ๊ตญ ์๋ฅ ์ฌ์ด์ฆ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["55์ฌ์ด์ฆ", "์ค์ค ์ธ์ด์ฆ"], ["55 ์ฌ์ด์ฆ", "์ค์ค ์ธ์ด์ฆ"],["66์ฌ์ด์ฆ", "์ต์ก ์ธ์ด์ฆ"], ["66 ์ฌ์ด์ฆ", "์ต์ก ์ธ์ด์ฆ"],["77์ฌ์ด์ฆ", "์น ์น ์ธ์ด์ฆ"], ["77 ์ฌ์ด์ฆ", "์น ์น ์ธ์ด์ฆ"],["88์ฌ์ด์ฆ", "ํํ ์ธ์ด์ฆ"], ["88 ์ฌ์ด์ฆ", "ํํ ์ธ์ด์ฆ"],["95์ฌ์ด์ฆ", "๊ตฌ์ค ์ธ์ด์ฆ"], ["95 ์ฌ์ด์ฆ", "๊ตฌ์ค ์ธ์ด์ฆ"],["100์ฌ์ด์ฆ", "๋ฐฑ ์ธ์ด์ฆ"], ["100 ์ฌ์ด์ฆ", "๋ฐฑ ์ธ์ด์ฆ"],["105์ฌ์ด์ฆ", "๋ฐฑ์ค ์ธ์ด์ฆ"], ["105 ์ฌ์ด์ฆ", "๋ฐฑ์ค ์ธ์ด์ฆ"],["110์ฌ์ด์ฆ", "๋ฐฑ์ญ ์ธ์ด์ฆ"], ["110 ์ฌ์ด์ฆ", "๋ฐฑ์ญ ์ธ์ด์ฆ"],["๋จ๋ ์ฌ์ด์ฆ", "๋จ๋ ์ฌ์ด์ฆ"], ["๋จ๋ ์ฌ์ด์ฆ", "๋จ๋ ์ฌ์ด์ฆ"],# โโ %, 100% โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["100%", "๋ฐฑํ๋ก"], ["100 %", "๋ฐฑํ๋ก"], ["100ํ๋ก", "๋ฐฑํ๋ก"],["50%", "์ค์ญํ๋ก"], ["50 %", "์ค์ญํ๋ก"],["20%", "์ด์ญํ๋ก"], ["20 %", "์ด์ญํ๋ก"],["30%", "์ผ์ญํ๋ก"], ["30 %", "์ผ์ญํ๋ก"],["90%", "๊ตฌ์ญํ๋ก"], ["90 %", "๊ตฌ์ญํ๋ก"],# โโ ๊ฐ๊ฒฉ (์ฝค๋ง ์๋ ๊ฒ ์ฐ์ โ ์ผ๋ฐ ๊ฒ ์ฐจํ) โโโโโโโโโโโโโโโโโโโโโโ["99,000์", "๊ตฌ๋ง ๊ตฌ์ฒ ์"], ["99,000 ์", "๊ตฌ๋ง ๊ตฌ์ฒ ์"],["59,900์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["59,900 ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["49,900์", "์ฌ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["49,900 ์", "์ฌ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["39,900์", "์ผ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["39,900 ์", "์ผ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["29,900์", "์ด๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["29,900 ์", "์ด๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["19,900์", "๏ฟฝ๏ฟฝ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["19,900 ์", "์ผ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["9,900์", "๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["9,900 ์", "๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59,900", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ"], ["49,900", "์ฌ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ"],["39,900", "์ผ๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ"], ["29,900", "์ด๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ"],# โโ ๊ธฐ๊ฐ (์ผ๋ฐ ์ผ/์ฃผ/๋ ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["1์ผ", "ํ๋ฃจ"], ["2์ผ", "์ดํ"], ["3์ผ", "์ฌํ"],["1์ฃผ", "์ผ์ฃผ"], ["2์ฃผ", "์ด์ฃผ"], ["1๋ ", "์ผ๋ "],["24์๊ฐ", "์ด์ญ์ฌ ์๊ฐ"], ["12์๊ฐ", "์ญ์ด ์๊ฐ"],["3๊ฐ์", "์ผ๊ฐ์"], ["3 ๊ฐ์", "์ผ๊ฐ์"],["6๊ฐ์", "์ก๊ฐ์"], ["6 ๊ฐ์", "์ก๊ฐ์"],["12๊ฐ์", "์ญ์ด๊ฐ์"], ["12 ๊ฐ์", "์ญ์ด๊ฐ์"],["24๊ฐ์", "์ด์ญ์ฌ๊ฐ์"], ["24 ๊ฐ์", "์ด์ญ์ฌ๊ฐ์"],# โโ ์๊ฐยท์์ผ ํ๊ธฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["์ค์ ", "์ค์ "], ["์คํ", "์คํ"],["AM", "์์ด์ "], ["PM", "ํผ์ "], ["KST", "์ผ์ด์์คํฐ"],["์์์ผ", "์์์ผ"], ["ํ์์ผ", "ํ์์ผ"], ["ํ์ผ", "ํ์ผ"],# โโ ๊ฐ๊ฒฉ๋ ํํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["1๋ง์๋", "๋ง์๋"], ["1๋ง ์๋", "๋ง์๋"],["1๋ง ์ ๋", "๋ง์๋"], ["1๋ง์ ๋", "๋ง์๋"],["2๋ง์๋", "์ด๋ง์๋"], ["2๋ง ์๋", "์ด๋ง์๋"],["3๋ง์๋", "์ผ๋ง์๋"], ["3๋ง ์๋", "์ผ๋ง์๋"],["5๋ง์๋", "์ค๋ง์๋"], ["5๋ง ์๋", "์ค๋ง์๋"],# โโ ์์ฃผ ํ๋ฆฌ๋ ํ๊ตญ์ด ํ๊ธฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ["~์ ๋๋ค", "~์ต๋๋ค"], ["์ ๋๋ค", "์ ๋๋ค"],["์์ ์ ๋๋ค", "์์ ์ ๋๋ค"], ["๊ฒฐ๊ณผ์ ์ผ๋ก", "๊ฒฐ๊ณผ์ ์ผ๋ก"],["์ผ์ฑ", "์ผ์ฑ"], ["ํ๋", "ํ๋"], ["๊ธฐ์", "๊ธฐ์"],["์ํ", "์ํ"], ["์ ํ", "์ ํ"], ["๊ฐ๊ฒฉ", "๊ฐ๊ฒฉ"],["๋ฐฐ์ก", "๋ฐฐ์ก"], ["ํ ์ธ", "ํ ์ธ"], ["์ด๋ฒคํธ", "์ด๋ฒคํธ"]]# ---------- ์ฌ์ I/O ----------def _read_local_dict():try:with open(dict_path, "r", encoding="utf-8") as f:3 unmodified linesreturn {}def _write_local_dict(dict_map):items = [[k, v] for k, v in dict_map.items()]payload = {"items": items, "meta": {"app_title": "VoiceScript AI", "app_version": "149.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)2 unmodified linesos.replace(tmp_path, dict_path)def _push_remote():if not hf_api:return "๋ก์ปฌ ์ ์ฅ๋ง ๋จ (์ฌ์์ ์ ์ ์ค ๊ฐ๋ฅ - HF_TOKEN ์ค์ ํ์)"try:5 unmodified linesreturn f"์๊ฒฉ ์ ์ฅ ์คํจ({e}) - ๋ก์ปฌ๋ง ์ ์ฅ๋จ"def save_dict(dict_map):_write_local_dict(dict_map)_ENGINE_CACHE["sig"] = Nonemsg = _push_remote()return f"{msg} ยท ํ์ฌ ์ฌ์ {len(dict_map)}๊ฐ"def perform_startup_recovery():dict_map = _read_local_dict()remote_ok = Trueif hf_api:5 unmodified linesfor i in remote.get("items", []):if len(i) == 2:dict_map[i[0].strip()] = i[1].strip()except Exception as e:is_404 = getattr(getattr(e, "response", None), "status_code", None) == 404if is_404:print("[์ฌ์ ] ์๊ฒฉ์ ์์ง ์ฌ์ ํ๏ฟฝ๏ฟฝ๏ฟฝ ์์ โ ์ต์ด ๋ถํ ์ผ๋ก ์ฒ๋ฆฌ")else:remote_ok = Falseprint(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()6 unmodified linesflat_dict.update({k.strip(): v.strip() for k, v in user_temp_dict.items()})return flat_dict# ---------- 2๋จ๊ณ ๋งค์นญ ์์ง (ํ๊ธฐ ์ ๊ทํ โ ๋ฐ์ ์นํ) ----------_ENGINE_CACHE = {"sig": None}def _build_pattern(keys):r"""ํค ๊ธธ์ด ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ ํ ๊ณต๋ฐฑ์ \s* ๋ก ๋ฐ๊พผ ์ ์ฐ ํจํด. ๊ทธ๋ฃน๋ฒํธโํค."""ordered = sorted(keys, key=lambda x: -len(x.replace(" ", "")))if not ordered:return None, []pats = [r'\s*'.join(map(re.escape, k.split(" "))) for k in ordered]8 unmodified linesif not k:continueif k == v:shields.add(k)elif re.sub(r'[\s,\.ยท]', '', k) == re.sub(r'[\s,\.ยท]', '', v):phase1[k] = vshields.add(v)else:phase2[k] = vp1_pat, p1_keys = _build_pattern(phase1.keys())p2_pat, p2_keys = _build_pattern(phase2.keys())sh_pat, _ = _build_pattern(shields)8 unmodified linesreturn eng["p1_pat"].sub(lambda m: eng["phase1"][eng["p1_keys"][m.lastindex - 1]], text)def _segments_phase2(text, eng):if not eng["p2_pat"] or not text:return [(False, text)] if text else []segs, last = [], 08 unmodified linesreturn segsdef _g2p_shielded(chunk, mode, eng):if not chunk:return chunkif not eng["sh_pat"]:9 unmodified linesreturn "".join(res)def convert_text(text, mode, flat_dict):"""์๋ฌธ ๋น๊ณต๋ฐฑ ๋ฌธ์์ด ๋ณํ ์ ํ ๋์ผ์ฑ ์ ์ง.์ธํ ๋ณํ๋ ์ฌ์ ยท์ซ์ยท๋ฌธ์ฅ๋ถํธ ํธํก ๋ง์ปค๋ก๋ง ๋ง๋ค์ด์ง๋ค."""eng = get_engine(flat_dict)t = _sub_phase1(text, eng)out = []6 unmodified linesout.append(c2 if fixed2 else _g2p_shielded(c2, mode, eng))return "".join(out)# ---------- G2P ๋ชจ๋ ----------def apply_g2p(text, mode):if g2p is None or mode.startswith("๋๊ธฐ"):return text14 unmodified lineselse:res.append(w)return "".join(res)if mode.startswith("์ค๊ฐ ๋ง"):# ๋ช ์ฌ/๊ณ ์ ์ด (ํ๊ธ 2๊ธ์+) ๋ง ๋ณํ, ๋จ์ผ ์กฐ์ฌยท๋ณด์กฐ์ฌยท๊ธฐํธ๋ ๊ฑด๋๋tokens = re.split(r'([๊ฐ-ํฃ]+|[a-zA-Z]+|\d+|[^\w\s]+|\s+)', text)particles = {'์ด', '๊ฐ', '์', '๋ฅผ', '์', '๋', '์', '๋ก', '์ผ๋ก', '๊ณ ','๋ค', '์ด', '์', '๋ค', '์ ๋๋ค', '์ต๋๋ค'}res = []for t in tokens:if not t:10 unmodified linesreturn "".join(res)return text# ---------- ์ซ์/๋จ์ ํ๊ตญ์ด ๋ณํ ----------def number_to_korean(num_str):num_str = num_str.replace(',', '')try:2 unmodified linesreturn num_strif n == 0:return "์"digits, units, large_units = ["", "์ผ", "์ด", "์ผ", "์ฌ", "์ค", "์ก", "์น ", "ํ", "๊ตฌ"], \["", "์ญ", "๋ฐฑ", "์ฒ"], ["", "๋ง", "์ต", "์กฐ"]res, str_n = [], str(n)[::-1]for i in range(0, len(str_n), 4):chunk, chunk_res = str_n[i:i+4], []16 unmodified linestext = 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": "ํํ","95": "๊ตฌ์ค", "100": "๋ฐฑ", "105": "๋ฐฑ์ค", "110": "๋ฐฑ์ญ"}def _size_repl(m):k = m.group(1)return f"{size_dict[k]} ์ธ์ด์ฆ" if k in size_dict else m.group(0)text = re.sub(r'\b(55|66|77|88|95|100|105|110)\s*์ฌ์ด์ฆ\b', _size_repl, 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)unit_dict = {"mm": "๋ฐ๋ฆฌ๋ฏธํฐ", "cm": "์ผํฐ๋ฏธํฐ", "m": "๋ฏธํฐ", "km": "ํฌ๋ก๋ฏธํฐ","g": "๊ทธ๋จ", "kg": "ํฌ๋ก๊ทธ๋จ", "ml": "๋ฐ๋ฆฌ๋ฆฌํฐ", "L": "๋ฆฌํฐ"}def _unit_repl(m):num_u, u = m.group(1), m.group(2).lower() if m.group(2).lower() != 'l' else 'L'return f"{number_to_korean(num_u)} {unit_dict[u]}"text = re.sub(r'(\d+)\s*(mm|cm|m|km|g|kg|ml|L)\b', _unit_repl, text, flags=re.IGNORECASE)return text# ---------- ์ฒ๋ฆฌ ํ ์ค์ ๋ชจ์ผ๊ธฐ (ํธํก์ ๋ง ๋ฌธ์ฅ๋ถํธ ๊ธฐ๋ฐ) ----------def _join_breath_marks(text):"""์ผํยท๊ฐ์ด๋์ ยท๋ง์ค์ํ ๋ค์ ํธํก ๋งํฌ('/')๋ฅผ ๋ฃ๊ณ , `@@` ๋ง์ปค๋ ๋ณด์กดํ๋ค."""text = re.sub(r'[,ใ]\s*', ', / ', text)text = re.sub(r'[ยทโข]\s*', 'ยท / ', text)text = re.sub(r'โฆ\s*', 'โฆ / ', text)return textdef 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์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."char_count = f"๊ณต๋ฐฑ ํฌํจ: {len(input_text)}์ / ๊ณต๋ฐฑ ์ ์ธ: {len(input_text.replace(' ', '').replace(chr(10), ''))}์"flat_dict = load_dict(user_temp_dict)if not flat_dict:1 unmodified lineelse:final_text = convert_text(input_text, ai_mode, flat_dict)if smart_pause:final_text = _join_breath_marks(final_text)if auto_break:final_text = final_text.replace(". ", ".\n\n").replace("? ", "?\n\n").replace("! ", "!\n\n")seq = difflib.SequenceMatcher(None, input_text, final_text)html_parts = []17 unmodified linesdef get_file():return dict_path if os.path.exists(dict_path) else Nonedef 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_dictword, pronun = word.strip(), pronun.strip()9 unmodified linesuser_temp_dict[word] = pronunstatus_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)return status_msg, ft, ho, cc, mr, user_temp_dictreturn status_msg, gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict72 unmodified linesreturn gr.update(visible=False), gr.update(visible=True), current_role, "", gr.update(), gr.update()return gr.update(visible=True), gr.update(visible=False), "none", "", "", ""# ---------- UI ๋น๋ ----------custom_css = """#prompter_html_wrap, #prompter_html_wrap .prose { width: 100% !important; text-align: left !important; }#input_text_area textarea, #output_text_area textarea, #prompter_output_div {font-family: sans-serif !important; height: 500px !important;overflow-y: auto !important; padding: 16px !important; line-height: 1.8 !important;}#prompter_output_div { background-color: #333 !important; color: #fff !important; border-radius: 8px !important; white-space: pre-wrap !important; }.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; }/* ๋ฒํผ ํ 4๊ฐ ๋์ผ ํญ (Gradio use_container_width ๋์ CSS ๋ณด๊ฐ) */.convert-row { gap: 8px !important; }.convert-row > button { flex: 1 1 0 !important; min-width: 0 !important; }"""script_js = """function() {36 unmodified lines}"""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; }"_mode_choices = ["๋๊ธฐ (์๋ ์ฌ์ ๋ง ์ ์ฉ)", "์ค๊ฐ ๋ง (๋ช ์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)","์ํ ๋ง (๋จ์ด๋ณ ๋๊ธฐ)", "๋งค์ด ๋ง (์์ฐ ์ฐ์)"]_GRADIO_MAJOR = int(gr.__version__.split(".")[0])_BLOCKS_KW = {"theme": "default", "css": custom_css} if _GRADIO_MAJOR < 6 else {}19 unmodified lineswith gr.Column():input_text = gr.Textbox(label="1๏ธโฃ ์๋ณธ ํ ์คํธ ์ ๋ ฅ", elem_id="input_text_area")with gr.Group():ai_mode = gr.Radio(choices=_mode_choices,value="์ค๊ฐ ๋ง (๋ช ์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)",label="๐ค AI ๋ฐ์ ๋ณํ ๊ฐ๋")auto_break = gr.Checkbox(label="๐ ๋ฌธ์ฅ ๋ ์๋ ์ค๋ฐ๊ฟ", value=False)smart_pause = gr.Checkbox(label="โฑ๏ธ ์ผํ ๋ค ํธํก์ ( / ) ์ถ๊ฐ", value=False)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)# === ๋ณํ ๋ฒํผ ํ (๋์ผ scaleยทsize, ์ด๊ธฐํ๋ ๋ง์ง๋ง ์๋ฆฌ) ===with gr.Row(elem_classes="convert-row"):btn_conv = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary")btn_copy = gr.Button("๐ ๋ณต์ฌ", variant="secondary")btn_clr = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop")2 unmodified lineswith gr.Accordion("๐ ํ ์คํธ ๊ฒฐ๊ณผ (ํด๋ฆญํ์ฌ ํผ์น๊ธฐ)", open=False):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("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ** โ ๋จ์ด์ฅ์ด ๊ฐ์ฅ ํจ๊ณผ์ ์ธ ๋ฐ์ ๊ต์ ์๋จ์ ๋๋ค.")with gr.Row():nw = gr.Textbox(label="๋จ์ด")np = gr.Textbox(label="์ํ๋ ๋ฐ์")with gr.Column():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, 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)1 unmodified linebtn_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_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_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=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)2 unmodified linesup_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)
app.py+191-330