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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
import tempfile
|
| 7 |
+
import logging
|
| 8 |
+
import random
|
| 9 |
+
import uuid
|
| 10 |
+
import shutil
|
| 11 |
+
import glob
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
from gradio_client import Client
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
|
| 16 |
+
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
# ๋ก๊น
์ค์ (API ์๋ํฌ์ธํธ ์ ๋ณด๋ ์ ์ธ)
|
| 20 |
+
logging.basicConfig(
|
| 21 |
+
level=logging.INFO,
|
| 22 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 23 |
+
handlers=[
|
| 24 |
+
logging.StreamHandler(),
|
| 25 |
+
logging.FileHandler('control_tower_app.log', mode='a')
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
logger = logging.getLogger(__name__)
|
| 30 |
+
|
| 31 |
+
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 32 |
+
def get_api_client():
|
| 33 |
+
"""ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ๋ฅผ ๊ฐ์ ธ์ ํด๋ผ์ด์ธํธ ์์ฑ"""
|
| 34 |
+
endpoint = os.getenv('API_ENDPOINT')
|
| 35 |
+
if not endpoint:
|
| 36 |
+
logger.error("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 37 |
+
raise ValueError("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
|
| 38 |
+
return Client(endpoint)
|
| 39 |
+
|
| 40 |
+
# ์ธ์
๋ณ ์์ ํ์ผ ๊ด๋ฆฌ๋ฅผ ์ํ ๋์
๋๋ฆฌ
|
| 41 |
+
session_temp_files = {}
|
| 42 |
+
session_data = {}
|
| 43 |
+
|
| 44 |
+
def cleanup_huggingface_temp_folders():
|
| 45 |
+
"""ํ๊น
ํ์ด์ค ์์ ํด๋ ์ด๊ธฐ ์ ๋ฆฌ"""
|
| 46 |
+
try:
|
| 47 |
+
# ์ผ๋ฐ์ ์ธ ์์ ๋๋ ํ ๋ฆฌ๋ค
|
| 48 |
+
temp_dirs = [
|
| 49 |
+
tempfile.gettempdir(),
|
| 50 |
+
"/tmp",
|
| 51 |
+
"/var/tmp",
|
| 52 |
+
os.path.join(os.getcwd(), "temp"),
|
| 53 |
+
os.path.join(os.getcwd(), "tmp"),
|
| 54 |
+
"/gradio_cached_examples",
|
| 55 |
+
"/flagged"
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
cleanup_count = 0
|
| 59 |
+
|
| 60 |
+
for temp_dir in temp_dirs:
|
| 61 |
+
if os.path.exists(temp_dir):
|
| 62 |
+
try:
|
| 63 |
+
# ๊ธฐ์กด ์ธ์
ํ์ผ๋ค ์ ๋ฆฌ
|
| 64 |
+
session_files = glob.glob(os.path.join(temp_dir, "session_*.xlsx"))
|
| 65 |
+
session_files.extend(glob.glob(os.path.join(temp_dir, "session_*.csv")))
|
| 66 |
+
session_files.extend(glob.glob(os.path.join(temp_dir, "*keyword*.xlsx")))
|
| 67 |
+
session_files.extend(glob.glob(os.path.join(temp_dir, "*keyword*.csv")))
|
| 68 |
+
session_files.extend(glob.glob(os.path.join(temp_dir, "tmp*.xlsx")))
|
| 69 |
+
session_files.extend(glob.glob(os.path.join(temp_dir, "tmp*.csv")))
|
| 70 |
+
|
| 71 |
+
for file_path in session_files:
|
| 72 |
+
try:
|
| 73 |
+
# ํ์ผ์ด 1์๊ฐ ์ด์ ์ค๋๋ ๊ฒฝ์ฐ๋ง ์ญ์
|
| 74 |
+
if os.path.getmtime(file_path) < time.time() - 3600:
|
| 75 |
+
os.remove(file_path)
|
| 76 |
+
cleanup_count += 1
|
| 77 |
+
logger.info(f"์ด๊ธฐ ์ ๋ฆฌ: ์ค๋๋ ์์ ํ์ผ ์ญ์ - {file_path}")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.warning(f"ํ์ผ ์ญ์ ์คํจ (๋ฌด์๋จ): {file_path} - {e}")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger.warning(f"์์ ๋๋ ํ ๋ฆฌ ์ ๋ฆฌ ์คํจ (๋ฌด์๋จ): {temp_dir} - {e}")
|
| 83 |
+
|
| 84 |
+
logger.info(f"โ
ํ๊น
ํ์ด์ค ์์ ํด๋ ์ด๊ธฐ ์ ๋ฆฌ ์๋ฃ - {cleanup_count}๊ฐ ํ์ผ ์ญ์ ")
|
| 85 |
+
|
| 86 |
+
# Gradio ์บ์ ํด๋๋ ์ ๋ฆฌ
|
| 87 |
+
try:
|
| 88 |
+
gradio_temp_dir = os.path.join(os.getcwd(), "gradio_cached_examples")
|
| 89 |
+
if os.path.exists(gradio_temp_dir):
|
| 90 |
+
shutil.rmtree(gradio_temp_dir, ignore_errors=True)
|
| 91 |
+
logger.info("Gradio ์บ์ ํด๋ ์ ๋ฆฌ ์๋ฃ")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
logger.warning(f"Gradio ์บ์ ํด๋ ์ ๋ฆฌ ์คํจ (๋ฌด์๋จ): {e}")
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error(f"์ด๊ธฐ ์์ ํด๋ ์ ๋ฆฌ ์ค ์ค๋ฅ (๊ณ์ ์งํ): {e}")
|
| 97 |
+
|
| 98 |
+
def setup_clean_temp_environment():
|
| 99 |
+
"""๊นจ๋ํ ์์ ํ๊ฒฝ ์ค์ """
|
| 100 |
+
try:
|
| 101 |
+
# 1. ๊ธฐ์กด ์์ ํ์ผ๋ค ์ ๋ฆฌ
|
| 102 |
+
cleanup_huggingface_temp_folders()
|
| 103 |
+
|
| 104 |
+
# 2. ์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 105 |
+
app_temp_dir = os.path.join(tempfile.gettempdir(), "control_tower_app")
|
| 106 |
+
if os.path.exists(app_temp_dir):
|
| 107 |
+
shutil.rmtree(app_temp_dir, ignore_errors=True)
|
| 108 |
+
os.makedirs(app_temp_dir, exist_ok=True)
|
| 109 |
+
|
| 110 |
+
# 3. ํ๊ฒฝ ๋ณ์ ์ค์ (์์ ๋๋ ํ ๋ฆฌ ์ง์ )
|
| 111 |
+
os.environ['CONTROL_TOWER_TEMP'] = app_temp_dir
|
| 112 |
+
|
| 113 |
+
logger.info(f"โ
์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์ค์ : {app_temp_dir}")
|
| 114 |
+
|
| 115 |
+
return app_temp_dir
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
logger.error(f"์์ ํ๊ฒฝ ์ค์ ์คํจ: {e}")
|
| 119 |
+
return tempfile.gettempdir()
|
| 120 |
+
|
| 121 |
+
def get_app_temp_dir():
|
| 122 |
+
"""์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ๋ฐํ"""
|
| 123 |
+
return os.environ.get('CONTROL_TOWER_TEMP', tempfile.gettempdir())
|
| 124 |
+
|
| 125 |
+
def get_session_id():
|
| 126 |
+
"""์ธ์
ID ์์ฑ"""
|
| 127 |
+
return str(uuid.uuid4())
|
| 128 |
+
|
| 129 |
+
def cleanup_session_files(session_id, delay=300):
|
| 130 |
+
"""์ธ์
๋ณ ์์ ํ์ผ ์ ๋ฆฌ ํจ์"""
|
| 131 |
+
def cleanup():
|
| 132 |
+
time.sleep(delay)
|
| 133 |
+
if session_id in session_temp_files:
|
| 134 |
+
files_to_remove = session_temp_files[session_id].copy()
|
| 135 |
+
del session_temp_files[session_id]
|
| 136 |
+
|
| 137 |
+
for file_path in files_to_remove:
|
| 138 |
+
try:
|
| 139 |
+
if os.path.exists(file_path):
|
| 140 |
+
os.remove(file_path)
|
| 141 |
+
logger.info(f"์ธ์
{session_id[:8]}... ์์ ํ์ผ ์ญ์ : {file_path}")
|
| 142 |
+
except Exception as e:
|
| 143 |
+
logger.error(f"์ธ์
{session_id[:8]}... ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 144 |
+
|
| 145 |
+
threading.Thread(target=cleanup, daemon=True).start()
|
| 146 |
+
|
| 147 |
+
def register_session_file(session_id, file_path):
|
| 148 |
+
"""์ธ์
๋ณ ํ์ผ ๋ฑ๋ก"""
|
| 149 |
+
if session_id not in session_temp_files:
|
| 150 |
+
session_temp_files[session_id] = []
|
| 151 |
+
session_temp_files[session_id].append(file_path)
|
| 152 |
+
|
| 153 |
+
def cleanup_old_sessions():
|
| 154 |
+
"""์ค๋๋ ์ธ์
๋ฐ์ดํฐ ์ ๋ฆฌ"""
|
| 155 |
+
current_time = time.time()
|
| 156 |
+
sessions_to_remove = []
|
| 157 |
+
|
| 158 |
+
for session_id, data in session_data.items():
|
| 159 |
+
if current_time - data.get('last_activity', 0) > 3600: # 1์๊ฐ ์ด๊ณผ
|
| 160 |
+
sessions_to_remove.append(session_id)
|
| 161 |
+
|
| 162 |
+
for session_id in sessions_to_remove:
|
| 163 |
+
# ํ์ผ ์ ๋ฆฌ
|
| 164 |
+
if session_id in session_temp_files:
|
| 165 |
+
for file_path in session_temp_files[session_id]:
|
| 166 |
+
try:
|
| 167 |
+
if os.path.exists(file_path):
|
| 168 |
+
os.remove(file_path)
|
| 169 |
+
logger.info(f"์ค๋๋ ์ธ์
{session_id[:8]}... ํ์ผ ์ญ์ : {file_path}")
|
| 170 |
+
except Exception as e:
|
| 171 |
+
logger.error(f"์ค๋๋ ์ธ์
ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 172 |
+
del session_temp_files[session_id]
|
| 173 |
+
|
| 174 |
+
# ์ธ์
๋ฐ์ดํฐ ์ ๋ฆฌ
|
| 175 |
+
if session_id in session_data:
|
| 176 |
+
del session_data[session_id]
|
| 177 |
+
logger.info(f"์ค๋๋ ์ธ์
๋ฐ์ดํฐ ์ญ์ : {session_id[:8]}...")
|
| 178 |
+
|
| 179 |
+
def update_session_activity(session_id):
|
| 180 |
+
"""์ธ์
ํ๋ ์๊ฐ ์
๋ฐ์ดํธ"""
|
| 181 |
+
if session_id not in session_data:
|
| 182 |
+
session_data[session_id] = {}
|
| 183 |
+
session_data[session_id]['last_activity'] = time.time()
|
| 184 |
+
|
| 185 |
+
def create_session_temp_file(session_id, suffix='.xlsx'):
|
| 186 |
+
"""์ธ์
๋ณ ์์ ํ์ผ ์์ฑ (์ ์ฉ ๋๋ ํ ๋ฆฌ ์ฌ์ฉ)"""
|
| 187 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 188 |
+
random_suffix = str(random.randint(1000, 9999))
|
| 189 |
+
|
| 190 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์ฌ์ฉ
|
| 191 |
+
temp_dir = get_app_temp_dir()
|
| 192 |
+
filename = f"session_{session_id[:8]}_{timestamp}_{random_suffix}{suffix}"
|
| 193 |
+
temp_file_path = os.path.join(temp_dir, filename)
|
| 194 |
+
|
| 195 |
+
# ๋น ํ์ผ ์์ฑ
|
| 196 |
+
with open(temp_file_path, 'w') as f:
|
| 197 |
+
pass
|
| 198 |
+
|
| 199 |
+
register_session_file(session_id, temp_file_path)
|
| 200 |
+
return temp_file_path
|
| 201 |
+
|
| 202 |
+
def wrapper_modified(keyword, korean_only, apply_main_keyword_option, exclude_zero_volume, session_id):
|
| 203 |
+
"""ํค์๋ ๊ฒ์ ๋ฐ ์ฒ๋ฆฌ ๋ํผ ํจ์ (API ํด๋ผ์ด์ธํธ ์ฌ์ฉ)"""
|
| 204 |
+
update_session_activity(session_id)
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
client = get_api_client()
|
| 208 |
+
|
| 209 |
+
# API ํธ์ถ
|
| 210 |
+
result = client.predict(
|
| 211 |
+
keyword=keyword,
|
| 212 |
+
korean_only=korean_only,
|
| 213 |
+
apply_main_keyword=apply_main_keyword_option,
|
| 214 |
+
exclude_zero_volume=exclude_zero_volume,
|
| 215 |
+
api_name="/process_search_results"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
# ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
| 219 |
+
table_html, cat_choices, vol_choices, selected_cat, download_file = result
|
| 220 |
+
|
| 221 |
+
# ๋ค์ด๋ก๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 222 |
+
excel_path = None
|
| 223 |
+
if download_file:
|
| 224 |
+
excel_path = create_session_temp_file(session_id, '.xlsx')
|
| 225 |
+
# ์๊ฒฉ ํ์ผ์ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 226 |
+
try:
|
| 227 |
+
import shutil
|
| 228 |
+
shutil.copy2(download_file, excel_path)
|
| 229 |
+
except Exception as e:
|
| 230 |
+
logger.error(f"ํ์ผ ๋ณต์ฌ ์ค๋ฅ: {e}")
|
| 231 |
+
excel_path = None
|
| 232 |
+
|
| 233 |
+
# ๊ฐ์์ DataFrame ์ํ (์ค์ ๋ก๋ ์ฌ์ฉ๋์ง ์์)
|
| 234 |
+
df_state = pd.DataFrame()
|
| 235 |
+
|
| 236 |
+
if table_html and "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค" not in table_html:
|
| 237 |
+
return (gr.update(value=table_html), gr.update(choices=cat_choices),
|
| 238 |
+
gr.update(choices=vol_choices), df_state,
|
| 239 |
+
gr.update(choices=cat_choices, value=selected_cat), excel_path,
|
| 240 |
+
gr.update(visible=True), gr.update(visible=True), keyword)
|
| 241 |
+
else:
|
| 242 |
+
return (gr.update(value="<p>๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ๋ค๋ฅธ ํค์๋๋ก ์๋ํด๋ณด์ธ์.</p>"),
|
| 243 |
+
gr.update(choices=["์ ์ฒด ๋ณด๊ธฐ"]), gr.update(choices=["์ ์ฒด"]),
|
| 244 |
+
df_state, gr.update(choices=["์ ์ฒด ๋ณด๊ธฐ"], value="์ ์ฒด ๋ณด๊ธฐ"), None,
|
| 245 |
+
gr.update(visible=False), gr.update(visible=False), keyword)
|
| 246 |
+
|
| 247 |
+
except Exception as e:
|
| 248 |
+
logger.error(f"API ํธ์ถ ์ค๋ฅ: {e}")
|
| 249 |
+
return (gr.update(value="<p>์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.</p>"),
|
| 250 |
+
gr.update(choices=["์ ์ฒด ๋ณด๊ธฐ"]), gr.update(choices=["์ ์ฒด"]),
|
| 251 |
+
pd.DataFrame(), gr.update(choices=["์ ์ฒด ๋ณด๊ธฐ"], value="์ ์ฒด ๋ณด๊ธฐ"), None,
|
| 252 |
+
gr.update(visible=False), gr.update(visible=False), "")
|
| 253 |
+
|
| 254 |
+
def analyze_with_auto_download(analysis_keywords, selected_category, state_df, session_id):
|
| 255 |
+
"""์นดํ
๊ณ ๋ฆฌ ์ผ์น ๋ถ์ ์คํ ๋ฐ ์๋ ๋ค์ด๋ก๋ (API ํด๋ผ์ด์ธํธ ์ฌ์ฉ)"""
|
| 256 |
+
update_session_activity(session_id)
|
| 257 |
+
|
| 258 |
+
try:
|
| 259 |
+
client = get_api_client()
|
| 260 |
+
|
| 261 |
+
# API ํธ์ถ
|
| 262 |
+
result = client.predict(
|
| 263 |
+
analysis_keywords=analysis_keywords,
|
| 264 |
+
selected_category=selected_category,
|
| 265 |
+
api_name="/process_analyze_results"
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
analysis_result, download_file = result
|
| 269 |
+
|
| 270 |
+
# ๋ค์ด๋ก๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 271 |
+
excel_path = None
|
| 272 |
+
if download_file:
|
| 273 |
+
excel_path = create_session_temp_file(session_id, '.xlsx')
|
| 274 |
+
try:
|
| 275 |
+
import shutil
|
| 276 |
+
shutil.copy2(download_file, excel_path)
|
| 277 |
+
except Exception as e:
|
| 278 |
+
logger.error(f"๋ถ์ ๊ฒฐ๊ณผ ํ์ผ ๋ณต์ฌ ์ค๋ฅ: {e}")
|
| 279 |
+
excel_path = None
|
| 280 |
+
|
| 281 |
+
return analysis_result, excel_path, gr.update(visible=True)
|
| 282 |
+
|
| 283 |
+
except Exception as e:
|
| 284 |
+
logger.error(f"๋ถ์ API ํธ์ถ ์ค๋ฅ: {e}")
|
| 285 |
+
return "๋ถ์ ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.", None, gr.update(visible=True)
|
| 286 |
+
|
| 287 |
+
def filter_and_sort_table(df, selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume, session_id):
|
| 288 |
+
"""ํ
์ด๋ธ ํํฐ๋ง ๋ฐ ์ ๋ ฌ ํจ์ (API ํด๋ผ์ด์ธํธ ์ฌ์ฉ)"""
|
| 289 |
+
update_session_activity(session_id)
|
| 290 |
+
|
| 291 |
+
try:
|
| 292 |
+
client = get_api_client()
|
| 293 |
+
|
| 294 |
+
# API ํธ์ถ
|
| 295 |
+
result = client.predict(
|
| 296 |
+
selected_cat=selected_cat,
|
| 297 |
+
keyword_sort=keyword_sort,
|
| 298 |
+
total_volume_sort=total_volume_sort,
|
| 299 |
+
usage_count_sort=usage_count_sort,
|
| 300 |
+
selected_volume_range=selected_volume_range,
|
| 301 |
+
exclude_zero_volume=exclude_zero_volume,
|
| 302 |
+
api_name="/filter_and_sort_table"
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
return result
|
| 306 |
+
|
| 307 |
+
except Exception as e:
|
| 308 |
+
logger.error(f"ํํฐ๋ง API ํธ์ถ ์ค๋ฅ: {e}")
|
| 309 |
+
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 310 |
+
|
| 311 |
+
def update_category_selection(selected_cat, session_id):
|
| 312 |
+
"""์นดํ
๊ณ ๋ฆฌ ํํฐ ์ ํ ์ ๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ๋ ๊ฐ์ ๊ฐ์ผ๋ก ์
๋ฐ์ดํธ (API ํด๋ผ์ด์ธํธ ์ฌ์ฉ)"""
|
| 313 |
+
update_session_activity(session_id)
|
| 314 |
+
|
| 315 |
+
try:
|
| 316 |
+
client = get_api_client()
|
| 317 |
+
|
| 318 |
+
result = client.predict(
|
| 319 |
+
selected_cat=selected_cat,
|
| 320 |
+
api_name="/update_category_selection"
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
return gr.update(value=result)
|
| 324 |
+
|
| 325 |
+
except Exception as e:
|
| 326 |
+
logger.error(f"์นดํ
๊ณ ๋ฆฌ ์ ํ API ํธ์ถ ์ค๋ฅ: {e}")
|
| 327 |
+
return gr.update(value=selected_cat)
|
| 328 |
+
|
| 329 |
+
def reset_interface(session_id):
|
| 330 |
+
"""์ธํฐํ์ด์ค ๋ฆฌ์
ํจ์ - ์ธ์
๋ณ ๋ฐ์ดํฐ ์ด๊ธฐํ"""
|
| 331 |
+
update_session_activity(session_id)
|
| 332 |
+
|
| 333 |
+
# ์ธ์
๋ณ ์์ ํ์ผ ์ ๋ฆฌ
|
| 334 |
+
if session_id in session_temp_files:
|
| 335 |
+
for file_path in session_temp_files[session_id]:
|
| 336 |
+
try:
|
| 337 |
+
if os.path.exists(file_path):
|
| 338 |
+
os.remove(file_path)
|
| 339 |
+
logger.info(f"์ธ์
{session_id[:8]}... ๋ฆฌ์
์ ํ์ผ ์ญ์ : {file_path}")
|
| 340 |
+
except Exception as e:
|
| 341 |
+
logger.error(f"์ธ์
{session_id[:8]}... ๋ฆฌ์
์ ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 342 |
+
session_temp_files[session_id] = []
|
| 343 |
+
|
| 344 |
+
try:
|
| 345 |
+
client = get_api_client()
|
| 346 |
+
|
| 347 |
+
result = client.predict(api_name="/reset_interface")
|
| 348 |
+
|
| 349 |
+
return result
|
| 350 |
+
|
| 351 |
+
except Exception as e:
|
| 352 |
+
logger.error(f"๋ฆฌ์
API ํธ์ถ ์ค๋ฅ: {e}")
|
| 353 |
+
# ๊ธฐ๋ณธ ๋ฆฌ์
๊ฐ ๋ฐํ
|
| 354 |
+
return (
|
| 355 |
+
"", # ๊ฒ์ ํค์๋
|
| 356 |
+
True, # ํ๊ธ๋ง ์ถ์ถ
|
| 357 |
+
False, # ๊ฒ์๋ 0 ํค์๋ ์ ์ธ
|
| 358 |
+
"๋ฉ์ธํค์๋ ์ ์ฉ", # ์กฐํฉ ๋ฐฉ์
|
| 359 |
+
"", # HTML ํ
์ด๋ธ
|
| 360 |
+
["์ ์ฒด ๋ณด๊ธฐ"], # ์นดํ
๊ณ ๋ฆฌ ํํฐ
|
| 361 |
+
"์ ์ฒด ๋ณด๊ธฐ", # ์นดํ
๊ณ ๋ฆฌ ํํฐ ์ ํ
|
| 362 |
+
["์ ์ฒด"], # ๊ฒ์๋ ๊ตฌ๊ฐ ํํฐ
|
| 363 |
+
"์ ์ฒด", # ๊ฒ์๋ ๊ตฌ๊ฐ ์ ํ
|
| 364 |
+
"์ ๋ ฌ ์์", # ์ด๊ฒ์๋ ์ ๋ ฌ
|
| 365 |
+
"์ ๋ ฌ ์์", # ํค์๋ ์ฌ์ฉํ์ ์ ๋ ฌ
|
| 366 |
+
["์ ์ฒด ๋ณด๊ธฐ"], # ๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ
|
| 367 |
+
"์ ์ฒด ๋ณด๊ธฐ", # ๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ ์ ํ
|
| 368 |
+
"", # ํค์๋ ์
๋ ฅ
|
| 369 |
+
"", # ๋ถ์ ๊ฒฐ๊ณผ
|
| 370 |
+
None, # ๋ค์ด๋ก๋ ํ์ผ
|
| 371 |
+
gr.update(visible=False), # ํค์๋ ๋ถ์ ์น์
|
| 372 |
+
gr.update(visible=False), # ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ์น์
|
| 373 |
+
"" # ํค์๋ ์ํ
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
# ๋ํผ ํจ์๋ค
|
| 377 |
+
def search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume, session_id):
|
| 378 |
+
update_session_activity(session_id)
|
| 379 |
+
return (
|
| 380 |
+
gr.update(visible=True),
|
| 381 |
+
gr.update(visible=False)
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
def process_search_results(keyword, korean_only, apply_main_keyword, exclude_zero_volume, session_id):
|
| 385 |
+
update_session_activity(session_id)
|
| 386 |
+
|
| 387 |
+
result = wrapper_modified(keyword, korean_only, apply_main_keyword, exclude_zero_volume, session_id)
|
| 388 |
+
|
| 389 |
+
table_html, cat_choices, vol_choices, df, selected_cat, excel, keyword_section_vis, cat_section_vis, new_keyword_state = result
|
| 390 |
+
|
| 391 |
+
if table_html and "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค" not in table_html:
|
| 392 |
+
empty_placeholder_vis = False
|
| 393 |
+
keyword_section_visibility = True
|
| 394 |
+
execution_section_visibility = True
|
| 395 |
+
else:
|
| 396 |
+
empty_placeholder_vis = True
|
| 397 |
+
keyword_section_visibility = False
|
| 398 |
+
execution_section_visibility = False
|
| 399 |
+
|
| 400 |
+
return (
|
| 401 |
+
table_html, cat_choices, vol_choices, df, selected_cat, excel,
|
| 402 |
+
gr.update(visible=keyword_section_visibility),
|
| 403 |
+
gr.update(visible=cat_section_vis),
|
| 404 |
+
gr.update(visible=False),
|
| 405 |
+
gr.update(visible=empty_placeholder_vis),
|
| 406 |
+
gr.update(visible=execution_section_visibility),
|
| 407 |
+
new_keyword_state
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
def analyze_with_loading(analysis_keywords, selected_category, state_df, session_id):
|
| 411 |
+
update_session_activity(session_id)
|
| 412 |
+
return gr.update(visible=True)
|
| 413 |
+
|
| 414 |
+
def process_analyze_results(analysis_keywords, selected_category, state_df, session_id):
|
| 415 |
+
update_session_activity(session_id)
|
| 416 |
+
results = analyze_with_auto_download(analysis_keywords, selected_category, state_df, session_id)
|
| 417 |
+
return results + (gr.update(visible=False),)
|
| 418 |
+
|
| 419 |
+
# ์ธ์
์ ๋ฆฌ ์ค์ผ์ค๋ฌ
|
| 420 |
+
def start_session_cleanup_scheduler():
|
| 421 |
+
"""์ธ์
์ ๋ฆฌ ์ค์ผ์ค๋ฌ ์์"""
|
| 422 |
+
def cleanup_scheduler():
|
| 423 |
+
while True:
|
| 424 |
+
time.sleep(600) # 10๋ถ๋ง๋ค ์คํ
|
| 425 |
+
cleanup_old_sessions()
|
| 426 |
+
# ์ถ๊ฐ๋ก ํ๊น
ํ์ด์ค ์์ ํด๋๋ ์ฃผ๊ธฐ์ ์ ๋ฆฌ
|
| 427 |
+
cleanup_huggingface_temp_folders()
|
| 428 |
+
|
| 429 |
+
threading.Thread(target=cleanup_scheduler, daemon=True).start()
|
| 430 |
+
|
| 431 |
+
def cleanup_on_startup():
|
| 432 |
+
"""์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์ ์ฒด ์ ๋ฆฌ"""
|
| 433 |
+
logger.info("๐งน ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์ - ์ด๊ธฐ ์ ๋ฆฌ ์์
์์...")
|
| 434 |
+
|
| 435 |
+
# 1. ํ๊น
ํ์ด์ค ์์ ํด๋ ์ ๋ฆฌ
|
| 436 |
+
cleanup_huggingface_temp_folders()
|
| 437 |
+
|
| 438 |
+
# 2. ๊นจ๋ํ ์์ ํ๊ฒฝ ์ค์
|
| 439 |
+
app_temp_dir = setup_clean_temp_environment()
|
| 440 |
+
|
| 441 |
+
# 3. ์ ์ญ ๋ณ์ ์ด๊ธฐํ
|
| 442 |
+
global session_temp_files, session_data
|
| 443 |
+
session_temp_files.clear()
|
| 444 |
+
session_data.clear()
|
| 445 |
+
|
| 446 |
+
logger.info(f"โ
์ด๊ธฐ ์ ๋ฆฌ ์์
์๋ฃ - ์ฑ ์ ์ฉ ๋๋ ํ ๋ฆฌ: {app_temp_dir}")
|
| 447 |
+
|
| 448 |
+
return app_temp_dir
|
| 449 |
+
|
| 450 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 451 |
+
def create_app():
|
| 452 |
+
fontawesome_html = """
|
| 453 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 454 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
|
| 455 |
+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700&display=swap">
|
| 456 |
+
"""
|
| 457 |
+
|
| 458 |
+
# CSS ํ์ผ ๋ก๋
|
| 459 |
+
try:
|
| 460 |
+
with open('style.css', 'r', encoding='utf-8') as f:
|
| 461 |
+
custom_css = f.read()
|
| 462 |
+
except:
|
| 463 |
+
custom_css = """
|
| 464 |
+
:root {
|
| 465 |
+
--primary-color: #FB7F0D;
|
| 466 |
+
--secondary-color: #ff9a8b;
|
| 467 |
+
}
|
| 468 |
+
.custom-button {
|
| 469 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
|
| 470 |
+
color: white !important;
|
| 471 |
+
border-radius: 30px !important;
|
| 472 |
+
height: 45px !important;
|
| 473 |
+
font-size: 16px !important;
|
| 474 |
+
font-weight: bold !important;
|
| 475 |
+
width: 100% !important;
|
| 476 |
+
text-align: center !important;
|
| 477 |
+
display: flex !important;
|
| 478 |
+
align-items: center !important;
|
| 479 |
+
justify-content: center !important;
|
| 480 |
+
}
|
| 481 |
+
"""
|
| 482 |
+
|
| 483 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Default(
|
| 484 |
+
primary_hue="orange",
|
| 485 |
+
secondary_hue="orange",
|
| 486 |
+
font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"]
|
| 487 |
+
)) as demo:
|
| 488 |
+
gr.HTML(fontawesome_html)
|
| 489 |
+
|
| 490 |
+
# ์ธ์
ID ์ํ (๊ฐ ์ฌ์ฉ์๋ณ๋ก ๊ณ ์ )
|
| 491 |
+
session_id = gr.State(get_session_id)
|
| 492 |
+
|
| 493 |
+
# ํค์๋ ์ํ ๊ด๋ฆฌ
|
| 494 |
+
keyword_state = gr.State("")
|
| 495 |
+
|
| 496 |
+
# ์
๋ ฅ ์น์
|
| 497 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 498 |
+
gr.HTML('<div class="section-title"><i class="fas fa-search"></i> ๊ฒ์ ์
๋ ฅ</div>')
|
| 499 |
+
|
| 500 |
+
with gr.Row():
|
| 501 |
+
with gr.Column(scale=1):
|
| 502 |
+
keyword = gr.Textbox(
|
| 503 |
+
label="๋ฉ์ธ ํค์๋",
|
| 504 |
+
placeholder="์: ์ค์ง์ด"
|
| 505 |
+
)
|
| 506 |
+
with gr.Column(scale=1):
|
| 507 |
+
search_btn = gr.Button(
|
| 508 |
+
"๋ฉ์ธํค์๋ ๋ถ์",
|
| 509 |
+
elem_classes="custom-button"
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
with gr.Accordion("์ต์
์ค์ ", open=False):
|
| 513 |
+
with gr.Row():
|
| 514 |
+
with gr.Column(scale=1):
|
| 515 |
+
korean_only = gr.Checkbox(
|
| 516 |
+
label="ํ๊ธ๋ง ์ถ์ถ",
|
| 517 |
+
value=True
|
| 518 |
+
)
|
| 519 |
+
with gr.Column(scale=1):
|
| 520 |
+
exclude_zero_volume = gr.Checkbox(
|
| 521 |
+
label="๊ฒ์๋ 0 ํค์๋ ์ ์ธ",
|
| 522 |
+
value=False
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
with gr.Row():
|
| 526 |
+
with gr.Column(scale=1):
|
| 527 |
+
apply_main_keyword = gr.Radio(
|
| 528 |
+
["๋ฉ์ธํค์๋ ์ ์ฉ", "๋ฉ์ธํค์๋ ๋ฏธ์ ์ฉ"],
|
| 529 |
+
label="์กฐํฉ ๋ฐฉ์",
|
| 530 |
+
value="๋ฉ์ธํค์๋ ์ ์ฉ"
|
| 531 |
+
)
|
| 532 |
+
with gr.Column(scale=1):
|
| 533 |
+
gr.HTML("")
|
| 534 |
+
|
| 535 |
+
# ์งํ ์ํ ํ์ ์น์
|
| 536 |
+
with gr.Column(elem_classes="custom-frame fade-in", visible=False) as progress_section:
|
| 537 |
+
gr.HTML('<div class="section-title"><i class="fas fa-spinner"></i> ๋ถ์ ์งํ ์ํ</div>')
|
| 538 |
+
progress_html = gr.HTML("""
|
| 539 |
+
<div style="padding: 15px; background-color: #f9f9f9; border-radius: 5px; margin: 10px 0; border: 1px solid #ddd;">
|
| 540 |
+
<div style="margin-bottom: 10px; display: flex; align-items: center;">
|
| 541 |
+
<i class="fas fa-spinner fa-spin" style="color: #FB7F0D; margin-right: 10px;"></i>
|
| 542 |
+
<span>ํค์๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ์์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์...</span>
|
| 543 |
+
</div>
|
| 544 |
+
<div style="background-color: #e9ecef; height: 10px; border-radius: 5px; overflow: hidden;">
|
| 545 |
+
<div class="progress-bar"></div>
|
| 546 |
+
</div>
|
| 547 |
+
</div>
|
| 548 |
+
""")
|
| 549 |
+
|
| 550 |
+
# ๋ฉ์ธํค์๋ ๋ถ์ ๊ฒฐ๊ณผ ์น์
|
| 551 |
+
with gr.Column(elem_classes="custom-frame fade-in") as main_keyword_section:
|
| 552 |
+
gr.HTML('<div class="section-title"><i class="fas fa-table"></i> ๋ฉ์ธํค์๋ ๋ถ์ ๊ฒฐ๊ณผ</div>')
|
| 553 |
+
|
| 554 |
+
empty_table_html = gr.HTML("""
|
| 555 |
+
<table class="empty-table">
|
| 556 |
+
<thead>
|
| 557 |
+
<tr>
|
| 558 |
+
<th>์๋ฒ</th>
|
| 559 |
+
<th>์กฐํฉ ํค์๋</th>
|
| 560 |
+
<th>PC๊ฒ์๋</th>
|
| 561 |
+
<th>๋ชจ๋ฐ์ผ๊ฒ์๋</th>
|
| 562 |
+
<th>์ด๊ฒ์๋</th>
|
| 563 |
+
<th>๊ฒ์๋๊ตฌ๊ฐ</th>
|
| 564 |
+
<th>ํค์๋ ์ฌ์ฉ์์์</th>
|
| 565 |
+
<th>ํค์๋ ์ฌ์ฉํ์</th>
|
| 566 |
+
<th>์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ</th>
|
| 567 |
+
</tr>
|
| 568 |
+
</thead>
|
| 569 |
+
<tbody>
|
| 570 |
+
<tr>
|
| 571 |
+
<td colspan="9" style="padding: 30px; text-align: center;">
|
| 572 |
+
๊ฒ์์ ์คํํ๋ฉด ์ฌ๊ธฐ์ ๊ฒฐ๊ณผ๊ฐ ํ์๋ฉ๋๋ค
|
| 573 |
+
</td>
|
| 574 |
+
</tr>
|
| 575 |
+
</tbody>
|
| 576 |
+
</table>
|
| 577 |
+
""")
|
| 578 |
+
|
| 579 |
+
with gr.Column(visible=False) as keyword_analysis_section:
|
| 580 |
+
with gr.Row():
|
| 581 |
+
with gr.Column(scale=1):
|
| 582 |
+
category_filter = gr.Dropdown(
|
| 583 |
+
choices=["์ ์ฒด ๋ณด๊ธฐ"],
|
| 584 |
+
label="์นดํ
๊ณ ๋ฆฌ ํํฐ",
|
| 585 |
+
value="์ ์ฒด ๋ณด๊ธฐ",
|
| 586 |
+
interactive=True
|
| 587 |
+
)
|
| 588 |
+
with gr.Column(scale=1):
|
| 589 |
+
total_volume_sort = gr.Dropdown(
|
| 590 |
+
choices=["์ ๋ ฌ ์์", "์ค๋ฆ์ฐจ์", "๋ด๋ฆผ์ฐจ์"],
|
| 591 |
+
label="์ด๊ฒ์๋ ์ ๋ ฌ",
|
| 592 |
+
value="์ ๋ ฌ ์์",
|
| 593 |
+
interactive=True
|
| 594 |
+
)
|
| 595 |
+
|
| 596 |
+
with gr.Row():
|
| 597 |
+
with gr.Column(scale=1):
|
| 598 |
+
search_volume_filter = gr.Dropdown(
|
| 599 |
+
choices=["์ ์ฒด"],
|
| 600 |
+
label="๊ฒ์๋ ๊ตฌ๊ฐ ํํฐ",
|
| 601 |
+
value="์ ์ฒด",
|
| 602 |
+
interactive=True
|
| 603 |
+
)
|
| 604 |
+
with gr.Column(scale=1):
|
| 605 |
+
usage_count_sort = gr.Dropdown(
|
| 606 |
+
choices=["์ ๋ ฌ ์์", "์ค๋ฆ์ฐจ์", "๋ด๋ฆผ์ฐจ์"],
|
| 607 |
+
label="ํค์๋ ์ฌ์ฉํ์ ์ ๋ ฌ",
|
| 608 |
+
value="์ ๋ ฌ ์์",
|
| 609 |
+
interactive=True
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
gr.HTML("<div class='data-container' id='table_container'></div>")
|
| 613 |
+
table_output = gr.HTML(elem_classes="fade-in")
|
| 614 |
+
|
| 615 |
+
# ์นดํ
๊ณ ๋ฆฌ ๋ถ์ ์น์
|
| 616 |
+
with gr.Column(elem_classes="custom-frame fade-in", visible=False) as category_analysis_section:
|
| 617 |
+
gr.HTML('<div class="section-title"><i class="fas fa-chart-bar"></i> ํค์๋ ๋ถ์</div>')
|
| 618 |
+
|
| 619 |
+
with gr.Row():
|
| 620 |
+
with gr.Column(scale=1):
|
| 621 |
+
analysis_keywords = gr.Textbox(
|
| 622 |
+
label="ํค์๋ ์
๋ ฅ (์ต๋ 20๊ฐ, ์ผํ ๋๋ ์ํฐ๋ก ๊ตฌ๋ถ)",
|
| 623 |
+
placeholder="์: ์ค์ง์ด๋ณถ์, ์ค์ง์ด ์์ง, ์ค์ง์ด ์๋ฆฌ...",
|
| 624 |
+
lines=5
|
| 625 |
+
)
|
| 626 |
+
|
| 627 |
+
with gr.Column(scale=1):
|
| 628 |
+
selected_category = gr.Dropdown(
|
| 629 |
+
label="๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ(๋ถ์ ์ ๋ฐ๋์ ์ ํํด์ฃผ์ธ์)",
|
| 630 |
+
choices=["์ ์ฒด ๋ณด๊ธฐ"],
|
| 631 |
+
value="์ ์ฒด ๋ณด๊ธฐ",
|
| 632 |
+
interactive=True
|
| 633 |
+
)
|
| 634 |
+
|
| 635 |
+
# ์คํ ์น์
|
| 636 |
+
with gr.Column(elem_classes="execution-section", visible=False) as execution_section:
|
| 637 |
+
gr.HTML('<div class="section-title"><i class="fas fa-play-circle"></i> ์คํ</div>')
|
| 638 |
+
with gr.Row():
|
| 639 |
+
with gr.Column(scale=1):
|
| 640 |
+
analyze_btn = gr.Button(
|
| 641 |
+
"์นดํ
๊ณ ๋ฆฌ ์ผ์น ๋ถ์",
|
| 642 |
+
elem_classes=["execution-button", "primary-button"]
|
| 643 |
+
)
|
| 644 |
+
with gr.Column(scale=1):
|
| 645 |
+
reset_btn = gr.Button(
|
| 646 |
+
"๋ชจ๋ ์
๋ ฅ ์ด๊ธฐํ",
|
| 647 |
+
elem_classes=["execution-button", "secondary-button"]
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
# ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ์น์
|
| 651 |
+
with gr.Column(elem_classes="custom-frame fade-in", visible=False) as analysis_output_section:
|
| 652 |
+
gr.HTML('<div class="section-title"><i class="fas fa-list-ul"></i> ๋ถ์ ๊ฒฐ๊ณผ ์์ฝ</div>')
|
| 653 |
+
|
| 654 |
+
analysis_result = gr.HTML(elem_classes="fade-in")
|
| 655 |
+
|
| 656 |
+
with gr.Row():
|
| 657 |
+
download_output = gr.File(
|
| 658 |
+
label="ํค์๋ ๋ชฉ๋ก ๋ค์ด๋ก๋",
|
| 659 |
+
visible=True
|
| 660 |
+
)
|
| 661 |
+
|
| 662 |
+
# ์ํ ์ ์ฅ์ฉ ๋ณ์
|
| 663 |
+
state_df = gr.State()
|
| 664 |
+
|
| 665 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ - ๋ชจ๋ ํจ์์ session_id ์ถ๊ฐ
|
| 666 |
+
search_btn.click(
|
| 667 |
+
fn=search_with_loading,
|
| 668 |
+
inputs=[keyword, korean_only, apply_main_keyword, exclude_zero_volume, session_id],
|
| 669 |
+
outputs=[progress_section, empty_table_html]
|
| 670 |
+
).then(
|
| 671 |
+
fn=process_search_results,
|
| 672 |
+
inputs=[keyword, korean_only, apply_main_keyword, exclude_zero_volume, session_id],
|
| 673 |
+
outputs=[
|
| 674 |
+
table_output, category_filter, search_volume_filter,
|
| 675 |
+
state_df, selected_category, download_output,
|
| 676 |
+
keyword_analysis_section, category_analysis_section,
|
| 677 |
+
progress_section, empty_table_html, execution_section,
|
| 678 |
+
keyword_state
|
| 679 |
+
]
|
| 680 |
+
)
|
| 681 |
+
|
| 682 |
+
# ํํฐ ๋ฐ ์ ๋ ฌ ๋ณ๊ฒฝ ์ด๋ฒคํธ ์ฐ๊ฒฐ - session_id ์ถ๊ฐ
|
| 683 |
+
category_filter.change(
|
| 684 |
+
fn=filter_and_sort_table,
|
| 685 |
+
inputs=[
|
| 686 |
+
state_df, category_filter, gr.Textbox(value="์ ๋ ฌ ์์", visible=False),
|
| 687 |
+
total_volume_sort, usage_count_sort,
|
| 688 |
+
search_volume_filter, exclude_zero_volume, session_id
|
| 689 |
+
],
|
| 690 |
+
outputs=[table_output]
|
| 691 |
+
)
|
| 692 |
+
|
| 693 |
+
category_filter.change(
|
| 694 |
+
fn=update_category_selection,
|
| 695 |
+
inputs=[category_filter, session_id],
|
| 696 |
+
outputs=[selected_category]
|
| 697 |
+
)
|
| 698 |
+
|
| 699 |
+
total_volume_sort.change(
|
| 700 |
+
fn=filter_and_sort_table,
|
| 701 |
+
inputs=[
|
| 702 |
+
state_df, category_filter, gr.Textbox(value="์ ๋ ฌ ์์", visible=False),
|
| 703 |
+
total_volume_sort, usage_count_sort,
|
| 704 |
+
search_volume_filter, exclude_zero_volume, session_id
|
| 705 |
+
],
|
| 706 |
+
outputs=[table_output]
|
| 707 |
+
)
|
| 708 |
+
|
| 709 |
+
usage_count_sort.change(
|
| 710 |
+
fn=filter_and_sort_table,
|
| 711 |
+
inputs=[
|
| 712 |
+
state_df, category_filter, gr.Textbox(value="์ ๋ ฌ ์์", visible=False),
|
| 713 |
+
total_volume_sort, usage_count_sort,
|
| 714 |
+
search_volume_filter, exclude_zero_volume, session_id
|
| 715 |
+
],
|
| 716 |
+
outputs=[table_output]
|
| 717 |
+
)
|
| 718 |
+
|
| 719 |
+
search_volume_filter.change(
|
| 720 |
+
fn=filter_and_sort_table,
|
| 721 |
+
inputs=[
|
| 722 |
+
state_df, category_filter, gr.Textbox(value="์ ๋ ฌ ์์", visible=False),
|
| 723 |
+
total_volume_sort, usage_count_sort,
|
| 724 |
+
search_volume_filter, exclude_zero_volume, session_id
|
| 725 |
+
],
|
| 726 |
+
outputs=[table_output]
|
| 727 |
+
)
|
| 728 |
+
|
| 729 |
+
exclude_zero_volume.change(
|
| 730 |
+
fn=filter_and_sort_table,
|
| 731 |
+
inputs=[
|
| 732 |
+
state_df, category_filter, gr.Textbox(value="์ ๋ ฌ ์์", visible=False),
|
| 733 |
+
total_volume_sort, usage_count_sort,
|
| 734 |
+
search_volume_filter, exclude_zero_volume, session_id
|
| 735 |
+
],
|
| 736 |
+
outputs=[table_output]
|
| 737 |
+
)
|
| 738 |
+
|
| 739 |
+
# ์นดํ
๊ณ ๋ฆฌ ๋ถ์ ๋ฒํผ ์ด๋ฒคํธ - session_id ์ถ๊ฐ
|
| 740 |
+
analyze_btn.click(
|
| 741 |
+
fn=analyze_with_loading,
|
| 742 |
+
inputs=[analysis_keywords, selected_category, state_df, session_id],
|
| 743 |
+
outputs=[progress_section]
|
| 744 |
+
).then(
|
| 745 |
+
fn=process_analyze_results,
|
| 746 |
+
inputs=[analysis_keywords, selected_category, state_df, session_id],
|
| 747 |
+
outputs=[analysis_result, download_output, analysis_output_section, progress_section]
|
| 748 |
+
)
|
| 749 |
+
|
| 750 |
+
# ๋ฆฌ์
๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ - session_id ์ถ๊ฐ
|
| 751 |
+
reset_btn.click(
|
| 752 |
+
fn=reset_interface,
|
| 753 |
+
inputs=[session_id],
|
| 754 |
+
outputs=[
|
| 755 |
+
keyword, korean_only, exclude_zero_volume, apply_main_keyword,
|
| 756 |
+
table_output, category_filter, category_filter,
|
| 757 |
+
search_volume_filter, search_volume_filter,
|
| 758 |
+
total_volume_sort, usage_count_sort,
|
| 759 |
+
selected_category, selected_category,
|
| 760 |
+
analysis_keywords, analysis_result, download_output,
|
| 761 |
+
keyword_analysis_section, analysis_output_section,
|
| 762 |
+
keyword_state
|
| 763 |
+
]
|
| 764 |
+
)
|
| 765 |
+
|
| 766 |
+
return demo
|
| 767 |
+
|
| 768 |
+
if __name__ == "__main__":
|
| 769 |
+
# ========== ์์ ์ ์ ์ฒด ์ด๊ธฐํ ==========
|
| 770 |
+
logger.info("๐ ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์...")
|
| 771 |
+
|
| 772 |
+
# 1. ์ฒซ ๋ฒ์งธ: ํ๊น
ํ์ด์ค ์์ ํด๋ ์ ๋ฆฌ ๋ฐ ํ๊ฒฝ ์ค์
|
| 773 |
+
app_temp_dir = cleanup_on_startup()
|
| 774 |
+
|
| 775 |
+
# 2. ์ธ์
์ ๋ฆฌ ์ค์ผ์ค๋ฌ ์์
|
| 776 |
+
start_session_cleanup_scheduler()
|
| 777 |
+
|
| 778 |
+
# 3. API ์ฐ๊ฒฐ ํ
์คํธ
|
| 779 |
+
try:
|
| 780 |
+
test_client = get_api_client()
|
| 781 |
+
logger.info("โ
API ์ฐ๊ฒฐ ํ
์คํธ ์ฑ๊ณต")
|
| 782 |
+
except Exception as e:
|
| 783 |
+
logger.error(f"โ API ์ฐ๊ฒฐ ์คํจ: {e}")
|
| 784 |
+
raise
|
| 785 |
+
|
| 786 |
+
logger.info("===== ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์ ์๋ฃ at %s =====", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 787 |
+
logger.info(f"๐ ์์ ํ์ผ ์ ์ฅ ์์น: {app_temp_dir}")
|
| 788 |
+
|
| 789 |
+
# ========== ์ฑ ์คํ ==========
|
| 790 |
+
try:
|
| 791 |
+
app = create_app()
|
| 792 |
+
app.launch(
|
| 793 |
+
share=False, # ๋ณด์์ ์ํด share ๋นํ์ฑํ
|
| 794 |
+
server_name="0.0.0.0", # ๋ชจ๋ IP์์ ์ ๊ทผ ํ์ฉ
|
| 795 |
+
server_port=7860, # ํฌํธ ์ง์
|
| 796 |
+
max_threads=40, # ๋ฉํฐ์ ์ ๋ฅผ ์ํ ์ค๋ ๋ ์ ์ฆ๊ฐ
|
| 797 |
+
auth=None, # ํ์์ ์ธ์ฆ ์ถ๊ฐ ๊ฐ๋ฅ
|
| 798 |
+
show_error=True, # ์๋ฌ ํ์
|
| 799 |
+
quiet=False, # ๋ก๊ทธ ํ์
|
| 800 |
+
favicon_path=None, # ํ๋น์ฝ ์ค์
|
| 801 |
+
ssl_verify=False # SSL ๊ฒ์ฆ ๋นํ์ฑํ (๊ฐ๋ฐ์ฉ)
|
| 802 |
+
)
|
| 803 |
+
except Exception as e:
|
| 804 |
+
logger.error(f"์ ํ๋ฆฌ์ผ์ด์
์คํ ์คํจ: {e}")
|
| 805 |
+
raise
|
| 806 |
+
finally:
|
| 807 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์ข
๋ฃ ์ ์ ๋ฆฌ
|
| 808 |
+
logger.info("๐งน ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์ข
๋ฃ - ์ต์ข
์ ๋ฆฌ ์์
...")
|
| 809 |
+
try:
|
| 810 |
+
cleanup_huggingface_temp_folders()
|
| 811 |
+
if os.path.exists(app_temp_dir):
|
| 812 |
+
shutil.rmtree(app_temp_dir, ignore_errors=True)
|
| 813 |
+
logger.info("โ
์ต์ข
์ ๋ฆฌ ์๋ฃ")
|
| 814 |
+
except Exception as e:
|
| 815 |
+
logger.error(f"์ต์ข
์ ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|