Spaces:
Running
Running
Update app.py
Browse files22 unmodified lines2324252627282930313227333435366 unmodified lines43444540414647484950514852535418 unmodified lines73747573767778794 unmodified lines8485868788899091929386878894959692979899961001011023 unmodified lines1061071081061091081101111121131141141151161171171181191201211221241231241251 unmodified line1271281291321301311321361331341357 unmodified lines1431441451501461471481491501561511521531601541551631561571661581591691701711721731601611751621631641781651661671684 unmodified lines17317417517618917717817918054 unmodified lines23523623723823924025024124225224324424524624724824925025126026126225225325425526426526626726825625725825926026126226326426526626726826927027027127227327427527627727827928028128228328428527127227327427527627727827928028128228328428828929028528628728828929229029129429529629729829229329429529629729830230330429930030130230330430530630730830730830931031231331131231331631731431531632031731831932032532132232732332432933033132532632732832933033433533133233733833934034133333433533633733833934034334434534634734834935035134134234334434534634734834935035135235335335435535635735835435535635735835936036136236336436536036136636336736836937037136836937037137237237337437537637437737837937837938038138238338038138238338438538638738838939039139238538638738838939039339439539239339439539639739639739839940040140240339940040440240540640740812 unmodified lines4214224234214244254244254264274264274284294304314334324334343 unmodified lines4384394404434444414424434444454464474514524484494504515 unmodified lines4574584594644654664674684694704714724734744754764774784794804814824834844854864874604614624634644654664674683 unmodified lines47247347449950050150250350450550650750847547647747814 unmodified lines4934944955294964974984992 unmodified lines5025035045055065395075085415095105115445455465475485125135145155165175185195201 unmodified line5225235245565255265275281 unmodified line53053153256453353453553656856953753853954054157357457554254354454557854654754854955058355155255355455555622 unmodified lines
dict_path = "custom_dict.json"
# ํ๊น
ํ์ด์ค Secrets์์ ์์ด๋/๋น๋ฒ ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์ธํ
)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")
# ==========================================# โ
1. G2P ๋ฉด์ ๊ถ ๋จ์ด ์ถ๊ฐ (๊ธฐ์กด ๋ก์ง 100% ์ ์ง)# โ
๊ธฐ์กด ์์ง 100% ์๋ณธ ์ ์ง (๊ฑด๋๋ฆฌ์ง ์์)# ==========================================AUTO_MERGE_WORDS = [ ["GS๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS ๋ง์ด์ต", "์ง์์ค๋ง์ด์ต"],["GS์ต", "์ง์์ค์ต"],["GS ์ต", "์ง์์ค์ต"],6 unmodified lines ["59,900์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59,900 ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59900์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["59900 ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"], ["5๋ง 9์ฒ 9๋ฐฑ ์", "์ค๋ง ๊ตฌ์ฒ๊ตฌ๋ฐฑ ์"],["3๊ฐ์", "์ผ๊ฐ์"],["3 ๊ฐ์", "์ผ๊ฐ์"], ["1๋ง์๋", "๋ง์๋"],["1๋ง ์๋", "๋ง์๋"],["1๋ง ์ ๋", "๋ง์๋"],["1๋ง์ ๋", "๋ง์๋"], # G2P๊ฐ ํผ์ํ์ง ๋ชปํ๊ฒ ๋ง๋ ๊ณ ์ ๋ช
์ฌ๋ค (๋ฉด์ ๊ถ) ["~์
๋๋ค", "~์ต๋๋ค"], ["์์ ์
๋๋ค", "์์ ์
๋๋ค"], ["๊ฒฐ๊ณผ์ ์ผ๋ก", "๊ฒฐ๊ณผ์ ์ผ๋ก"], ["์ผ์ฑ", "์ผ์ฑ"], ["LG", "์์ง"], ["SK", "์์ค์ผ์ด"], ["์ํ", "์ํ"], ["์ ํ", "์ ํ"], ["๊ฐ๊ฒฉ", "๊ฐ๊ฒฉ"], ["๋ฐฐ์ก", "๋ฐฐ์ก"], ["ํ ์ธ", "ํ ์ธ"], ["์ด๋ฒคํธ", "์ด๋ฒคํธ"]]
# ์๋ณธ์ ์์ ํ ์ฌ์ ๋ณต๊ตฌ ํจ์ ๊ทธ๋๋ก ์ ์งdef perform_auto_recovery_and_merge(): if not os.path.exists(dict_path): with open(dict_path, "w", encoding="utf-8") as f:18 unmodified lines
perform_auto_recovery_and_merge()
def load_dict():def load_dict(user_temp_dict=None): flat_dict = {} try: if os.path.exists(dict_path):4 unmodified lines flat_dict[item[0].strip()] = item[1].strip() except: pass # โ
๊ฒ์คํธ์ ์์ ๋จ์ด์ฅ์ด ์๋ค๋ฉด ์๋ณธ ์ฌ์ ์ ๋ฎ์ด์์์ ์ ์ฉ (์๋ณธ ํ์ผ์ ๊ฑด๋๋ฆฌ์ง ์์) if user_temp_dict: flat_dict.update(user_temp_dict) return flat_dict
# ==========================================# โ
2. 4๋จ๊ณ ํ์ด๋ธ๋ฆฌ๋ G2P ์ ์ฉ (์์ ํ๊ฒ ๋์ด์ฐ๊ธฐ ๋ณด์กด)# ==========================================def apply_g2p(text, mode): if g2p is None or mode.startswith("๋๊ธฐ"): return text if mode.startswith("๋งค์ด ๋ง"): try: return g2p(text) except: return text if mode.startswith("์ํ ๋ง"): words = re.split(r'(\s+)', text) res =[]3 unmodified lines except: res.append(w) else: 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 # ๊ณต๋ฐฑ, ์กฐ์ฌ, ํ๊ธ์ ๋จ์ด๋ ์๋ณธ ์ ์ง (G2P ์ ๊ฑฐ์นจ) if t.isspace() or t in particles or len(t) == 1: res.append(t) # 2๊ธ์ ์ด์ ํ๊ธ๋ง G2P ์๋ elif re.match(r'^[๊ฐ-ํฃ]{2,}$', t): try: res.append(g2p(t)) except: res.append(t) else: res.append(t) return "".join(res) return text
def number_to_korean(num_str):1 unmodified line try: n = int(num_str) except: 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):7 unmodified lines chunk_res.append(word + units[j]) if chunk_res: res.append("".join(chunk_res[::-1]) + large_units[i//4]) final_str = "".join(res[::-1]) return final_str if final_str else num_str
def apply_smart_regex(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":"ํํ"} 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": "๋ฆฌํฐ" } 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
# โ
ํ
์คํธ ์ฒ๋ฆฌ ์ user_temp_dict ์ธ์ ์ถ๊ฐdef process_text(input_text, ai_mode, auto_break, smart_pause, font_size):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์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." 4 unmodified lines text_for_processing = apply_smart_regex(input_text) # ์๋ฒ ์ฌ์ + ๊ฐ์ธ ์์ ์ฌ์ ๋ณํฉํด์ ๋ถ๋ฌ์ค๊ธฐ flat_dict = load_dict() flat_dict = load_dict(user_temp_dict) if not flat_dict: final_text = apply_g2p(text_for_processing, ai_mode)54 unmodified linesdef clear_all(): return "", "", '<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:22px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
# ==========================================# โ
๊ถํ ๋ถ๋ฆฌ ๋ก์ง ์ ์ฉ (Admin vs Guest)# ==========================================def add_and_update(word, pronun, current_input, ai_mode, auto_break, smart_pause, font_size):def add_and_update(word, pronun, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, request: gr.Request): if not word or not pronun: return "โ ๏ธ ๊ธฐ์กด ๋จ์ด์ ์ํ๋ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update() return "โ ๏ธ ๊ธฐ์กด ๋จ์ด์ ์ํ๋ ๋ฐ์์ ๋ชจ๋ ์
๋ ฅํ์ธ์.", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict word = word.strip() pronun = pronun.strip() username = request.username if request else GUEST_ID if len(word) == 1: error_msg = f"๐ซ [๋ฑ๋ก ์คํจ] '{word}'์ฒ๋ผ 1๊ธ์๋ง ๋ฑ๋กํ๋ฉด ์๋ณธ์ด ํผ์๋ฉ๋๋ค! 2๊ธ์ ์ด์์ผ๋ก ๋ฑ๋กํด์ฃผ์ธ์." if current_input and current_input.strip(): final_text, html_output, char_count, marker_result = process_text(current_input, ai_mode, auto_break, smart_pause, font_size) return error_msg, final_text, html_output, char_count, marker_result return error_msg, "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." final_text, html_output, char_count, marker_result = process_text(current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict) return error_msg, final_text, html_output, char_count, marker_result, user_temp_dict return error_msg, "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค.", user_temp_dict try: with open(dict_path, "r", encoding="utf-8") as f: j_struct = json.load(f) except: j_struct = {"items":[], "meta": {"count": 0}} # ๐ ๊ด๋ฆฌ์ ๋ก๊ทธ์ธ ์: ์๋ฒ ํ์ผ์ ์๊ตฌ ์ ์ฅ if username == ADMIN_ID: 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]) 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}" 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}" # ๐ค ๊ฒ์คํธ ๋ก๊ทธ์ธ ์: ๋ธ๋ผ์ฐ์ ์ ์์ ์ ์ฅ else: user_temp_dict[word] = pronun status_msg = f"๐ค [๊ฒ์คํธ] '{word}' -> '{pronun}' ์์ ์ ์ฅ ์๋ฃ! (์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ)"
if current_input and current_input.strip(): final_text, html_output, char_count, marker_result = process_text(current_input, ai_mode, auto_break, smart_pause, font_size) return status_msg, final_text, html_output, char_count, marker_result return status_msg, "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." final_text, html_output, char_count, marker_result = process_text(current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict) return status_msg, final_text, html_output, char_count, marker_result, user_temp_dict return status_msg, "", f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:{font_size}px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค.", user_temp_dict
def load_current_json_to_text():def load_current_json_to_text(user_temp_dict): try: with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) dict_map = {item[0]: item[1] for item in data.get("items",[]) if len(item) == 2} pretty_json = json.dumps({"items": dict_map}, ensure_ascii=False, indent=4) return pretty_json, "โ
ํ์ฌ ์ฌ์ ์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค! ์ด์ํ ๋จ์ด๋ฅผ ์ฐพ์ ์ง์ฐ๊ฑฐ๋ ์์ ํด๋ณด์ธ์." # ์๋ฒ ์ฌ์ + ๊ฐ์ธ ์ฌ์ ์ ๋ชจ๋ ๋ณด์ฌ์ค flat_dict = load_dict(user_temp_dict) pretty_json = json.dumps({"items": flat_dict}, ensure_ascii=False, indent=4) return pretty_json, "โ
ํ์ฌ ์ฌ์ ์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค!" except Exception as e: return "", f"โ ๋ถ๋ฌ์ค๊ธฐ ์คํจ: {e}"
def overwrite_json_text(json_string): if not json_string or not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์."def overwrite_json_text(json_string, request: gr.Request): username = request.username if request else GUEST_ID if username != ADMIN_ID: return "๐ซ [๊ถํ ์์] ๋ฎ์ด์ฐ๊ธฐ๋ ์ด๋๋ฏผ ๊ณ์ ๋ง ๊ฐ๋ฅํฉ๋๋ค." if not json_string or not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์." try: new_data = json.loads(json_string) if "items" not in new_data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค. ('items' ํค๊ฐ ์์ต๋๋ค)" if "items" not in new_data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค." new_items =[] if isinstance(new_data["items"], dict): for k, v in new_data["items"].items(): new_items.append([k.strip(), v.strip()]) for k, v in new_data["items"].items(): new_items.append([k.strip(), v.strip()]) elif isinstance(new_data["items"], list): for item in new_data["items"]: if len(item) == 2: new_items.append([item[0].strip(), item[1].strip()]) if len(item) == 2: new_items.append([item[0].strip(), item[1].strip()])
final_data = {"items": new_items, "meta": {"count": len(new_items)}} with open(dict_path, "w", encoding="utf-8") as f: json.dump(final_data, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return f"๐จ[๋ฎ์ด์ฐ๊ธฐ ์๋ฃ] ์์ ํ ๋ด์ฉ({len(new_items)}๊ฐ)์ผ๋ก ์ฌ์ ์ ์๋ฒฝํ ๊ต์ฒดํ์ต๋๋ค!" return f"๐ [์ด๋๋ฏผ ์๋ฃ] ์์ ํ ๋ด์ฉ({len(new_items)}๊ฐ)์ผ๋ก ์๋ฒ ์ฌ์ ์ ์๋ฒฝํ ๊ต์ฒดํ์ต๋๋ค!" except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ (๋ฐ์ดํ๋ ์ผํ๊ฐ ๋น ์ก๋์ง ํ์ธํ์ธ์): {e}" return f"โ JSON ํ์ฑ ์๋ฌ: {e}"
def apply_json_text(json_string): if not json_string or not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์."def apply_json_text(json_string, user_temp_dict, request: gr.Request): username = request.username if request else GUEST_ID if not json_string or not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์.", user_temp_dict try: new_data = json.loads(json_string) if "items" not in new_data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค." if "items" not in new_data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค.", user_temp_dict try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f) except: existing_data = {"items":[], "meta": {"count": 0}} # ์ด๋๋ฏผ: ์๋ฒ์ ์ ์ฅ if username == ADMIN_ID: 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 = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2} if isinstance(new_data["items"], dict): for k, v in new_data["items"].items(): dict_map[k.strip()] = v.strip() elif isinstance(new_data["items"], list): for item in new_data["items"]: if len(item) == 2: dict_map[item[0].strip()] = item[1].strip() if isinstance(new_data["items"], dict): for k, v in new_data["items"].items(): dict_map[k.strip()] = v.strip() elif isinstance(new_data["items"], list): for item in new_data["items"]: if len(item) == 2: dict_map[item[0].strip()] = item[1].strip() 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) 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() return f"๐ [์ด๋๋ฏผ ์๋ฃ] ๊ธฐ์กด ๋จ์ด๋ฅผ ์ ์งํ๋ฉฐ ์๋ฒ์ ์๊ตฌ ์ถ๊ฐํ์ต๋๋ค!", user_temp_dict # ๊ฒ์คํธ: ์์ ์ฌ์ ์๋ง ์ถ๊ฐ else: if isinstance(new_data["items"], dict): for k, v in new_data["items"].items(): user_temp_dict[k.strip()] = v.strip() elif isinstance(new_data["items"], list): for item in new_data["items"]: if len(item) == 2: user_temp_dict[item[0].strip()] = item[1].strip() return f"๐ค [๊ฒ์คํธ ์๋ฃ] ๋ธ๋ผ์ฐ์ ์์ ๋จ์ด์ฅ์ ์ถ๊ฐ๋์์ต๋๋ค!", user_temp_dict perform_auto_recovery_and_merge() return f"โ
๊ธฐ์กด ๋จ์ด๋ฅผ ์ ์งํ๋ฉฐ ์ฑ๊ณต์ ์ผ๋ก ํ๋ฌ์ค(+) ํ์ต๋๋ค!" except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ: {e}" return f"โ JSON ํ์ฑ ์๋ฌ: {e}", user_temp_dict
def get_file(): return dict_path if os.path.exists(dict_path) else None
# ==========================================# โ
3. ๋ฐฑ์
ํ์ผ ์
๋ก๋ ์ ๊ธฐ์กด ๋ฐ์ดํฐ ๋ณดํธ (์์ ๋ณํฉ)# ==========================================def upload_file(file_path): if file_path is None: return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค."def upload_file(file_path, user_temp_dict, request: gr.Request): username = request.username if request else GUEST_ID 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) # ๊ธฐ์กด ์ฌ์ ์ฝ๊ธฐ try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f) except: existing_data = {"items":[], "meta": {"count": 0}} if username == ADMIN_ID: 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 item in uploaded_data.get("items", []): if len(item) == 2: dict_map[item[0].strip()] = item[1].strip() merged_items = [[k, v] for k, v in dict_map.items()] existing_data["items"] = merged_items existing_data["meta"]["count"] = len(merged_items) # ๋ฐ์ดํฐ ๋ณํฉ (๊ธฐ์กด ๋จ์ด ์ด๋ฆฌ๊ณ ์ถ๊ฐ) dict_map = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2} for item in uploaded_data.get("items", []): if len(item) == 2: dict_map[item[0].strip()] = item[1].strip() with open(dict_path, "w", encoding="utf-8") as f: json.dump(existing_data, f, ensure_ascii=False, indent=2) 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() return "๐ [์ด๋๋ฏผ] ๊ธฐ์กด ๋จ์ด๋ฅผ ๋ณดํธํ๋ฉฐ ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์๊ตฌ ๋ณํฉํ์ต๋๋ค!", user_temp_dict else: for item in uploaded_data.get("items", []): if len(item) == 2: user_temp_dict[item[0].strip()] = item[1].strip() return "๐ค [๊ฒ์คํธ] ๋ด ๋ธ๋ผ์ฐ์ ์์ ๋จ์ด์ฅ์ ์์ ํ๊ฒ ๋ณํฉ๋์์ต๋๋ค!", user_temp_dict perform_auto_recovery_and_merge() return "โ
๊ธฐ์กด ๋จ์ด๋ฅผ ๋ณดํธํ๋ฉฐ ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐ(๋ณํฉ)ํ์ต๋๋ค!" except Exception as e: return f"โ ํ์ผ ์ฒ๋ฆฌ ์คํจ (์ฌ๋ฐ๋ฅธ ๋ฐฑ์
ํ์ผ์ธ์ง ํ์ธํ์ธ์): {e}" return f"โ ํ์ผ ์ฒ๋ฆฌ ์คํจ: {e}", user_temp_dict
custom_css = """#prompter_html_wrap { width: 100% !important; max-width: 100% !important; margin: 0 !important; padding: 0 !important; }12 unmodified lines let ta2 = document.querySelector('#output_text_area textarea'); if(ta2) { ta2.style.fontSize = size + 'px'; ta2.style.lineHeight = '1.8'; } }
setTimeout(function() { applyFontSize(22); }, 500);
let isSyncing1 = false; let isSyncing2 = false; let isSyncing3 = false; let isSyncing1 = false; let isSyncing2 = false; let isSyncing3 = false; document.addEventListener('scroll', function(e) { let target = e.target; let ta1 = document.querySelector('#input_text_area textarea'); let ta2 = document.querySelector('#output_text_area textarea'); let div3 = document.getElementById('prompter_output_div');
if (!ta1 || !div3) return;
if (target === ta1) {3 unmodified lines let percentage = ta1.scrollTop / (ta1.scrollHeight - ta1.clientHeight); if (ta2 && ta2.scrollHeight - ta2.clientHeight > 0) { ta2.scrollTop = percentage * (ta2.scrollHeight - ta2.clientHeight); } if (div3.scrollHeight - div3.clientHeight > 0) { div3.scrollTop = percentage * (div3.scrollHeight - div3.clientHeight); } } else if (target === ta2) { } else if (target === ta2) { if (isSyncing2) { isSyncing2 = false; return; } if (ta2 && ta2.scrollHeight - ta2.clientHeight <= 0) return; isSyncing1 = true; isSyncing3 = true; let percentage = ta2.scrollTop / (ta2.scrollHeight - ta2.clientHeight); if (ta1.scrollHeight - ta1.clientHeight > 0) { ta1.scrollTop = percentage * (ta1.scrollHeight - ta1.clientHeight); } if (div3.scrollHeight - div3.clientHeight > 0) { div3.scrollTop = percentage * (div3.scrollHeight - div3.clientHeight); } } else if (target === div3) { } else if (target === div3) { if (isSyncing3) { isSyncing3 = false; return; } if (div3.scrollHeight - div3.clientHeight <= 0) return; isSyncing1 = true; isSyncing2 = true;5 unmodified lines}"""
insert_marker_js = """(current_text) => { let ta = document.querySelector('#input_text_area textarea'); if (!ta) return current_text; let start = ta.selectionStart; let end = ta.selectionEnd; let text = ta.value; let before = text.substring(0, start); let after = text.substring(end, text.length); let new_text = before + "@@" + after; ta.value = new_text; let event = new Event('input', { bubbles: true }); ta.dispatchEvent(event); ta.selectionStart = ta.selectionEnd = start + 2; ta.focus(); return new_text;}"""insert_marker_js = "(current_text) => { let ta = document.querySelector('#input_text_area textarea'); if (!ta) return current_text; let start = ta.selectionStart; let end = ta.selectionEnd; let text = ta.value; let before = text.substring(0, start); let after = text.substring(end, text.length); let new_text = before + '@@' + after; ta.value = new_text; let event = new Event('input', { bubbles: true }); ta.dispatchEvent(event); ta.selectionStart = ta.selectionEnd = start + 2; ta.focus(); return new_text; }"
with gr.Blocks(theme="default", css=custom_css) as iface: # ๊ฐ์ธ ์์ ์ฌ์ ์ ์ฅ์ ์ํ State (๊ฒ์คํธ์ฉ) user_temp_dict = gr.State(lambda: {})
gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (ํ๋กฌํํฐ ๋ชจ๋)") with gr.Tabs():3 unmodified lines input_text = gr.Textbox(label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ", elem_id="input_text_area") with gr.Group(): gr.Markdown("**โ๏ธ ๋ณํ ๋ฐ ํ๋ฉด ์ค์ **") ai_mode = gr.Radio( choices=[ "๋๊ธฐ (์๋ ์ฌ์ ๋ง ์ ์ฉ)", "์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", "์ํ ๋ง (๋จ์ด๋ณ ๋๊ธฐ - ๊ณผ๋ํ ์ฐ์ ๋ฐฉ์ง)", "๋งค์ด ๋ง (๋ฌธ์ฅ ํต์งธ๋ก - ์์ฐ์ค๋ฌ์ด ์ฐ์)" ], value="์ค๊ฐ ๋ง (๋ช
์ฌ/๊ณ ์ ์ด๋ง - ์ถ์ฒ โญ)", label="๐ค AI ๋ฐ์ ๋ณํ ๊ฐ๋ ์ ํ" ) ai_mode = gr.Radio(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")14 unmodified lines marker_collection = gr.Textbox(label="๐จ ์์ ํ์ ๋จ์ด ๋ชจ์๋ณด๊ธฐ (@@)", interactive=False, lines=2) gr.Markdown("---") gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ** (โป 1๊ธ์ ๋จ์ด๋ ์๋ณธ ํผ์์ ๋ง๊ธฐ ์ํด ๋ฑ๋ก ๋ถ๊ฐํฉ๋๋ค!)") gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ** (์ด๋๋ฏผ: ์๊ตฌ ์ ์ฅ / ๊ฒ์คํธ: ์์ ์ ์ฅ)") with gr.Row(): nw = gr.Textbox(label="๊ธฐ์กด ๋จ์ด ๋๋ ๋ฌธ์ฅ (2๊ธ์ ์ด์)") np = gr.Textbox(label="์ํ๋ ๋ฐ์")2 unmodified lines btn_clear_dict_inputs = gr.Button("๐งน ๋จ์ด ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ", variant="secondary") status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) # ๋ชจ๋ ํ
์คํธ ์ฒ๋ฆฌ ์ด๋ฒคํธ์ user_temp_dict ์ฐ๋ 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], outputs=[output_text, output_html, char_count, marker_collection], 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], 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_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="(text) => { if(text) { navigator.clipboard.writeText(text); alert('โ
๋ณต์ฌ ์๋ฃ!'); } }", api_name=False) font_size.change(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) ai_mode.change(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) auto_break.change(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) smart_pause.change(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) btn_add.click(fn=add_and_update, inputs=[nw, np, input_text, ai_mode, auto_break, smart_pause, font_size], outputs=[status, output_text, output_html, char_count, marker_collection], api_name=False) font_size.change(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) ai_mode.change(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) auto_break.change(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) smart_pause.change(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_add.click(fn=add_and_update, inputs=[nw, np, 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)
with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/์กฐํ/์์ (๋๋ ๋ฑ๋ก)"):1 unmodified line dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) gr.Markdown("---") gr.Markdown("### ๐ 2. ํ๋ฉด์์ ์ง์ ์ฌ์ ์กฐํ/์์ /์ญ์ ํ๊ธฐ (๋ค์ด๋ก๋ ๋ถ๊ฐ ํ๊ฒฝ์ฉ)") gr.Markdown("### ๐ 2. ํ๋ฉด์์ ์ง์ ์ฌ์ ์กฐํ/์์ /์ญ์ ํ๊ธฐ (์ด๋๋ฏผ ์ ์ฉ ๊ธฐ๋ฅ ํฌํจ)") with gr.Row(): btn_load_json = gr.Button("๐ 1๋จ๊ณ: ํ์ฌ ๋ฑ๋ก๋ ๋ชจ๋ ๋จ์ด ๋ถ๋ฌ์ค๊ธฐ", variant="secondary") 1 unmodified line with gr.Row(): btn_apply_json = gr.Button("๐ 2๋จ๊ณ: ๊ธฐ์กด ๋จ์ด์ฅ์ ํ๋ฌ์ค(+) ํ๊ธฐ", variant="primary") btn_overwrite_json = gr.Button("๐จ 2๋จ๊ณ: ์์ ํ ๋ด์ฉ์ผ๋ก ์์ ํ ๋ฎ์ด์ฐ๊ธฐ (๊ธฐ์กด ์ญ์ )", variant="stop") btn_overwrite_json = gr.Button("๐จ 2๋จ๊ณ: ์์ ํ ๋ด์ฉ์ผ๋ก ์์ ํ ๋ฎ์ด์ฐ๊ธฐ (์ด๋๋ฏผ๋ง ๊ฐ๋ฅ)", variant="stop") json_stat = gr.Textbox(label="์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) btn_load_json.click(fn=load_current_json_to_text, outputs=[json_input, json_stat], api_name=False) btn_apply_json.click(fn=apply_json_text, inputs=[json_input], outputs=[json_stat], api_name=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_temp_dict], outputs=[json_stat, user_temp_dict], api_name=False) btn_overwrite_json.click(fn=overwrite_json_text, inputs=[json_input], outputs=[json_stat], api_name=False)
gr.Markdown("---") # โ
์ค์ ํด๋ฆญ ๋ฐฉ์ง๋ฅผ ์ํด ์์ฝ๋์ธ(์ ๊ธฐ/ํผ์น๊ธฐ)์ผ๋ก ์์ ํ๊ฒ ์จ๊น ์ฒ๋ฆฌ! with gr.Accordion("๐ค 3. ๋ฐฑ์
ํ์ผ๋ก ๊ธฐ์กด ์ฌ์ ์ ์ถ๊ฐํ๊ธฐ (์์ ๋ณํฉ)", open=False): gr.Markdown("โป ์
๋ก๋ํ ํ์ผ์ ๋จ์ด๋ค์ด ๊ธฐ์กด ์ฌ์ ์ ์ถ๊ฐ๋ฉ๋๋ค. (๊ธฐ์กด ๋จ์ด๋ ์ญ์ ๋์ง ์๊ณ ์์ ํ๊ฒ ๋ณดํธ๋ฉ๋๋ค)") with gr.Accordion("๐ค 3. ๋ฐฑ์
ํ์ผ๋ก ๊ธฐ์กด ์ฌ์ ์ ๋ณํฉํ๊ธฐ", open=False): gr.Markdown("โป ์ด๋๋ฏผ์ ์๋ฒ์ ์๊ตฌ ๋ณํฉ, ๊ฒ์คํธ๋ ๋ธ๋ผ์ฐ์ ์ ์์ ๋ณํฉ๋ฉ๋๋ค.") up_file = gr.File(label="๋ฐฑ์
ํ์ผ ์
๋ก๋", type="filepath") up_stat = gr.Textbox(label="ํ์ผ ๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file], outputs=[up_stat], api_name=False) up_file.upload(fn=upload_file, inputs=[up_file, user_temp_dict], outputs=[up_stat, user_temp_dict], api_name=False)
iface.load(None, js=script_js)
if __name__ == "__main__": iface.launch() # โ
๋ก๊ทธ์ธ ๊ถํ ์ค์ (์์์ ์ค์ ํ ํ๊ฒฝ ๋ณ์๋ก ์ธ์ฆ) auth_list = [ (ADMIN_ID, ADMIN_PW), (GUEST_ID, GUEST_PW) ] iface.launch(auth=auth_list)app.py+166-193