Update app.py
Browse files
app.py
CHANGED
|
@@ -10,127 +10,115 @@ import uuid
|
|
| 10 |
import shutil
|
| 11 |
import glob
|
| 12 |
from datetime import datetime
|
| 13 |
-
|
|
|
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
| 16 |
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
| 17 |
load_dotenv()
|
| 18 |
|
| 19 |
-
# ๋ก๊น
์ค์ (API
|
| 20 |
-
|
| 21 |
-
"""API ์๋ํฌ์ธํธ ์ ๋ณด๋ฅผ ๋ก๊ทธ์์ ์ ๊ฑฐํ๋ ํํฐ"""
|
| 22 |
-
def filter(self, record):
|
| 23 |
-
# API ์๋ํฌ์ธํธ ๊ด๋ จ ๋ก๊ทธ ํํฐ๋ง
|
| 24 |
-
message = record.getMessage()
|
| 25 |
-
if any(keyword in message.lower() for keyword in ['hf.space', 'happydoggg', 'gradio_api', 'http request']):
|
| 26 |
-
return False
|
| 27 |
-
return True
|
| 28 |
-
|
| 29 |
-
# ๋ณด์ ํํฐ ์ ์ฉ
|
| 30 |
-
secure_filter = SecureFilter()
|
| 31 |
-
|
| 32 |
-
logging.basicConfig(
|
| 33 |
-
level=logging.INFO,
|
| 34 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 35 |
-
handlers=[
|
| 36 |
-
logging.StreamHandler(),
|
| 37 |
-
logging.FileHandler('control_tower_app.log', mode='a')
|
| 38 |
-
]
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
logger = logging.getLogger(__name__)
|
| 42 |
-
logger.addFilter(secure_filter)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 49 |
def get_api_client():
|
| 50 |
-
"""ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ๋ฅผ ๊ฐ์ ธ์
|
| 51 |
endpoint = os.getenv('API_ENDPOINT')
|
| 52 |
if not endpoint:
|
| 53 |
-
logger.error("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 54 |
raise ValueError("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def cleanup_huggingface_temp_folders():
|
| 62 |
"""ํ๊น
ํ์ด์ค ์์ ํด๋ ์ด๊ธฐ ์ ๋ฆฌ"""
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
temp_dirs = [
|
| 66 |
-
tempfile.gettempdir(),
|
| 67 |
-
"/tmp",
|
| 68 |
-
"/var/tmp",
|
| 69 |
-
os.path.join(os.getcwd(), "temp"),
|
| 70 |
-
os.path.join(os.getcwd(), "tmp"),
|
| 71 |
-
"/gradio_cached_examples",
|
| 72 |
-
"/flagged"
|
| 73 |
-
]
|
| 74 |
-
|
| 75 |
cleanup_count = 0
|
| 76 |
|
| 77 |
for temp_dir in temp_dirs:
|
| 78 |
if os.path.exists(temp_dir):
|
| 79 |
try:
|
| 80 |
-
# ๊ธฐ์กด ์ธ์
ํ์ผ๋ค ์ ๋ฆฌ
|
| 81 |
session_files = glob.glob(os.path.join(temp_dir, "session_*.xlsx"))
|
| 82 |
session_files.extend(glob.glob(os.path.join(temp_dir, "session_*.csv")))
|
| 83 |
-
session_files.extend(glob.glob(os.path.join(temp_dir, "*keyword*.xlsx")))
|
| 84 |
-
session_files.extend(glob.glob(os.path.join(temp_dir, "*keyword*.csv")))
|
| 85 |
-
session_files.extend(glob.glob(os.path.join(temp_dir, "tmp*.xlsx")))
|
| 86 |
-
session_files.extend(glob.glob(os.path.join(temp_dir, "tmp*.csv")))
|
| 87 |
|
| 88 |
for file_path in session_files:
|
| 89 |
try:
|
| 90 |
-
# ํ์ผ์ด 1์๊ฐ ์ด์ ์ค๋๋ ๊ฒฝ์ฐ๋ง ์ญ์
|
| 91 |
if os.path.getmtime(file_path) < time.time() - 3600:
|
| 92 |
os.remove(file_path)
|
| 93 |
cleanup_count += 1
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
logger.warning(f"์์ ๋๋ ํ ๋ฆฌ ์ ๋ฆฌ ์คํจ (๋ฌด์๋จ): {temp_dir} - {e}")
|
| 100 |
-
|
| 101 |
-
logger.info(f"โ
ํ๊น
ํ์ด์ค ์์ ํด๋ ์ด๊ธฐ ์ ๋ฆฌ ์๋ฃ - {cleanup_count}๊ฐ ํ์ผ ์ญ์ ")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
try:
|
| 105 |
-
gradio_temp_dir = os.path.join(os.getcwd(), "gradio_cached_examples")
|
| 106 |
-
if os.path.exists(gradio_temp_dir):
|
| 107 |
-
shutil.rmtree(gradio_temp_dir, ignore_errors=True)
|
| 108 |
-
logger.info("Gradio ์บ์ ํด๋ ์ ๋ฆฌ ์๋ฃ")
|
| 109 |
-
except Exception as e:
|
| 110 |
-
logger.warning(f"Gradio ์บ์ ํด๋ ์ ๋ฆฌ ์คํจ (๋ฌด์๋จ): {e}")
|
| 111 |
-
|
| 112 |
except Exception as e:
|
| 113 |
-
logger.error(f"
|
| 114 |
|
| 115 |
def setup_clean_temp_environment():
|
| 116 |
"""๊นจ๋ํ ์์ ํ๊ฒฝ ์ค์ """
|
| 117 |
try:
|
| 118 |
-
# 1. ๊ธฐ์กด ์์ ํ์ผ๋ค ์ ๋ฆฌ
|
| 119 |
cleanup_huggingface_temp_folders()
|
| 120 |
|
| 121 |
-
# 2. ์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 122 |
app_temp_dir = os.path.join(tempfile.gettempdir(), "control_tower_app")
|
| 123 |
if os.path.exists(app_temp_dir):
|
| 124 |
shutil.rmtree(app_temp_dir, ignore_errors=True)
|
| 125 |
os.makedirs(app_temp_dir, exist_ok=True)
|
| 126 |
|
| 127 |
-
# 3. ํ๊ฒฝ ๋ณ์ ์ค์ (์์ ๋๋ ํ ๋ฆฌ ์ง์ )
|
| 128 |
os.environ['CONTROL_TOWER_TEMP'] = app_temp_dir
|
| 129 |
|
| 130 |
logger.info(f"โ
์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์ค์ : {app_temp_dir}")
|
| 131 |
-
|
| 132 |
return app_temp_dir
|
| 133 |
-
|
| 134 |
except Exception as e:
|
| 135 |
logger.error(f"์์ ํ๊ฒฝ ์ค์ ์คํจ: {e}")
|
| 136 |
return tempfile.gettempdir()
|
|
@@ -140,13 +128,12 @@ def get_app_temp_dir():
|
|
| 140 |
return os.environ.get('CONTROL_TOWER_TEMP', tempfile.gettempdir())
|
| 141 |
|
| 142 |
def get_session_id():
|
| 143 |
-
"""์ธ์
ID ์์ฑ
|
| 144 |
try:
|
| 145 |
client = get_api_client()
|
| 146 |
result = client.predict(api_name="/get_session_id")
|
| 147 |
-
return result
|
| 148 |
-
except Exception
|
| 149 |
-
logger.error(f"์ธ์
ID ์์ฑ API ํธ์ถ ์ค๋ฅ: {e}")
|
| 150 |
return str(uuid.uuid4())
|
| 151 |
|
| 152 |
def cleanup_session_files(session_id, delay=300):
|
|
@@ -179,11 +166,10 @@ def cleanup_old_sessions():
|
|
| 179 |
sessions_to_remove = []
|
| 180 |
|
| 181 |
for session_id, data in session_data.items():
|
| 182 |
-
if current_time - data.get('last_activity', 0) > 3600:
|
| 183 |
sessions_to_remove.append(session_id)
|
| 184 |
|
| 185 |
for session_id in sessions_to_remove:
|
| 186 |
-
# ํ์ผ ์ ๋ฆฌ
|
| 187 |
if session_id in session_temp_files:
|
| 188 |
for file_path in session_temp_files[session_id]:
|
| 189 |
try:
|
|
@@ -194,7 +180,6 @@ def cleanup_old_sessions():
|
|
| 194 |
logger.error(f"์ค๋๋ ์ธ์
ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 195 |
del session_temp_files[session_id]
|
| 196 |
|
| 197 |
-
# ์ธ์
๋ฐ์ดํฐ ์ ๋ฆฌ
|
| 198 |
if session_id in session_data:
|
| 199 |
del session_data[session_id]
|
| 200 |
logger.info(f"์ค๋๋ ์ธ์
๋ฐ์ดํฐ ์ญ์ : {session_id[:8]}...")
|
|
@@ -206,26 +191,22 @@ def update_session_activity(session_id):
|
|
| 206 |
session_data[session_id]['last_activity'] = time.time()
|
| 207 |
|
| 208 |
def create_session_temp_file(session_id, suffix='.xlsx'):
|
| 209 |
-
"""์ธ์
๋ณ ์์ ํ์ผ ์์ฑ
|
| 210 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 211 |
random_suffix = str(random.randint(1000, 9999))
|
| 212 |
|
| 213 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์ฌ์ฉ
|
| 214 |
temp_dir = get_app_temp_dir()
|
| 215 |
filename = f"session_{session_id[:8]}_{timestamp}_{random_suffix}{suffix}"
|
| 216 |
temp_file_path = os.path.join(temp_dir, filename)
|
| 217 |
|
| 218 |
-
# ๋น ํ์ผ ์์ฑ
|
| 219 |
with open(temp_file_path, 'w') as f:
|
| 220 |
pass
|
| 221 |
|
| 222 |
register_session_file(session_id, temp_file_path)
|
| 223 |
return temp_file_path
|
| 224 |
|
| 225 |
-
# ========== ์๋ณธ API์ ์ ํํ ๋์ผํ ํจ์๋ค ==========
|
| 226 |
-
|
| 227 |
def search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 228 |
-
"""์๋ณธ API: /search_with_loading
|
| 229 |
try:
|
| 230 |
client = get_api_client()
|
| 231 |
result = client.predict(
|
|
@@ -235,13 +216,13 @@ def search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_v
|
|
| 235 |
exclude_zero_volume=exclude_zero_volume,
|
| 236 |
api_name="/search_with_loading"
|
| 237 |
)
|
| 238 |
-
return result
|
| 239 |
except Exception as e:
|
| 240 |
logger.error(f"search_with_loading API ํธ์ถ ์ค๋ฅ: {e}")
|
| 241 |
-
return
|
| 242 |
|
| 243 |
def process_search_results(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 244 |
-
"""์๋ณธ API: /process_search_results
|
| 245 |
try:
|
| 246 |
client = get_api_client()
|
| 247 |
result = client.predict(
|
|
@@ -252,42 +233,37 @@ def process_search_results(keyword, korean_only, apply_main_keyword, exclude_zer
|
|
| 252 |
api_name="/process_search_results"
|
| 253 |
)
|
| 254 |
|
| 255 |
-
#
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
else:
|
| 263 |
-
logger.error(f"์์๊ณผ ๋ค๋ฅธ ๋ฐํ๊ฐ ๊ฐ์: {len(result)}")
|
| 264 |
return (
|
| 265 |
-
"<p>
|
| 266 |
-
["์ ์ฒด ๋ณด๊ธฐ"], ["์ ์ฒด"], "์ ์ฒด ๋ณด๊ธฐ", None
|
| 267 |
)
|
| 268 |
|
| 269 |
-
# ๋ค์ด๋ก๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 270 |
-
local_download_file = None
|
| 271 |
-
if download_file:
|
| 272 |
-
session_id = get_session_id()
|
| 273 |
-
local_download_file = create_session_temp_file(session_id, '.xlsx')
|
| 274 |
-
try:
|
| 275 |
-
shutil.copy2(download_file, local_download_file)
|
| 276 |
-
except Exception as e:
|
| 277 |
-
logger.error(f"ํ์ผ ๋ณต์ฌ ์ค๋ฅ: {e}")
|
| 278 |
-
local_download_file = None
|
| 279 |
-
|
| 280 |
-
return table_html, cat_choices, vol_choices, selected_cat, local_download_file, progress_html
|
| 281 |
-
|
| 282 |
except Exception as e:
|
| 283 |
-
logger.error(f"API ํธ์ถ ์ค๋ฅ: {e}")
|
| 284 |
return (
|
| 285 |
"<p>์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.</p>",
|
| 286 |
-
["์ ์ฒด ๋ณด๊ธฐ"], ["์ ์ฒด"], "์ ์ฒด ๋ณด๊ธฐ", None
|
| 287 |
)
|
| 288 |
|
| 289 |
def filter_and_sort_table(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 290 |
-
"""์๋ณธ API: /filter_and_sort_table
|
| 291 |
try:
|
| 292 |
client = get_api_client()
|
| 293 |
result = client.predict(
|
|
@@ -299,98 +275,26 @@ def filter_and_sort_table(selected_cat, keyword_sort, total_volume_sort, usage_c
|
|
| 299 |
exclude_zero_volume=exclude_zero_volume,
|
| 300 |
api_name="/filter_and_sort_table"
|
| 301 |
)
|
| 302 |
-
return result
|
| 303 |
except Exception as e:
|
| 304 |
logger.error(f"filter_and_sort_table API ํธ์ถ ์ค๋ฅ: {e}")
|
| 305 |
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 306 |
|
| 307 |
-
def filter_and_sort_table_1(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 308 |
-
"""์๋ณธ API: /filter_and_sort_table_1"""
|
| 309 |
-
try:
|
| 310 |
-
client = get_api_client()
|
| 311 |
-
result = client.predict(
|
| 312 |
-
selected_cat=selected_cat,
|
| 313 |
-
keyword_sort=keyword_sort,
|
| 314 |
-
total_volume_sort=total_volume_sort,
|
| 315 |
-
usage_count_sort=usage_count_sort,
|
| 316 |
-
selected_volume_range=selected_volume_range,
|
| 317 |
-
exclude_zero_volume=exclude_zero_volume,
|
| 318 |
-
api_name="/filter_and_sort_table_1"
|
| 319 |
-
)
|
| 320 |
-
return result
|
| 321 |
-
except Exception as e:
|
| 322 |
-
logger.error(f"filter_and_sort_table_1 API ํธ์ถ ์ค๋ฅ: {e}")
|
| 323 |
-
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 324 |
-
|
| 325 |
-
def filter_and_sort_table_2(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 326 |
-
"""์๋ณธ API: /filter_and_sort_table_2"""
|
| 327 |
-
try:
|
| 328 |
-
client = get_api_client()
|
| 329 |
-
result = client.predict(
|
| 330 |
-
selected_cat=selected_cat,
|
| 331 |
-
keyword_sort=keyword_sort,
|
| 332 |
-
total_volume_sort=total_volume_sort,
|
| 333 |
-
usage_count_sort=usage_count_sort,
|
| 334 |
-
selected_volume_range=selected_volume_range,
|
| 335 |
-
exclude_zero_volume=exclude_zero_volume,
|
| 336 |
-
api_name="/filter_and_sort_table_2"
|
| 337 |
-
)
|
| 338 |
-
return result
|
| 339 |
-
except Exception as e:
|
| 340 |
-
logger.error(f"filter_and_sort_table_2 API ํธ์ถ ์ค๋ฅ: {e}")
|
| 341 |
-
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 342 |
-
|
| 343 |
-
def filter_and_sort_table_3(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 344 |
-
"""์๋ณธ API: /filter_and_sort_table_3"""
|
| 345 |
-
try:
|
| 346 |
-
client = get_api_client()
|
| 347 |
-
result = client.predict(
|
| 348 |
-
selected_cat=selected_cat,
|
| 349 |
-
keyword_sort=keyword_sort,
|
| 350 |
-
total_volume_sort=total_volume_sort,
|
| 351 |
-
usage_count_sort=usage_count_sort,
|
| 352 |
-
selected_volume_range=selected_volume_range,
|
| 353 |
-
exclude_zero_volume=exclude_zero_volume,
|
| 354 |
-
api_name="/filter_and_sort_table_3"
|
| 355 |
-
)
|
| 356 |
-
return result
|
| 357 |
-
except Exception as e:
|
| 358 |
-
logger.error(f"filter_and_sort_table_3 API ํธ์ถ ์ค๋ฅ: {e}")
|
| 359 |
-
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 360 |
-
|
| 361 |
-
def filter_and_sort_table_4(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 362 |
-
"""์๋ณธ API: /filter_and_sort_table_4"""
|
| 363 |
-
try:
|
| 364 |
-
client = get_api_client()
|
| 365 |
-
result = client.predict(
|
| 366 |
-
selected_cat=selected_cat,
|
| 367 |
-
keyword_sort=keyword_sort,
|
| 368 |
-
total_volume_sort=total_volume_sort,
|
| 369 |
-
usage_count_sort=usage_count_sort,
|
| 370 |
-
selected_volume_range=selected_volume_range,
|
| 371 |
-
exclude_zero_volume=exclude_zero_volume,
|
| 372 |
-
api_name="/filter_and_sort_table_4"
|
| 373 |
-
)
|
| 374 |
-
return result
|
| 375 |
-
except Exception as e:
|
| 376 |
-
logger.error(f"filter_and_sort_table_4 API ํธ์ถ ์ค๋ฅ: {e}")
|
| 377 |
-
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 378 |
-
|
| 379 |
def update_category_selection(selected_cat):
|
| 380 |
-
"""์๋ณธ API: /update_category_selection
|
| 381 |
try:
|
| 382 |
client = get_api_client()
|
| 383 |
result = client.predict(
|
| 384 |
selected_cat=selected_cat,
|
| 385 |
api_name="/update_category_selection"
|
| 386 |
)
|
| 387 |
-
return gr.update(value=result)
|
| 388 |
except Exception as e:
|
| 389 |
logger.error(f"update_category_selection API ํธ์ถ ์ค๋ฅ: {e}")
|
| 390 |
return gr.update(value=selected_cat)
|
| 391 |
|
| 392 |
def analyze_with_loading(analysis_keywords, selected_category):
|
| 393 |
-
"""์๋ณธ API: /analyze_with_loading
|
| 394 |
try:
|
| 395 |
client = get_api_client()
|
| 396 |
result = client.predict(
|
|
@@ -398,13 +302,13 @@ def analyze_with_loading(analysis_keywords, selected_category):
|
|
| 398 |
selected_category=selected_category,
|
| 399 |
api_name="/analyze_with_loading"
|
| 400 |
)
|
| 401 |
-
return result
|
| 402 |
except Exception as e:
|
| 403 |
logger.error(f"analyze_with_loading API ํธ์ถ ์ค๋ฅ: {e}")
|
| 404 |
-
return
|
| 405 |
|
| 406 |
def process_analyze_results(analysis_keywords, selected_category):
|
| 407 |
-
"""์๋ณธ API: /process_analyze_results
|
| 408 |
try:
|
| 409 |
client = get_api_client()
|
| 410 |
result = client.predict(
|
|
@@ -413,58 +317,50 @@ def process_analyze_results(analysis_keywords, selected_category):
|
|
| 413 |
api_name="/process_analyze_results"
|
| 414 |
)
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
except Exception as e:
|
| 432 |
logger.error(f"process_analyze_results API ํธ์ถ ์ค๋ฅ: {e}")
|
| 433 |
return "๋ถ์ ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.", None
|
| 434 |
|
| 435 |
def reset_interface():
|
| 436 |
-
"""์๋ณธ API: /reset_interface
|
| 437 |
try:
|
| 438 |
client = get_api_client()
|
| 439 |
result = client.predict(api_name="/reset_interface")
|
| 440 |
-
return result
|
| 441 |
except Exception as e:
|
| 442 |
logger.error(f"reset_interface API ํธ์ถ ์ค๋ฅ: {e}")
|
| 443 |
-
|
| 444 |
-
return (
|
| 445 |
-
"", # ๊ฒ์ ํค์๋
|
| 446 |
-
True, # ํ๊ธ๋ง ์ถ์ถ
|
| 447 |
-
False, # ๊ฒ์๋ 0 ํค์๋ ์ ์ธ
|
| 448 |
-
"๋ฉ์ธํค์๋ ์ ์ฉ", # ์กฐํฉ ๋ฐฉ์
|
| 449 |
-
"", # HTML ํ
์ด๋ธ
|
| 450 |
-
["์ ์ฒด ๋ณด๊ธฐ"], # ์นดํ
๊ณ ๋ฆฌ ํํฐ choices
|
| 451 |
-
"์ ์ฒด ๋ณด๊ธฐ", # ์นดํ
๊ณ ๋ฆฌ ํํฐ value
|
| 452 |
-
["์ ์ฒด"], # ๊ฒ์๋ ๊ตฌ๊ฐ ํํฐ choices
|
| 453 |
-
"์ ์ฒด", # ๊ฒ์๋ ๊ตฌ๊ฐ ํํฐ value
|
| 454 |
-
"์ ๋ ฌ ์์", # ์ด๊ฒ์๋ ์ ๋ ฌ
|
| 455 |
-
"์ ๋ ฌ ์์", # ํค์๋ ์ฌ์ฉํ์ ์ ๋ ฌ
|
| 456 |
-
["์ ์ฒด ๋ณด๊ธฐ"], # ๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ choices
|
| 457 |
-
"์ ์ฒด ๋ณด๊ธฐ", # ๋ถ์ํ ์นดํ
๊ณ ๋ฆฌ value
|
| 458 |
-
"", # ํค์๋ ์
๋ ฅ
|
| 459 |
-
"", # ๋ถ์ ๊ฒฐ๊ณผ
|
| 460 |
-
None # ๋ค์ด๋ก๋ ํ์ผ
|
| 461 |
-
)
|
| 462 |
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
|
|
|
| 465 |
def wrapper_search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 466 |
"""๊ฒ์ ๋ก๋ฉ UI ์ฒ๋ฆฌ"""
|
| 467 |
-
|
| 468 |
return (
|
| 469 |
gr.update(visible=True), # progress_section
|
| 470 |
gr.update(visible=False) # empty_table_html
|
|
@@ -474,18 +370,7 @@ def wrapper_process_search_results(keyword, korean_only, apply_main_keyword, exc
|
|
| 474 |
"""๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ UI"""
|
| 475 |
result = process_search_results(keyword, korean_only, apply_main_keyword, exclude_zero_volume)
|
| 476 |
|
| 477 |
-
|
| 478 |
-
if len(result) >= 5:
|
| 479 |
-
table_html, cat_choices, vol_choices, selected_cat, download_file = result[:5]
|
| 480 |
-
progress_html = result[5] if len(result) > 5 else ""
|
| 481 |
-
else:
|
| 482 |
-
logger.error(f"๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค๋ฅ: ์์๋ณด๋ค ์ ์ ๋ฐํ๊ฐ {len(result)}")
|
| 483 |
-
table_html = "<p>๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 484 |
-
cat_choices = ["์ ์ฒด ๋ณด๊ธฐ"]
|
| 485 |
-
vol_choices = ["์ ์ฒด"]
|
| 486 |
-
selected_cat = "์ ์ฒด ๋ณด๊ธฐ"
|
| 487 |
-
download_file = None
|
| 488 |
-
progress_html = ""
|
| 489 |
|
| 490 |
# UI ํ์ ์ฌ๋ถ ๊ฒฐ์
|
| 491 |
if table_html and "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค" not in table_html and "๋ฌธ์ ๊ฐ ๋ฐ์ํ๏ฟฝ๏ฟฝ๏ฟฝ๋๋ค" not in table_html:
|
|
@@ -499,7 +384,7 @@ def wrapper_process_search_results(keyword, korean_only, apply_main_keyword, exc
|
|
| 499 |
empty_placeholder_vis = True
|
| 500 |
execution_section_visibility = False
|
| 501 |
|
| 502 |
-
# ๊ฐ์์ state_df
|
| 503 |
state_df = pd.DataFrame()
|
| 504 |
|
| 505 |
return (
|
|
@@ -519,7 +404,7 @@ def wrapper_process_search_results(keyword, korean_only, apply_main_keyword, exc
|
|
| 519 |
|
| 520 |
def wrapper_analyze_with_loading(analysis_keywords, selected_category, state_df):
|
| 521 |
"""๋ถ์ ๋ก๋ฉ UI ์ฒ๋ฆฌ"""
|
| 522 |
-
|
| 523 |
return gr.update(visible=True) # progress_section
|
| 524 |
|
| 525 |
def wrapper_process_analyze_results(analysis_keywords, selected_category, state_df):
|
|
@@ -547,19 +432,14 @@ def cleanup_on_startup():
|
|
| 547 |
"""์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์ ์ฒด ์ ๋ฆฌ"""
|
| 548 |
logger.info("๐งน ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์ - ์ด๊ธฐ ์ ๋ฆฌ ์์
์์...")
|
| 549 |
|
| 550 |
-
# 1. ํ๊น
ํ์ด์ค ์์ ํด๋ ์ ๋ฆฌ
|
| 551 |
cleanup_huggingface_temp_folders()
|
| 552 |
-
|
| 553 |
-
# 2. ๊นจ๋ํ ์์ ํ๊ฒฝ ์ค์
|
| 554 |
app_temp_dir = setup_clean_temp_environment()
|
| 555 |
|
| 556 |
-
# 3. ์ ์ญ ๋ณ์ ์ด๊ธฐํ
|
| 557 |
global session_temp_files, session_data
|
| 558 |
session_temp_files.clear()
|
| 559 |
session_data.clear()
|
| 560 |
|
| 561 |
logger.info(f"โ
์ด๊ธฐ ์ ๋ฆฌ ์์
์๋ฃ - ์ฑ ์ ์ฉ ๋๋ ํ ๋ฆฌ: {app_temp_dir}")
|
| 562 |
-
|
| 563 |
return app_temp_dir
|
| 564 |
|
| 565 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
|
@@ -588,10 +468,6 @@ def create_app():
|
|
| 588 |
font-size: 16px !important;
|
| 589 |
font-weight: bold !important;
|
| 590 |
width: 100% !important;
|
| 591 |
-
text-align: center !important;
|
| 592 |
-
display: flex !important;
|
| 593 |
-
align-items: center !important;
|
| 594 |
-
justify-content: center !important;
|
| 595 |
}
|
| 596 |
"""
|
| 597 |
|
|
@@ -873,4 +749,61 @@ def create_app():
|
|
| 873 |
]
|
| 874 |
)
|
| 875 |
|
| 876 |
-
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import shutil
|
| 11 |
import glob
|
| 12 |
from datetime import datetime
|
| 13 |
+
import requests
|
| 14 |
+
import json
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
|
| 17 |
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
| 18 |
load_dotenv()
|
| 19 |
|
| 20 |
+
# ๋ก๊น
์ค์ (API ์ ๋ณด ์์ ์ฐจ๋จ)
|
| 21 |
+
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
logger = logging.getLogger(__name__)
|
|
|
|
| 23 |
|
| 24 |
+
# ์ธ์
๋ณ ์์ ํ์ผ ๊ด๋ฆฌ๋ฅผ ์ํ ๋์
๋๋ฆฌ
|
| 25 |
+
session_temp_files = {}
|
| 26 |
+
session_data = {}
|
| 27 |
|
|
|
|
| 28 |
def get_api_client():
|
| 29 |
+
"""ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ๋ฅผ ๊ฐ์ ธ์ ์์ฒญ ํจ์ ์์ฑ"""
|
| 30 |
endpoint = os.getenv('API_ENDPOINT')
|
| 31 |
if not endpoint:
|
|
|
|
| 32 |
raise ValueError("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
|
| 33 |
+
|
| 34 |
+
def make_request(api_name, **kwargs):
|
| 35 |
+
try:
|
| 36 |
+
# gradio_client์ ๋์ผํ ๋ฐฉ์์ผ๋ก API ํธ์ถ
|
| 37 |
+
if not endpoint.startswith('http'):
|
| 38 |
+
base_url = f"https://{endpoint}.hf.space"
|
| 39 |
+
else:
|
| 40 |
+
base_url = endpoint
|
| 41 |
+
|
| 42 |
+
# Gradio API ์๋ํฌ์ธํธ ํ์ ๋ง์ถ๊ธฐ
|
| 43 |
+
url = f"{base_url}/call{api_name}"
|
| 44 |
+
|
| 45 |
+
# ๋งค๊ฐ๋ณ์๋ฅผ ์์๋๋ก ๋ฐฐ์ด๋ก ๋ณํ
|
| 46 |
+
if api_name == "/process_search_results":
|
| 47 |
+
data = [kwargs.get('keyword', ''), kwargs.get('korean_only', True),
|
| 48 |
+
kwargs.get('apply_main_keyword', '๋ฉ์ธํค์๋ ์ ์ฉ'), kwargs.get('exclude_zero_volume', False)]
|
| 49 |
+
elif api_name == "/search_with_loading":
|
| 50 |
+
data = [kwargs.get('keyword', ''), kwargs.get('korean_only', True),
|
| 51 |
+
kwargs.get('apply_main_keyword', '๋ฉ์ธํค์๋ ์ ์ฉ'), kwargs.get('exclude_zero_volume', False)]
|
| 52 |
+
elif api_name == "/filter_and_sort_table":
|
| 53 |
+
data = [kwargs.get('selected_cat', '์ ์ฒด ๋ณด๊ธฐ'), kwargs.get('keyword_sort', '์ ๋ ฌ ์์'),
|
| 54 |
+
kwargs.get('total_volume_sort', '์ ๋ ฌ ์์'), kwargs.get('usage_count_sort', '์ ๋ ฌ ์์'),
|
| 55 |
+
kwargs.get('selected_volume_range', '์ ์ฒด'), kwargs.get('exclude_zero_volume', False)]
|
| 56 |
+
elif api_name == "/update_category_selection":
|
| 57 |
+
data = [kwargs.get('selected_cat', '์ ์ฒด ๋ณด๊ธฐ')]
|
| 58 |
+
elif api_name == "/process_analyze_results":
|
| 59 |
+
data = [kwargs.get('analysis_keywords', ''), kwargs.get('selected_category', '์ ์ฒด ๋ณด๊ธฐ')]
|
| 60 |
+
elif api_name == "/analyze_with_loading":
|
| 61 |
+
data = [kwargs.get('analysis_keywords', ''), kwargs.get('selected_category', '์ ์ฒด ๋ณด๊ธฐ')]
|
| 62 |
+
elif api_name == "/reset_interface":
|
| 63 |
+
data = []
|
| 64 |
+
elif api_name == "/get_session_id":
|
| 65 |
+
data = []
|
| 66 |
+
else:
|
| 67 |
+
data = []
|
| 68 |
+
|
| 69 |
+
response = requests.post(url, json={"data": data}, timeout=60)
|
| 70 |
+
|
| 71 |
+
if response.status_code == 200:
|
| 72 |
+
result = response.json()
|
| 73 |
+
return result.get('data', [])
|
| 74 |
+
else:
|
| 75 |
+
raise Exception(f"API ํธ์ถ ์คํจ: {response.status_code}")
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
raise Exception(f"API ์ฐ๊ฒฐ ์ค๋ฅ: {str(e)}")
|
| 79 |
+
|
| 80 |
+
return type('APIClient', (), {'predict': lambda self, **kwargs: make_request(kwargs.pop('api_name'), **kwargs)})()
|
| 81 |
|
| 82 |
def cleanup_huggingface_temp_folders():
|
| 83 |
"""ํ๊น
ํ์ด์ค ์์ ํด๋ ์ด๊ธฐ ์ ๋ฆฌ"""
|
| 84 |
try:
|
| 85 |
+
temp_dirs = [tempfile.gettempdir(), "/tmp", "/var/tmp"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
cleanup_count = 0
|
| 87 |
|
| 88 |
for temp_dir in temp_dirs:
|
| 89 |
if os.path.exists(temp_dir):
|
| 90 |
try:
|
|
|
|
| 91 |
session_files = glob.glob(os.path.join(temp_dir, "session_*.xlsx"))
|
| 92 |
session_files.extend(glob.glob(os.path.join(temp_dir, "session_*.csv")))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
for file_path in session_files:
|
| 95 |
try:
|
|
|
|
| 96 |
if os.path.getmtime(file_path) < time.time() - 3600:
|
| 97 |
os.remove(file_path)
|
| 98 |
cleanup_count += 1
|
| 99 |
+
except Exception:
|
| 100 |
+
pass
|
| 101 |
+
except Exception:
|
| 102 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
logger.info(f"โ
์์ ํด๋ ์ ๋ฆฌ ์๋ฃ - {cleanup_count}๊ฐ ํ์ผ ์ญ์ ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
+
logger.error(f"์์ ํด๋ ์ ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|
| 107 |
|
| 108 |
def setup_clean_temp_environment():
|
| 109 |
"""๊นจ๋ํ ์์ ํ๊ฒฝ ์ค์ """
|
| 110 |
try:
|
|
|
|
| 111 |
cleanup_huggingface_temp_folders()
|
| 112 |
|
|
|
|
| 113 |
app_temp_dir = os.path.join(tempfile.gettempdir(), "control_tower_app")
|
| 114 |
if os.path.exists(app_temp_dir):
|
| 115 |
shutil.rmtree(app_temp_dir, ignore_errors=True)
|
| 116 |
os.makedirs(app_temp_dir, exist_ok=True)
|
| 117 |
|
|
|
|
| 118 |
os.environ['CONTROL_TOWER_TEMP'] = app_temp_dir
|
| 119 |
|
| 120 |
logger.info(f"โ
์ ํ๋ฆฌ์ผ์ด์
์ ์ฉ ์์ ๋๋ ํ ๋ฆฌ ์ค์ : {app_temp_dir}")
|
|
|
|
| 121 |
return app_temp_dir
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
logger.error(f"์์ ํ๊ฒฝ ์ค์ ์คํจ: {e}")
|
| 124 |
return tempfile.gettempdir()
|
|
|
|
| 128 |
return os.environ.get('CONTROL_TOWER_TEMP', tempfile.gettempdir())
|
| 129 |
|
| 130 |
def get_session_id():
|
| 131 |
+
"""์ธ์
ID ์์ฑ"""
|
| 132 |
try:
|
| 133 |
client = get_api_client()
|
| 134 |
result = client.predict(api_name="/get_session_id")
|
| 135 |
+
return result[0] if result else str(uuid.uuid4())
|
| 136 |
+
except Exception:
|
|
|
|
| 137 |
return str(uuid.uuid4())
|
| 138 |
|
| 139 |
def cleanup_session_files(session_id, delay=300):
|
|
|
|
| 166 |
sessions_to_remove = []
|
| 167 |
|
| 168 |
for session_id, data in session_data.items():
|
| 169 |
+
if current_time - data.get('last_activity', 0) > 3600:
|
| 170 |
sessions_to_remove.append(session_id)
|
| 171 |
|
| 172 |
for session_id in sessions_to_remove:
|
|
|
|
| 173 |
if session_id in session_temp_files:
|
| 174 |
for file_path in session_temp_files[session_id]:
|
| 175 |
try:
|
|
|
|
| 180 |
logger.error(f"์ค๋๋ ์ธ์
ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 181 |
del session_temp_files[session_id]
|
| 182 |
|
|
|
|
| 183 |
if session_id in session_data:
|
| 184 |
del session_data[session_id]
|
| 185 |
logger.info(f"์ค๋๋ ์ธ์
๋ฐ์ดํฐ ์ญ์ : {session_id[:8]}...")
|
|
|
|
| 191 |
session_data[session_id]['last_activity'] = time.time()
|
| 192 |
|
| 193 |
def create_session_temp_file(session_id, suffix='.xlsx'):
|
| 194 |
+
"""์ธ์
๋ณ ์์ ํ์ผ ์์ฑ"""
|
| 195 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 196 |
random_suffix = str(random.randint(1000, 9999))
|
| 197 |
|
|
|
|
| 198 |
temp_dir = get_app_temp_dir()
|
| 199 |
filename = f"session_{session_id[:8]}_{timestamp}_{random_suffix}{suffix}"
|
| 200 |
temp_file_path = os.path.join(temp_dir, filename)
|
| 201 |
|
|
|
|
| 202 |
with open(temp_file_path, 'w') as f:
|
| 203 |
pass
|
| 204 |
|
| 205 |
register_session_file(session_id, temp_file_path)
|
| 206 |
return temp_file_path
|
| 207 |
|
|
|
|
|
|
|
| 208 |
def search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 209 |
+
"""์๋ณธ API: /search_with_loading"""
|
| 210 |
try:
|
| 211 |
client = get_api_client()
|
| 212 |
result = client.predict(
|
|
|
|
| 216 |
exclude_zero_volume=exclude_zero_volume,
|
| 217 |
api_name="/search_with_loading"
|
| 218 |
)
|
| 219 |
+
return result[0] if result else ""
|
| 220 |
except Exception as e:
|
| 221 |
logger.error(f"search_with_loading API ํธ์ถ ์ค๋ฅ: {e}")
|
| 222 |
+
return ""
|
| 223 |
|
| 224 |
def process_search_results(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 225 |
+
"""์๋ณธ API: /process_search_results"""
|
| 226 |
try:
|
| 227 |
client = get_api_client()
|
| 228 |
result = client.predict(
|
|
|
|
| 233 |
api_name="/process_search_results"
|
| 234 |
)
|
| 235 |
|
| 236 |
+
# ๊ฒฐ๊ณผ ์์ ํ๊ฒ ์ฒ๋ฆฌ
|
| 237 |
+
if len(result) >= 5:
|
| 238 |
+
table_html, cat_choices, vol_choices, selected_cat, download_file = result[:5]
|
| 239 |
+
|
| 240 |
+
# ๋ค์ด๋ก๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 241 |
+
local_download_file = None
|
| 242 |
+
if download_file:
|
| 243 |
+
session_id = get_session_id()
|
| 244 |
+
local_download_file = create_session_temp_file(session_id, '.xlsx')
|
| 245 |
+
try:
|
| 246 |
+
shutil.copy2(download_file, local_download_file)
|
| 247 |
+
except Exception as e:
|
| 248 |
+
logger.error(f"ํ์ผ ๋ณต์ฌ ์ค๋ฅ: {e}")
|
| 249 |
+
local_download_file = None
|
| 250 |
+
|
| 251 |
+
return table_html, cat_choices, vol_choices, selected_cat, local_download_file
|
| 252 |
else:
|
|
|
|
| 253 |
return (
|
| 254 |
+
"<p>๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.</p>",
|
| 255 |
+
["์ ์ฒด ๋ณด๊ธฐ"], ["์ ์ฒด"], "์ ์ฒด ๋ณด๊ธฐ", None
|
| 256 |
)
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
except Exception as e:
|
| 259 |
+
logger.error(f"process_search_results API ํธ์ถ ์ค๋ฅ: {e}")
|
| 260 |
return (
|
| 261 |
"<p>์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.</p>",
|
| 262 |
+
["์ ์ฒด ๋ณด๊ธฐ"], ["์ ์ฒด"], "์ ์ฒด ๋ณด๊ธฐ", None
|
| 263 |
)
|
| 264 |
|
| 265 |
def filter_and_sort_table(selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume):
|
| 266 |
+
"""์๋ณธ API: /filter_and_sort_table"""
|
| 267 |
try:
|
| 268 |
client = get_api_client()
|
| 269 |
result = client.predict(
|
|
|
|
| 275 |
exclude_zero_volume=exclude_zero_volume,
|
| 276 |
api_name="/filter_and_sort_table"
|
| 277 |
)
|
| 278 |
+
return result[0] if result else ""
|
| 279 |
except Exception as e:
|
| 280 |
logger.error(f"filter_and_sort_table API ํธ์ถ ์ค๋ฅ: {e}")
|
| 281 |
return "<p>ํํฐ๋ง ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.</p>"
|
| 282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
def update_category_selection(selected_cat):
|
| 284 |
+
"""์๋ณธ API: /update_category_selection"""
|
| 285 |
try:
|
| 286 |
client = get_api_client()
|
| 287 |
result = client.predict(
|
| 288 |
selected_cat=selected_cat,
|
| 289 |
api_name="/update_category_selection"
|
| 290 |
)
|
| 291 |
+
return gr.update(value=result[0] if result else selected_cat)
|
| 292 |
except Exception as e:
|
| 293 |
logger.error(f"update_category_selection API ํธ์ถ ์ค๋ฅ: {e}")
|
| 294 |
return gr.update(value=selected_cat)
|
| 295 |
|
| 296 |
def analyze_with_loading(analysis_keywords, selected_category):
|
| 297 |
+
"""์๋ณธ API: /analyze_with_loading"""
|
| 298 |
try:
|
| 299 |
client = get_api_client()
|
| 300 |
result = client.predict(
|
|
|
|
| 302 |
selected_category=selected_category,
|
| 303 |
api_name="/analyze_with_loading"
|
| 304 |
)
|
| 305 |
+
return result[0] if result else ""
|
| 306 |
except Exception as e:
|
| 307 |
logger.error(f"analyze_with_loading API ํธ์ถ ์ค๋ฅ: {e}")
|
| 308 |
+
return ""
|
| 309 |
|
| 310 |
def process_analyze_results(analysis_keywords, selected_category):
|
| 311 |
+
"""์๋ณธ API: /process_analyze_results"""
|
| 312 |
try:
|
| 313 |
client = get_api_client()
|
| 314 |
result = client.predict(
|
|
|
|
| 317 |
api_name="/process_analyze_results"
|
| 318 |
)
|
| 319 |
|
| 320 |
+
if len(result) >= 2:
|
| 321 |
+
analysis_result, download_file = result[:2]
|
| 322 |
+
|
| 323 |
+
# ๋ค์ด๋ก๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ๋ก ๋ณต์ฌ
|
| 324 |
+
local_download_file = None
|
| 325 |
+
if download_file:
|
| 326 |
+
session_id = get_session_id()
|
| 327 |
+
local_download_file = create_session_temp_file(session_id, '.xlsx')
|
| 328 |
+
try:
|
| 329 |
+
shutil.copy2(download_file, local_download_file)
|
| 330 |
+
except Exception as e:
|
| 331 |
+
logger.error(f"๋ถ์ ๊ฒฐ๊ณผ ํ์ผ ๋ณต์ฌ ์ค๋ฅ: {e}")
|
| 332 |
+
local_download_file = None
|
| 333 |
+
|
| 334 |
+
return analysis_result, local_download_file
|
| 335 |
+
else:
|
| 336 |
+
return "๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.", None
|
| 337 |
|
| 338 |
except Exception as e:
|
| 339 |
logger.error(f"process_analyze_results API ํธ์ถ ์ค๋ฅ: {e}")
|
| 340 |
return "๋ถ์ ์๋น์ค ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.", None
|
| 341 |
|
| 342 |
def reset_interface():
|
| 343 |
+
"""์๋ณธ API: /reset_interface"""
|
| 344 |
try:
|
| 345 |
client = get_api_client()
|
| 346 |
result = client.predict(api_name="/reset_interface")
|
| 347 |
+
return result if result else get_default_reset_values()
|
| 348 |
except Exception as e:
|
| 349 |
logger.error(f"reset_interface API ํธ์ถ ์ค๋ฅ: {e}")
|
| 350 |
+
return get_default_reset_values()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
def get_default_reset_values():
|
| 353 |
+
"""๊ธฐ๋ณธ ๋ฆฌ์
๊ฐ ๋ฐํ"""
|
| 354 |
+
return (
|
| 355 |
+
"", True, False, "๋ฉ์ธํค์๋ ์ ์ฉ", "", ["์ ์ฒด ๋ณด๊ธฐ"], "์ ์ฒด ๋ณด๊ธฐ",
|
| 356 |
+
["์ ์ฒด"], "์ ์ฒด", "์ ๋ ฌ ์์", "์ ๋ ฌ ์์", ["์ ์ฒด ๋ณด๊ธฐ"], "์ ์ฒด ๋ณด๊ธฐ",
|
| 357 |
+
"", "", None
|
| 358 |
+
)
|
| 359 |
|
| 360 |
+
# UI ์ฒ๋ฆฌ ๋ํผ ํจ์๋ค
|
| 361 |
def wrapper_search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume):
|
| 362 |
"""๊ฒ์ ๋ก๋ฉ UI ์ฒ๋ฆฌ"""
|
| 363 |
+
search_with_loading(keyword, korean_only, apply_main_keyword, exclude_zero_volume)
|
| 364 |
return (
|
| 365 |
gr.update(visible=True), # progress_section
|
| 366 |
gr.update(visible=False) # empty_table_html
|
|
|
|
| 370 |
"""๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ UI"""
|
| 371 |
result = process_search_results(keyword, korean_only, apply_main_keyword, exclude_zero_volume)
|
| 372 |
|
| 373 |
+
table_html, cat_choices, vol_choices, selected_cat, download_file = result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
# UI ํ์ ์ฌ๋ถ ๊ฒฐ์
|
| 376 |
if table_html and "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค" not in table_html and "๋ฌธ์ ๊ฐ ๋ฐ์ํ๏ฟฝ๏ฟฝ๏ฟฝ๋๋ค" not in table_html:
|
|
|
|
| 384 |
empty_placeholder_vis = True
|
| 385 |
execution_section_visibility = False
|
| 386 |
|
| 387 |
+
# ๊ฐ์์ state_df
|
| 388 |
state_df = pd.DataFrame()
|
| 389 |
|
| 390 |
return (
|
|
|
|
| 404 |
|
| 405 |
def wrapper_analyze_with_loading(analysis_keywords, selected_category, state_df):
|
| 406 |
"""๋ถ์ ๋ก๋ฉ UI ์ฒ๋ฆฌ"""
|
| 407 |
+
analyze_with_loading(analysis_keywords, selected_category)
|
| 408 |
return gr.update(visible=True) # progress_section
|
| 409 |
|
| 410 |
def wrapper_process_analyze_results(analysis_keywords, selected_category, state_df):
|
|
|
|
| 432 |
"""์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์ ์ฒด ์ ๋ฆฌ"""
|
| 433 |
logger.info("๐งน ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์ - ์ด๊ธฐ ์ ๋ฆฌ ์์
์์...")
|
| 434 |
|
|
|
|
| 435 |
cleanup_huggingface_temp_folders()
|
|
|
|
|
|
|
| 436 |
app_temp_dir = setup_clean_temp_environment()
|
| 437 |
|
|
|
|
| 438 |
global session_temp_files, session_data
|
| 439 |
session_temp_files.clear()
|
| 440 |
session_data.clear()
|
| 441 |
|
| 442 |
logger.info(f"โ
์ด๊ธฐ ์ ๋ฆฌ ์์
์๋ฃ - ์ฑ ์ ์ฉ ๋๋ ํ ๋ฆฌ: {app_temp_dir}")
|
|
|
|
| 443 |
return app_temp_dir
|
| 444 |
|
| 445 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
|
|
|
| 468 |
font-size: 16px !important;
|
| 469 |
font-weight: bold !important;
|
| 470 |
width: 100% !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
}
|
| 472 |
"""
|
| 473 |
|
|
|
|
| 749 |
]
|
| 750 |
)
|
| 751 |
|
| 752 |
+
return demo
|
| 753 |
+
|
| 754 |
+
if __name__ == "__main__":
|
| 755 |
+
# ========== ์์ ์ ์ ์ฒด ์ด๊ธฐํ ==========
|
| 756 |
+
print("===== Application Startup at %s =====" % time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 757 |
+
logger.info("๐ ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์...")
|
| 758 |
+
|
| 759 |
+
# 1. ์ฒซ ๋ฒ์งธ: ํ๊น
ํ์ด์ค ์์ ํด๋ ์ ๋ฆฌ ๋ฐ ํ๊ฒฝ ์ค์
|
| 760 |
+
app_temp_dir = cleanup_on_startup()
|
| 761 |
+
|
| 762 |
+
# 2. ์ธ์
์ ๋ฆฌ ์ค์ผ์ค๋ฌ ์์
|
| 763 |
+
start_session_cleanup_scheduler()
|
| 764 |
+
|
| 765 |
+
# 3. API ์ฐ๊ฒฐ ํ
์คํธ
|
| 766 |
+
try:
|
| 767 |
+
test_client = get_api_client()
|
| 768 |
+
logger.info("โ
API ์ฐ๊ฒฐ ํ
์คํธ ์ฑ๊ณต")
|
| 769 |
+
except Exception as e:
|
| 770 |
+
logger.error("โ API ์ฐ๊ฒฐ ์คํจ - ํ๊ฒฝ๋ณ์ API_ENDPOINT๋ฅผ ํ์ธํ์ธ์")
|
| 771 |
+
print("โ API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 772 |
+
print("๐ก .env ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ์ธ์:")
|
| 773 |
+
print("API_ENDPOINT=your-endpoint-here")
|
| 774 |
+
raise SystemExit(1)
|
| 775 |
+
|
| 776 |
+
logger.info("===== ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์์ ์๋ฃ at %s =====", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 777 |
+
logger.info(f"๐ ์์ ํ์ผ ์ ์ฅ ์์น: {app_temp_dir}")
|
| 778 |
+
|
| 779 |
+
# ========== ์ฑ ์คํ ==========
|
| 780 |
+
try:
|
| 781 |
+
app = create_app()
|
| 782 |
+
print("๐ Gradio ์ ํ๋ฆฌ์ผ์ด์
์ด ์์๋ฉ๋๋ค...")
|
| 783 |
+
app.launch(
|
| 784 |
+
share=False, # ๋ณด์์ ์ํด share ๋นํ์ฑํ
|
| 785 |
+
server_name="0.0.0.0", # ๋ชจ๋ IP์์ ์ ๊ทผ ํ์ฉ
|
| 786 |
+
server_port=7860, # ํฌํธ ์ง์
|
| 787 |
+
max_threads=40, # ๋ฉํฐ์ ์ ๋ฅผ ์ํ ์ค๋ ๋ ์ ์ฆ๊ฐ
|
| 788 |
+
auth=None, # ํ์์ ์ธ์ฆ ์ถ๊ฐ ๊ฐ๋ฅ
|
| 789 |
+
show_error=True, # ์๋ฌ ํ์
|
| 790 |
+
quiet=False, # ๋ก๊ทธ ํ์
|
| 791 |
+
favicon_path=None, # ํ๋น์ฝ ์ค์
|
| 792 |
+
ssl_verify=False, # SSL ๊ฒ์ฆ ๋นํ์ฑํ (๊ฐ๋ฐ์ฉ)
|
| 793 |
+
inbrowser=False, # ์๋ ๋ธ๋ผ์ฐ์ ์ด๊ธฐ ๋นํ์ฑํ
|
| 794 |
+
prevent_thread_lock=False # ์ค๋ ๋ ์ ๊ธ ๋ฐฉ์ง ๋นํ์ฑํ
|
| 795 |
+
)
|
| 796 |
+
except Exception as e:
|
| 797 |
+
logger.error(f"์ ํ๋ฆฌ์ผ์ด์
์คํ ์คํจ: {e}")
|
| 798 |
+
print(f"โ ์ ํ๋ฆฌ์ผ์ด์
์คํ ์คํจ: {e}")
|
| 799 |
+
raise SystemExit(1)
|
| 800 |
+
finally:
|
| 801 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์ข
๋ฃ ์ ์ ๋ฆฌ
|
| 802 |
+
logger.info("๐งน ์ปจํธ๋กค ํ์ ์ ํ๋ฆฌ์ผ์ด์
์ข
๋ฃ - ์ต์ข
์ ๋ฆฌ ์์
...")
|
| 803 |
+
try:
|
| 804 |
+
cleanup_huggingface_temp_folders()
|
| 805 |
+
if os.path.exists(app_temp_dir):
|
| 806 |
+
shutil.rmtree(app_temp_dir, ignore_errors=True)
|
| 807 |
+
logger.info("โ
์ต์ข
์ ๋ฆฌ ์๋ฃ")
|
| 808 |
+
except Exception as e:
|
| 809 |
+
logger.error(f"์ต์ข
์ ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|