File size: 24,814 Bytes
a502bd6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
import gradio as gr
import pandas as pd
import os
import logging
from datetime import datetime
import pytz
import time
import tempfile
import zipfile
import re
import json
# ๋ก๊น
์ค์ - ํด๋ผ์ด์ธํธ ์ ๋ณด ์จ๊น
logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๊ทธ ๋นํ์ฑํ
logging.getLogger('gradio').setLevel(logging.WARNING)
logging.getLogger('gradio_client').setLevel(logging.WARNING)
logging.getLogger('httpx').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
# ===== API ํด๋ผ์ด์ธํธ ์ค์ =====
def get_api_client():
"""ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ๋ฅผ ๊ฐ์ ธ์ ํด๋ผ์ด์ธํธ ์์ฑ"""
try:
from gradio_client import Client
# ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ ๊ฐ์ ธ์ค๊ธฐ
api_endpoint = os.getenv('API_ENDPOINT')
if not api_endpoint:
logger.error("API ์๋ํฌ์ธํธ๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
raise ValueError("API ์๋ํฌ์ธํธ๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
client = Client(api_endpoint)
logger.info("์๊ฒฉ API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ฑ๊ณต")
return client
except Exception as e:
logger.error(f"API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ: {e}")
return None
# ===== ํ๊ตญ์๊ฐ ๊ด๋ จ ํจ์ =====
def get_korean_time():
"""ํ๊ตญ์๊ฐ ๋ฐํ"""
try:
korea_tz = pytz.timezone('Asia/Seoul')
return datetime.now(korea_tz)
except:
return datetime.now()
def format_korean_datetime(dt=None, format_type="filename"):
"""ํ๊ตญ์๊ฐ ํฌ๋งทํ
"""
if dt is None:
dt = get_korean_time()
if format_type == "filename":
return dt.strftime("%y%m%d_%H%M")
elif format_type == "display":
return dt.strftime('%Y๋
%m์ %d์ผ %H์ %M๋ถ')
elif format_type == "full":
return dt.strftime('%Y-%m-%d %H:%M:%S')
else:
return dt.strftime("%y%m%d_%H%M")
# ===== ๋ก๋ฉ ์ ๋๋ฉ์ด์
=====
def create_loading_animation():
"""๋ก๋ฉ ์ ๋๋ฉ์ด์
HTML"""
return """
<div style="display: flex; flex-direction: column; align-items: center; padding: 40px; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
<div style="width: 60px; height: 60px; border: 4px solid #f3f3f3; border-top: 4px solid #FB7F0D; border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 20px;"></div>
<h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">๋ถ์ ์ค์
๋๋ค...</h3>
<p style="color: #666; margin: 5px 0; text-align: center;">์๊ฒฉ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ AI๊ฐ ๋ถ์ํ๊ณ ์์ต๋๋ค.<br>์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์.</p>
<div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
<div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
</div>
</div>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes progress {
0% { transform: translateX(-100%); }
100% { transform: translateX(100%); }
}
</style>
"""
# ===== ์๋ฌ ์ฒ๋ฆฌ ํจ์ =====
def generate_error_response(error_message):
"""์๋ฌ ์๋ต ์์ฑ"""
return f'''
<div style="color: red; padding: 30px; text-align: center; width: 100%;
background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
<h3 style="margin-bottom: 15px;">โ ์ฐ๊ฒฐ ์ค๋ฅ</h3>
<p style="margin-bottom: 20px;">{error_message}</p>
<div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
<h4>ํด๊ฒฐ ๋ฐฉ๋ฒ:</h4>
<ul style="text-align: left; padding-left: 20px;">
<li>๋คํธ์ํฌ ์ฐ๊ฒฐ์ ํ์ธํด์ฃผ์ธ์</li>
<li>์๊ฒฉ ์๋ฒ ์ํ๋ฅผ ํ์ธํด์ฃผ์ธ์</li>
<li>์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์</li>
<li>๋ฌธ์ ๊ฐ ์ง์๋๋ฉด ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์</li>
</ul>
</div>
</div>
'''
# ===== ํ์ผ ์ถ๋ ฅ ํจ์๋ค =====
def create_timestamp_filename(analysis_keyword):
"""ํ์์คํฌํ๊ฐ ํฌํจ๋ ํ์ผ๋ช
์์ฑ - ํ๊ตญ์๊ฐ ์ ์ฉ"""
timestamp = format_korean_datetime(format_type="filename")
safe_keyword = re.sub(r'[^\w\s-]', '', analysis_keyword).strip()
safe_keyword = re.sub(r'[-\s]+', '_', safe_keyword)
return f"{safe_keyword}_{timestamp}_๋ถ์๊ฒฐ๊ณผ"
def export_to_html(analysis_html, filename_base):
"""HTML ํ์ผ๋ก ์ถ๋ ฅ - ํ๊ตญ์๊ฐ ์ ์ฉ"""
try:
html_filename = f"{filename_base}.html"
html_path = os.path.join(tempfile.gettempdir(), html_filename)
# ํ๊ตญ์๊ฐ์ผ๋ก ์์ฑ ์๊ฐ ํ์
korean_time = format_korean_datetime(format_type="display")
# ์์ ํ HTML ๋ฌธ์ ์์ฑ
full_html = f"""
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํค์๋ ์ฌ์ธต๋ถ์ ๊ฒฐ๊ณผ</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
<style>
body {{
font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
line-height: 1.6;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
overflow: hidden;
}}
.header {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}}
.header h1 {{
margin: 0;
font-size: 28px;
font-weight: 700;
}}
.header p {{
margin: 10px 0 0 0;
font-size: 16px;
opacity: 0.9;
}}
.content {{
padding: 30px;
}}
.timestamp {{
text-align: center;
padding: 20px;
background: #f8f9fa;
color: #6c757d;
font-size: 14px;
border-top: 1px solid #dee2e6;
}}
/* ์ฐจํธ ์คํ์ผ ๊ฐ์ */
.chart-container {{
margin: 20px 0;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}}
/* ๋ฐ์ํ ์คํ์ผ */
@media (max-width: 768px) {{
.container {{
margin: 10px;
border-radius: 8px;
}}
.header {{
padding: 20px;
}}
.header h1 {{
font-size: 24px;
}}
.content {{
padding: 20px;
}}
}}
/* ์ ๋๋ฉ์ด์
*/
@keyframes spin {{
0% {{ transform: rotate(0deg); }}
100% {{ transform: rotate(360deg); }}
}}
@keyframes progress {{
0% {{ transform: translateX(-100%); }}
100% {{ transform: translateX(100%); }}
}}
/* ํ๋ฆฐํธ ์คํ์ผ */
@media print {{
body {{
background: white;
padding: 0;
}}
.container {{
box-shadow: none;
border-radius: 0;
}}
.header {{
background: #667eea !important;
-webkit-print-color-adjust: exact;
}}
}}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ธต๋ถ์ ๊ฒฐ๊ณผ</h1>
<p>AI ์ํ ์์ฑ ๋ถ์ ์์คํ
v2.10</p>
</div>
<div class="content">
{analysis_html}
</div>
<div class="timestamp">
<i class="fas fa-clock"></i> ์์ฑ ์๊ฐ: {korean_time} (ํ๊ตญ์๊ฐ)
</div>
</div>
</body>
</html>
"""
with open(html_path, 'w', encoding='utf-8') as f:
f.write(full_html)
logger.info(f"HTML ํ์ผ ์์ฑ ์๋ฃ: {html_path}")
return html_path
except Exception as e:
logger.error(f"HTML ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
return None
def create_zip_file(html_path, filename_base):
"""์์ถ ํ์ผ ์์ฑ (HTML๋ง)"""
try:
zip_filename = f"{filename_base}.zip"
zip_path = os.path.join(tempfile.gettempdir(), zip_filename)
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
if html_path and os.path.exists(html_path):
zipf.write(html_path, f"{filename_base}.html")
logger.info(f"HTML ํ์ผ ์์ถ ์ถ๊ฐ: {filename_base}.html")
logger.info(f"์์ถ ํ์ผ ์์ฑ ์๋ฃ: {zip_path}")
return zip_path
except Exception as e:
logger.error(f"์์ถ ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
return None
def export_analysis_results(export_data):
"""๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ๋ฉ์ธ ํจ์ - ์ธ์
๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ"""
try:
# ์ถ๋ ฅํ ๋ฐ์ดํฐ ํ์ธ
if not export_data or not isinstance(export_data, dict):
return None, "๋ถ์ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค. ๋จผ์ ํค์๋ ์ฌ์ธต๋ถ์์ ์คํํด์ฃผ์ธ์."
analysis_keyword = export_data.get("analysis_keyword", "")
analysis_html = export_data.get("analysis_html", "")
if not analysis_keyword:
return None, "๋ถ์ํ ํค์๋๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ๋จผ์ ํค์๋ ๋ถ์์ ์คํํด์ฃผ์ธ์."
if not analysis_html:
return None, "๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ๋จผ์ ํค์๋ ์ฌ์ธต๋ถ์์ ์คํํด์ฃผ์ธ์."
# ํ์ผ๋ช
์์ฑ (ํ๊ตญ์๊ฐ ์ ์ฉ)
filename_base = create_timestamp_filename(analysis_keyword)
logger.info(f"์ถ๋ ฅ ํ์ผ๋ช
: {filename_base}")
# HTML ํ์ผ ์์ฑ
html_path = export_to_html(analysis_html, filename_base)
# ์์ถ ํ์ผ ์์ฑ
if html_path:
zip_path = create_zip_file(html_path, filename_base)
if zip_path:
return zip_path, f"โ
๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ถ๋ ฅ๋์์ต๋๋ค!\nํ์ผ๋ช
: {filename_base}.zip"
else:
return None, "์์ถ ํ์ผ ์์ฑ์ ์คํจํ์ต๋๋ค."
else:
return None, "์ถ๋ ฅํ ํ์ผ์ด ์์ต๋๋ค."
except Exception as e:
logger.error(f"๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ์ค๋ฅ: {e}")
return None, f"์ถ๋ ฅ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
# ===== ์๊ฒฉ API ํธ์ถ ํจ์๋ค =====
def call_analyze_keyword_api(analysis_keyword):
"""ํค์๋ ์ฌ์ธต๋ถ์ API ํธ์ถ"""
try:
client = get_api_client()
if not client:
return generate_error_response("API ํด๋ผ์ด์ธํธ๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค."), {}
logger.info("์๊ฒฉ API ํธ์ถ: ํค์๋ ์ฌ์ธต๋ถ์")
result = client.predict(
analysis_keyword=analysis_keyword,
api_name="/on_analyze_keyword"
)
logger.info(f"ํค์๋ ๋ถ์ API ๊ฒฐ๊ณผ ํ์
: {type(result)}")
# ๋ถ์ ๊ฒฐ๊ณผ๋ก export ๋ฐ์ดํฐ ์์ฑ
if isinstance(result, str) and len(result) > 100:
export_data = {
"analysis_keyword": analysis_keyword,
"analysis_html": result,
"analysis_completed": True,
"created_at": get_korean_time().isoformat()
}
return result, export_data
else:
return str(result), {}
except Exception as e:
logger.error(f"ํค์๋ ์ฌ์ธต๋ถ์ API ํธ์ถ ์ค๋ฅ: {e}")
return generate_error_response(f"์๊ฒฉ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {str(e)}"), {}
def call_export_results_api(export_data):
"""๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ API ํธ์ถ"""
try:
client = get_api_client()
if not client:
return None, "API ํด๋ผ์ด์ธํธ๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค."
logger.info("์๊ฒฉ API ํธ์ถ: ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ")
result = client.predict(
api_name="/on_export_results"
)
logger.info(f"์ถ๋ ฅ API ๊ฒฐ๊ณผ ํ์
: {type(result)}")
# ๊ฒฐ๊ณผ๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์๋ ๋ฉ์์ง, ๋ ๋ฒ์งธ๋ ํ์ผ
if isinstance(result, tuple) and len(result) == 2:
message, file_path = result
if file_path:
return file_path, message
else:
return None, message
else:
return None, str(result)
except Exception as e:
logger.error(f"๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ API ํธ์ถ ์ค๋ฅ: {e}")
return None, f"์๊ฒฉ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {str(e)}"
# ===== ๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค =====
def create_interface():
# CSS ํ์ผ ๋ก๋
try:
with open('style.css', 'r', encoding='utf-8') as f:
custom_css = f.read()
except:
custom_css = """
:root { --primary-color: #FB7F0D; --secondary-color: #ff9a8b; }
.custom-button {
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
color: white !important; border-radius: 30px !important; height: 45px !important;
font-size: 16px !important; font-weight: bold !important; width: 100% !important;
}
.export-button {
background: linear-gradient(135deg, #28a745, #20c997) !important;
color: white !important; border-radius: 25px !important; height: 50px !important;
font-size: 17px !important; font-weight: bold !important; width: 100% !important;
margin-top: 20px !important;
}
.custom-frame {
background-color: white !important;
border: 1px solid #e5e5e5 !important;
border-radius: 18px;
padding: 20px;
margin: 10px 0;
box-shadow: 0 8px 30px rgba(251, 127, 13, 0.08) !important;
}
.section-title {
display: flex;
align-items: center;
font-size: 20px;
font-weight: 700;
color: #334155 !important;
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 2px solid var(--primary-color);
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
}
.section-title img, .section-title i {
margin-right: 10px;
font-size: 20px;
color: var(--primary-color);
}
"""
with gr.Blocks(
css=custom_css,
title="๐ AI ์ํ ์์ฑ ๋ถ์๊ธฐ v2.10",
theme=gr.themes.Default(primary_hue="orange", secondary_hue="orange")
) as interface:
# ํฐํธ ๋ฐ ์์ด์ฝ ๋ก๋
gr.HTML("""
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
""")
# ์ธ์
๋ณ ์ํ ๊ด๋ฆฌ (๋ฉํฐ ์ฌ์ฉ์ ์์ )
export_data_state = gr.State({})
# === ํค์๋ ์ฌ์ธต๋ถ์ ์
๋ ฅ ===
with gr.Column(elem_classes="custom-frame fade-in"):
gr.HTML('<div class="section-title"><i class="fas fa-bullseye"></i> ํค์๋ ์ฌ์ธต๋ถ์ ์
๋ ฅ</div>')
analysis_keyword_input = gr.Textbox(
label="๋ถ์ํ ํค์๋",
placeholder="์ฌ์ธต ๋ถ์ํ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์ (์: ํต๊ตฝ ์ฌ๋ฆฌํผ)",
value="",
elem_id="analysis_keyword_input"
)
analyze_keyword_btn = gr.Button("ํค์๋ ์ฌ์ธต๋ถ์ ํ๊ธฐ", elem_classes="custom-button", size="lg")
# === ํค์๋ ์ฌ์ธต๋ถ์ ===
with gr.Column(elem_classes="custom-frame fade-in"):
gr.HTML('<div class="section-title"><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ธต๋ถ์</div>')
analysis_result = gr.HTML(label="ํค์๋ ์ฌ์ธต๋ถ์")
# === ๊ฒฐ๊ณผ ์ถ๋ ฅ ์น์
===
with gr.Column(elem_classes="custom-frame fade-in"):
gr.HTML('<div class="section-title"><i class="fas fa-download"></i> ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ</div>')
export_btn = gr.Button("๐ ๋ถ์๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ", elem_classes="export-button", size="lg")
export_result = gr.HTML()
download_file = gr.File(label="๋ค์ด๋ก๋", visible=False)
# ===== ์ด๋ฒคํธ ํธ๋ค๋ฌ =====
def on_analyze_keyword(analysis_keyword):
if not analysis_keyword.strip():
return "๋ถ์ํ ํค์๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", {}
# ๋ก๋ฉ ์ํ ํ์
yield create_loading_animation(), {}
# ์ค์ ํค์๋ ๋ถ์ ์คํ
keyword_result, session_export_data = call_analyze_keyword_api(analysis_keyword)
# ๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์๊ณผ ๐ฏ ํค์๋ ๋ถ์ ํ์
yield keyword_result, session_export_data
def on_export_results(export_data):
"""๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ํธ๋ค๋ฌ - ์ธ์
๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ"""
try:
# ๋ก์ปฌ ์ถ๋ ฅ ์๋
zip_path, message = export_analysis_results(export_data)
if zip_path:
# ์ฑ๊ณต ๋ฉ์์ง์ ํจ๊ป ๋ค์ด๋ก๋ ํ์ผ ์ ๊ณต
success_html = f"""
<div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
<h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> ์ถ๋ ฅ ์๋ฃ!</h4>
<p style="color: #155724; margin: 0; line-height: 1.6;">
{message}<br>
<strong>ํฌํจ ํ์ผ:</strong><br>
โข ๐ HTML ํ์ผ: ํค์๋ ์ฌ์ธต๋ถ์ ๊ฒฐ๊ณผ (๊ทธ๋ํ ํฌํจ)<br>
<br>
<i class="fas fa-download"></i> ์๋ ๋ค์ด๋ก๋ ๋ฒํผ์ ํด๋ฆญํ์ฌ ํ์ผ์ ์ ์ฅํ์ธ์.<br>
<small style="color: #666;">โฐ ํ๊ตญ์๊ฐ ๊ธฐ์ค์ผ๋ก ํ์ผ๋ช
์ด ์์ฑ๋ฉ๋๋ค.</small>
</p>
</div>
"""
return success_html, gr.update(value=zip_path, visible=True)
else:
# ๋ก์ปฌ ์ถ๋ ฅ ์คํจ์ ์๊ฒฉ API ์๋
try:
remote_file, remote_message = call_export_results_api(export_data)
if remote_file:
success_html = f"""
<div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
<h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> ์๊ฒฉ ์ถ๋ ฅ ์๋ฃ!</h4>
<p style="color: #155724; margin: 0; line-height: 1.6;">
{remote_message}<br>
<i class="fas fa-download"></i> ์๋ ๋ค์ด๋ก๋ ๋ฒํผ์ ํด๋ฆญํ์ฌ ํ์ผ์ ์ ์ฅํ์ธ์.
</p>
</div>
"""
return success_html, gr.update(value=remote_file, visible=True)
else:
# ์คํจ ๋ฉ์์ง
error_html = f"""
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถ๋ ฅ ์คํจ</h4>
<p style="color: #721c24; margin: 0;">{message}<br>์๊ฒฉ ์ถ๋ ฅ๋ ์คํจ: {remote_message}</p>
</div>
"""
return error_html, gr.update(visible=False)
except:
# ์๊ฒฉ API๋ ์คํจ
error_html = f"""
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถ๋ ฅ ์คํจ</h4>
<p style="color: #721c24; margin: 0;">{message}</p>
</div>
"""
return error_html, gr.update(visible=False)
except Exception as e:
logger.error(f"์ถ๋ ฅ ํธ๋ค๋ฌ ์ค๋ฅ: {e}")
error_html = f"""
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์์คํ
์ค๋ฅ</h4>
<p style="color: #721c24; margin: 0;">์ถ๋ ฅ ์ค ์์คํ
์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}</p>
</div>
"""
return error_html, gr.update(visible=False)
# ===== ์ด๋ฒคํธ ์ฐ๊ฒฐ =====
analyze_keyword_btn.click(
fn=on_analyze_keyword,
inputs=[analysis_keyword_input],
outputs=[analysis_result, export_data_state]
)
export_btn.click(
fn=on_export_results,
inputs=[export_data_state],
outputs=[export_result, download_file]
)
return interface
# ===== ๋ฉ์ธ ์คํ =====
if __name__ == "__main__":
# pytz ๋ชจ๋ ์ค์น ํ์ธ
try:
import pytz
logger.info("โ
pytz ๋ชจ๋ ๋ก๋ ์ฑ๊ณต - ํ๊ตญ์๊ฐ ์ง์")
except ImportError:
logger.warning("โ ๏ธ pytz ๋ชจ๋์ด ์ค์น๋์ง ์์ - pip install pytz ์คํ ํ์")
logger.info("์์คํ
์๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
logger.info("===== ์ํ ์์ฑ ๋ถ์ ์์คํ
v2.10 (์ปจํธ๋กค ํ์ ๋ฒ์ ) ์์ =====")
# ํ์ํ ํจํค์ง ์๋ด
print("๐ฆ ํ์ํ ํจํค์ง:")
print(" pip install gradio gradio_client pandas pytz")
print()
# API ์๋ํฌ์ธํธ ์ค์ ์๋ด
api_endpoint = os.getenv('API_ENDPOINT')
if not api_endpoint:
print("โ ๏ธ API_ENDPOINT ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
print(" export API_ENDPOINT='your-endpoint-url'")
print()
else:
print("โ
API ์๋ํฌ์ธํธ ์ค์ ์๋ฃ!")
print()
print("๐ v2.10 ์ปจํธ๋กค ํ์ ๋ฒ์ ํน์ง:")
print(" โข ํ๊น
ํ์ด์ค ๊ทธ๋ผ๋์ค ์๋ํฌ์ธํธ ํ์ฉ")
print(" โข ์์ ํ ๋์ผํ UI์ ๊ธฐ๋ฅ ๊ตฌํ")
print(" โข ๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์๊ณผ ๐ฏ ํค์๋ ๋ถ์ ํ์")
print(" โข โ
์ถ๋ ฅ ๊ธฐ๋ฅ: HTML ํ์ผ ์์ฑ ๋ฐ ZIP ๋ค์ด๋ก๋")
print(" โข โ
ํ๊ตญ์๊ฐ ๊ธฐ์ค ํ์ผ๋ช
์์ฑ")
print(" โข โ
๋ฉํฐ ์ฌ์ฉ์ ์์ : gr.State๋ก ์ธ์
๋ณ ๋ฐ์ดํฐ ๊ด๋ฆฌ")
print(" โข ๐ ํด๋ผ์ด์ธํธ ์ ๋ณด ํ๊ฒฝ๋ณ์๋ก ์์ ์จ๊น")
print(" โข ์๊ฒฉ ์๋ฒ์ ๋ก์ปฌ ์ฒ๋ฆฌ ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์")
print()
# ์ฑ ์คํ
app = create_interface()
app.launch(server_name="0.0.0.0", server_port=7860, share=True) |