Spaces:
Running
Running
Update app.py
Browse files12 unmodified lines13141516171819161718192021222326272425293026272819 unmodified lines4849505651525320 unmodified lines747576838485777886798889908081828394958485868712 unmodified lines10010110211411510310410510611912010710810911016 unmodified lines12712812914314413013113213314 unmodified lines1481491501651511521531681691701541551561573 unmodified lines1611621631801811821641651661851861871881891671681691911921931941701711721971981992002011731741751761771781792072082092101801811822122132142151831841851862182192202212222232242252262272282291871881891902321911921932351941952371961971981992412422432442002012462472482492502022032522042052062072082562092102112122132142152632642652162172182192202212222712722232242252262272752282292302312322802812332342352362372862872882892382392402912922932942952962412422432442452993003013023033043053063073082462473103112482493133143152502512523173183193202532542552563233243252572582592602612622633292643313323333343353363372652663393403412673433442682693463472702713493503513523532722733553563572742753593602762773623632782793652803673683693702812822832842852862873742883763773782892903803813822913843852922933873882942953903913923933943952962972982993003983994004014024033013023033043053063074064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524533083093104563113123133141 unmodified line31631731846446546646746846931932032132232332447132532632747447532832933033147847948033233333448248333533633748633833948834034134249049149249334334434534634734834949735035150050150250350450550635235335435535635751035835936036151451551651751836236336436536652152252336736836937037152752837237337437553153237637737853453537938038138238338454054154254354454538512 unmodified lines
print("G2P ๋ชจ๋ธ ๋ก๋ฉ ์ค...")try: if G2p: g2p = G2p() else: g2p = None if G2p: g2p = G2p() else: g2p = Noneexcept Exception as e: print(f"G2P ๋ก๋ฉ ์คํจ: {e}") g2p = None
dict_path = "custom_dict.json"
# ํ๊น
ํ์ด์ค Secrets์์ ์์ด๋/๋น๋ฒ ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์ธํ
)ADMIN_ID = os.environ.get("ADMIN_ID", "admin")# ํ๊น
ํ์ด์ค Secrets์์ ์ด๋๋ฏผ ๋น๋ฐ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด admin1234)ADMIN_PW = os.environ.get("ADMIN_PW", "admin1234")GUEST_ID = os.environ.get("GUEST_ID", "guest")GUEST_PW = os.environ.get("GUEST_PW", "guest1234")
# ==========================================# โ
๊ธฐ์กด ์์ง 100% ์๋ณธ ์ ์ง19 unmodified lines if not os.path.exists(dict_path): with open(dict_path, "w", encoding="utf-8") as f: json.dump({"items":[], "meta": {"count": 0}}, f) try: with open(dict_path, "r", encoding="utf-8") as f: existing_data = json.load(f)20 unmodified lines with open(dict_path, "r", encoding="utf-8") as f: data = json.load(f) for item in data.get("items",[]): if len(item) == 2: flat_dict[item[0].strip()] = item[1].strip() except: if len(item) == 2: flat_dict[item[0].strip()] = item[1].strip() except: pass pass if user_temp_dict: flat_dict.update(user_temp_dict) if user_temp_dict: flat_dict.update(user_temp_dict) return flat_dict
def apply_g2p(text, mode): if g2p is None or mode.startswith("๋๊ธฐ"): return text if g2p is None or mode.startswith("๋๊ธฐ"): return text if mode.startswith("๋งค์ด ๋ง"): try: return g2p(text) except: return text12 unmodified lines res = [] for t in tokens: if not t: continue if t.isspace() or t in particles or len(t) == 1: res.append(t) if t.isspace() or t in particles or len(t) == 1: res.append(t) elif re.match(r'^[๊ฐ-ํฃ]{2,}$', t): try: res.append(g2p(t)) except: res.append(t) else: res.append(t) else: res.append(t) return "".join(res) return text
16 unmodified lines word = digits[d] if d > 1 or j == 0 else "" if d == 1 and j == 0 and len(chunk) == 1 and i == 0: word = "์ผ" chunk_res.append(word + units[j]) if chunk_res: res.append("".join(chunk_res[::-1]) + large_units[i//4]) 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
14 unmodified lines
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์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." 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', '') len_with_spaces = len(clean_text) len_without_spaces = len(clean_text.replace(' ', '').replace('\n', '')) char_count = f"๊ณต๋ฐฑ ํฌํจ: {len_with_spaces}์ / ๊ณต๋ฐฑ ์ ์ธ: {len_without_spaces}์" char_count = f"๊ณต๋ฐฑ ํฌํจ: {len(clean_text)}์ / ๊ณต๋ฐฑ ์ ์ธ: {len(clean_text.replace(' ', '').replace(chr(10), ''))}์" text_for_processing = apply_smart_regex(input_text) flat_dict = load_dict(user_temp_dict)3 unmodified lines else: sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True) pattern = re.compile("|".join(map(re.escape, sorted_keys))) parts = [] last_end = 0 parts, last_end = [], 0 for match in pattern.finditer(text_for_processing): start, end = match.span() chunk = text_for_processing[last_end:start] if chunk: parts.append(apply_g2p(chunk, ai_mode)) matched_word = match.group() parts.append(flat_dict[matched_word]) if chunk := text_for_processing[last_end:start]: parts.append(apply_g2p(chunk, ai_mode)) parts.append(flat_dict[match.group()]) last_end = end tail = text_for_processing[last_end:] if tail: parts.append(apply_g2p(tail, ai_mode)) if tail := text_for_processing[last_end:]: parts.append(apply_g2p(tail, ai_mode)) final_text = "".join(parts)
if smart_pause: final_text = re.sub(r',\s*', ', / ', final_text)
if auto_break: final_text = final_text.replace(". ", ".\n\n").replace("? ", "?\n\n") if smart_pause: final_text = re.sub(r',\s*', ', / ', final_text) if auto_break: final_text = final_text.replace(". ", ".\n\n").replace("? ", "?\n\n")
seq = difflib.SequenceMatcher(None, input_text, final_text) html_parts = [] for tag, i1, i2, j1, j2 in seq.get_opcodes(): chunk = html.escape(final_text[j1:j2]).replace('\n', '<br>') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold; padding: 2px; border-radius: 3px; color: #000;">{chunk}</span>') if tag == 'equal': html_parts.append(chunk) elif tag in ['replace', 'insert']: html_parts.append(f'<span style="background-color: #ffe066; font-weight: bold;">{chunk}</span>') raw_html = "".join(html_parts) raw_html = re.sub(r'([^\s<]*@@[^\s<]*)', r'<span style="background-color: #ff4d4d; color: white; font-weight: bold; padding: 2px 6px; border-radius: 4px; box-shadow: 0 0 5px #ff4d4d;">\1</span>', raw_html)
html_output = f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:{font_size}px;">{raw_html}</div>' raw_html = re.sub(r'([^\s<]*@@[^\s<]*)', r'<span style="background-color: #ff4d4d; color: white;">\1</span>', "".join(html_parts)) html_output = f'<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:{font_size}px;">{raw_html}</div>' markers = re.findall(r'[^\s]*@@[^\s]*', input_text) unique_markers = [] for m in markers: clean_m = m.strip('.,!?\n') if clean_m and clean_m not in unique_markers: unique_markers.append(clean_m) if unique_markers: marker_result = " / ".join(unique_markers) else: marker_result = "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
return final_text, html_output, char_count, marker_result unique_markers = list(dict.fromkeys([m.strip('.,!?\n') for m in markers if m.strip('.,!?\n')])) return final_text, html_output, char_count, " / ".join(unique_markers) if unique_markers else "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
def clear_all(): return "", "", '<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:22px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค." return "", "", '<div id="prompter_output_div" style="font-size:22px;"></div>', "๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", "ํ์๋ ๋ง์ปค(@@)๊ฐ ์์ต๋๋ค."
# ==========================================# โ
๊ถํ ๋ถ๋ฆฌ ๋ก์ง ์ ์ฉ (Admin vs Guest)# โ
์๋ฌ ์๋ ํ๋ฉด ๋ด ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ ๋ก์ง# ==========================================def add_and_update(word, pronun, current_input, ai_mode, auto_break, smart_pause, font_size, user_temp_dict, request: gr.Request):def add_and_update(word, pronun, pw_input, 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_dict word = word.strip() pronun = pronun.strip() username = request.username if request else GUEST_ID word, pronun = word.strip(), pronun.strip() 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, 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 return f"๐ซ [๋ฑ๋ก ์คํจ] 1๊ธ์๋ง ๋ฑ๋กํ๋ฉด ์๋ณธ์ด ํผ์๋ฉ๋๋ค!", gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict if username == ADMIN_ID: # ๐ ๋น๋ฐ๋ฒํธ ์ผ์น (์ด๋๋ฏผ ์๊ตฌ ์ ์ฅ) if pw_input == ADMIN_PW: 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]) 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}" except Exception as e: status_msg = f"โ ์ ์ฅ ์คํจ: {e}" # ๐ค ๋น๋ฐ๋ฒํธ ๋ถ์ผ์น/๊ณต๋ฐฑ (๊ฒ์คํธ ์์ ์ ์ฅ) else: user_temp_dict[word] = pronun status_msg = f"๐ค [๊ฒ์คํธ] '{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, 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 return status_msg, gr.update(), gr.update(), gr.update(), gr.update(), user_temp_dict
def load_current_json_to_text(user_temp_dict): try: 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}" return json.dumps({"items": flat_dict}, ensure_ascii=False, indent=4), "โ
ํ์ฌ ์ฌ์ ์ ๋ถ๋ฌ์์ต๋๋ค!" except Exception as e: return "", f"โ ๋ถ๋ฌ์ค๊ธฐ ์คํจ: {e}"
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 "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์."def overwrite_json_text(json_string, pw_input): if pw_input != ADMIN_PW: return "๐ซ [๊ถํ ์์] ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ฆฝ๋๋ค. ์ด๋๋ฏผ๋ง ๋ฎ์ด์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํฉ๋๋ค." if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์." try: new_data = json.loads(json_string) 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()]) 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()])
final_data = {"items": new_items, "meta": {"count": len(new_items)}} new_items = [[k.strip(), v.strip()] for k, v in (new_data.get("items", {}).items() if isinstance(new_data.get("items"), dict) else {i[0]: i[1] for i in new_data.get("items", []) if len(i)==2}.items())] with open(dict_path, "w", encoding="utf-8") as f: json.dump(final_data, f, ensure_ascii=False, indent=2) json.dump({"items": new_items, "meta": {"count": len(new_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return f"๐ [์ด๋๋ฏผ ์๋ฃ] ์์ ํ ๋ด์ฉ({len(new_items)}๊ฐ)์ผ๋ก ์๋ฒ ์ฌ์ ์ ์๋ฒฝํ ๊ต์ฒดํ์ต๋๋ค!" except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ: {e}" return f"๐ [์ด๋๋ฏผ ์๋ฃ] ์๋ฒ ์ฌ์ ์ ์๋ฒฝํ ๊ต์ฒดํ์ต๋๋ค!" except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ: {e}"
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 def apply_json_text(json_string, pw_input, user_temp_dict): if not json_string.strip(): return "โ ๏ธ ๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์.", user_temp_dict try: new_data = json.loads(json_string) if "items" not in new_data: return "โ ๏ธ ์ฌ๋ฐ๋ฅธ ์ฌ์ ํ์์ด ์๋๋๋ค.", user_temp_dict if username == ADMIN_ID: items = new_data.get("items", {}) parsed_items = items if isinstance(items, dict) else {i[0].strip(): i[1].strip() for i in items if len(i)==2} if pw_input == ADMIN_PW: 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} 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() dict_map.update(parsed_items) 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) json.dump({"items": merged_items, "meta": {"count": len(merged_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return f"๐ [์ด๋๋ฏผ ์๋ฃ] ๊ธฐ์กด ๋จ์ด๋ฅผ ์ ์งํ๋ฉฐ ์๋ฒ์ ์๊ตฌ ์ถ๊ฐํ์ต๋๋ค!", user_temp_dict 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() user_temp_dict.update(parsed_items) return f"๐ค [๊ฒ์คํธ ์๋ฃ] ๋ธ๋ผ์ฐ์ ์์ ๋จ์ด์ฅ์ ์ถ๊ฐ๋์์ต๋๋ค!", user_temp_dict except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ: {e}", user_temp_dict except Exception as e: return f"โ JSON ํ์ฑ ์๋ฌ: {e}", user_temp_dict
def get_file(): return dict_path if os.path.exists(dict_path) else Nonedef get_file(): return dict_path if os.path.exists(dict_path) else None
def upload_file(file_path, user_temp_dict, request: gr.Request): username = request.username if request else GUEST_IDdef upload_file(file_path, pw_input, user_temp_dict): if file_path is None: return "โ ๏ธ ํ์ผ์ด ์์ต๋๋ค.", user_temp_dict try: with open(file_path, "r", encoding="utf-8") as f: uploaded_data = json.load(f) if username == ADMIN_ID: with open(file_path, "r", encoding="utf-8") as f: uploaded_data = json.load(f) parsed_items = {item[0].strip(): item[1].strip() for item in uploaded_data.get("items", []) if len(item) == 2} if pw_input == ADMIN_PW: 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() dict_map.update(parsed_items) 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) json.dump({"items": merged_items, "meta": {"count": len(merged_items)}}, f, ensure_ascii=False, indent=2) perform_auto_recovery_and_merge() return "๐ [์ด๋๋ฏผ] ๊ธฐ์กด ๋จ์ด๋ฅผ ๋ณดํธํ๋ฉฐ ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์๊ตฌ ๋ณํฉํ์ต๋๋ค!", user_temp_dict 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 except Exception as e: return f"โ ํ์ผ ์ฒ๋ฆฌ ์คํจ: {e}", user_temp_dict user_temp_dict.update(parsed_items) return "๐ค [๊ฒ์คํธ] ๋ด ๋ธ๏ฟฝ๏ฟฝ๏ฟฝ์ฐ์ ์์ ๋จ์ด์ฅ์ ๋ณํฉ๋์์ต๋๋ค!", user_temp_dict except Exception as 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; }#prompter_html_wrap .prose { width: 100% !important; max-width: 100% !important; text-align: left !important; margin: 0 !important; }#input_text_area textarea, #output_text_area textarea, #prompter_output_div { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important; letter-spacing: normal !important; word-spacing: normal !important; }#input_text_area textarea { height: 500px !important; max-height: 500px !important; overflow-y: auto !important; padding: 16px !important; line-height: 1.8 !important; box-sizing: border-box !important; white-space: pre-wrap !important; word-break: break-word !important; }#prompter_label_space { font-size: 13px !important; color: var(--body-text-color-subdued, #6b7280) !important; margin-bottom: 8px !important; display: block !important; text-align: left !important; font-weight: 400 !important; }#prompter_output_div { height: 500px !important; max-height: 500px !important; overflow-y: auto !important; padding: 16px !important; line-height: 1.8 !important; box-sizing: border-box !important; background-color: #333 !important; color: #fff !important; border: 1px solid #ddd !important; border-radius: 8px !important; white-space: pre-wrap !important; word-break: break-word !important; text-align: left !important; margin-top: 0 !important; width: 100% !important; max-width: 100% !important; }#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; }"""script_js = "function(){ setTimeout(function(){ document.querySelector('#input_text_area textarea').style.fontSize='22px'; document.querySelector('#output_text_area textarea').style.fontSize='22px'; }, 500); }"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; }"
script_js = """function() { function applyFontSize(size) { let ta1 = document.querySelector('#input_text_area textarea'); if(ta1) { ta1.style.fontSize = size + 'px'; } 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; 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) { if (isSyncing1) { isSyncing1 = false; return; } if (ta1.scrollHeight - ta1.clientHeight <= 0) return; isSyncing2 = true; isSyncing3 = true; 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) { 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) { if (isSyncing3) { isSyncing3 = false; return; } if (div3.scrollHeight - div3.clientHeight <= 0) return; isSyncing1 = true; isSyncing2 = true; let percentage = div3.scrollTop / (div3.scrollHeight - div3.clientHeight); if (ta1.scrollHeight - ta1.clientHeight > 0) { ta1.scrollTop = percentage * (ta1.scrollHeight - ta1.clientHeight); } if (ta2 && ta2.scrollHeight - ta2.clientHeight > 0) { ta2.scrollTop = percentage * (ta2.scrollHeight - ta2.clientHeight); } } }, true);}"""
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; }"
# โ
์ฌ๊ธฐ์ ssr=False๋ฅผ ๋ฃ์ด์ผ ์๋ฌ๊ฐ ๋์ง ์์ต๋๋ค!with gr.Blocks(theme="default", css=custom_css, ssr=False) as iface:with gr.Blocks(theme="default", css=custom_css) as iface: user_temp_dict = gr.State(lambda: {})
gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ (ํ๋กฌํํฐ ๋ชจ๋)") gr.Markdown("## ๐๏ธ ๋๋ง์ TTS ์ ์ฒ๋ฆฌ ๋ณํ๊ธฐ") with gr.Tabs(): with gr.Tab("๐๏ธ ๋ฉ์ธ ํ๋ฉด"):1 unmodified line with gr.Column(): input_text = gr.Textbox(label="1๏ธโฃ ์๋ณธ ํ
์คํธ ์
๋ ฅ", elem_id="input_text_area") with gr.Group(): gr.Markdown("**โ๏ธ ๋ณํ ๋ฐ ํ๋ฉด ์ค์ **") 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") font_size = gr.Slider(minimum=16, maximum=40, value=22, step=2, label="๐ ๊ฒฐ๊ณผ์ฐฝ ๊ธ์ ํฌ๊ธฐ ์กฐ์ ") 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") font_size = gr.Slider(minimum=16, maximum=40, value=22, step=2, label="๐ ๊ธ์ ํฌ๊ธฐ") char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="๊ณต๋ฐฑ ํฌํจ: 0์ / ๊ณต๋ฐฑ ์ ์ธ: 0์", interactive=False) char_count = gr.Textbox(label="๊ธ์ ์ ํ์ธ", value="๊ณต๋ฐฑ ํฌํจ: 0์", interactive=False) with gr.Row(): btn_conv = gr.Button("โจ ๋ณํํ๊ธฐ", variant="primary") btn_copy = gr.Button("๐ ๋ณํ๋ณต์ฌ", variant="secondary") btn_clr = gr.Button("๐๏ธ ๋๋ณธ ์ด๊ธฐํ", variant="stop") btn_copy = gr.Button("๐ ๋ณต์ฌ", variant="secondary") btn_clr = gr.Button("๐๏ธ ์ด๊ธฐํ", variant="stop") with gr.Column(): initial_html = '<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ (๋ณ๊ฒฝ์ ํ์ธ)</div><div id="prompter_output_div" style="font-size:22px;"></div>' output_html = gr.HTML(value=initial_html, elem_id="prompter_html_wrap") with gr.Accordion("๐ ์์ ํ
์คํธ ๊ฒฐ๊ณผ (๋ณต์ฌ์ฉ) - ํด๋ฆญํด์ ํผ์น๊ธฐ", open=False): output_html = gr.HTML(value='<div id="prompter_label_space">๐บ ํ๋กฌํํฐ ๋ทฐ</div><div id="prompter_output_div" style="font-size:22px;"></div>', elem_id="prompter_html_wrap") with gr.Accordion("๐ ํ
์คํธ ๊ฒฐ๊ณผ (ํด๋ฆญํ์ฌ ํผ์น๊ธฐ)", open=False): output_text = gr.Textbox(lines=8, show_label=False, show_copy_button=True, elem_id="output_text_area") marker_collection = gr.Textbox(label="๐จ ์์ ํ์ ๋จ์ด ๋ชจ์๋ณด๊ธฐ (@@)", interactive=False, lines=2) marker_collection = gr.Textbox(label="๐จ ์์ ํ์ ๋จ์ด (@@)", interactive=False, lines=2) gr.Markdown("---") gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ** (์ด๋๋ฏผ: ์๊ตฌ ์ ์ฅ / ๊ฒ์คํธ: ์์ ์ ์ฅ)") gr.Markdown("**๐ก ๋๋ง์ ๋ฐ์ ์ฌ์ ์ถ๊ฐ**") with gr.Row(): nw = gr.Textbox(label="๊ธฐ์กด ๋จ์ด ๋๋ ๋ฌธ์ฅ (2๊ธ์ ์ด์)") nw = gr.Textbox(label="๋จ์ด (2๊ธ์ ์ด์)") np = gr.Textbox(label="์ํ๋ ๋ฐ์") admin_pw_input1 = gr.Textbox(label="๐ ๊ด๋ฆฌ์ ๋น๋ฐ๋ฒํธ (๊ฒ์คํธ๋ ๊ณต๋ฐฑ)", type="password") with gr.Column(min_width=200): btn_add = gr.Button("๐พ ์ฌ์ ์ ์ถ๊ฐํ๊ณ ์ฆ์ ๋๋ณธ ๋ณํํ๊ธฐ", variant="primary") btn_clear_dict_inputs = gr.Button("๐งน ๋จ์ด ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ", variant="secondary") status = gr.Textbox(label="์ ์ฅ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) with gr.Column(): btn_add = gr.Button("๐พ ์ ์ฉ ๋ฐ ๋ณํ", variant="primary") btn_clear_dict_inputs = gr.Button("๐งน ์
๋ ฅ์นธ ๋น์ฐ๊ธฐ", variant="secondary") status = gr.Textbox(label="์ ์ฅ ์ํ", interactive=False) btn_insert_marker.click(fn=None, inputs=[input_text], outputs=[input_text], js=insert_marker_js, api_name=False) btn_insert_marker.click(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) btn_conv.click(fn=process_text, inputs=[input_text, ai_mode, auto_break, smart_pause, font_size, user_temp_dict], outputs=[output_text, output_html, char_count, marker_collection], api_name=False) btn_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, 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_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, admin_pw_input1, 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=[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)
with gr.Tab("๐ฅ ์๋ ๋ฐฑ์
/์กฐํ/์์ (๋๋ ๋ฑ๋ก)"): dl_btn = gr.DownloadButton("๐ฅ 1. ํ์ฌ ์์ฑ๋ ์ฌ์ ๋ฐฑ์
ํ๊ธฐ (PC ๋ค์ด๋ก๋)") dl_btn = gr.DownloadButton("๐ฅ 1. ์ฌ์ ๋ฐฑ์
(PC ๋ค์ด๋ก๋)") dl_btn.click(fn=get_file, outputs=[dl_btn], api_name=False) gr.Markdown("---") gr.Markdown("### ๐ 2. ํ๋ฉด์์ ์ง์ ์ฌ์ ์กฐํ/์์ /์ญ์ ํ๊ธฐ (์ด๋๋ฏผ ์ ์ฉ ๊ธฐ๋ฅ ํฌํจ)") with gr.Row(): btn_load_json = gr.Button("๐ 1๋จ๊ณ: ํ์ฌ ๋ฑ๋ก๋ ๋ชจ๋ ๋จ์ด ๋ถ๋ฌ์ค๊ธฐ", variant="secondary") json_input = gr.Textbox(lines=15, label="์ฌ๊ธฐ์ ๋จ์ด๋ค์ด ํ์๋ฉ๋๋ค. ์ด์ํ ๋จ์ด(์: '์ด': '์ข
')๋ฅผ ์ฐพ์ ๊ทธ ์ค์ ์ญ์ ํ์ธ์!") btn_load_json = gr.Button("๐ 2. ๋ฑ๋ก๋ ๋จ์ด์ฅ ๋ถ๋ฌ์ค๊ธฐ", variant="secondary") json_input = gr.Textbox(lines=10, label="ํธ์ง์ฐฝ") admin_pw_input2 = gr.Textbox(label="๐ ๊ด๋ฆฌ์ ๋น๋ฐ๋ฒํธ (๊ฒ์คํธ๋ ๊ณต๋ฐฑ)", type="password") with gr.Row(): btn_apply_json = gr.Button("๐ 2๋จ๊ณ: ๊ธฐ์กด ๋จ์ด์ฅ์ ํ๋ฌ์ค(+) ํ๊ธฐ", variant="primary") btn_overwrite_json = gr.Button("๐จ 2๋จ๊ณ: ์์ ํ ๋ด์ฉ์ผ๋ก ์์ ํ ๋ฎ์ด์ฐ๊ธฐ (์ด๋๋ฏผ๋ง ๊ฐ๋ฅ)", variant="stop") 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_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) btn_apply_json.click(fn=apply_json_text, inputs=[json_input, admin_pw_input2, user_temp_dict], outputs=[json_stat, user_temp_dict], api_name=False) btn_overwrite_json.click(fn=overwrite_json_text, inputs=[json_input, admin_pw_input2], outputs=[json_stat], api_name=False)
gr.Markdown("---") with gr.Accordion("๐ค 3. ๋ฐฑ์
ํ์ผ๋ก ๊ธฐ์กด ์ฌ์ ์ ๋ณํฉํ๊ธฐ", open=False): gr.Markdown("โป ์ด๋๋ฏผ์ ์๋ฒ์ ์๊ตฌ ๋ณํฉ, ๊ฒ์คํธ๋ ๋ธ๋ผ์ฐ์ ์ ์์ ๋ณํฉ๋ฉ๋๋ค.") with gr.Accordion("๐ค 3. ๋ฐฑ์
ํ์ผ๋ก ์ฌ์ ์ ๋ณํฉํ๊ธฐ", open=False): admin_pw_input3 = gr.Textbox(label="๐ ๊ด๋ฆฌ์ ๋น๋ฐ๋ฒํธ (๊ฒ์คํธ๋ ๊ณต๋ฐฑ)", type="password") up_file = gr.File(label="๋ฐฑ์
ํ์ผ ์
๋ก๋", type="filepath") up_stat = gr.Textbox(label="ํ์ผ ๋ณต๊ตฌ ์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file, user_temp_dict], outputs=[up_stat, user_temp_dict], api_name=False) up_stat = gr.Textbox(label="์ํ ๊ฒฐ๊ณผ์ฐฝ", interactive=False) up_file.upload(fn=upload_file, inputs=[up_file, admin_pw_input3, user_temp_dict], outputs=[up_stat, user_temp_dict], api_name=False)
iface.load(None, js=script_js)
if __name__ == "__main__": auth_list = [ (ADMIN_ID, ADMIN_PW), (GUEST_ID, GUEST_PW) ] # launch์๋ ๊น๋ํ๊ฒ auth๋ง ์ ๋ฌํฉ๋๋ค. iface.launch(auth=auth_list) iface.launch()app.py+112-272