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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
AI μν μμ± λΆμ μμ€ν
v3.2 - 컨νΈλ‘€ νμ (λλ―Έ λ°μ΄ν° μ κ±° λ²μ )
|
| 4 |
+
- νκΉ
νμ΄μ€ κ·ΈλΌλμ€ μλν¬μΈνΈ νμ©
|
| 5 |
+
- λΆμ κ²°κ³Ό HTML κΈ°λ° μμ ν νμΌ μμ± μμ€ν
|
| 6 |
+
- λ°μ΄ν° νλ¦ κ°μ λ° fallback λ‘μ§ κ°ν
|
| 7 |
+
- νκ΅μκ° μ²λ¦¬ λ° νμΌλͺ
μμ± λ‘μ§ ν¬ν¨
|
| 8 |
+
- λλ―Έ λ°μ΄ν° μμ± λ‘μ§ μμ μ κ±°
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import pandas as pd
|
| 13 |
import os
|
| 14 |
+
import logging
|
| 15 |
+
from datetime import datetime
|
| 16 |
+
import pytz
|
| 17 |
+
import time
|
| 18 |
+
import tempfile
|
| 19 |
+
import zipfile
|
| 20 |
+
import re
|
| 21 |
+
import json
|
| 22 |
+
|
| 23 |
+
# λ‘κΉ
μ€μ
|
| 24 |
+
logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
# μΈλΆ λΌμ΄λΈλ¬λ¦¬ λ‘κ·Έ λΉνμ±ν
|
| 28 |
+
logging.getLogger('gradio').setLevel(logging.WARNING)
|
| 29 |
+
logging.getLogger('gradio_client').setLevel(logging.WARNING)
|
| 30 |
+
logging.getLogger('httpx').setLevel(logging.WARNING)
|
| 31 |
+
logging.getLogger('urllib3').setLevel(logging.WARNING)
|
| 32 |
+
|
| 33 |
+
# ===== API ν΄λΌμ΄μΈνΈ μ€μ =====
|
| 34 |
+
def get_api_client():
|
| 35 |
+
"""νκ²½λ³μμμ API μλν¬μΈνΈλ₯Ό κ°μ Έμ ν΄λΌμ΄μΈνΈ μμ±"""
|
| 36 |
+
try:
|
| 37 |
+
from gradio_client import Client
|
| 38 |
+
|
| 39 |
+
# νκ²½λ³μμμ API μλν¬μΈνΈ κ°μ Έμ€κΈ°
|
| 40 |
+
api_endpoint = os.getenv('API_ENDPOINT')
|
| 41 |
+
|
| 42 |
+
if not api_endpoint:
|
| 43 |
+
logger.error("API_ENDPOINT νκ²½λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
| 44 |
+
raise ValueError("API_ENDPOINT νκ²½λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
| 45 |
+
|
| 46 |
+
client = Client(api_endpoint)
|
| 47 |
+
logger.info("μ격 API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν μ±κ³΅")
|
| 48 |
+
return client
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.error(f"API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν μ€ν¨: {e}")
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
# ===== νκ΅μκ° κ΄λ ¨ ν¨μ =====
|
| 55 |
+
def get_korean_time():
|
| 56 |
+
"""νκ΅μκ° λ°ν"""
|
| 57 |
+
korea_tz = pytz.timezone('Asia/Seoul')
|
| 58 |
+
return datetime.now(korea_tz)
|
| 59 |
+
|
| 60 |
+
def format_korean_datetime(dt=None, format_type="filename"):
|
| 61 |
+
"""νκ΅μκ° ν¬λ§·ν
"""
|
| 62 |
+
if dt is None:
|
| 63 |
+
dt = get_korean_time()
|
| 64 |
+
|
| 65 |
+
if format_type == "filename":
|
| 66 |
+
return dt.strftime("%y%m%d_%H%M")
|
| 67 |
+
elif format_type == "display":
|
| 68 |
+
return dt.strftime('%Yλ
%mμ %dμΌ %Hμ %MλΆ')
|
| 69 |
+
elif format_type == "full":
|
| 70 |
+
return dt.strftime('%Y-%m-%d %H:%M:%S')
|
| 71 |
+
else:
|
| 72 |
+
return dt.strftime("%y%m%d_%H%M")
|
| 73 |
+
|
| 74 |
+
# ===== λ°μ΄ν° μ²λ¦¬ λ° κ²μ¦ ν¨μλ€ =====
|
| 75 |
+
def create_export_data_from_html(analysis_keyword, main_keyword, analysis_html, step1_data=None):
|
| 76 |
+
"""λΆμ HTMLκ³Ό 1λ¨κ³ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ exportμ© λ°μ΄ν° ꡬ쑰 μμ± (λλ―Έ λ°μ΄ν° μ κ±°)"""
|
| 77 |
+
logger.info("=== π Export λ°μ΄ν° ꡬ쑰 μμ± μμ (λλ―Έ λ°μ΄ν° μ κ±° λ²μ ) ===")
|
| 78 |
+
|
| 79 |
+
# κΈ°λ³Έ export λ°μ΄ν° ꡬ쑰
|
| 80 |
+
export_data = {
|
| 81 |
+
"main_keyword": main_keyword or analysis_keyword,
|
| 82 |
+
"analysis_keyword": analysis_keyword,
|
| 83 |
+
"analysis_html": analysis_html,
|
| 84 |
+
"main_keywords_df": None,
|
| 85 |
+
"related_keywords_df": None,
|
| 86 |
+
"analysis_completed": True,
|
| 87 |
+
"created_at": get_korean_time().isoformat()
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
# 1λ¨κ³ λ°μ΄ν°μμ main_keywords_df μΆμΆ (μ€μ λ°μ΄ν°λ§)
|
| 91 |
+
if step1_data and isinstance(step1_data, dict):
|
| 92 |
+
if "keywords_df" in step1_data:
|
| 93 |
+
keywords_df = step1_data["keywords_df"]
|
| 94 |
+
if isinstance(keywords_df, dict):
|
| 95 |
+
try:
|
| 96 |
+
export_data["main_keywords_df"] = pd.DataFrame(keywords_df)
|
| 97 |
+
logger.info(f"β
1λ¨κ³ ν€μλ λ°μ΄ν°λ₯Ό DataFrameμΌλ‘ λ³ν: {export_data['main_keywords_df'].shape}")
|
| 98 |
+
except Exception as e:
|
| 99 |
+
logger.warning(f"β οΈ 1λ¨κ³ λ°μ΄ν° λ³ν μ€ν¨: {e}")
|
| 100 |
+
export_data["main_keywords_df"] = None
|
| 101 |
+
elif hasattr(keywords_df, 'shape'):
|
| 102 |
+
export_data["main_keywords_df"] = keywords_df
|
| 103 |
+
logger.info(f"β
1λ¨κ³ ν€μλ DataFrame μ¬μ©: {keywords_df.shape}")
|
| 104 |
+
else:
|
| 105 |
+
logger.info("π 1λ¨κ³ ν€μλ λ°μ΄ν°κ° μ ν¨νμ§ μμ - NoneμΌλ‘ μ μ§")
|
| 106 |
+
export_data["main_keywords_df"] = None
|
| 107 |
+
|
| 108 |
+
# λΆμ HTMLμμ μ°κ΄κ²μμ΄ μ 보 μΆμΆ μλ (μ€μ λ°μ΄ν°λ§)
|
| 109 |
+
if analysis_html and "μ°κ΄κ²μμ΄ λΆμ" in analysis_html:
|
| 110 |
+
logger.info("π λΆμ HTMLμμ μ°κ΄κ²μμ΄ μ 보 λ°κ²¬ - μ€μ νμ± νμ")
|
| 111 |
+
# μ€μ HTML νμ± λ‘μ§μ΄ νμν λΆλΆ
|
| 112 |
+
# νμ¬λ λλ―Έ λ°μ΄ν° λμ NoneμΌλ‘ μ μ§
|
| 113 |
+
export_data["related_keywords_df"] = None
|
| 114 |
+
logger.info("π‘ μ€μ HTML νμ± λ‘μ§ κ΅¬ν νμ - μ°κ΄κ²μμ΄ λ°μ΄ν°λ NoneμΌλ‘ μ μ§")
|
| 115 |
+
|
| 116 |
+
logger.info(f"π Export λ°μ΄ν° ꡬ쑰 μμ± μλ£ (λλ―Έ λ°μ΄ν° μμ):")
|
| 117 |
+
logger.info(f" - analysis_keyword: {export_data['analysis_keyword']}")
|
| 118 |
+
logger.info(f" - main_keywords_df: {export_data['main_keywords_df'].shape if export_data['main_keywords_df'] is not None else 'None'}")
|
| 119 |
+
logger.info(f" - related_keywords_df: {export_data['related_keywords_df'].shape if export_data['related_keywords_df'] is not None else 'None'}")
|
| 120 |
+
logger.info(f" - analysis_html: {len(str(export_data['analysis_html']))} λ¬Έμ")
|
| 121 |
+
|
| 122 |
+
return export_data
|
| 123 |
+
|
| 124 |
+
def validate_and_repair_export_data(export_data):
|
| 125 |
+
"""Export λ°μ΄ν° μ ν¨μ± κ²μ¬ λ° λ³΅κ΅¬ (λλ―Έ λ°μ΄ν° μ κ±°)"""
|
| 126 |
+
logger.info("π§ Export λ°μ΄ν° μ ν¨μ± κ²μ¬ λ° λ³΅κ΅¬ μμ (λλ―Έ λ°μ΄ν° μ κ±° λ²μ )")
|
| 127 |
+
|
| 128 |
+
if not export_data or not isinstance(export_data, dict):
|
| 129 |
+
logger.warning("β οΈ Export λ°μ΄ν°κ° μκ±°λ λμ
λλ¦¬κ° μλ - κΈ°λ³Έ ꡬ쑰 μμ±")
|
| 130 |
+
return {
|
| 131 |
+
"main_keyword": "κΈ°λ³Έν€μλ",
|
| 132 |
+
"analysis_keyword": "κΈ°λ³ΈλΆμν€μλ",
|
| 133 |
+
"analysis_html": "<div>κΈ°λ³Έ λΆμ κ²°κ³Ό</div>",
|
| 134 |
+
"main_keywords_df": None, # λλ―Έ λ°μ΄ν° λμ None
|
| 135 |
+
"related_keywords_df": None, # λλ―Έ λ°μ΄ν° λμ None
|
| 136 |
+
"analysis_completed": True
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
# νμ ν€λ€ νμΈ λ° λ³΅κ΅¬
|
| 140 |
+
required_keys = {
|
| 141 |
+
"analysis_keyword": "λΆμν€μλ",
|
| 142 |
+
"main_keyword": "λ©μΈν€μλ",
|
| 143 |
+
"analysis_html": "<div>λΆμ μλ£</div>",
|
| 144 |
+
"analysis_completed": True
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
for key, default_value in required_keys.items():
|
| 148 |
+
if key not in export_data or not export_data[key]:
|
| 149 |
+
export_data[key] = default_value
|
| 150 |
+
logger.info(f"π§ {key} ν€ λ³΅κ΅¬: {default_value}")
|
| 151 |
+
|
| 152 |
+
# DataFrame λ°μ΄ν° κ²μ¦ λ° λ³ν (λλ―Έ λ°μ΄ν° μμ± μν¨)
|
| 153 |
+
for df_key in ["main_keywords_df", "related_keywords_df"]:
|
| 154 |
+
if df_key in export_data and export_data[df_key] is not None:
|
| 155 |
+
df_data = export_data[df_key]
|
| 156 |
+
|
| 157 |
+
# λμ
λ리λ₯Ό DataFrameμΌλ‘ λ³ν
|
| 158 |
+
if isinstance(df_data, dict):
|
| 159 |
+
try:
|
| 160 |
+
# λΉ λμ
λ리λ NoneμΌλ‘ μ²λ¦¬
|
| 161 |
+
if not df_data:
|
| 162 |
+
export_data[df_key] = None
|
| 163 |
+
logger.info(f"π {df_key} λΉ λμ
λ리 - NoneμΌλ‘ μ€μ ")
|
| 164 |
+
else:
|
| 165 |
+
export_data[df_key] = pd.DataFrame(df_data)
|
| 166 |
+
logger.info(f"β
{df_key} λμ
λ리λ₯Ό DataFrameμΌλ‘ λ³ν μ±κ³΅")
|
| 167 |
+
except Exception as e:
|
| 168 |
+
logger.warning(f"β οΈ {df_key} λ³ν μ€ν¨: {e}")
|
| 169 |
+
export_data[df_key] = None
|
| 170 |
+
elif not hasattr(df_data, 'shape'):
|
| 171 |
+
logger.warning(f"β οΈ {df_key}κ° DataFrameμ΄ μλ - NoneμΌλ‘ μ€μ ")
|
| 172 |
+
export_data[df_key] = None
|
| 173 |
+
|
| 174 |
+
logger.info("β
Export λ°μ΄ν° μ ν¨μ± κ²μ¬ λ° λ³΅κ΅¬ μλ£ (λλ―Έ λ°μ΄ν° μμ)")
|
| 175 |
+
return export_data
|
| 176 |
+
|
| 177 |
+
# ===== νμΌ μΆλ ₯ ν¨μλ€ =====
|
| 178 |
+
def create_timestamp_filename(analysis_keyword):
|
| 179 |
+
"""νμμ€ν¬νκ° ν¬ν¨λ νμΌλͺ
μμ± - νκ΅μκ° μ μ©"""
|
| 180 |
+
timestamp = format_korean_datetime(format_type="filename")
|
| 181 |
+
safe_keyword = re.sub(r'[^\w\s-]', '', analysis_keyword).strip()
|
| 182 |
+
safe_keyword = re.sub(r'[-\s]+', '_', safe_keyword)
|
| 183 |
+
return f"{safe_keyword}_{timestamp}_λΆμκ²°κ³Ό"
|
| 184 |
+
|
| 185 |
+
def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_keywords_df, filename_base):
|
| 186 |
+
"""μμ
νμΌλ‘ μΆλ ₯ (μ€μ λ°μ΄ν°λ§)"""
|
| 187 |
+
try:
|
| 188 |
+
# μ€μ λ°μ΄ν°κ° μλμ§ νμΈ
|
| 189 |
+
has_main_data = main_keywords_df is not None and not main_keywords_df.empty
|
| 190 |
+
has_related_data = related_keywords_df is not None and not related_keywords_df.empty
|
| 191 |
+
|
| 192 |
+
if not has_main_data and not has_related_data:
|
| 193 |
+
logger.info("π μμ±ν λ°μ΄ν°κ° μμ΄ μμ
νμΌ μμ± κ±΄λλ")
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
+
excel_filename = f"{filename_base}.xlsx"
|
| 197 |
+
excel_path = os.path.join(tempfile.gettempdir(), excel_filename)
|
| 198 |
+
|
| 199 |
+
with pd.ExcelWriter(excel_path, engine='xlsxwriter') as writer:
|
| 200 |
+
# μν¬λΆκ³Ό μν¬μνΈ μ€νμΌ μ€μ
|
| 201 |
+
workbook = writer.book
|
| 202 |
+
|
| 203 |
+
# ν€λ μ€νμΌ
|
| 204 |
+
header_format = workbook.add_format({
|
| 205 |
+
'bold': True,
|
| 206 |
+
'text_wrap': True,
|
| 207 |
+
'valign': 'top',
|
| 208 |
+
'fg_color': '#D7E4BC',
|
| 209 |
+
'border': 1
|
| 210 |
+
})
|
| 211 |
+
|
| 212 |
+
# λ°μ΄ν° μ€νμΌ
|
| 213 |
+
data_format = workbook.add_format({
|
| 214 |
+
'text_wrap': True,
|
| 215 |
+
'valign': 'top',
|
| 216 |
+
'border': 1
|
| 217 |
+
})
|
| 218 |
+
|
| 219 |
+
# μ«μ ν¬λ§·
|
| 220 |
+
number_format = workbook.add_format({
|
| 221 |
+
'num_format': '#,##0',
|
| 222 |
+
'text_wrap': True,
|
| 223 |
+
'valign': 'top',
|
| 224 |
+
'border': 1
|
| 225 |
+
})
|
| 226 |
+
|
| 227 |
+
# 첫 λ²μ§Έ μνΈ: λ©μΈν€μλ μ‘°ν©ν€μλ (μ€μ λ°μ΄ν°λ§)
|
| 228 |
+
if has_main_data:
|
| 229 |
+
main_keywords_df.to_excel(writer, sheet_name=f'{main_keyword}_μ‘°ν©ν€μλ', index=False)
|
| 230 |
+
worksheet1 = writer.sheets[f'{main_keyword}_μ‘°ν©ν€μλ']
|
| 231 |
+
|
| 232 |
+
# ν€λ μ€νμΌ μ μ©
|
| 233 |
+
for col_num, value in enumerate(main_keywords_df.columns.values):
|
| 234 |
+
worksheet1.write(0, col_num, value, header_format)
|
| 235 |
+
|
| 236 |
+
# λ°μ΄ν° μ€νμΌ μ μ©
|
| 237 |
+
for row_num in range(1, len(main_keywords_df) + 1):
|
| 238 |
+
for col_num, value in enumerate(main_keywords_df.iloc[row_num-1]):
|
| 239 |
+
if isinstance(value, (int, float)) and col_num in [1, 2, 3]: # κ²μλ 컬λΌ
|
| 240 |
+
worksheet1.write(row_num, col_num, value, number_format)
|
| 241 |
+
else:
|
| 242 |
+
worksheet1.write(row_num, col_num, value, data_format)
|
| 243 |
+
|
| 244 |
+
# μ΄ λλΉ μλ μ‘°μ
|
| 245 |
+
for i, col in enumerate(main_keywords_df.columns):
|
| 246 |
+
max_len = max(
|
| 247 |
+
main_keywords_df[col].astype(str).map(len).max(),
|
| 248 |
+
len(str(col))
|
| 249 |
+
)
|
| 250 |
+
worksheet1.set_column(i, i, min(max_len + 2, 50))
|
| 251 |
+
|
| 252 |
+
logger.info(f"β
λ©μΈν€μλ μνΈ μμ±: {main_keywords_df.shape}")
|
| 253 |
+
|
| 254 |
+
# λ λ²μ§Έ μνΈ: λΆμν€μλ μ°κ΄κ²μμ΄ (μ€μ λ°μ΄ν°λ§)
|
| 255 |
+
if has_related_data:
|
| 256 |
+
related_keywords_df.to_excel(writer, sheet_name=f'{analysis_keyword}_μ°κ΄κ²μμ΄', index=False)
|
| 257 |
+
worksheet2 = writer.sheets[f'{analysis_keyword}_μ°κ΄κ²μμ΄']
|
| 258 |
+
|
| 259 |
+
# ν€λ μ€νμΌ μ μ©
|
| 260 |
+
for col_num, value in enumerate(related_keywords_df.columns.values):
|
| 261 |
+
worksheet2.write(0, col_num, value, header_format)
|
| 262 |
+
|
| 263 |
+
# λ°μ΄ν° μ€νμΌ μ μ©
|
| 264 |
+
for row_num in range(1, len(related_keywords_df) + 1):
|
| 265 |
+
for col_num, value in enumerate(related_keywords_df.iloc[row_num-1]):
|
| 266 |
+
if isinstance(value, (int, float)) and col_num in [1, 2, 3]: # κ²μλ 컬λΌ
|
| 267 |
+
worksheet2.write(row_num, col_num, value, number_format)
|
| 268 |
+
else:
|
| 269 |
+
worksheet2.write(row_num, col_num, value, data_format)
|
| 270 |
+
|
| 271 |
+
# μ΄ λλΉ μλ μ‘°μ
|
| 272 |
+
for i, col in enumerate(related_keywords_df.columns):
|
| 273 |
+
max_len = max(
|
| 274 |
+
related_keywords_df[col].astype(str).map(len).max(),
|
| 275 |
+
len(str(col))
|
| 276 |
+
)
|
| 277 |
+
worksheet2.set_column(i, i, min(max_len + 2, 50))
|
| 278 |
+
|
| 279 |
+
logger.info(f"β
μ°κ΄κ²μμ΄ μνΈ μμ±: {related_keywords_df.shape}")
|
| 280 |
+
|
| 281 |
+
logger.info(f"μμ
νμΌ μμ± μλ£: {excel_path}")
|
| 282 |
+
return excel_path
|
| 283 |
+
|
| 284 |
+
except Exception as e:
|
| 285 |
+
logger.error(f"μμ
νμΌ μμ± μ€λ₯: {e}")
|
| 286 |
+
return None
|
| 287 |
+
|
| 288 |
+
def export_to_html(analysis_html, filename_base):
|
| 289 |
+
"""HTML νμΌλ‘ μΆλ ₯ - νκ΅μκ° μ μ©"""
|
| 290 |
+
try:
|
| 291 |
+
html_filename = f"{filename_base}.html"
|
| 292 |
+
html_path = os.path.join(tempfile.gettempdir(), html_filename)
|
| 293 |
+
|
| 294 |
+
# νκ΅μκ°μΌλ‘ μμ± μκ° νμ
|
| 295 |
+
korean_time = format_korean_datetime(format_type="display")
|
| 296 |
+
|
| 297 |
+
# μμ ν HTML λ¬Έμ μμ±
|
| 298 |
+
full_html = f"""
|
| 299 |
+
<!DOCTYPE html>
|
| 300 |
+
<html lang="ko">
|
| 301 |
+
<head>
|
| 302 |
+
<meta charset="UTF-8">
|
| 303 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 304 |
+
<title>ν€μλ μ¬μΆ©λΆμ κ²°κ³Ό</title>
|
| 305 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 306 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
|
| 307 |
+
<style>
|
| 308 |
+
body {{
|
| 309 |
+
font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 310 |
+
margin: 0;
|
| 311 |
+
padding: 20px;
|
| 312 |
+
background-color: #f5f5f5;
|
| 313 |
+
line-height: 1.6;
|
| 314 |
+
}}
|
| 315 |
+
.container {{
|
| 316 |
+
max-width: 1200px;
|
| 317 |
+
margin: 0 auto;
|
| 318 |
+
background: white;
|
| 319 |
+
border-radius: 12px;
|
| 320 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 321 |
+
overflow: hidden;
|
| 322 |
+
}}
|
| 323 |
+
.header {{
|
| 324 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 325 |
+
color: white;
|
| 326 |
+
padding: 30px;
|
| 327 |
+
text-align: center;
|
| 328 |
+
}}
|
| 329 |
+
.header h1 {{
|
| 330 |
+
margin: 0;
|
| 331 |
+
font-size: 28px;
|
| 332 |
+
font-weight: 700;
|
| 333 |
+
}}
|
| 334 |
+
.header p {{
|
| 335 |
+
margin: 10px 0 0 0;
|
| 336 |
+
font-size: 16px;
|
| 337 |
+
opacity: 0.9;
|
| 338 |
+
}}
|
| 339 |
+
.content {{
|
| 340 |
+
padding: 30px;
|
| 341 |
+
}}
|
| 342 |
+
.timestamp {{
|
| 343 |
+
text-align: center;
|
| 344 |
+
padding: 20px;
|
| 345 |
+
background: #f8f9fa;
|
| 346 |
+
color: #6c757d;
|
| 347 |
+
font-size: 14px;
|
| 348 |
+
border-top: 1px solid #dee2e6;
|
| 349 |
+
}}
|
| 350 |
+
|
| 351 |
+
/* μ°¨νΈ μ€νμΌ κ°μ */
|
| 352 |
+
.chart-container {{
|
| 353 |
+
margin: 20px 0;
|
| 354 |
+
padding: 20px;
|
| 355 |
+
background: white;
|
| 356 |
+
border-radius: 8px;
|
| 357 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 358 |
+
}}
|
| 359 |
+
|
| 360 |
+
/* λ°μν μ€νμΌ */
|
| 361 |
+
@media (max-width: 768px) {{
|
| 362 |
+
.container {{
|
| 363 |
+
margin: 10px;
|
| 364 |
+
border-radius: 8px;
|
| 365 |
+
}}
|
| 366 |
+
.header {{
|
| 367 |
+
padding: 20px;
|
| 368 |
+
}}
|
| 369 |
+
.header h1 {{
|
| 370 |
+
font-size: 24px;
|
| 371 |
+
}}
|
| 372 |
+
.content {{
|
| 373 |
+
padding: 20px;
|
| 374 |
+
}}
|
| 375 |
+
}}
|
| 376 |
+
|
| 377 |
+
/* μ λλ©μ΄μ
*/
|
| 378 |
+
@keyframes spin {{
|
| 379 |
+
0% {{ transform: rotate(0deg); }}
|
| 380 |
+
100% {{ transform: rotate(360deg); }}
|
| 381 |
+
}}
|
| 382 |
+
|
| 383 |
+
@keyframes progress {{
|
| 384 |
+
0% {{ transform: translateX(-100%); }}
|
| 385 |
+
100% {{ transform: translateX(100%); }}
|
| 386 |
+
}}
|
| 387 |
+
|
| 388 |
+
/* νλ¦°νΈ μ€νμΌ */
|
| 389 |
+
@media print {{
|
| 390 |
+
body {{
|
| 391 |
+
background: white;
|
| 392 |
+
padding: 0;
|
| 393 |
+
}}
|
| 394 |
+
.container {{
|
| 395 |
+
box-shadow: none;
|
| 396 |
+
border-radius: 0;
|
| 397 |
+
}}
|
| 398 |
+
.header {{
|
| 399 |
+
background: #667eea !important;
|
| 400 |
+
-webkit-print-color-adjust: exact;
|
| 401 |
+
}}
|
| 402 |
+
}}
|
| 403 |
+
</style>
|
| 404 |
+
</head>
|
| 405 |
+
<body>
|
| 406 |
+
<div class="container">
|
| 407 |
+
<div class="header">
|
| 408 |
+
<h1><i class="fas fa-chart-line"></i> ν€μλ μ¬μΆ©λΆμ κ²°κ³Ό</h1>
|
| 409 |
+
<p>AI μν μμ± λΆμ μμ€ν
v3.2 (λλ―Έ λ°μ΄ν° μ κ±° λ²μ )</p>
|
| 410 |
+
</div>
|
| 411 |
+
<div class="content">
|
| 412 |
+
{analysis_html}
|
| 413 |
+
</div>
|
| 414 |
+
<div class="timestamp">
|
| 415 |
+
<i class="fas fa-clock"></i> μμ± μκ°: {korean_time} (νκ΅μκ°)
|
| 416 |
+
</div>
|
| 417 |
+
</div>
|
| 418 |
+
</body>
|
| 419 |
+
</html>
|
| 420 |
+
"""
|
| 421 |
+
|
| 422 |
+
with open(html_path, 'w', encoding='utf-8') as f:
|
| 423 |
+
f.write(full_html)
|
| 424 |
+
|
| 425 |
+
logger.info(f"HTML νμΌ μμ± μλ£: {html_path}")
|
| 426 |
+
return html_path
|
| 427 |
+
|
| 428 |
+
except Exception as e:
|
| 429 |
+
logger.error(f"HTML νμΌ μμ± μ€λ₯: {e}")
|
| 430 |
+
return None
|
| 431 |
+
|
| 432 |
+
def create_zip_file(excel_path, html_path, filename_base):
|
| 433 |
+
"""μμΆ νμΌ μμ±"""
|
| 434 |
+
try:
|
| 435 |
+
zip_filename = f"{filename_base}.zip"
|
| 436 |
+
zip_path = os.path.join(tempfile.gettempdir(), zip_filename)
|
| 437 |
+
|
| 438 |
+
files_added = 0
|
| 439 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 440 |
+
if excel_path and os.path.exists(excel_path):
|
| 441 |
+
zipf.write(excel_path, f"{filename_base}.xlsx")
|
| 442 |
+
logger.info(f"μμ
νμΌ μμΆ μΆκ°: {filename_base}.xlsx")
|
| 443 |
+
files_added += 1
|
| 444 |
+
|
| 445 |
+
if html_path and os.path.exists(html_path):
|
| 446 |
+
zipf.write(html_path, f"{filename_base}.html")
|
| 447 |
+
logger.info(f"HTML νμΌ μμΆ μΆκ°: {filename_base}.html")
|
| 448 |
+
files_added += 1
|
| 449 |
+
|
| 450 |
+
if files_added == 0:
|
| 451 |
+
logger.warning("μμΆν νμΌμ΄ μμ")
|
| 452 |
+
return None
|
| 453 |
+
|
| 454 |
+
logger.info(f"μμΆ νμΌ μμ± μλ£: {zip_path} ({files_added}κ° νμΌ)")
|
| 455 |
+
return zip_path
|
| 456 |
+
|
| 457 |
+
except Exception as e:
|
| 458 |
+
logger.error(f"μμΆ νμΌ μμ± μ€λ₯: {e}")
|
| 459 |
+
return None
|
| 460 |
+
|
| 461 |
+
def export_analysis_results_enhanced(export_data):
|
| 462 |
+
"""κ°νλ λΆμ κ²°κ³Ό μΆλ ₯ λ©μΈ ν¨μ (λλ―Έ λ°μ΄ν° μ κ±°)"""
|
| 463 |
+
try:
|
| 464 |
+
logger.info("=== π κ°νλ μΆλ ₯ ν¨μ μμ (λλ―Έ λ°μ΄ν° μ κ±° λ²μ ) ===")
|
| 465 |
+
|
| 466 |
+
# λ°μ΄ν° μ ν¨μ± κ²μ¬ λ° λ³΅κ΅¬
|
| 467 |
+
export_data = validate_and_repair_export_data(export_data)
|
| 468 |
+
|
| 469 |
+
analysis_keyword = export_data.get("analysis_keyword", "κΈ°λ³Έν€μλ")
|
| 470 |
+
analysis_html = export_data.get("analysis_html", "<div>λΆμ μλ£</div>")
|
| 471 |
+
main_keyword = export_data.get("main_keyword", analysis_keyword)
|
| 472 |
+
main_keywords_df = export_data.get("main_keywords_df")
|
| 473 |
+
related_keywords_df = export_data.get("related_keywords_df")
|
| 474 |
+
|
| 475 |
+
logger.info(f"π μ²λ¦¬ν λ°μ΄ν°:")
|
| 476 |
+
logger.info(f" - analysis_keyword: '{analysis_keyword}'")
|
| 477 |
+
logger.info(f" - main_keyword: '{main_keyword}'")
|
| 478 |
+
logger.info(f" - analysis_html: {len(str(analysis_html))} λ¬Έμ")
|
| 479 |
+
logger.info(f" - main_keywords_df: {main_keywords_df.shape if main_keywords_df is not None else 'None'}")
|
| 480 |
+
logger.info(f" - related_keywords_df: {related_keywords_df.shape if related_keywords_df is not None else 'None'}")
|
| 481 |
+
|
| 482 |
+
# νμΌλͺ
μμ± (νκ΅μκ° μ μ©)
|
| 483 |
+
filename_base = create_timestamp_filename(analysis_keyword)
|
| 484 |
+
logger.info(f"π μΆλ ₯ νμΌλͺ
: {filename_base}")
|
| 485 |
+
|
| 486 |
+
# HTML νμΌμ λΆμ κ²°κ³Όκ° μμΌλ©΄ μμ±
|
| 487 |
+
html_path = None
|
| 488 |
+
if analysis_html and len(str(analysis_html).strip()) > 20: # μλ―Έμλ HTMLμΈμ§ νμΈ
|
| 489 |
+
logger.info("π HTML νμΌ μμ± μμ...")
|
| 490 |
+
html_path = export_to_html(analysis_html, filename_base)
|
| 491 |
+
if html_path:
|
| 492 |
+
logger.info(f"β
HTML νμΌ μμ± μ±κ³΅: {html_path}")
|
| 493 |
+
else:
|
| 494 |
+
logger.error("β HTML νμΌ μμ± μ€ν¨")
|
| 495 |
+
else:
|
| 496 |
+
logger.info("π λΆμ HTMLμ΄ μμ΄ HTML νμΌ μμ± κ±΄λλ")
|
| 497 |
+
|
| 498 |
+
# μμ
νμΌ μμ± (μ€μ DataFrameμ΄ μλ κ²½μ°λ§)
|
| 499 |
+
excel_path = None
|
| 500 |
+
if (main_keywords_df is not None and not main_keywords_df.empty) or \
|
| 501 |
+
(related_keywords_df is not None and not related_keywords_df.empty):
|
| 502 |
+
logger.info("π μμ
νμΌ μμ± μμ...")
|
| 503 |
+
excel_path = export_to_excel(
|
| 504 |
+
main_keyword,
|
| 505 |
+
main_keywords_df,
|
| 506 |
+
analysis_keyword,
|
| 507 |
+
related_keywords_df,
|
| 508 |
+
filename_base
|
| 509 |
+
)
|
| 510 |
+
if excel_path:
|
| 511 |
+
logger.info(f"β
μμ
νμΌ μμ± μ±κ³΅: {excel_path}")
|
| 512 |
+
else:
|
| 513 |
+
logger.warning("β οΈ μμ
νμΌ μμ± μ€ν¨")
|
| 514 |
+
else:
|
| 515 |
+
logger.info("π μ€μ DataFrame λ°μ΄ν°κ° μμ΄ μμ
νμΌ μμ± μλ΅")
|
| 516 |
+
|
| 517 |
+
# μμ±λ νμΌμ΄ μλμ§ νμΈ
|
| 518 |
+
if not html_path and not excel_path:
|
| 519 |
+
logger.warning("β οΈ μμ±λ νμΌμ΄ μμ")
|
| 520 |
+
return None, "β οΈ μμ±ν μ μλ λ°μ΄ν°κ° μμ΅λλ€. λΆμμ λ¨Όμ μλ£ν΄μ£ΌμΈμ."
|
| 521 |
+
|
| 522 |
+
# μμΆ νμΌ μμ±
|
| 523 |
+
logger.info("π¦ μμΆ νμΌ μμ± μμ...")
|
| 524 |
+
zip_path = create_zip_file(excel_path, html_path, filename_base)
|
| 525 |
+
if zip_path:
|
| 526 |
+
file_types = []
|
| 527 |
+
if html_path:
|
| 528 |
+
file_types.append("HTML")
|
| 529 |
+
if excel_path:
|
| 530 |
+
file_types.append("μμ
")
|
| 531 |
+
|
| 532 |
+
file_list = " + ".join(file_types)
|
| 533 |
+
logger.info(f"β
μμΆ νμΌ μμ± μ±κ³΅: {zip_path} ({file_list})")
|
| 534 |
+
return zip_path, f"β
λΆμ κ²°κ³Όκ° μ±κ³΅μ μΌλ‘ μΆλ ₯λμμ΅λλ€!\nνμΌλͺ
: {filename_base}.zip\nν¬ν¨ νμΌ: {file_list}\n\nπ‘ λλ―Έ λ°μ΄ν° μ κ±° λ²μ - μ€μ λΆμ λ°μ΄ν°λ§ ν¬ν¨λ©λλ€."
|
| 535 |
+
else:
|
| 536 |
+
logger.error("β μμΆ νμΌ μμ± μ€ν¨")
|
| 537 |
+
return None, "μμΆ νμΌ μμ±μ μ€ν¨νμ΅λλ€."
|
| 538 |
+
|
| 539 |
+
except Exception as e:
|
| 540 |
+
logger.error(f"β κ°νλ μΆλ ₯ ν¨μ μ 체 μ€λ₯: {e}")
|
| 541 |
+
import traceback
|
| 542 |
+
logger.error(f"μ€ν νΈλ μ΄μ€:\n{traceback.format_exc()}")
|
| 543 |
+
return None, f"μΆλ ₯ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 544 |
+
|
| 545 |
+
# ===== λ‘λ© μ λλ©μ΄μ
=====
|
| 546 |
+
def create_loading_animation():
|
| 547 |
+
"""λ‘λ© μ λλ©μ΄μ
HTML"""
|
| 548 |
+
return """
|
| 549 |
+
<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);">
|
| 550 |
+
<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>
|
| 551 |
+
<h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">λΆμ μ€μ
λλ€...</h3>
|
| 552 |
+
<p style="color: #666; margin: 5px 0; text-align: center;">μ격 μλ²μμ λ°μ΄ν°λ₯Ό μμ§νκ³ AIκ° λΆμνκ³ μμ΅λλ€.<br>μ μλ§ κΈ°λ€λ €μ£ΌμΈμ.</p>
|
| 553 |
+
<div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
|
| 554 |
+
<div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
|
| 555 |
+
</div>
|
| 556 |
+
</div>
|
| 557 |
+
|
| 558 |
+
<style>
|
| 559 |
+
@keyframes spin {
|
| 560 |
+
0% { transform: rotate(0deg); }
|
| 561 |
+
100% { transform: rotate(360deg); }
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
@keyframes progress {
|
| 565 |
+
0% { transform: translateX(-100%); }
|
| 566 |
+
100% { transform: translateX(100%); }
|
| 567 |
+
}
|
| 568 |
+
</style>
|
| 569 |
+
"""
|
| 570 |
+
|
| 571 |
+
# ===== μλ¬ μ²λ¦¬ ν¨μ =====
|
| 572 |
+
def generate_error_response(error_message):
|
| 573 |
+
"""μλ¬ μλ΅ μμ±"""
|
| 574 |
+
return f'''
|
| 575 |
+
<div style="color: red; padding: 30px; text-align: center; width: 100%;
|
| 576 |
+
background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
|
| 577 |
+
<h3 style="margin-bottom: 15px;">β μ°κ²° μ€λ₯</h3>
|
| 578 |
+
<p style="margin-bottom: 20px;">{error_message}</p>
|
| 579 |
+
<div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
|
| 580 |
+
<h4>ν΄κ²° λ°©λ²:</h4>
|
| 581 |
+
<ul style="text-align: left; padding-left: 20px;">
|
| 582 |
+
<li>λ€νΈμν¬ μ°κ²°μ νμΈν΄μ£ΌμΈμ</li>
|
| 583 |
+
<li>μ격 μλ² μνλ₯Ό νμΈν΄μ£ΌμΈμ</li>
|
| 584 |
+
<li>μ μ ν λ€μ μλν΄μ£ΌμΈμ</li>
|
| 585 |
+
<li>λ¬Έμ κ° μ§μλλ©΄ κ΄λ¦¬μμκ² λ¬ΈμνμΈμ</li>
|
| 586 |
+
</ul>
|
| 587 |
+
</div>
|
| 588 |
+
</div>
|
| 589 |
+
'''
|
| 590 |
+
|
| 591 |
+
# ===== μ격 API νΈμΆ ν¨μλ€ =====
|
| 592 |
+
def call_collect_data_api(keyword):
|
| 593 |
+
"""1λ¨κ³: μν λ°μ΄ν° μμ§ API νΈμΆ"""
|
| 594 |
+
try:
|
| 595 |
+
client = get_api_client()
|
| 596 |
+
if not client:
|
| 597 |
+
return generate_error_response("API ν΄λΌμ΄μΈνΈλ₯Ό μ΄κΈ°νν μ μμ΅λλ€."), {}
|
| 598 |
+
|
| 599 |
+
logger.info("μ격 API νΈμΆ: μν λ°μ΄ν° μμ§")
|
| 600 |
+
result = client.predict(
|
| 601 |
+
keyword=keyword,
|
| 602 |
+
api_name="/on_collect_data"
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
+
logger.info(f"λ°μ΄ν° μμ§ API κ²°κ³Ό νμ
: {type(result)}")
|
| 606 |
+
|
| 607 |
+
# κ²°κ³Όκ° ννμΈ κ²½μ° μ²« λ²μ§Έ μμλ HTML, λ λ²μ§Έλ μΈμ
λ°μ΄ν°
|
| 608 |
+
if isinstance(result, tuple) and len(result) == 2:
|
| 609 |
+
html_result, session_data = result
|
| 610 |
+
|
| 611 |
+
# μΈμ
λ°μ΄ν°κ° μ λλ‘ μλμ§ νμΈ
|
| 612 |
+
if isinstance(session_data, dict):
|
| 613 |
+
logger.info(f"λ°μ΄ν° μμ§ μΈμ
λ°μ΄ν° μμ : {list(session_data.keys()) if session_data else 'λΉ λμ
λ리'}")
|
| 614 |
+
return html_result, session_data
|
| 615 |
+
else:
|
| 616 |
+
logger.warning("μΈμ
λ°μ΄ν°κ° λμ
λλ¦¬κ° μλλλ€.")
|
| 617 |
+
return html_result, {}
|
| 618 |
+
else:
|
| 619 |
+
logger.warning("μμκ³Ό λ€λ₯Έ λ°μ΄ν° μμ§ κ²°κ³Ό νν")
|
| 620 |
+
return str(result), {"keywords_collected": True}
|
| 621 |
+
|
| 622 |
+
except Exception as e:
|
| 623 |
+
logger.error(f"μν λ°μ΄ν° μμ§ API νΈμΆ μ€λ₯: {e}")
|
| 624 |
+
return generate_error_response(f"μ격 μλ² μ°κ²° μ€ν¨: {str(e)}"), {}
|
| 625 |
+
|
| 626 |
+
def call_analyze_keyword_api_enhanced(analysis_keyword, base_keyword, keywords_data):
|
| 627 |
+
"""3λ¨κ³: κ°νλ ν€μλ μ¬μΆ©λΆμ API νΈμΆ (λλ―Έ λ°μ΄ν° μ κ±°)"""
|
| 628 |
+
try:
|
| 629 |
+
client = get_api_client()
|
| 630 |
+
if not client:
|
| 631 |
+
return generate_error_response("API ν΄λΌμ΄μΈνΈλ₯Ό μ΄κΈ°νν μ μμ΅λλ€."), {}
|
| 632 |
+
|
| 633 |
+
logger.info("=== π κ°νλ ν€μλ μ¬μΆ©λΆμ API νΈμΆ (λλ―Έ λ°μ΄ν° μ κ±°) ===")
|
| 634 |
+
logger.info(f"νλΌλ―Έν° - analysis_keyword: '{analysis_keyword}'")
|
| 635 |
+
logger.info(f"νλΌλ―Έν° - base_keyword: '{base_keyword}'")
|
| 636 |
+
logger.info(f"νλΌλ―Έν° - keywords_data νμ
: {type(keywords_data)}")
|
| 637 |
+
|
| 638 |
+
# μ격 API νΈμΆ
|
| 639 |
+
result = client.predict(
|
| 640 |
+
analysis_keyword,
|
| 641 |
+
base_keyword,
|
| 642 |
+
keywords_data,
|
| 643 |
+
api_name="/on_analyze_keyword"
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
+
logger.info(f"π‘ μ격 API μλ΅ μμ :")
|
| 647 |
+
logger.info(f" - μλ΅ νμ
: {type(result)}")
|
| 648 |
+
logger.info(f" - μλ΅ κΈΈμ΄: {len(result) if hasattr(result, '__len__') else 'N/A'}")
|
| 649 |
+
|
| 650 |
+
# μλ΅ μ²λ¦¬ λ° Export λ°μ΄ν° ꡬ쑰 μμ±
|
| 651 |
+
if isinstance(result, tuple) and len(result) == 2:
|
| 652 |
+
html_result, remote_export_data = result
|
| 653 |
+
|
| 654 |
+
logger.info(f"π μ격 export λ°μ΄ν°:")
|
| 655 |
+
logger.info(f" - νμ
: {type(remote_export_data)}")
|
| 656 |
+
logger.info(f" - ν€λ€: {list(remote_export_data.keys()) if isinstance(remote_export_data, dict) else 'None'}")
|
| 657 |
+
|
| 658 |
+
# HTML κ²°κ³Όκ° μμΌλ©΄ Export λ°μ΄ν° ꡬ쑰 μμ± (λλ―Έ λ°μ΄ν° μμ΄)
|
| 659 |
+
if html_result:
|
| 660 |
+
logger.info("π§ Export λ°μ΄ν° ꡬ쑰 μμ± μμ (λλ―Έ λ°μ΄ν° μ κ±°)")
|
| 661 |
+
enhanced_export_data = create_export_data_from_html(
|
| 662 |
+
analysis_keyword=analysis_keyword,
|
| 663 |
+
main_keyword=base_keyword,
|
| 664 |
+
analysis_html=html_result,
|
| 665 |
+
step1_data=keywords_data
|
| 666 |
+
)
|
| 667 |
+
|
| 668 |
+
# μ격μμ μ¨ μ€μ λ°μ΄ν°κ° μμΌλ©΄ λ³ν©
|
| 669 |
+
if isinstance(remote_export_data, dict) and remote_export_data:
|
| 670 |
+
logger.info("π μ격 μ€μ λ°μ΄ν°μ λ‘컬 λ°μ΄ν° λ³ν©")
|
| 671 |
+
for key, value in remote_export_data.items():
|
| 672 |
+
if value is not None and key in ["main_keywords_df", "related_keywords_df"]:
|
| 673 |
+
# DataFrame λ°μ΄ν°λ§ κ²μ¦νμ¬ λ³ν©
|
| 674 |
+
if isinstance(value, dict) and value: # λΉ λμ
λλ¦¬κ° μλ κ²½μ°λ§
|
| 675 |
+
enhanced_export_data[key] = value
|
| 676 |
+
logger.info(f" - {key} μ격 μ€μ λ°μ΄ν°λ‘ μ
λ°μ΄νΈ")
|
| 677 |
+
elif hasattr(value, 'shape') and not value.empty: # DataFrameμ΄κ³ λΉμ΄μμ§ μμ κ²½μ°
|
| 678 |
+
enhanced_export_data[key] = value
|
| 679 |
+
logger.info(f" - {key} μ격 DataFrame λ°μ΄ν°λ‘ μ
λ°μ΄νΈ")
|
| 680 |
+
elif value is not None and key not in ["main_keywords_df", "related_keywords_df"]:
|
| 681 |
+
enhanced_export_data[key] = value
|
| 682 |
+
logger.info(f" - {key} μ격 λ°μ΄ν°λ‘ μ
λ°μ΄νΈ")
|
| 683 |
+
|
| 684 |
+
logger.info(f"β
μ΅μ’
Export λ°μ΄ν° ꡬ쑰 (λλ―Έ λ°μ΄ν° μμ):")
|
| 685 |
+
logger.info(f" - ν€ κ°μ: {len(enhanced_export_data)}")
|
| 686 |
+
logger.info(f" - ν€ λͺ©λ‘: {list(enhanced_export_data.keys())}")
|
| 687 |
+
|
| 688 |
+
return html_result, enhanced_export_data
|
| 689 |
+
else:
|
| 690 |
+
logger.warning("β οΈ HTML κ²°κ³Όκ° λΉμ΄μμ")
|
| 691 |
+
return str(result), {}
|
| 692 |
+
else:
|
| 693 |
+
logger.warning("β οΈ μμκ³Ό λ€λ₯Έ API μλ΅ νν")
|
| 694 |
+
# HTMLλ§ λ°νλ κ²½μ°λ μ²λ¦¬
|
| 695 |
+
if isinstance(result, str) and len(result) > 100: # HTMLμΌ κ°λ₯μ±μ΄ λμ
|
| 696 |
+
logger.info("π HTML λ¬Έμμ΄λ‘ μΆμ λλ μλ΅ - Export λ°μ΄ν° μμ± (λλ―Έ λ°μ΄ν° μμ΄)")
|
| 697 |
+
enhanced_export_data = create_export_data_from_html(
|
| 698 |
+
analysis_keyword=analysis_keyword,
|
| 699 |
+
main_keyword=base_keyword,
|
| 700 |
+
analysis_html=result,
|
| 701 |
+
step1_data=keywords_data
|
| 702 |
+
)
|
| 703 |
+
return result, enhanced_export_data
|
| 704 |
+
else:
|
| 705 |
+
return str(result), {}
|
| 706 |
+
|
| 707 |
+
except Exception as e:
|
| 708 |
+
logger.error(f"β ν€μλ μ¬μΆ©λΆμ API νΈμΆ μ€λ₯: {e}")
|
| 709 |
+
import traceback
|
| 710 |
+
logger.error(f"μ€ν νΈλ μ΄μ€:\n{traceback.format_exc()}")
|
| 711 |
+
return generate_error_response(f"μ격 μλ² μ°κ²° μ€ν¨: {str(e)}"), {}
|
| 712 |
+
|
| 713 |
+
# ===== κ·ΈλΌλμ€ μΈν°νμ΄μ€ =====
|
| 714 |
+
def create_interface():
|
| 715 |
+
# CSS μ€νμΌλ§ (κΈ°μ‘΄κ³Ό λμΌ)
|
| 716 |
+
custom_css = """
|
| 717 |
+
/* κΈ°μ‘΄ λ€ν¬λͺ¨λ μλ λ³κ²½ AI μν μμ± λΆμ μμ€ν
CSS */
|
| 718 |
+
:root {
|
| 719 |
+
--primary-color: #FB7F0D;
|
| 720 |
+
--secondary-color: #ff9a8b;
|
| 721 |
+
--accent-color: #FF6B6B;
|
| 722 |
+
--background-color: #FFFFFF;
|
| 723 |
+
--card-bg: #ffffff;
|
| 724 |
+
--input-bg: #ffffff;
|
| 725 |
+
--text-color: #334155;
|
| 726 |
+
--text-secondary: #64748b;
|
| 727 |
+
--border-color: #dddddd;
|
| 728 |
+
--border-light: #e5e5e5;
|
| 729 |
+
--table-even-bg: #f3f3f3;
|
| 730 |
+
--table-hover-bg: #f0f0f0;
|
| 731 |
+
--shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
|
| 732 |
+
--shadow-light: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 733 |
+
--border-radius: 18px;
|
| 734 |
+
}
|
| 735 |
+
@media (prefers-color-scheme: dark) {
|
| 736 |
+
:root {
|
| 737 |
+
--background-color: #1a1a1a;
|
| 738 |
+
--card-bg: #2d2d2d;
|
| 739 |
+
--input-bg: #2d2d2d;
|
| 740 |
+
--text-color: #e5e5e5;
|
| 741 |
+
--text-secondary: #a1a1aa;
|
| 742 |
+
--border-color: #404040;
|
| 743 |
+
--border-light: #525252;
|
| 744 |
+
--table-even-bg: #333333;
|
| 745 |
+
--table-hover-bg: #404040;
|
| 746 |
+
--shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
|
| 747 |
+
--shadow-light: 0 2px 4px rgba(0, 0, 0, 0.2);
|
| 748 |
+
}
|
| 749 |
+
}
|
| 750 |
+
body {
|
| 751 |
+
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 752 |
+
background-color: var(--background-color) !important;
|
| 753 |
+
color: var(--text-color) !important;
|
| 754 |
+
line-height: 1.6;
|
| 755 |
+
margin: 0;
|
| 756 |
+
padding: 0;
|
| 757 |
+
transition: background-color 0.3s ease, color 0.3s ease;
|
| 758 |
+
}
|
| 759 |
+
.gradio-container {
|
| 760 |
+
width: 100%;
|
| 761 |
+
margin: 0 auto;
|
| 762 |
+
padding: 20px;
|
| 763 |
+
background-color: var(--background-color) !important;
|
| 764 |
+
}
|
| 765 |
+
.custom-frame {
|
| 766 |
+
background-color: var(--card-bg) !important;
|
| 767 |
+
border: 1px solid var(--border-light) !important;
|
| 768 |
+
border-radius: var(--border-radius);
|
| 769 |
+
padding: 20px;
|
| 770 |
+
margin: 10px 0;
|
| 771 |
+
box-shadow: var(--shadow) !important;
|
| 772 |
+
color: var(--text-color) !important;
|
| 773 |
+
}
|
| 774 |
+
.custom-button {
|
| 775 |
+
border-radius: 30px !important;
|
| 776 |
+
background: var(--primary-color) !important;
|
| 777 |
+
color: white !important;
|
| 778 |
+
font-size: 18px !important;
|
| 779 |
+
padding: 10px 20px !important;
|
| 780 |
+
border: none;
|
| 781 |
+
box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25);
|
| 782 |
+
transition: transform 0.3s ease;
|
| 783 |
+
height: 45px !important;
|
| 784 |
+
width: 100% !important;
|
| 785 |
+
}
|
| 786 |
+
.custom-button:hover {
|
| 787 |
+
transform: translateY(-2px);
|
| 788 |
+
box-shadow: 0 6px 12px rgba(251, 127, 13, 0.3);
|
| 789 |
+
}
|
| 790 |
+
.export-button {
|
| 791 |
+
background: linear-gradient(135deg, #28a745, #20c997) !important;
|
| 792 |
+
color: white !important;
|
| 793 |
+
border-radius: 25px !important;
|
| 794 |
+
height: 50px !important;
|
| 795 |
+
font-size: 17px !important;
|
| 796 |
+
font-weight: bold !important;
|
| 797 |
+
width: 100% !important;
|
| 798 |
+
margin-top: 20px !important;
|
| 799 |
+
}
|
| 800 |
+
.section-title {
|
| 801 |
+
display: flex;
|
| 802 |
+
align-items: center;
|
| 803 |
+
font-size: 20px;
|
| 804 |
+
font-weight: 700;
|
| 805 |
+
color: var(--text-color) !important;
|
| 806 |
+
margin-bottom: 10px;
|
| 807 |
+
padding-bottom: 5px;
|
| 808 |
+
border-bottom: 2px solid var(--primary-color);
|
| 809 |
+
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 810 |
+
}
|
| 811 |
+
.section-title img, .section-title i {
|
| 812 |
+
margin-right: 10px;
|
| 813 |
+
font-size: 20px;
|
| 814 |
+
color: var(--primary-color);
|
| 815 |
+
}
|
| 816 |
+
.gr-input, .gr-text-input, .gr-sample-inputs,
|
| 817 |
+
input[type="text"], input[type="number"], textarea, select {
|
| 818 |
+
border-radius: var(--border-radius) !important;
|
| 819 |
+
border: 1px solid var(--border-color) !important;
|
| 820 |
+
padding: 12px !important;
|
| 821 |
+
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important;
|
| 822 |
+
transition: all 0.3s ease !important;
|
| 823 |
+
background-color: var(--input-bg) !important;
|
| 824 |
+
color: var(--text-color) !important;
|
| 825 |
+
}
|
| 826 |
+
.gr-input:focus, .gr-text-input:focus,
|
| 827 |
+
input[type="text"]:focus, textarea:focus, select:focus {
|
| 828 |
+
border-color: var(--primary-color) !important;
|
| 829 |
+
outline: none !important;
|
| 830 |
+
box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important;
|
| 831 |
+
}
|
| 832 |
+
.fade-in {
|
| 833 |
+
animation: fadeIn 0.5s ease-out;
|
| 834 |
+
}
|
| 835 |
+
@keyframes fadeIn {
|
| 836 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 837 |
+
to { opacity: 1; transform: translateY(0); }
|
| 838 |
+
}
|
| 839 |
+
"""
|
| 840 |
+
|
| 841 |
+
with gr.Blocks(
|
| 842 |
+
css=custom_css,
|
| 843 |
+
title="π AI μν μμ± λΆμκΈ° v3.2 (λλ―Έ λ°μ΄ν° μ κ±°)",
|
| 844 |
+
theme=gr.themes.Default(primary_hue="orange", secondary_hue="orange")
|
| 845 |
+
) as interface:
|
| 846 |
+
|
| 847 |
+
# ν°νΈ λ° μμ΄μ½ λ‘λ
|
| 848 |
+
gr.HTML("""
|
| 849 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 850 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
|
| 851 |
+
""")
|
| 852 |
+
|
| 853 |
+
# μΈμ
λ³ μν λ³μ
|
| 854 |
+
keywords_data_state = gr.State()
|
| 855 |
+
export_data_state = gr.State({})
|
| 856 |
+
|
| 857 |
+
# === UI μ»΄ν¬λνΈλ€ ===
|
| 858 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 859 |
+
gr.HTML('<div class="section-title"><i class="fas fa-search"></i> 1λ¨κ³: λ©μΈ ν€μλ μ
λ ₯</div>')
|
| 860 |
+
|
| 861 |
+
keyword_input = gr.Textbox(
|
| 862 |
+
label="μν λ©μΈν€μλ",
|
| 863 |
+
placeholder="μ: μ¬λ¦¬νΌ, 무μ μ΄μ΄ν°, νΈλν¬λ¦Ό",
|
| 864 |
+
value="",
|
| 865 |
+
elem_id="keyword_input"
|
| 866 |
+
)
|
| 867 |
+
|
| 868 |
+
collect_data_btn = gr.Button("1λ¨κ³: μν λ°μ΄ν° μμ§νκΈ°", elem_classes="custom-button", size="lg")
|
| 869 |
+
|
| 870 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 871 |
+
gr.HTML('<div class="section-title"><i class="fas fa-database"></i> 2λ¨κ³: μμ§λ ν€μλ λͺ©λ‘</div>')
|
| 872 |
+
keywords_result = gr.HTML()
|
| 873 |
+
|
| 874 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 875 |
+
gr.HTML('<div class="section-title"><i class="fas fa-bullseye"></i> 3λ¨κ³: λΆμν ν€μλ μ ν</div>')
|
| 876 |
+
|
| 877 |
+
analysis_keyword_input = gr.Textbox(
|
| 878 |
+
label="λΆμν ν€μλ",
|
| 879 |
+
placeholder="μ λͺ©λ‘μμ μνλ ν€μλλ₯Ό μ
λ ₯νμΈμ (μ: ν΅κ΅½ μ¬λ¦¬νΌ)",
|
| 880 |
+
value="",
|
| 881 |
+
elem_id="analysis_keyword_input"
|
| 882 |
+
)
|
| 883 |
+
|
| 884 |
+
analyze_keyword_btn = gr.Button("ν€μλ μ¬μΆ©λΆμ νκΈ°", elem_classes="custom-button", size="lg")
|
| 885 |
+
|
| 886 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 887 |
+
gr.HTML('<div class="section-title"><i class="fas fa-chart-line"></i> ν€μλ μ¬μΆ©λΆμ</div>')
|
| 888 |
+
analysis_result = gr.HTML(label="ν€μλ μ¬μΆ©λΆμ")
|
| 889 |
+
|
| 890 |
+
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 891 |
+
gr.HTML('<div class="section-title"><i class="fas fa-download"></i> λΆμ κ²°κ³Ό μΆλ ₯</div>')
|
| 892 |
+
|
| 893 |
+
gr.HTML("""
|
| 894 |
+
<div style="background: #e3f2fd; border-left: 4px solid #2196f3; padding: 15px; margin: 10px 0; border-radius: 5px;">
|
| 895 |
+
<h4 style="margin: 0 0 10px 0; color: #1976d2;"><i class="fas fa-info-circle"></i> μ€μ λ°μ΄ν° μΆλ ₯ λ²μ </h4>
|
| 896 |
+
<p style="margin: 0; color: #1976d2; font-size: 14px;">
|
| 897 |
+
β’ λΆμλ λ°μ΄ν°λ₯Ό νμΌλ‘ μΆλ ₯λ©λλ€<br>
|
| 898 |
+
</p>
|
| 899 |
+
</div>
|
| 900 |
+
""")
|
| 901 |
+
|
| 902 |
+
export_btn = gr.Button("π λΆμκ²°κ³Ό μΆλ ₯νκΈ°", elem_classes="export-button", size="lg")
|
| 903 |
+
export_result = gr.HTML()
|
| 904 |
+
download_file = gr.File(label="λ€μ΄λ‘λ", visible=False)
|
| 905 |
+
|
| 906 |
+
# ===== μ΄λ²€νΈ νΈλ€λ¬ =====
|
| 907 |
+
def on_collect_data(keyword):
|
| 908 |
+
if not keyword.strip():
|
| 909 |
+
return ("<div style='color: red; padding: 20px; text-align: center; width: 100%;'>ν€μλλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.</div>", None)
|
| 910 |
+
|
| 911 |
+
# λ‘λ© μν νμ
|
| 912 |
+
yield (create_loading_animation(), None)
|
| 913 |
+
|
| 914 |
+
# μ격 API νΈμΆ
|
| 915 |
+
result_html, result_data = call_collect_data_api(keyword)
|
| 916 |
+
|
| 917 |
+
yield (result_html, result_data)
|
| 918 |
+
|
| 919 |
+
def on_analyze_keyword(analysis_keyword, base_keyword, keywords_data):
|
| 920 |
+
if not analysis_keyword.strip():
|
| 921 |
+
return "<div style='color: red; padding: 20px; text-align: center; width: 100%;'>λΆμν ν€μλλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.</div>", {}
|
| 922 |
+
|
| 923 |
+
# λ‘λ© μν νμ
|
| 924 |
+
yield create_loading_animation(), {}
|
| 925 |
+
|
| 926 |
+
# κ°νλ API νΈμΆ (λλ―Έ λ°μ΄ν° μ κ±°)
|
| 927 |
+
html_result, enhanced_export_data = call_analyze_keyword_api_enhanced(
|
| 928 |
+
analysis_keyword, base_keyword, keywords_data
|
| 929 |
+
)
|
| 930 |
+
|
| 931 |
+
yield html_result, enhanced_export_data
|
| 932 |
+
|
| 933 |
+
def on_export_results(export_data):
|
| 934 |
+
"""κ°νλ λΆμ κ²°κ³Ό μΆλ ₯ νΈλ€λ¬ (λλ―Έ λ°μ΄ν° μ κ±°)"""
|
| 935 |
+
try:
|
| 936 |
+
logger.info(f"π μ
λ ₯ export_data: {type(export_data)}")
|
| 937 |
+
if isinstance(export_data, dict):
|
| 938 |
+
logger.info(f"π export_data ν€λ€: {list(export_data.keys())}")
|
| 939 |
+
|
| 940 |
+
# κ°νλ μΆλ ₯ ν¨μ νΈμΆ (λλ―Έ λ°μ΄ν° μ κ±°)
|
| 941 |
+
zip_path, message = export_analysis_results_enhanced(export_data)
|
| 942 |
+
|
| 943 |
+
if zip_path:
|
| 944 |
+
success_html = f"""
|
| 945 |
+
<div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 946 |
+
<h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> μΆλ ₯ μλ£!</h4>
|
| 947 |
+
<p style="color: #155724; margin: 0; line-height: 1.6;">
|
| 948 |
+
{message}<br>
|
| 949 |
+
<strong>λ°μ΄ν°μΆλ ₯:</strong><br>
|
| 950 |
+
<br>
|
| 951 |
+
<i class="fas fa-download"></i> μλ λ€μ΄λ‘λ λ²νΌμ ν΄λ¦νμ¬ νμΌμ μ μ₯νμΈμ.<br>
|
| 952 |
+
<small style="color: #666;">β° νκ΅μκ° κΈ°μ€μΌλ‘ νμΌλͺ
μ΄ μμ±λ©λλ€.</small>
|
| 953 |
+
</p>
|
| 954 |
+
</div>
|
| 955 |
+
"""
|
| 956 |
+
return success_html, gr.update(value=zip_path, visible=True)
|
| 957 |
+
else:
|
| 958 |
+
error_html = f"""
|
| 959 |
+
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 960 |
+
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> μΆλ ₯ μ€ν¨</h4>
|
| 961 |
+
<p style="color: #721c24; margin: 0;">{message}</p>
|
| 962 |
+
<div style="margin-top: 15px; padding: 15px; background: white; border-radius: 5px;">
|
| 963 |
+
<h5 style="color: #721c24; margin: 0 0 10px 0;">π λλ²κΉ
μ 보:</h5>
|
| 964 |
+
<ul style="color: #721c24; margin: 0; padding-left: 20px;">
|
| 965 |
+
<li>Export λ°μ΄ν° νμ
: {type(export_data)}</li>
|
| 966 |
+
<li>Export λ°μ΄ν° μ ν¨μ±: {'μ ν¨' if export_data else '무ν¨'}</li>
|
| 967 |
+
<li>ν€μλ μ¬μΆ©λΆμ μν: {'μλ£' if export_data.get('analysis_completed') else 'λ―Έμλ£'}</li>
|
| 968 |
+
</ul>
|
| 969 |
+
</div>
|
| 970 |
+
</div>
|
| 971 |
+
"""
|
| 972 |
+
logger.error("β κ°νλ μΆλ ₯ μ€ν¨")
|
| 973 |
+
return error_html, gr.update(visible=False)
|
| 974 |
+
|
| 975 |
+
except Exception as e:
|
| 976 |
+
logger.error(f"β κ°νλ μΆλ ₯ νΈλ€λ¬ μ€λ₯: {e}")
|
| 977 |
+
import traceback
|
| 978 |
+
logger.error(f"μ€ν νΈλ μ΄μ€:\n{traceback.format_exc()}")
|
| 979 |
+
|
| 980 |
+
error_html = f"""
|
| 981 |
+
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 982 |
+
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> μμ€ν
μ€λ₯</h4>
|
| 983 |
+
<p style="color: #721c24; margin: 0;">κ°νλ μΆλ ₯ μ€ μμ€ν
μ€λ₯κ° λ°μνμ΅λλ€:</p>
|
| 984 |
+
<code style="display: block; margin: 10px 0; padding: 10px; background: #f8f9fa; border-radius: 3px; color: #721c24;">
|
| 985 |
+
{type(e).__name__}: {str(e)}
|
| 986 |
+
</code>
|
| 987 |
+
<div style="margin-top: 15px; padding: 10px; background: #fff3cd; border-radius: 5px;">
|
| 988 |
+
<p style="margin: 0; color: #856404; font-size: 14px;">
|
| 989 |
+
π‘ μ€μ λΆμ κ²°κ³Όκ° μμ΄μΌλ§ νμΌμ΄ μμ±λ©λλ€.
|
| 990 |
+
</p>
|
| 991 |
+
</div>
|
| 992 |
+
</div>
|
| 993 |
+
"""
|
| 994 |
+
return error_html, gr.update(visible=False)
|
| 995 |
+
|
| 996 |
+
# ===== μ΄λ²€νΈ μ°κ²° =====
|
| 997 |
+
collect_data_btn.click(
|
| 998 |
+
fn=on_collect_data,
|
| 999 |
+
inputs=[keyword_input],
|
| 1000 |
+
outputs=[keywords_result, keywords_data_state],
|
| 1001 |
+
api_name="on_collect_data"
|
| 1002 |
+
)
|
| 1003 |
+
|
| 1004 |
+
analyze_keyword_btn.click(
|
| 1005 |
+
fn=on_analyze_keyword,
|
| 1006 |
+
inputs=[analysis_keyword_input, keyword_input, keywords_data_state],
|
| 1007 |
+
outputs=[analysis_result, export_data_state],
|
| 1008 |
+
api_name="on_analyze_keyword"
|
| 1009 |
+
)
|
| 1010 |
+
|
| 1011 |
+
export_btn.click(
|
| 1012 |
+
fn=on_export_results,
|
| 1013 |
+
inputs=[export_data_state],
|
| 1014 |
+
outputs=[export_result, download_file],
|
| 1015 |
+
api_name="on_export_results"
|
| 1016 |
+
)
|
| 1017 |
+
|
| 1018 |
+
return interface
|
| 1019 |
+
|
| 1020 |
+
# ===== λ©μΈ μ€ν =====
|
| 1021 |
+
if __name__ == "__main__":
|
| 1022 |
+
# pytz λͺ¨λ μ€μΉ νμΈ
|
| 1023 |
+
try:
|
| 1024 |
+
import pytz
|
| 1025 |
+
logger.info("β
pytz λͺ¨λ λ‘λ μ±κ³΅ - νκ΅μκ° μ§μ")
|
| 1026 |
+
except ImportError:
|
| 1027 |
+
logger.info("μμ€ν
μκ°μ μ¬μ©ν©λλ€.")
|
| 1028 |
+
|
| 1029 |
+
# μ± μ€ν
|
| 1030 |
+
app = create_interface()
|
| 1031 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|