easyadaptor's picture
Update app.py
53df6b8 verified
Raw History Blame
22.3 kB
import gradio as gr
import json
import os
import difflib
import shutil
import re
try:
from g2pk import G2p
except ImportError:
G2p = None
print("G2P ๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘...")
try:
if G2p:
g2p = G2p()
else:
g2p = None
except Exception as e:
print(f"G2P ๋กœ๋”ฉ ์‹คํŒจ: {e}")
g2p = None
dict_path = "custom_dict.json"
# =====================================================================
# โ˜… AI ์ž๋™ ๋ณต๊ตฌ & ๋ณ‘ํ•ฉ ๋กœ์ง (๊ธฐ์กด ์™„๋ฒฝ ์œ ์ง€) โ˜…
# =====================================================================
AUTO_MERGE_WORDS = [["GS๋งˆ์ด์ƒต", "์ง€์—์Šค๋งˆ์ด์ƒต"],["์ง€์—์Šค ๋งˆ์ด์ƒต", "์ง€์—์Šค ๋งˆ์ด์ƒต"],["1+1", "์›ํ”Œ๋Ÿฌ์Šค์›"],["2+1", "ํˆฌํ”Œ๋Ÿฌ์Šค์›"],["F/W", "์—ํ”„ ๋”๋ธ”์œ "],["S/S", "์—์Šค ์—์Šค"],["VIP", "๋ธŒ์ด์•„์ดํ”ผ"],["ํ• ์ธ๊ฐ€", "ํ•˜๋ฆฐ๊นŒ"],["ํŠน๊ฐ€", "ํŠน๊นŒ"],["์ตœ์ €๊ฐ€", "์ตœ์ €๊นŒ"],["๋‹จ๋…๊ตฌ์„ฑ", "๋‹จ๋…๊พธ์„ฑ"],["์—ญ๋Œ€๊ธ‰", "์—ญ๋•Œ๋•"],["์•ฑ๊ฒฐ์ œ", "์•ฑ๊ปผ์ œ"],["์ผ์‹œ๋ถˆ", "์ผ์”จ๋ถˆ"],["๋ฌด์ด์ž", "๋ฌด์ด์ž"],["๋งค์ง„์ž„๋ฐ•", "๋งค์ง€๋‹˜๋ฐ•"],["์ž๋™์ฃผ๋ฌธ", "์ž๋™์ฃผ๋ฌธ"],["์‚ฌ์€ํ’ˆ", "์‚ฌ์€ํ’ˆ"],["ํ€„๋ฆฌํ‹ฐ๋ฅผ", "ํ€„๋ฆฌํ‹ฐ๋ฅผ"],["๋””ํ…Œ์ผ์ด", "๋””ํ…Œ์ด๋ฆฌ"],["์‹ค๋ฃจ์—ฃ์ด", "์‹ค๋ฃจ์—์‹œ"],["์ปคํ„ฐ์•ค๋ฒ…", "์ปคํ„ฐ์•ค๋ฒ…"],["์„ธ๋ฅด์ฆˆ๋ธ”๋ž‘์ฝ”", "์„ธ๋ฅด์ฆˆ๋ธ”๋ž‘์ฝ”"],["ํฌ๋จธํ‹ฐ๋ธŒ", "ํฌ๋จธํ‹ฐ๋ธŒ"],["๋นˆํŠธ๋ฆด", "๋นˆํŠธ๋ฆด"],["๋ ˆ๋ฏธ์•ค๋ฅด๋ฃจ", "๋ ˆ๋ฏธ์•ค๋ฅด๋ฃจ"],["์žญ๋‹ˆํด๋ผ์šฐ์Šค", "์Ÿ๋‹ˆํด๋ผ์šฐ์Šค"],["ํ‘ธ๋งˆ๊ณจํ”„", "ํ‘ธ๋งˆ๊ณจํ”„"],["์ปฌ๋Ÿผ๋น„์•„", "์ปฌ๋Ÿผ๋น„์•„"],["์—์Šค๊นŒ๋‹ค์Šคํฌ์ธ ", "์—์Šค๊นŒ๋‹ค์Šคํฌ์ธ "],["์—์Šค์นด๋‹ค์Šคํฌ์ธ ", "์—์Šค๊นŒ๋‹ค์Šคํฌ์ธ "],["์˜์šธ", "์˜์šธ"],["์—”๋‹ˆํŠœ๋“œ", "์—”๋‹ˆํŠœ๋“œ"],["๋ผ์‚์•„ํ”„", "๋ผ์‚์•„ํ”„"],["๋ชจ๋ฅด๊ฐ„", "๋ชจ๋ฅด๊ฐ„"],["๋ง‰์Šค์ŠคํŠœ๋””์˜ค", "๋ง‰์“ฐ์ŠคํŠœ๋””์˜ค"],["๋ฐ์ผ๋ฆฌ๋ธ”๋ฃจ", "๋ฐ์ผ๋ฆฌ๋ธ”๋ฃจ"],["ํŠธ๋ผ์ด์—„ํ”„", "ํŠธ๋ผ์ด์—„ํ”„"],["์•Œ์”จ๋น„ ํด๋กœํด๋Ÿฝ", "์•Œ์”จ๋น„ ํด๋กœํด๋Ÿฝ"],["๊ธฐํŽ ", "๊ธฐํŽ "],["์ƒค๋ก ๋А", "์ƒค๋ก ๋А"],["SJ์™€๋‹ˆ", "์—์Šค์ œ์ด์™€๋‹ˆ"],["์ฝ”์–ด ์–ด์„ผํ‹ฑ", "์ฝ”์–ด ์–ด์„ผํ‹ฑ"],["๋ฅด๋„คํฌ๋ฃจ", "๋ฅด๋„คํฌ๋ฃจ"],["๋””ํŽ˜", "๋””ํŽ˜"],["1์ข…", "์ผ์ข…"],["2์ข…", "์ด์ข…"],["3์ข…", "์‚ผ์ข…"],["4์ข…", "์‚ฌ์ข…"],["5์ข…", "์˜ค์ข…"],["10์ข…", "์‹ญ์ข…"],["4in1", "ํฌ์ธ์›"],["25FW", "์ด์˜ค ์—ํ”„ ๋”๋ธ”์œ "],["25 F/W", "์ด์˜ค ์—ํ”„ ๋”๋ธ”์œ "],["์ด์˜ค ์—ํ”„๋– ๋ธ”์œ ", "์ด์˜ค ์—ํ”„ ๋”๋ธ”์œ "],["26SS", "์ด์‹ญ๋‰ต์—์Šค์—์Šค"],["2026 SS", "์ด์ฒœ์ด์‹ญ๋‰ต ์—์Šค์—์Šค"],["SS์‹œ์ฆŒ", "์—์Šค์—์Šค ์‹œ์ฆŒ"],["์…‹์—…", "์„ธ๋ฅ"],["๋ฆฌ๋ฒ„์‹œ๋ธ”", "๋ฆฌ๋ฒ„์‹œ๋ธ”"],["๋งจํˆฌ๋งจ", "๋งจํˆฌ๋งจ"],["์œˆํ„ฐํŒฌ์ธ ", "์œˆํ„ฐํŒฌ์ธ "],["๋ณธ๋”ฉํŒฌ์ธ ", "๋ณธ๋”ฉํŒฌ์ธ "],["์Šค์œ™์ž์ผ“", "์Šค์œ™์ž์ผ“"],["ํ•˜์ด๋ธŒ๋ฆฌ๋“œ", "ํ•˜์ด๋ธŒ๋ฆฌ๋“œ"],["๋ ˆ์ด์–ด๋ง", "๋ ˆ์ด์–ด๋ง"],["๋ ˆ์ด์–ด๋“œ", "๋ ˆ์ด์–ด๋“œ"],["์ˆ„์นด๋ผ", "์ˆ„์นด๋ผ"],["์ธํƒ€์ƒค", "์ธํƒ€์ƒค"],["๋•๋‹ค์šดํŒจ๋”ฉ", "๋•๋”ฐ์šดํŒจ๋”ฉ"],["๋•๋‹ค์šด์Šค์ปคํŠธ", "๋•๋”ฐ์šด์Šค์ปคํŠธ"],["๊ทธ๋ ˆ์ด์ง€", "๊ทธ๋ ˆ์ด์ง€"],["ํŠธ์œŒ", "ํŠธ์œŒ"],["ํ™€๊ฐ€๋จผํŠธ", "ํ™€๊ฐ€๋จผํŠธ"],["ํ”Œ๋Ÿฌํ”ผ", "ํ”Œ๋Ÿฌํ”ผ"],["๋…ธ๋ฅด๋”•", "๋…ธ๋ฅด๋”•"],["์šธ๋ธ”๋ Œ๋“œ", "์šธ๋ธ”๋ Œ๋“œ"],["๋ผ์ฟค ๋ธ”๋ Œ๋“œ", "๋ผ์ฟค ๋ธ”๋ Œ๋“œ"],["์„ธ์ด์ง€๊ทธ๋ฆฐ", "์„ธ์ด์ง€๊ทธ๋ฆฐ"],["๋ชจ๋ธŒ์™€์ธ", "๋ชจ๋ธŒ์™€์ธ"],["ํฌ๋กœ์‰", "ํฌ๋กœ์…ฐ"],["๋ ˆ์ด์Šค ํ“จ์ง•", "๋ ˆ์ด์Šค ํ“จ์ง•"],["์‹œ์–ด์„œ์ปค", "์‹œ์–ด์„œ์ปค"],["ํ•ธ๋”” ์Šคํƒ€์ผ๋Ÿฌ", "ํ•ธ๋”” ์Šคํƒ€์ผ๋Ÿฌ"],["์•„์ผ๋ ›", "์•„์ผ๋ ›"],["์‹œ์–ด ๋ธ”๋ ˆ์ด์ €", "์‹œ์–ด ๋ธ”๋ ˆ์ด์ €"],["ํ‹ธ๋ธ”๋ฃจ", "ํ‹ธ๋ธ”๋ฃจ"],["์—์–ด๋ฆฌ", "์—์–ด๋ฆฌ"],["ํ•€ํ„ฑ", "ํ•€ํ„ฑ"],["์Šฌ๋ฆฌ๋ธŒ๋ฆฌ์Šค", "์Šฌ๋ฆฌ๋ธŒ๋ฆฌ์Šค"],["ํŠค ๋ ˆ์ด์–ด๋“œ", "ํŠค ๋ ˆ์ด์–ด๋“œ"],["์‰ฌํฐ", "์‰ฌํฐ"],["์ž‰ํฌ๋ธ”๋ž™", "์ž‰ํฌ๋ธ”๋ž™"],["์ธ๋จธ๋ธŒ๋ผ์šด", "์ธ๋จธ๋ธŒ๋ผ์šด"],["๋ธ”๋ฃธํ•‘ํฌ", "๋ธ”๋ฃธํ•‘ํฌ"],["๋ฏธ์ŠคํŠธ๋ธ”๋ฃจ", "๋ฏธ์ŠคํŠธ๋ธ”๋ฃจ"],["๋ฅด๋„ค๋ฒ ์ด์ง€", "๋ฅด๋„ค๋ฒ ์ด์ง€"],["์ธ๋จธ์นดํ‚ค", "์ธ๋จธ์นดํ‚ค"],["์ธํ”ผ๋‹ˆํ‹ฐ๋ธ”๋ฃจ", "์ธํ”ผ๋‹ˆํ‹ฐ๋ธ”๋ฃจ"],["๋ฅด๋„ค๋ธ”๋ž™", "๋ฅด๋„ค๋ธ”๋ž™"],["๋ธ”๋ฃธํ™”์ดํŠธ", "๋ธ”๋ฃธํ™”์ดํŠธ"],["์ธ๋จธ๋„ค์ด๋น„", "์ธ๋จธ๋„ค์ด๋น„"],["๋ธ”๋ผ์ธํ•‘ํฌ", "๋ธ”๋ผ์ธํ•‘ํฌ"],["์ธ๋จธ๋ฒ ์ด์ง€", "์ธ๋จธ๋ฒ ์ด์ง€"],["๋”ฅ์™€์ธ", "๋”ฅ์™€์ธ"],["100ํ”„๋กœ", "๋ฐฑํ”„๋กœ"],["100%", "๋ฐฑํ”„๋กœ"],["86%", "ํŒ”์‹ญ์œกํ”„๋กœ"],["14%", "์‹ญ์‚ฌํ”„๋กœ"],["1,499,900์›", "๋ฐฑ์‚ฌ์‹ญ๊ตฌ๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["169,900์›", "์‹ญ์œก๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["159,900์›", "์‹ญ์˜ค๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["149,000์›", "์‹ญ์‚ฌ๋งŒ ๊ตฌ์ฒœ ์›"],["12๋งŒ 9์ฒœ 8๋ฐฑ ์›", "์‹ญ์ด๋งŒ ๊ตฌ์ฒœํŒ”๋ฐฑ ์›"],["10๋งŒ 9์ฒœ์›", "์‹ญ๋งŒ ๊ตฌ์ฒœ์›"],["89,900์›", "ํŒ”๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["79,900์›", "์น ๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["79,900 ์›", "์น ๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["6๋งŒ 9์ฒœ 9๋ฐฑ ์›", "์œก๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["69,900์›", "์œก๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["69,900 ์›", "์œก๋งŒ ๊ตฌ์ฒœ๊ตฌ๋ฐฑ ์›"],["3๋งŒ 6์ฒœ์›๋Œ€", "์‚ผ๋งŒ ์œก์ฒœ์›๋Œ€"]
]
def perform_auto_recovery_and_merge():
if not os.path.exists(dict_path):
with open(dict_path, "w", encoding="utf-8") as f:
json.dump({"items":[], "meta": {"count": 0}}, f)
try:
with open(dict_path, "r", encoding="utf-8") as f:
existing_data = json.load(f)
except:
existing_data = {"items":[], "meta": {"count": 0}}
dict_map = {item[0].strip(): item[1].strip() for item in existing_data.get("items",[]) if len(item) == 2}
for w in AUTO_MERGE_WORDS:
dict_map[w[0]] = w[1]
merged_items = [[k, v] for k, v in dict_map.items()]
existing_data["items"] = merged_items
existing_data["meta"]["count"] = len(merged_items)
with open(dict_path, "w", encoding="utf-8") as f:
json.dump(existing_data, f, ensure_ascii=False, indent=2)
perform_auto_recovery_and_merge()
def load_dict():
flat_dict = {}
try:
if os.path.exists(dict_path):
with open(dict_path, "r", encoding="utf-8") as f:
data = json.load(f)
for item in data.get("items",[]):
if len(item) == 2:
flat_dict[item[0].strip()] = item[1].strip()
except:
pass
return flat_dict
def process_text(input_text, use_g2p, auto_break, font_size):
if not input_text or not input_text.strip():
return "", "", "์ž…๋ ฅ ๊ธ€์ž ์ˆ˜: 0์ž"
char_count = f"์ž…๋ ฅ ๊ธ€์ž ์ˆ˜: {len(input_text)}์ž"
flat_dict = load_dict()
if not flat_dict:
final_text = input_text
if use_g2p and g2p is not None:
try:
final_text = g2p(input_text)
except:
pass
else:
sorted_keys = sorted(flat_dict.keys(), key=len, reverse=True)
pattern = re.compile("|".join(map(re.escape, sorted_keys)))
parts =[]
last_end = 0
for match in pattern.finditer(input_text):
start, end = match.span()
chunk = input_text[last_end:start]
if chunk:
if use_g2p and g2p is not None:
try:
parts.append(g2p(chunk))
except:
parts.append(chunk)
else:
parts.append(chunk)
matched_word = match.group()
parts.append(flat_dict[matched_word])
last_end = end
tail = input_text[last_end:]
if tail:
if use_g2p and g2p is not None:
try:
parts.append(g2p(tail))
except:
parts.append(tail)
else:
parts.append(tail)
final_text = "".join(parts)
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 = 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>')
html_output = f'''
<div id="prompter_label_space">๐Ÿ“บ ํ”„๋กฌํ”„ํ„ฐ ๋ทฐ (๋ณ€๊ฒฝ์  ํ™•์ธ)</div>
<div id="prompter_output_div" style="font-size:{font_size}px;">
{"".join(html_parts)}
</div>
'''
return final_text, html_output, char_count
def clear_all():
return "", "", "", "์ž…๋ ฅ ๊ธ€์ž ์ˆ˜: 0์ž"
def add_and_update(word, pronun, current_input, use_g2p, auto_break, font_size):
if not word or not pronun:
return "โš ๏ธ ๊ธฐ์กด ๋‹จ์–ด์™€ ์›ํ•˜๋Š” ๋ฐœ์Œ์„ ๋ชจ๋‘ ์ž…๋ ฅํ•˜์„ธ์š”.", gr.update(), gr.update(), gr.update()
word = word.strip()
pronun = pronun.strip()
try:
with open(dict_path, "r", encoding="utf-8") as f:
j_struct = json.load(f)
except:
j_struct = {"items":[], "meta": {"count": 0}}
found = False
for item in j_struct.get("items",[]):
if item[0].strip() == word:
item[1] = pronun
found = True
break
if not found:
j_struct["items"].append([word, pronun])
try:
with open(dict_path, "w", encoding="utf-8") as f:
json.dump(j_struct, f, ensure_ascii=False, indent=2)
if current_input and word not in current_input:
status_msg = f"โš ๏ธ ์‚ฌ์ „ ์ €์žฅ ์™„๋ฃŒ! ํ•˜์ง€๋งŒ ์›๋ณธ ํ…์ŠคํŠธ์— '{word}'(์ด)๊ฐ€ ์—†์–ด ํ™”๋ฉด์— ๋ณ€ํ™”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
else:
status_msg = f"โœ… '{word}' -> '{pronun}' ์ €์žฅ ๋ฐ ์ฆ‰์‹œ ์žฌ๋ณ€ํ™˜ ์ ์šฉ ์™„๋ฃŒ!"
except Exception as e:
status_msg = f"โŒ ์ €์žฅ ์‹คํŒจ: {e}"
if current_input and current_input.strip():
final_text, html_output, char_count = process_text(current_input, use_g2p, auto_break, font_size)
return status_msg, final_text, html_output, char_count
else:
return status_msg, "", "", "์ž…๋ ฅ ๊ธ€์ž ์ˆ˜: 0์ž"
def get_file():
return dict_path if os.path.exists(dict_path) else None
def upload_file(file_path):
if file_path is not None:
shutil.copy(file_path, dict_path)
return "โœ… ํŒŒ์ผ ๋ณต๊ตฌ ์™„๋ฃŒ!"
return "โš ๏ธ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค."
def apply_json_text(json_string):
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' ํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค)"
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 new_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)
with open(dict_path, "w", encoding="utf-8") as f:
json.dump(existing_data, f, ensure_ascii=False, indent=2)
return f"โœ… ๊ธฐ์กด ๋‹จ์–ด๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ {len(new_data.get('items',[]))}๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ํ”Œ๋Ÿฌ์Šค(+) ํ–ˆ์Šต๋‹ˆ๋‹ค!"
except Exception as e:
return f"โŒ JSON ํŒŒ์‹ฑ ์—๋Ÿฌ: {e}"
# โ˜… ํ•ต์‹ฌ! ๋“ค์—ฌ์“ฐ๊ธฐ ์›์ธ(max-width)์„ ๋ฌด๋ ฅํ™”ํ•˜๊ณ  ์–‘์ชฝ์„ 100% ๋™์ผํ•˜๊ฒŒ ์„ธํŒ…ํ•˜๋Š” ์™„๋ฒฝ CSS โ˜…
custom_css = """
/* Gradio์˜ HTML ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ•์ œ๋กœ ํญ์„ ์ขํžˆ๊ณ  ์ค‘์•™ ์ •๋ ฌํ•˜๋Š” ์•…์„ฑ ๊ธฐ๋ณธ๊ฐ’์„ ์™„๋ฒฝํžˆ ๋ฌด๋ ฅํ™”ํ•ฉ๋‹ˆ๋‹ค! */
#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 {
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;
font-family: inherit !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;
}
"""
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);
}
"""
with gr.Blocks(theme="default", css=custom_css) as iface:
gr.Markdown("## ๐ŸŽ™๏ธ ๋‚˜๋งŒ์˜ TTS ์ „์ฒ˜๋ฆฌ ๋ณ€ํ™˜๊ธฐ (ํ”„๋กฌํ”„ํ„ฐ ๋ชจ๋“œ)")
with gr.Tabs():
with gr.Tab("๐ŸŽ™๏ธ ๋ฉ”์ธ ํ™”๋ฉด"):
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="1๏ธโƒฃ ์›๋ณธ ํ…์ŠคํŠธ ์ž…๋ ฅ", elem_id="input_text_area")
with gr.Group():
gr.Markdown("**โš™๏ธ ๋ณ€ํ™˜ ๋ฐ ํ™”๋ฉด ์„ค์ •**")
use_g2p = gr.Checkbox(label="๐Ÿค– AI ๋ฐœ์Œ ๋ณ€ํ™˜ ์ผœ๊ธฐ (์—ฐ์Œ ์ฒ˜๋ฆฌ์šฉ)", value=False)
auto_break = gr.Checkbox(label="๐Ÿ“– ๋ฌธ์žฅ ๋์—์„œ ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆ ํ•˜๊ธฐ", value=True)
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_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>'
# โ˜… ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„: elem_id="prompter_html_wrap" ๋ฅผ ๋ถ€์—ฌํ•ด์„œ Gradio์˜ ๊ฐ•์ œ ์ค‘์•™์ •๋ ฌ/ํญ์ œํ•œ ์†์„ฑ์„ CSS๋กœ ๋ฌด๋ ฅํ™”ํ•ฉ๋‹ˆ๋‹ค! โ˜…
output_html = gr.HTML(value=initial_html, 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")
btn_conv.click(fn=process_text, inputs=[input_text, use_g2p, auto_break, font_size], outputs=[output_text, output_html, char_count], api_name=False)
btn_clr.click(fn=clear_all, outputs=[input_text, output_text, output_html, char_count], 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, use_g2p, auto_break, font_size],
outputs=[output_text, output_html, char_count],
api_name=False
).then(
fn=None,
inputs=[font_size],
js="(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'; } "
"}"
)
auto_break.change(fn=process_text, inputs=[input_text, use_g2p, auto_break, font_size], outputs=[output_text, output_html, char_count], api_name=False)
gr.Markdown("---")
with gr.Row():
nw = gr.Textbox(label="๊ธฐ์กด ๋‹จ์–ด ๋˜๋Š” ๋ฌธ์žฅ (โ˜…๋ฐ˜๋“œ์‹œ 1๋ฒˆ ์ฐฝ์—์„œ ๋ณต์‚ฌํ•ด์˜ค์„ธ์š”โ˜…)")
np = gr.Textbox(label="์›ํ•˜๋Š” ๋ฐœ์Œ")
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)
btn_add.click(
fn=add_and_update,
inputs=[nw, np, input_text, use_g2p, auto_break, font_size],
outputs=[status, output_text, output_html, char_count],
api_name=False
)
btn_clear_dict_inputs.click(
fn=lambda: ("", ""),
outputs=[nw, np],
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("---")
gr.Markdown("### ๐Ÿ“ 2. ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ง์ ‘ ๋ถ™์—ฌ๋„ฃ๊ธฐ")
json_input = gr.Textbox(lines=10, label="์—ฌ๊ธฐ์— ์ถ”์ถœ๋œ JSON ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์œผ์„ธ์š”")
btn_apply_json = gr.Button("๐Ÿš€ ๊ธฐ์กด ๋‹จ์–ด์žฅ์— ํ”Œ๋Ÿฌ์Šค(+) ํ•˜๊ธฐ", variant="primary")
json_stat = gr.Textbox(label="๋ณต๊ตฌ ์ƒํƒœ ๊ฒฐ๊ณผ์ฐฝ", interactive=False)
btn_apply_json.click(fn=apply_json_text, inputs=[json_input], outputs=[json_stat], api_name=False)
gr.Markdown("---")
gr.Markdown("### ๐Ÿ“ค 3. ๋ฐฑ์—… ํŒŒ์ผ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ (โ€ป ์ฃผ์˜: ๊ธฐ์กด ๋‹จ์–ด ์ดˆ๊ธฐํ™”๋จ)")
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)
iface.load(None, js=script_js)
if __name__ == "__main__":
iface.launch()