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