ssboost commited on
Commit
99f1333
ยท
verified ยท
1 Parent(s): a1d7f38

Upload 11 files

Browse files
Files changed (10) hide show
  1. api_utils.py +253 -0
  2. app.py +351 -173
  3. export_utils.py +478 -0
  4. keyword_analysis.py +1773 -0
  5. keyword_analysis_report.css +422 -0
  6. keyword_diversity_fix.py +943 -0
  7. keyword_search.py +194 -0
  8. style.css +264 -43
  9. text_utils.py +227 -0
  10. trend_analysis_v2.py +1128 -0
api_utils.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ API ๊ด€๋ จ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ ๋ชจ์Œ (ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ฒ„์ „)
3
+ - API ํ‚ค ๊ด€๋ฆฌ (ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๋กœ๋“œ)
4
+ - ์‹œ๊ทธ๋‹ˆ์ฒ˜ ์ƒ์„ฑ
5
+ - API ํ—ค๋” ์ƒ์„ฑ
6
+ - Gemini API ํ‚ค ๋žœ๋ค ๋กœํ…Œ์ด์…˜ ์ถ”๊ฐ€
7
+ """
8
+
9
+ import os
10
+ import time
11
+ import hmac
12
+ import hashlib
13
+ import base64
14
+ import requests
15
+ import threading
16
+ import random
17
+ import google.generativeai as genai
18
+ import logging
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+ # ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ์„ค์ • ๋กœ๋“œ
23
+ def get_api_configs():
24
+ # ํ™˜๊ฒฝ๋ณ€์ˆ˜ 'API_CONFIGS'์—์„œ ์ „์ฒด ์„ค์ •์„ ๊ฐ€์ ธ์˜ด
25
+ api_configs_str = os.getenv('API_CONFIGS', '')
26
+
27
+ if not api_configs_str:
28
+ logger.error("API_CONFIGS ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
29
+ return [], [], [], []
30
+
31
+ try:
32
+ # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๊ฐ’์„ exec๋กœ ์‹คํ–‰ํ•˜์—ฌ ์„ค์ • ๋กœ๋“œ
33
+ local_vars = {}
34
+ exec(api_configs_str, {}, local_vars)
35
+
36
+ return (
37
+ local_vars.get('NAVER_API_CONFIGS', []),
38
+ local_vars.get('NAVER_SHOPPING_CONFIGS', []),
39
+ local_vars.get('NAVER_DATALAB_CONFIGS', []),
40
+ local_vars.get('GEMINI_API_CONFIGS', [])
41
+ )
42
+ except Exception as e:
43
+ logger.error(f"ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {e}")
44
+ return [], [], [], []
45
+
46
+ # API ์„ค์ • ๋กœ๋“œ
47
+ NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS = get_api_configs()
48
+
49
+ # ์ˆœ์ฐจ ์‚ฌ์šฉ์„ ์œ„ํ•œ ์ธ๋ฑ์Šค์™€ ๋ฝ
50
+ current_api_index = 0
51
+ current_shopping_api_index = 0
52
+ current_datalab_api_index = 0
53
+ current_gemini_api_index = 0
54
+ api_lock = threading.Lock()
55
+ shopping_lock = threading.Lock()
56
+ datalab_lock = threading.Lock()
57
+ gemini_lock = threading.Lock()
58
+
59
+ # Gemini ๋ชจ๋ธ ์บ์‹œ
60
+ _gemini_models = {}
61
+
62
+ # API ์„ค์ • ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ ์ถ”๊ฐ€
63
+ def initialize_api_configs():
64
+ """API ์„ค์ •์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๋žœ๋คํ•˜๊ฒŒ ์ •๋ ฌ"""
65
+ global NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS
66
+
67
+ # API ์„ค์ •์„ ๋‹ค์‹œ ๋กœ๋“œ
68
+ NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS = get_api_configs()
69
+
70
+ # API ์„ค์ •์„ ๋žœ๋คํ•˜๊ฒŒ ์„ž๊ธฐ
71
+ if NAVER_API_CONFIGS:
72
+ random.shuffle(NAVER_API_CONFIGS)
73
+ if NAVER_SHOPPING_CONFIGS:
74
+ random.shuffle(NAVER_SHOPPING_CONFIGS)
75
+ if NAVER_DATALAB_CONFIGS:
76
+ random.shuffle(NAVER_DATALAB_CONFIGS)
77
+ if GEMINI_API_CONFIGS:
78
+ random.shuffle(GEMINI_API_CONFIGS)
79
+
80
+ print(f"API ์„ค์ • ์ดˆ๊ธฐํ™” ์™„๋ฃŒ:")
81
+ print(f" - ๊ฒ€์ƒ‰๊ด‘๊ณ  API: {len(NAVER_API_CONFIGS)}๊ฐœ")
82
+ print(f" - ์‡ผํ•‘ API: {len(NAVER_SHOPPING_CONFIGS)}๊ฐœ")
83
+ print(f" - ๋ฐ์ดํ„ฐ๋žฉ API: {len(NAVER_DATALAB_CONFIGS)}๊ฐœ")
84
+ print(f" - Gemini API: {len(GEMINI_API_CONFIGS)}๊ฐœ")
85
+
86
+
87
+ def generate_signature(timestamp, method, uri, secret_key):
88
+ """์‹œ๊ทธ๋‹ˆ์ฒ˜ ์ƒ์„ฑ ํ•จ์ˆ˜"""
89
+ message = f"{timestamp}.{method}.{uri}"
90
+ digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest()
91
+ return base64.b64encode(digest).decode()
92
+
93
+ def get_header(method, uri, api_key, secret_key, customer_id):
94
+ """API ํ—ค๋” ์ƒ์„ฑ ํ•จ์ˆ˜"""
95
+ timestamp = str(round(time.time() * 1000))
96
+ signature = generate_signature(timestamp, method, uri, secret_key)
97
+ return {
98
+ "Content-Type": "application/json; charset=UTF-8",
99
+ "X-Timestamp": timestamp,
100
+ "X-API-KEY": api_key,
101
+ "X-Customer": str(customer_id),
102
+ "X-Signature": signature
103
+ }
104
+
105
+ def get_next_api_config():
106
+ """์ˆœ์ฐจ์ ์œผ๋กœ ๋‹ค์Œ API ์„ค์ •์„ ๋ฐ˜ํ™˜ (์Šค๋ ˆ๋“œ ์•ˆ์ „)"""
107
+ global current_api_index
108
+
109
+ if not NAVER_API_CONFIGS:
110
+ logger.error("๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰๊ด‘๊ณ  API ์„ค์ •์ด ์—†์Šต๋‹ˆ๋‹ค.")
111
+ return None
112
+
113
+ with api_lock:
114
+ config = NAVER_API_CONFIGS[current_api_index]
115
+ current_api_index = (current_api_index + 1) % len(NAVER_API_CONFIGS)
116
+ return config
117
+
118
+ def get_next_shopping_api_config():
119
+ """์ˆœ์ฐจ์ ์œผ๋กœ ๋‹ค์Œ ์‡ผํ•‘ API ์„ค์ •์„ ๋ฐ˜ํ™˜ (์˜ค๋ฅ˜ ํ‚ค ๊ฑด๋„ˆ๋›ฐ๊ธฐ ์ถ”๊ฐ€)"""
120
+ global current_shopping_api_index
121
+
122
+ if not NAVER_SHOPPING_CONFIGS:
123
+ logger.error("๋„ค์ด๋ฒ„ ์‡ผํ•‘ API ์„ค์ •์ด ์—†์Šต๋‹ˆ๋‹ค.")
124
+ return None
125
+
126
+ with shopping_lock:
127
+ # ์ตœ๋Œ€ ์ „์ฒด ํ‚ค ์ˆ˜๋งŒํผ ์‹œ๋„ (๋ฌดํ•œ ๋ฃจํ”„ ๋ฐฉ์ง€)
128
+ for _ in range(len(NAVER_SHOPPING_CONFIGS)):
129
+ config = NAVER_SHOPPING_CONFIGS[current_shopping_api_index]
130
+ current_shopping_api_index = (current_shopping_api_index + 1) % len(NAVER_SHOPPING_CONFIGS)
131
+
132
+ # ๊ธฐ๋ณธ๊ฐ’ ์ฒดํฌ
133
+ if config["CLIENT_ID"] and not config["CLIENT_ID"].startswith("YOUR_"):
134
+ return config
135
+
136
+ # ๋ชจ๋“  ํ‚ค๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์ธ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ํ‚ค ๋ฐ˜ํ™˜
137
+ return NAVER_SHOPPING_CONFIGS[0] if NAVER_SHOPPING_CONFIGS else None
138
+
139
+ def get_next_datalab_api_config():
140
+ """์ˆœ์ฐจ์ ์œผ๋กœ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •์„ ๋ฐ˜ํ™˜ (์Šค๋ ˆ๋“œ ์•ˆ์ „)"""
141
+ global current_datalab_api_index
142
+
143
+ if not NAVER_DATALAB_CONFIGS:
144
+ logger.error("๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •์ด ์—†์Šต๋‹ˆ๋‹ค.")
145
+ return None
146
+
147
+ with datalab_lock:
148
+ # API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์œผ๋ฉด None ๋ฐ˜ํ™˜
149
+ if not NAVER_DATALAB_CONFIGS[0]["CLIENT_ID"] or NAVER_DATALAB_CONFIGS[0]["CLIENT_ID"].startswith("YOUR_"):
150
+ return None
151
+
152
+ config = NAVER_DATALAB_CONFIGS[current_datalab_api_index]
153
+ current_datalab_api_index = (current_datalab_api_index + 1) % len(NAVER_DATALAB_CONFIGS)
154
+ return config
155
+
156
+ def get_next_gemini_api_key():
157
+ """์ˆœ์ฐจ์ ์œผ๋กœ ๋‹ค์Œ Gemini API ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ (์Šค๋ ˆ๋“œ ์•ˆ์ „)"""
158
+ global current_gemini_api_index
159
+
160
+ if not GEMINI_API_CONFIGS:
161
+ logger.warning("์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Gemini API ํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
162
+ return None
163
+
164
+ with gemini_lock:
165
+ # ์ตœ๋Œ€ ์ „์ฒด ํ‚ค ์ˆ˜๋งŒํผ ์‹œ๋„ (๋ฌดํ•œ ๋ฃจํ”„ ๋ฐฉ์ง€)
166
+ for _ in range(len(GEMINI_API_CONFIGS)):
167
+ api_key = GEMINI_API_CONFIGS[current_gemini_api_index]
168
+ current_gemini_api_index = (current_gemini_api_index + 1) % len(GEMINI_API_CONFIGS)
169
+
170
+ # ๊ธฐ๋ณธ๊ฐ’์ด ์•„๋‹Œ ํ‚ค๋งŒ ๋ฐ˜ํ™˜
171
+ if api_key and not api_key.startswith("YOUR_") and api_key.strip():
172
+ return api_key
173
+
174
+ # ๋ชจ๋“  ํ‚ค๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์ธ ๊ฒฝ์šฐ None ๋ฐ˜ํ™˜
175
+ logger.warning("์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Gemini API ํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
176
+ return None
177
+
178
+ def get_gemini_model():
179
+ """์บ์‹œ๋œ Gemini ๋ชจ๋ธ์„ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ ์ƒ์„ฑ"""
180
+ api_key = get_next_gemini_api_key()
181
+
182
+ if not api_key:
183
+ logger.error("Gemini API ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
184
+ return None
185
+
186
+ # ์บ์‹œ์—์„œ ๋ชจ๋ธ ํ™•์ธ
187
+ if api_key in _gemini_models:
188
+ return _gemini_models[api_key]
189
+
190
+ try:
191
+ # ์ƒˆ ๋ชจ๋ธ ์ƒ์„ฑ
192
+ genai.configure(api_key=api_key)
193
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
194
+
195
+ # ์บ์‹œ์— ์ €์žฅ
196
+ _gemini_models[api_key] = model
197
+
198
+ logger.info(f"Gemini ๋ชจ๋ธ ์ƒ์„ฑ ์„ฑ๊ณต: {api_key[:8]}***{api_key[-4:]}")
199
+ return model
200
+
201
+ except Exception as e:
202
+ logger.error(f"Gemini ๋ชจ๋ธ ์ƒ์„ฑ ์‹คํŒจ ({api_key[:8]}***): {e}")
203
+ return None
204
+
205
+ def validate_api_config(api_config):
206
+ """API ์„ค์ • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ"""
207
+ if not api_config:
208
+ return False, "API ์„ค์ •์ด ์—†์Šต๋‹ˆ๋‹ค."
209
+
210
+ API_KEY = api_config.get("API_KEY", "")
211
+ SECRET_KEY = api_config.get("SECRET_KEY", "")
212
+ CUSTOMER_ID_STR = api_config.get("CUSTOMER_ID", "")
213
+
214
+ if not all([API_KEY, SECRET_KEY, CUSTOMER_ID_STR]):
215
+ return False, "API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
216
+
217
+ if CUSTOMER_ID_STR.startswith("YOUR_") or API_KEY.startswith("YOUR_"):
218
+ return False, "API ํ‚ค๊ฐ€ ํ”Œ๋ ˆ์ด์Šคํ™€๋”์ž…๋‹ˆ๋‹ค."
219
+
220
+ try:
221
+ CUSTOMER_ID = int(CUSTOMER_ID_STR)
222
+ except ValueError:
223
+ return False, f"CUSTOMER_ID ๋ณ€ํ™˜ ์˜ค๋ฅ˜: '{CUSTOMER_ID_STR}'๋Š” ์œ ํšจํ•œ ์ˆซ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."
224
+
225
+ return True, "์œ ํšจํ•œ API ์„ค์ •์ž…๋‹ˆ๋‹ค."
226
+
227
+ def validate_datalab_config(datalab_config):
228
+ """๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ"""
229
+ if not datalab_config:
230
+ return False, "๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •์ด ์—†์Šต๋‹ˆ๋‹ค."
231
+
232
+ CLIENT_ID = datalab_config.get("CLIENT_ID", "")
233
+ CLIENT_SECRET = datalab_config.get("CLIENT_SECRET", "")
234
+
235
+ if not all([CLIENT_ID, CLIENT_SECRET]):
236
+ return False, "๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
237
+
238
+ if CLIENT_ID.startswith("YOUR_") or CLIENT_SECRET.startswith("YOUR_"):
239
+ return False, "๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ํ”Œ๋ ˆ์ด์Šคํ™€๋”์ž…๋‹ˆ๋‹ค."
240
+
241
+ return True, "์œ ํšจํ•œ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •์ž…๋‹ˆ๋‹ค."
242
+
243
+ def validate_gemini_config():
244
+ """Gemini API ์„ค์ • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ"""
245
+ valid_keys = 0
246
+ for api_key in GEMINI_API_CONFIGS:
247
+ if api_key and not api_key.startswith("YOUR_") and api_key.strip():
248
+ valid_keys += 1
249
+
250
+ if valid_keys == 0:
251
+ return False, "์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Gemini API ํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
252
+
253
+ return True, f"{valid_keys}๊ฐœ์˜ ์œ ํšจํ•œ Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค."
app.py CHANGED
@@ -1,54 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
  import logging
5
- from datetime import datetime
6
- import pytz
7
  import time
8
- import tempfile
9
- import zipfile
10
  import re
11
- import json
 
 
 
 
 
 
 
 
12
 
13
- # ๋กœ๊น… ์„ค์ • - ํด๋ผ์ด์–ธํŠธ ์ •๋ณด ์ˆจ๊น€
14
- logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(levelname)s - %(message)s')
15
  logger = logging.getLogger(__name__)
16
 
17
- # ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๊ทธ ๋น„ํ™œ์„ฑํ™”
18
- logging.getLogger('gradio').setLevel(logging.WARNING)
19
- logging.getLogger('gradio_client').setLevel(logging.WARNING)
20
- logging.getLogger('httpx').setLevel(logging.WARNING)
21
- logging.getLogger('urllib3').setLevel(logging.WARNING)
22
 
23
- # ===== API ํด๋ผ์ด์–ธํŠธ ์„ค์ • =====
24
- def get_api_client():
25
- """ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ์—”๋“œํฌ์ธํŠธ๋ฅผ ๊ฐ€์ ธ์™€ ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ"""
26
  try:
27
- from gradio_client import Client
28
-
29
- # ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ์—”๋“œํฌ์ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
30
- api_endpoint = os.getenv('API_ENDPOINT')
31
-
32
- if not api_endpoint:
33
- logger.error("API ์—”๋“œํฌ์ธํŠธ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
34
- raise ValueError("API ์—”๋“œํฌ์ธํŠธ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
35
-
36
- client = Client(api_endpoint)
37
- logger.info("์›๊ฒฉ API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์„ฑ๊ณต")
38
- return client
39
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
- logger.error(f"API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์‹คํŒจ: {e}")
42
  return None
43
 
 
 
 
44
  # ===== ํ•œ๊ตญ์‹œ๊ฐ„ ๊ด€๋ จ ํ•จ์ˆ˜ =====
45
  def get_korean_time():
46
  """ํ•œ๊ตญ์‹œ๊ฐ„ ๋ฐ˜ํ™˜"""
47
- try:
48
- korea_tz = pytz.timezone('Asia/Seoul')
49
- return datetime.now(korea_tz)
50
- except:
51
- return datetime.now()
 
 
 
52
 
53
  def format_korean_datetime(dt=None, format_type="filename"):
54
  """ํ•œ๊ตญ์‹œ๊ฐ„ ํฌ๋งทํŒ…"""
@@ -71,7 +92,7 @@ def create_loading_animation():
71
  <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);">
72
  <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>
73
  <h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">๋ถ„์„ ์ค‘์ž…๋‹ˆ๋‹ค...</h3>
74
- <p style="color: #666; margin: 5px 0; text-align: center;">์›๊ฒฉ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  AI๊ฐ€ ๋ถ„์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.<br>์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”.</p>
75
  <div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
76
  <div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
77
  </div>
@@ -96,20 +117,203 @@ def generate_error_response(error_message):
96
  return f'''
97
  <div style="color: red; padding: 30px; text-align: center; width: 100%;
98
  background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
99
- <h3 style="margin-bottom: 15px;">โŒ ์—ฐ๊ฒฐ ์˜ค๋ฅ˜</h3>
100
  <p style="margin-bottom: 20px;">{error_message}</p>
101
  <div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
102
  <h4>ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•:</h4>
103
  <ul style="text-align: left; padding-left: 20px;">
 
 
104
  <li>๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”</li>
105
- <li>์›๊ฒฉ ์„œ๋ฒ„ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”</li>
106
  <li>์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”</li>
107
- <li>๋ฌธ์ œ๊ฐ€ ์ง€์†๋˜๋ฉด ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•˜์„ธ์š”</li>
108
  </ul>
109
  </div>
110
  </div>
111
  '''
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  # ===== ํŒŒ์ผ ์ถœ๋ ฅ ํ•จ์ˆ˜๋“ค =====
114
  def create_timestamp_filename(analysis_keyword):
115
  """ํƒ€์ž„์Šคํƒฌํ”„๊ฐ€ ํฌํ•จ๋œ ํŒŒ์ผ๋ช… ์ƒ์„ฑ - ํ•œ๊ตญ์‹œ๊ฐ„ ์ ์šฉ"""
@@ -317,72 +521,16 @@ def export_analysis_results(export_data):
317
  logger.error(f"๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜ค๋ฅ˜: {e}")
318
  return None, f"์ถœ๋ ฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
319
 
320
- # ===== ์›๊ฒฉ API ํ˜ธ์ถœ ํ•จ์ˆ˜๋“ค =====
321
- def call_analyze_keyword_api(analysis_keyword):
322
- """ํ‚ค์›Œ๋“œ ์‹ฌ์ธต๋ถ„์„ API ํ˜ธ์ถœ"""
323
- try:
324
- client = get_api_client()
325
- if not client:
326
- return generate_error_response("API ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), {}
327
-
328
- logger.info("์›๊ฒฉ API ํ˜ธ์ถœ: ํ‚ค์›Œ๋“œ ์‹ฌ์ธต๋ถ„์„")
329
- result = client.predict(
330
- analysis_keyword=analysis_keyword,
331
- api_name="/on_analyze_keyword"
332
- )
333
-
334
- logger.info(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ API ๊ฒฐ๊ณผ ํƒ€์ž…: {type(result)}")
335
-
336
- # ๋ถ„์„ ๊ฒฐ๊ณผ๋กœ export ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
337
- if isinstance(result, str) and len(result) > 100:
338
- export_data = {
339
- "analysis_keyword": analysis_keyword,
340
- "analysis_html": result,
341
- "analysis_completed": True,
342
- "created_at": get_korean_time().isoformat()
343
- }
344
- return result, export_data
345
- else:
346
- return str(result), {}
347
-
348
- except Exception as e:
349
- logger.error(f"ํ‚ค์›Œ๋“œ ์‹ฌ์ธต๋ถ„์„ API ํ˜ธ์ถœ ์˜ค๋ฅ˜: {e}")
350
- return generate_error_response(f"์›๊ฒฉ ์„œ๋ฒ„ ์—ฐ๊ฒฐ ์‹คํŒจ: {str(e)}"), {}
351
-
352
- def call_export_results_api(export_data):
353
- """๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ API ํ˜ธ์ถœ"""
354
- try:
355
- client = get_api_client()
356
- if not client:
357
- return None, "API ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
358
-
359
- logger.info("์›๊ฒฉ API ํ˜ธ์ถœ: ๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ")
360
- result = client.predict(
361
- api_name="/on_export_results"
362
- )
363
-
364
- logger.info(f"์ถœ๋ ฅ API ๊ฒฐ๊ณผ ํƒ€์ž…: {type(result)}")
365
-
366
- # ๊ฒฐ๊ณผ๊ฐ€ ํŠœํ”Œ์ธ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ๋ฉ”์‹œ์ง€, ๋‘ ๋ฒˆ์งธ๋Š” ํŒŒ์ผ
367
- if isinstance(result, tuple) and len(result) == 2:
368
- message, file_path = result
369
- if file_path:
370
- return file_path, message
371
- else:
372
- return None, message
373
- else:
374
- return None, str(result)
375
-
376
- except Exception as e:
377
- logger.error(f"๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ API ํ˜ธ์ถœ ์˜ค๋ฅ˜: {e}")
378
- return None, f"์›๊ฒฉ ์„œ๋ฒ„ ์—ฐ๊ฒฐ ์‹คํŒจ: {str(e)}"
379
-
380
  # ===== ๊ทธ๋ผ๋””์˜ค ์ธํ„ฐํŽ˜์ด์Šค =====
381
  def create_interface():
382
  # CSS ํŒŒ์ผ ๋กœ๋“œ
383
  try:
384
  with open('style.css', 'r', encoding='utf-8') as f:
385
  custom_css = f.read()
 
 
 
 
386
  except:
387
  custom_css = """
388
  :root { --primary-color: #FB7F0D; --secondary-color: #ff9a8b; }
@@ -397,30 +545,6 @@ def create_interface():
397
  font-size: 17px !important; font-weight: bold !important; width: 100% !important;
398
  margin-top: 20px !important;
399
  }
400
- .custom-frame {
401
- background-color: white !important;
402
- border: 1px solid #e5e5e5 !important;
403
- border-radius: 18px;
404
- padding: 20px;
405
- margin: 10px 0;
406
- box-shadow: 0 8px 30px rgba(251, 127, 13, 0.08) !important;
407
- }
408
- .section-title {
409
- display: flex;
410
- align-items: center;
411
- font-size: 20px;
412
- font-weight: 700;
413
- color: #334155 !important;
414
- margin-bottom: 10px;
415
- padding-bottom: 5px;
416
- border-bottom: 2px solid var(--primary-color);
417
- font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
418
- }
419
- .section-title img, .section-title i {
420
- margin-right: 10px;
421
- font-size: 20px;
422
- color: var(--primary-color);
423
- }
424
  """
425
 
426
  with gr.Blocks(
@@ -473,7 +597,7 @@ def create_interface():
473
  yield create_loading_animation(), {}
474
 
475
  # ์‹ค์ œ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹คํ–‰
476
- keyword_result, session_export_data = call_analyze_keyword_api(analysis_keyword)
477
 
478
  # ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„๊ณผ ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„ ํ‘œ์‹œ
479
  yield keyword_result, session_export_data
@@ -481,7 +605,6 @@ def create_interface():
481
  def on_export_results(export_data):
482
  """๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ•ธ๋“ค๋Ÿฌ - ์„ธ์…˜๋ณ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ"""
483
  try:
484
- # ๋กœ์ปฌ ์ถœ๋ ฅ ์‹œ๋„
485
  zip_path, message = export_analysis_results(export_data)
486
 
487
  if zip_path:
@@ -501,38 +624,14 @@ def create_interface():
501
  """
502
  return success_html, gr.update(value=zip_path, visible=True)
503
  else:
504
- # ๋กœ์ปฌ ์ถœ๋ ฅ ์‹คํŒจ์‹œ ์›๊ฒฉ API ์‹œ๋„
505
- try:
506
- remote_file, remote_message = call_export_results_api(export_data)
507
- if remote_file:
508
- success_html = f"""
509
- <div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
510
- <h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> ์›๊ฒฉ ์ถœ๋ ฅ ์™„๋ฃŒ!</h4>
511
- <p style="color: #155724; margin: 0; line-height: 1.6;">
512
- {remote_message}<br>
513
- <i class="fas fa-download"></i> ์•„๋ž˜ ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํŒŒ์ผ์„ ์ €์žฅํ•˜์„ธ์š”.
514
- </p>
515
- </div>
516
- """
517
- return success_html, gr.update(value=remote_file, visible=True)
518
- else:
519
- # ์‹คํŒจ ๋ฉ”์‹œ์ง€
520
- error_html = f"""
521
- <div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
522
- <h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถœ๋ ฅ ์‹คํŒจ</h4>
523
- <p style="color: #721c24; margin: 0;">{message}<br>์›๊ฒฉ ์ถœ๋ ฅ๋„ ์‹คํŒจ: {remote_message}</p>
524
- </div>
525
- """
526
- return error_html, gr.update(visible=False)
527
- except:
528
- # ์›๊ฒฉ API๋„ ์‹คํŒจ
529
- error_html = f"""
530
- <div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
531
- <h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถœ๋ ฅ ์‹คํŒจ</h4>
532
- <p style="color: #721c24; margin: 0;">{message}</p>
533
- </div>
534
- """
535
- return error_html, gr.update(visible=False)
536
 
537
  except Exception as e:
538
  logger.error(f"์ถœ๋ ฅ ํ•ธ๋“ค๋Ÿฌ ์˜ค๋ฅ˜: {e}")
@@ -559,42 +658,121 @@ def create_interface():
559
 
560
  return interface
561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
  # ===== ๋ฉ”์ธ ์‹คํ–‰ =====
563
  if __name__ == "__main__":
564
  # pytz ๋ชจ๋“ˆ ์„ค์น˜ ํ™•์ธ
565
- try:
566
- import pytz
567
  logger.info("โœ… pytz ๋ชจ๋“ˆ ๋กœ๋“œ ์„ฑ๊ณต - ํ•œ๊ตญ์‹œ๊ฐ„ ์ง€์›")
568
- except ImportError:
569
  logger.warning("โš ๏ธ pytz ๋ชจ๋“ˆ์ด ์„ค์น˜๋˜์ง€ ์•Š์Œ - pip install pytz ์‹คํ–‰ ํ•„์š”")
570
  logger.info("์‹œ์Šคํ…œ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.")
571
 
572
- logger.info("===== ์ƒํ’ˆ ์†Œ์‹ฑ ๋ถ„์„ ์‹œ์Šคํ…œ v2.10 (์ปจํŠธ๋กค ํƒ€์›Œ ๋ฒ„์ „) ์‹œ์ž‘ =====")
 
 
 
 
 
 
 
 
573
 
574
  # ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์•ˆ๋‚ด
575
  print("๐Ÿ“ฆ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€:")
576
- print(" pip install gradio gradio_client pandas pytz")
577
  print()
578
 
579
- # API ์—”๋“œํฌ์ธํŠธ ์„ค์ • ์•ˆ๋‚ด
580
- api_endpoint = os.getenv('API_ENDPOINT')
581
- if not api_endpoint:
582
- print("โš ๏ธ API_ENDPOINT ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”.")
583
- print(" export API_ENDPOINT='your-endpoint-url'")
 
 
 
 
 
 
 
 
 
584
  print()
585
  else:
586
- print("โœ… API ์—”๋“œํฌ์ธํŠธ ์„ค์ • ์™„๋ฃŒ!")
 
 
 
 
587
  print()
588
 
589
- print("๐Ÿš€ v2.10 ์ปจํŠธ๋กค ํƒ€์›Œ ๋ฒ„์ „ ํŠน์ง•:")
590
- print(" โ€ข ํ—ˆ๊น…ํŽ˜์ด์Šค ๊ทธ๋ผ๋””์˜ค ์—”๋“œํฌ์ธํŠธ ํ™œ์šฉ")
591
- print(" โ€ข ์™„์ „ํžˆ ๋™์ผํ•œ UI์™€ ๊ธฐ๋Šฅ ๊ตฌํ˜„")
592
- print(" โ€ข ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„๊ณผ ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„ ํ‘œ์‹œ")
593
- print(" โ€ข โœ… ์ถœ๋ ฅ ๊ธฐ๋Šฅ: HTML ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ZIP ๋‹ค์šด๋กœ๋“œ")
 
 
 
594
  print(" โ€ข โœ… ํ•œ๊ตญ์‹œ๊ฐ„ ๊ธฐ์ค€ ํŒŒ์ผ๋ช… ์ƒ์„ฑ")
595
  print(" โ€ข โœ… ๋ฉ€ํ‹ฐ ์‚ฌ์šฉ์ž ์•ˆ์ „: gr.State๋กœ ์„ธ์…˜๋ณ„ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ")
596
- print(" โ€ข ๐Ÿ”’ ํด๋ผ์ด์–ธํŠธ ์ •๋ณด ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์™„์ „ ์ˆจ๊น€")
597
- print(" โ€ข ์›๊ฒฉ ์„œ๋ฒ„์™€ ๋กœ์ปฌ ์ฒ˜๋ฆฌ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ๋ฐฉ์‹")
598
  print()
599
 
600
  # ์•ฑ ์‹คํ–‰
 
1
+ """
2
+ AI ์ƒํ’ˆ ์†Œ์‹ฑ ๋ถ„์„ ์‹œ์Šคํ…œ v2.10 - ๊ฐ„๋žต๋ฒ„์ „ + ์ถœ๋ ฅ๊ธฐ๋Šฅ + ๋ฉ€ํ‹ฐ์‚ฌ์šฉ์ž ์•ˆ์ „
3
+ - 2๋‹จ๊ณ„: ์ˆ˜์ง‘๋œ ํ‚ค์›Œ๋“œ ๋ชฉ๋ก ๊ธฐ๋Šฅ์„ ์ œ๊ฑฐํ•œ๋‹ค.
4
+ - 3๋‹จ๊ณ„: ๋ถ„์„ํ•  ํ‚ค์›Œ๋“œ ์„ ํƒ์—์„œ ๐Ÿ”— ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ๋ถ„์„์˜ ์ƒํ’ˆ์ถ”์ถœ ๋ฐ ๋ถ„์„๊ธฐ๋Šฅ์„์ œ๊ฑฐํ•œ๋‹ค.
5
+ - 3๋‹จ๊ณ„: ๋ถ„์„ํ•  ํ‚ค์›Œ๋“œ ์„ ํƒ์˜ ๋ช…์นญ์„ "ํ‚ค์›Œ๋“œ ์‹ฌ์ธต๋ถ„์„ ์ž…๋ ฅ"์ด๋ผ๊ณ  ๋ฐ”๊พผ๋‹ค.
6
+ - ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„, ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„์ด ๋‚˜์™€์•ผํ•œ๋‹ค.
7
+ - ์ถœ๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€: HTML ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ZIP ๋‹ค์šด๋กœ๋“œ
8
+ - Gemini API ํ‚ค ๋žœ๋ค ์ ์šฉ (api_utils ํ†ตํ•ฉ ๊ด€๋ฆฌ)
9
+ - ํ•œ๊ตญ์‹œ๊ฐ„ ์ ์šฉ
10
+ - ๋ฉ€ํ‹ฐ ์‚ฌ์šฉ์ž ์•ˆ์ „: gr.State ์‚ฌ์šฉ์œผ๋กœ ์„ธ์…˜๋ณ„ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
11
+ """
12
+
13
  import gradio as gr
14
  import pandas as pd
15
  import os
16
  import logging
17
+ import google.generativeai as genai
18
+ from datetime import datetime, timedelta
19
  import time
 
 
20
  import re
21
+ import zipfile
22
+ import tempfile
23
+
24
+ # ํ•œ๊ตญ์‹œ๊ฐ„ ์ ์šฉ์„ ์œ„ํ•œ ๋ชจ๋“ˆ (์„ ํƒ์ )
25
+ try:
26
+ import pytz
27
+ PYTZ_AVAILABLE = True
28
+ except ImportError:
29
+ PYTZ_AVAILABLE = False
30
 
31
+ # ๋กœ๊น… ์„ค์ •
32
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
33
  logger = logging.getLogger(__name__)
34
 
35
+ # ๋ชจ๋“ˆ ์ž„ํฌํŠธ (๊ฐ„๋žต ๋ฒ„์ „์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ)
36
+ import api_utils
37
+ import keyword_search
38
+ import keyword_analysis
39
+ import trend_analysis_v2
40
 
41
+ # ===== Gemini API ์„ค์ • =====
42
+ def setup_gemini_model():
43
+ """Gemini ๋ชจ๋ธ ์ดˆ๊ธฐํ™” - api_utils์—์„œ ๊ด€๋ฆฌ (๋žœ๋ค ํ‚ค ์ ์šฉ)"""
44
  try:
45
+ # api_utils์—์„œ Gemini ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ (๋žœ๋ค ํ‚ค ์ ์šฉ)
46
+ model = api_utils.get_gemini_model()
 
 
 
 
 
 
 
 
 
 
47
 
48
+ if model:
49
+ logger.info("Gemini ๋ชจ๋ธ ์ดˆ๊ธฐํ™” ์„ฑ๊ณต (api_utils ํ†ตํ•ฉ ๊ด€๋ฆฌ - ๋žœ๋ค ํ‚ค)")
50
+ return model
51
+ else:
52
+ logger.warning("Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
53
+ return None
54
+
55
  except Exception as e:
56
+ logger.error(f"Gemini ๋ชจ๋ธ ์ดˆ๊ธฐํ™” ์‹คํŒจ: {e}")
57
  return None
58
 
59
+ # Gemini ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
60
+ gemini_model = setup_gemini_model()
61
+
62
  # ===== ํ•œ๊ตญ์‹œ๊ฐ„ ๊ด€๋ จ ํ•จ์ˆ˜ =====
63
  def get_korean_time():
64
  """ํ•œ๊ตญ์‹œ๊ฐ„ ๋ฐ˜ํ™˜"""
65
+ if PYTZ_AVAILABLE:
66
+ try:
67
+ korea_tz = pytz.timezone('Asia/Seoul')
68
+ return datetime.now(korea_tz)
69
+ except:
70
+ pass
71
+ # pytz๊ฐ€ ์—†๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์‹œ ์‹œ์Šคํ…œ ์‹œ๊ฐ„ ์‚ฌ์šฉ
72
+ return datetime.now()
73
 
74
  def format_korean_datetime(dt=None, format_type="filename"):
75
  """ํ•œ๊ตญ์‹œ๊ฐ„ ํฌ๋งทํŒ…"""
 
92
  <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);">
93
  <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>
94
  <h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">๋ถ„์„ ์ค‘์ž…๋‹ˆ๋‹ค...</h3>
95
+ <p style="color: #666; margin: 5px 0; text-align: center;">๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  AI๊ฐ€ ๋ถ„์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.<br>์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”.</p>
96
  <div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
97
  <div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
98
  </div>
 
117
  return f'''
118
  <div style="color: red; padding: 30px; text-align: center; width: 100%;
119
  background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
120
+ <h3 style="margin-bottom: 15px;">โŒ ๋ถ„์„ ์˜ค๋ฅ˜</h3>
121
  <p style="margin-bottom: 20px;">{error_message}</p>
122
  <div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
123
  <h4>ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•:</h4>
124
  <ul style="text-align: left; padding-left: 20px;">
125
+ <li>ํ‚ค์›Œ๋“œ ์ฒ ์ž๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”</li>
126
+ <li>๋” ๊ฐ„๋‹จํ•œ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์„ธ์š”</li>
127
  <li>๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”</li>
 
128
  <li>์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”</li>
 
129
  </ul>
130
  </div>
131
  </div>
132
  '''
133
 
134
+ # ===== ๋ฉ”์ธ ํ‚ค์›Œ๋“œ ๋ถ„์„ ํ•จ์ˆ˜ =====
135
+ def safe_keyword_analysis(analysis_keyword):
136
+ """์—๋Ÿฌ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ์•ˆ์ „ํ•œ ํ‚ค์›Œ๋“œ ๋ถ„์„ - ๋ฉ€ํ‹ฐ์‚ฌ์šฉ์ž ์•ˆ์ „"""
137
+
138
+ # ์ž…๋ ฅ๊ฐ’ ๊ฒ€์ฆ
139
+ if not analysis_keyword or not analysis_keyword.strip():
140
+ return generate_error_response("๋ถ„์„ํ•  ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."), {}
141
+
142
+ analysis_keyword = analysis_keyword.strip()
143
+
144
+ try:
145
+ # ๊ฒ€์ƒ‰๋Ÿ‰ ์กฐํšŒ - ์—๋Ÿฌ ๋ฐฉ์ง€
146
+ api_keyword = keyword_analysis.normalize_keyword_for_api(analysis_keyword)
147
+ search_volumes = keyword_search.fetch_all_search_volumes([api_keyword])
148
+ volume_data = search_volumes.get(api_keyword, {"PC๊ฒ€์ƒ‰๋Ÿ‰": 0, "๋ชจ๋ฐ”์ผ๊ฒ€์ƒ‰๋Ÿ‰": 0, "์ด๊ฒ€์ƒ‰๋Ÿ‰": 0})
149
+
150
+ # ๊ฒ€์ƒ‰๋Ÿ‰์ด 0์ด๊ฑฐ๋‚˜ ํ‚ค์›Œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
151
+ if volume_data['์ด๊ฒ€์ƒ‰๋Ÿ‰'] == 0:
152
+ logger.warning(f"'{analysis_keyword}' ํ‚ค์›Œ๋“œ์˜ ๊ฒ€์ƒ‰๋Ÿ‰์ด 0์ด๊ฑฐ๋‚˜ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
153
+ error_result = f"""
154
+ <div style="padding: 30px; text-align: center; background: #fff3cd; border-radius: 12px; border: 1px solid #ffeaa7;">
155
+ <h3 style="color: #856404; margin-bottom: 15px;">โš ๏ธ ํ‚ค์›Œ๋“œ ๋ถ„์„ ๋ถˆ๊ฐ€</h3>
156
+ <p style="color: #856404; margin-bottom: 10px;"><strong>'{analysis_keyword}'</strong> ํ‚ค์›Œ๋“œ๋Š” ๊ฒ€์ƒ‰๋Ÿ‰์ด ์—†๊ฑฐ๋‚˜ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค.</p>
157
+ <div style="background: white; padding: 15px; border-radius: 8px; margin-top: 15px;">
158
+ <h4 style="color: #333; margin-bottom: 10px;">๐Ÿ’ก ๊ถŒ์žฅ์‚ฌํ•ญ</h4>
159
+ <ul style="text-align: left; color: #666; padding-left: 20px;">
160
+ <li>ํ‚ค์›Œ๋“œ ์ฒ ์ž๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”</li>
161
+ <li>๋” ์ผ๋ฐ˜์ ์ธ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์„ธ์š”</li>
162
+ <li>ํ‚ค์›Œ๋“œ๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„ํ•ด๋ณด์„ธ์š” (์˜ˆ: '์—ฌ์„ฑ ์Šฌ๋ฆฌํผ')</li>
163
+ </ul>
164
+ </div>
165
+ </div>
166
+ """
167
+ return error_result, {}
168
+
169
+ logger.info(f"'{analysis_keyword}' ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰: {volume_data['์ด๊ฒ€์ƒ‰๋Ÿ‰']:,}")
170
+
171
+ # ํŠธ๋ Œ๋“œ ๋ถ„์„ ์‹œ๋„
172
+ monthly_data_1year = {}
173
+ monthly_data_3year = {}
174
+ trend_available = False
175
+
176
+ try:
177
+ # ๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค ํ™•์ธ (๋žœ๋ค ์ ์šฉ)
178
+ datalab_config = api_utils.get_next_datalab_api_config()
179
+ if datalab_config and not datalab_config["CLIENT_ID"].startswith("YOUR_"):
180
+ logger.info("๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด 1๋…„, 3๋…„ ํŠธ๋ Œ๋“œ ๋ถ„์„์„ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.")
181
+
182
+ # ์ตœ์ ํ™”๋œ API ํ•จ์ˆ˜ ์‚ฌ์šฉ
183
+ # 1๋…„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ
184
+ trend_data_1year = trend_analysis_v2.get_naver_trend_data_v5([analysis_keyword], "1year", max_retries=3)
185
+ if trend_data_1year:
186
+ current_volumes = {api_keyword: volume_data}
187
+ monthly_data_1year = trend_analysis_v2.calculate_monthly_volumes_v7([analysis_keyword], current_volumes, trend_data_1year, "1year")
188
+
189
+ # 3๋…„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ
190
+ trend_data_3year = trend_analysis_v2.get_naver_trend_data_v5([analysis_keyword], "3year", max_retries=3)
191
+ if trend_data_3year:
192
+ current_volumes = {api_keyword: volume_data}
193
+ monthly_data_3year = trend_analysis_v2.calculate_monthly_volumes_v7([analysis_keyword], current_volumes, trend_data_3year, "3year")
194
+
195
+ # 3๋…„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ 1๋…„ ๋ฐ์ดํ„ฐ๋กœ ํ™•์žฅ
196
+ if not monthly_data_3year and monthly_data_1year:
197
+ logger.info("3๋…„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด 1๋…„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ 3๋…„ ์ฐจํŠธ ์ƒ์„ฑ")
198
+ keyword = analysis_keyword
199
+ if keyword in monthly_data_1year:
200
+ data_1y = monthly_data_1year[keyword]
201
+
202
+ # 3๋…„ ๋ถ„๋Ÿ‰์˜ ๋‚ ์งœ ์ƒ์„ฑ (24๊ฐœ์›” ์ถ”๊ฐ€)
203
+ extended_dates = []
204
+ extended_volumes = []
205
+
206
+ # ๊ธฐ์กด 1๋…„ ๋ฐ์ดํ„ฐ ์ด์ „์— 24๊ฐœ์›” ์ถ”๊ฐ€ (๋ชจ๋‘ 0์œผ๋กœ)
207
+ start_date = datetime.strptime(data_1y["dates"][0], "%Y-%m-%d")
208
+ for i in range(24, 0, -1):
209
+ prev_date = start_date - timedelta(days=30 * i)
210
+ extended_dates.append(prev_date.strftime("%Y-%m-%d"))
211
+ extended_volumes.append(0)
212
+
213
+ # ๊ธฐ์กด 1๋…„ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ œ์™ธ)
214
+ actual_count = data_1y.get("actual_count", len(data_1y["dates"]))
215
+ extended_dates.extend(data_1y["dates"][:actual_count])
216
+ extended_volumes.extend(data_1y["monthly_volumes"][:actual_count])
217
+
218
+ monthly_data_3year = {
219
+ keyword: {
220
+ "monthly_volumes": extended_volumes,
221
+ "dates": extended_dates,
222
+ "current_volume": data_1y["current_volume"],
223
+ "growth_rate": trend_analysis_v2.calculate_3year_growth_rate_improved(extended_volumes),
224
+ "volume_per_percent": data_1y["volume_per_percent"],
225
+ "current_ratio": data_1y["current_ratio"],
226
+ "actual_count": len(extended_volumes),
227
+ "predicted_count": 0
228
+ }
229
+ }
230
+
231
+ if monthly_data_1year or monthly_data_3year:
232
+ trend_available = True
233
+ logger.info("ํŠธ๋ Œ๋“œ ๋ถ„์„ ์„ฑ๊ณต")
234
+ else:
235
+ logger.info("ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹คํŒจ")
236
+ else:
237
+ logger.info("๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์Œ")
238
+ except Exception as e:
239
+ logger.info(f"ํŠธ๋ Œ๋“œ ๋ถ„์„ ๊ฑด๋„ˆ๋œ€: {str(e)[:100]}")
240
+
241
+ # === ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„ ์„น์…˜ ===
242
+ if trend_available and (monthly_data_1year or monthly_data_3year):
243
+ try:
244
+ trend_chart = trend_analysis_v2.create_trend_chart_v7(monthly_data_1year, monthly_data_3year)
245
+ except Exception as e:
246
+ logger.warning(f"ํŠธ๋ Œ๋“œ ์ฐจํŠธ ์ƒ์„ฑ ์‹คํŒจ, ๊ธฐ๋ณธ ์ฐจํŠธ ์‚ฌ์šฉ: {e}")
247
+ trend_chart = trend_analysis_v2.create_enhanced_current_chart(volume_data, analysis_keyword)
248
+ else:
249
+ trend_chart = trend_analysis_v2.create_enhanced_current_chart(volume_data, analysis_keyword)
250
+
251
+ # ํŠธ๋ Œ๋“œ ์„น์…˜
252
+ trend_section = f"""
253
+ <div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
254
+ <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
255
+ <h3 style="margin: 0; font-size: 18px; color: white;">๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„</h3>
256
+ </div>
257
+ <div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">
258
+ {trend_chart}
259
+ </div>
260
+ </div>
261
+ """
262
+
263
+ # === ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์„น์…˜ (AI ๋ถ„์„) ===
264
+ # api_utils์—์„œ Gemini ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ (๋žœ๋ค ํ‚ค ์ ์šฉ)
265
+ current_gemini_model = api_utils.get_gemini_model()
266
+
267
+ keyword_analysis_html = keyword_analysis.analyze_keyword_for_sourcing(
268
+ analysis_keyword, volume_data, monthly_data_1year, monthly_data_3year,
269
+ None, [], current_gemini_model # ๊ฐ„๋žต ๋ฒ„์ „์—์„œ๋Š” ์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ๋ฐ์ดํ„ฐ ์—†์Œ
270
+ )
271
+
272
+ keyword_analysis_section = f"""
273
+ <div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
274
+ <div style="background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
275
+ <h3 style="margin: 0; font-size: 18px; color: white;">๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„</h3>
276
+ </div>
277
+ <div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden;">
278
+ {keyword_analysis_html}
279
+ </div>
280
+ </div>
281
+ """
282
+
283
+ # ๊ฒฝ๊ณ  ์„น์…˜ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ)
284
+ warning_section = ""
285
+ if not trend_available:
286
+ warning_section = f"""
287
+ <div style="width: 100%; margin: 20px auto; padding: 15px; background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; font-family: 'Pretendard', sans-serif;">
288
+ <div style="display: flex; align-items: center;">
289
+ <span style="font-size: 20px; margin-right: 10px;">โš ๏ธ</span>
290
+ <div>
291
+ <strong style="color: #856404;">์ผ๋ถ€ ๊ธฐ๋Šฅ ์ œํ•œ</strong>
292
+ <div style="font-size: 14px; color: #856404; margin-top: 5px;">
293
+ ํŠธ๋ Œ๋“œ ๋ถ„์„์— ์ œํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ๋ถ„์„๊ณผ AI ์ถ”์ฒœ์€ ์ •์ƒ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.<br>
294
+ <small>์™„์ „ํ•œ ์›” ๋ฐ์ดํ„ฐ ๊ธฐ์ค€์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์‹  ์™„๋ฃŒ๋œ ์›”๊นŒ์ง€๋งŒ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.</small>
295
+ </div>
296
+ </div>
297
+ </div>
298
+ </div>
299
+ """
300
+
301
+ # ์ตœ์ข… ๊ฒฐ๊ณผ ์กฐํ•ฉ
302
+ final_result = warning_section + trend_section + keyword_analysis_section
303
+
304
+ # ์„ธ์…˜๋ณ„ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜ (๋ฉ€ํ‹ฐ ์‚ฌ์šฉ์ž ์•ˆ์ „)
305
+ session_export_data = {
306
+ "analysis_keyword": analysis_keyword,
307
+ "analysis_html": final_result
308
+ }
309
+
310
+ return final_result, session_export_data
311
+
312
+ except Exception as e:
313
+ logger.error(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์ค‘ ์ „์ฒด ์˜ค๋ฅ˜: {e}")
314
+ error_result = generate_error_response(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
315
+ return error_result, {}
316
+
317
  # ===== ํŒŒ์ผ ์ถœ๋ ฅ ํ•จ์ˆ˜๋“ค =====
318
  def create_timestamp_filename(analysis_keyword):
319
  """ํƒ€์ž„์Šคํƒฌํ”„๊ฐ€ ํฌํ•จ๋œ ํŒŒ์ผ๋ช… ์ƒ์„ฑ - ํ•œ๊ตญ์‹œ๊ฐ„ ์ ์šฉ"""
 
521
  logger.error(f"๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜ค๋ฅ˜: {e}")
522
  return None, f"์ถœ๋ ฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
  # ===== ๊ทธ๋ผ๋””์˜ค ์ธํ„ฐํŽ˜์ด์Šค =====
525
  def create_interface():
526
  # CSS ํŒŒ์ผ ๋กœ๋“œ
527
  try:
528
  with open('style.css', 'r', encoding='utf-8') as f:
529
  custom_css = f.read()
530
+
531
+ with open('keyword_analysis_report.css', 'r', encoding='utf-8') as f:
532
+ keyword_css = f.read()
533
+ custom_css += "\n" + keyword_css
534
  except:
535
  custom_css = """
536
  :root { --primary-color: #FB7F0D; --secondary-color: #ff9a8b; }
 
545
  font-size: 17px !important; font-weight: bold !important; width: 100% !important;
546
  margin-top: 20px !important;
547
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
  """
549
 
550
  with gr.Blocks(
 
597
  yield create_loading_animation(), {}
598
 
599
  # ์‹ค์ œ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹คํ–‰
600
+ keyword_result, session_export_data = safe_keyword_analysis(analysis_keyword)
601
 
602
  # ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„๊ณผ ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„ ํ‘œ์‹œ
603
  yield keyword_result, session_export_data
 
605
  def on_export_results(export_data):
606
  """๋ถ„์„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ•ธ๋“ค๋Ÿฌ - ์„ธ์…˜๋ณ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ"""
607
  try:
 
608
  zip_path, message = export_analysis_results(export_data)
609
 
610
  if zip_path:
 
624
  """
625
  return success_html, gr.update(value=zip_path, visible=True)
626
  else:
627
+ # ์‹คํŒจ ๋ฉ”์‹œ์ง€
628
+ error_html = f"""
629
+ <div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
630
+ <h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถœ๋ ฅ ์‹คํŒจ</h4>
631
+ <p style="color: #721c24; margin: 0;">{message}</p>
632
+ </div>
633
+ """
634
+ return error_html, gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
 
636
  except Exception as e:
637
  logger.error(f"์ถœ๋ ฅ ํ•ธ๋“ค๋Ÿฌ ์˜ค๋ฅ˜: {e}")
 
658
 
659
  return interface
660
 
661
+ # ===== API ์„ค์ • ํ™•์ธ ํ•จ์ˆ˜ =====
662
+ def check_datalab_api_config():
663
+ """๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ํ™•์ธ"""
664
+ logger.info("=== ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ํ™•์ธ ===")
665
+
666
+ datalab_config = api_utils.get_next_datalab_api_config()
667
+
668
+ if not datalab_config:
669
+ logger.warning("๏ฟฝ๏ฟฝ ๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
670
+ logger.info("ํŠธ๋ Œ๋“œ ๋ถ„์„ ๊ธฐ๋Šฅ์ด ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.")
671
+ return False
672
+
673
+ client_id = datalab_config["CLIENT_ID"]
674
+ client_secret = datalab_config["CLIENT_SECRET"]
675
+
676
+ logger.info(f"์ด {len(api_utils.NAVER_DATALAB_CONFIGS)}๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ์‚ฌ์šฉ ์ค‘")
677
+ logger.info(f"ํ˜„์žฌ ์„ ํƒ๋œ API:")
678
+ logger.info(f" CLIENT_ID: {client_id[:8]}***{client_id[-4:] if len(client_id) > 12 else '***'}")
679
+ logger.info(f" CLIENT_SECRET: {client_secret[:4]}***{client_secret[-2:] if len(client_secret) > 6 else '***'}")
680
+
681
+ # ๊ธฐ๋ณธ๊ฐ’ ์ฒดํฌ
682
+ if client_id.startswith("YOUR_"):
683
+ logger.error("โŒ CLIENT_ID๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค!")
684
+ return False
685
+
686
+ if client_secret.startswith("YOUR_"):
687
+ logger.error("โŒ CLIENT_SECRET์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค!")
688
+ return False
689
+
690
+ # ๊ธธ์ด ์ฒดํฌ
691
+ if len(client_id) < 10:
692
+ logger.warning("โš ๏ธ CLIENT_ID๊ฐ€ ์งง์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ํ‚ค์ธ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
693
+
694
+ if len(client_secret) < 5:
695
+ logger.warning("โš ๏ธ CLIENT_SECRET์ด ์งง์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ํ‚ค์ธ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
696
+
697
+ logger.info("โœ… ๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค ํ˜•์‹ ๊ฒ€์ฆ ์™„๋ฃŒ")
698
+ return True
699
+
700
+ def check_gemini_api_config():
701
+ """Gemini API ์„ค์ • ํ™•์ธ - ๋žœ๋ค ํ‚ค ์ ์šฉ"""
702
+ logger.info("=== Gemini API ์„ค์ • ํ™•์ธ ===")
703
+
704
+ is_valid, message = api_utils.validate_gemini_config()
705
+
706
+ if is_valid:
707
+ logger.info(f"โœ… {message}")
708
+ # ์ฒซ ๋ฒˆ์งธ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ‚ค ํ…Œ์ŠคํŠธ (๋žœ๋ค)
709
+ test_key = api_utils.get_next_gemini_api_key()
710
+ if test_key:
711
+ logger.info(f"ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ Gemini API ํ‚ค: {test_key[:8]}***{test_key[-4:]}")
712
+ return True
713
+ else:
714
+ logger.warning(f"โŒ {message}")
715
+ logger.info("AI ๋ถ„์„ ๊ธฐ๋Šฅ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
716
+ return False
717
+
718
  # ===== ๋ฉ”์ธ ์‹คํ–‰ =====
719
  if __name__ == "__main__":
720
  # pytz ๋ชจ๋“ˆ ์„ค์น˜ ํ™•์ธ
721
+ if PYTZ_AVAILABLE:
 
722
  logger.info("โœ… pytz ๋ชจ๋“ˆ ๋กœ๋“œ ์„ฑ๊ณต - ํ•œ๊ตญ์‹œ๊ฐ„ ์ง€์›")
723
+ else:
724
  logger.warning("โš ๏ธ pytz ๋ชจ๋“ˆ์ด ์„ค์น˜๋˜์ง€ ์•Š์Œ - pip install pytz ์‹คํ–‰ ํ•„์š”")
725
  logger.info("์‹œ์Šคํ…œ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.")
726
 
727
+ # API ์„ค์ • ์ดˆ๊ธฐํ™”
728
+ api_utils.initialize_api_configs()
729
+ logger.info("===== ์ƒํ’ˆ ์†Œ์‹ฑ ๋ถ„์„ ์‹œ์Šคํ…œ v2.10 (๊ฐ„๋žต๋ฒ„์ „ + ์ถœ๋ ฅ๊ธฐ๋Šฅ + ๋žœ๋คํ‚ค + ๋ฉ€ํ‹ฐ์‚ฌ์šฉ์ž ์•ˆ์ „) ์‹œ์ž‘ =====")
730
+
731
+ # ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ํ™•์ธ
732
+ datalab_available = check_datalab_api_config()
733
+
734
+ # Gemini API ์„ค์ • ํ™•์ธ (๋žœ๋ค ํ‚ค)
735
+ gemini_available = check_gemini_api_config()
736
 
737
  # ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์•ˆ๋‚ด
738
  print("๐Ÿ“ฆ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€:")
739
+ print(" pip install gradio google-generativeai pandas requests xlsxwriter markdown plotly pytz")
740
  print()
741
 
742
+ # API ํ‚ค ์„ค์ • ์•ˆ๋‚ด
743
+ if not gemini_available:
744
+ print("โš ๏ธ GEMINI_API_KEY ๋˜๋Š” GOOGLE_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”.")
745
+ print(" export GEMINI_API_KEY='your-api-key'")
746
+ print(" ๋˜๋Š”")
747
+ print(" export GOOGLE_API_KEY='your-api-key'")
748
+ print()
749
+
750
+ if not datalab_available:
751
+ print("โš ๏ธ ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ํŠธ๋ Œ๋“œ ๋ถ„์„์„ ์œ„ํ•ด์„œ๋Š”:")
752
+ print(" 1. ๋„ค์ด๋ฒ„ ๊ฐœ๋ฐœ์ž์„ผํ„ฐ(https://developers.naver.com)์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก")
753
+ print(" 2. '๋ฐ์ดํ„ฐ๋žฉ(๊ฒ€์ƒ‰์–ด ํŠธ๋ Œ๋“œ)' API ์ถ”๊ฐ€")
754
+ print(" 3. ๋ฐœ๊ธ‰๋ฐ›์€ CLIENT_ID์™€ CLIENT_SECRET์„ api_utils.py์˜ NAVER_DATALAB_CONFIGS์— ์„ค์ •")
755
+ print(" 4. ํ˜„์žฌ๋Š” ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ์ •๋ณด๋งŒ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.")
756
  print()
757
  else:
758
+ print("โœ… ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ • ์™„๋ฃŒ - 1๋…„, 3๋…„ ํŠธ๋ Œ๋“œ ๋ถ„์„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!")
759
+ print()
760
+
761
+ if gemini_available:
762
+ print("โœ… Gemini API ์„ค์ • ์™„๋ฃŒ - ๋žœ๋ค ํ‚ค ๋กœํ…Œ์ด์…˜ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค!")
763
  print()
764
 
765
+ print("๐Ÿš€ v2.10 ๊ฐœ์„ ์‚ฌํ•ญ:")
766
+ print(" โ€ข 2๋‹จ๊ณ„: ์ˆ˜์ง‘๋œ ํ‚ค์›Œ๋“œ ๋ชฉ๋ก ๊ธฐ๋Šฅ ๏ฟฝ๏ฟฝ๏ฟฝ๊ฑฐ")
767
+ print(" โ€ข 3๋‹จ๊ณ„: ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ๋ถ„์„์˜ ์ƒํ’ˆ์ถ”์ถœ ๋ฐ ๋ถ„์„ ๊ธฐ๋Šฅ ์ œ๊ฑฐ")
768
+ print(" โ€ข 3๋‹จ๊ณ„: '๋ถ„์„ํ•  ํ‚ค์›Œ๋“œ ์„ ํƒ' โ†’ 'ํ‚ค์›Œ๋“œ ์‹ฌ์ธต๋ถ„์„ ์ž…๋ ฅ'์œผ๋กœ ๋ช…์นญ ๋ณ€๊ฒฝ")
769
+ print(" โ€ข ๐Ÿ“ˆ ๊ฒ€์ƒ‰๋Ÿ‰ ํŠธ๋ Œ๋“œ ๋ถ„์„๊ณผ ๐ŸŽฏ ํ‚ค์›Œ๋“œ ๋ถ„์„๋งŒ ํ‘œ์‹œ")
770
+ print(" โ€ข โœ… ์ถœ๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€: HTML ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ZIP ๋‹ค์šด๋กœ๋“œ")
771
+ print(" โ€ข โœ… Gemini API ํ‚ค ๋žœ๋ค ๋กœํ…Œ์ด์…˜ ์ ์šฉ")
772
+ print(" โ€ข โœ… ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค ๋žœ๋ค ๋กœํ…Œ์ด์…˜ ์ ์šฉ")
773
  print(" โ€ข โœ… ํ•œ๊ตญ์‹œ๊ฐ„ ๊ธฐ์ค€ ํŒŒ์ผ๋ช… ์ƒ์„ฑ")
774
  print(" โ€ข โœ… ๋ฉ€ํ‹ฐ ์‚ฌ์šฉ์ž ์•ˆ์ „: gr.State๋กœ ์„ธ์…˜๋ณ„ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ")
775
+ print(" โ€ข ๋ถˆํ•„์š”ํ•œ ๋ชจ๋“ˆ ์ž„ํฌํŠธ ์ œ๊ฑฐ๋กœ ์•ˆ์ •์„ฑ ํ–ฅ์ƒ")
 
776
  print()
777
 
778
  # ์•ฑ ์‹คํ–‰
export_utils.py ADDED
@@ -0,0 +1,478 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๊ด€๋ จ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ ๋ชจ์Œ - ์นดํ…Œ๊ณ ๋ฆฌ ํ•ญ๋ชฉ ์ œ๊ฑฐ
3
+ - HTML ํ…Œ์ด๋ธ” ์ƒ์„ฑ
4
+ - ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ
5
+ """
6
+
7
+ import pandas as pd
8
+ import tempfile
9
+ import os
10
+ import threading
11
+ import time
12
+ import logging
13
+
14
+ # ๋กœ๊น… ์„ค์ •
15
+ logger = logging.getLogger(__name__)
16
+ logger.setLevel(logging.INFO)
17
+ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18
+ handler = logging.StreamHandler()
19
+ handler.setFormatter(formatter)
20
+ logger.addHandler(handler)
21
+
22
+ # ์ž„์‹œ ํŒŒ์ผ ์ถ”์  ๋ฆฌ์ŠคํŠธ
23
+ _temp_files = []
24
+
25
+ def create_table_without_checkboxes(df):
26
+ """DataFrame์„ HTML ํ…Œ์ด๋ธ”๋กœ ๋ณ€ํ™˜ - ํ‚ค์›Œ๋“œ ํด๋ฆญ ์‹œ ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ์ด๋™ ๊ธฐ๋Šฅ ์ถ”๊ฐ€"""
27
+ if df.empty:
28
+ return "<p>๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</p>"
29
+
30
+ # === ์ˆ˜์ •๋œ ๋ถ€๋ถ„: ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ จ ์—ด ์ œ๊ฑฐ ===
31
+ df_display = df.copy()
32
+
33
+ # "์ƒํ’ˆ ๋“ฑ๋ก ์นดํ…Œ๊ณ ๋ฆฌ(์ƒ์œ„100์œ„)" ๋˜๋Š” "๊ด€๋ จ ์นดํ…Œ๊ณ ๋ฆฌ", "์นดํ…Œ๊ณ ๋ฆฌ ํ•ญ๋ชฉ" ์—ด์ด ์žˆ์œผ๋ฉด ์ œ๊ฑฐ
34
+ columns_to_remove = ["์ƒํ’ˆ ๋“ฑ๋ก ์นดํ…Œ๊ณ ๋ฆฌ(์ƒ์œ„100์œ„)", "๊ด€๋ จ ์นดํ…Œ๊ณ ๋ฆฌ", "์นดํ…Œ๊ณ ๋ฆฌ ํ•ญ๋ชฉ"]
35
+ for col in columns_to_remove:
36
+ if col in df_display.columns:
37
+ df_display = df_display.drop(columns=[col])
38
+ logger.info(f"ํ…Œ์ด๋ธ”์—์„œ '{col}' ์—ด ์ œ๊ฑฐ๋จ")
39
+
40
+ # HTML ํ…Œ์ด๋ธ” ์Šคํƒ€์ผ ์ •์˜ - Z-INDEX ์ˆ˜์ •
41
+ html = '''
42
+ <style>
43
+ .table-container {
44
+ position: relative;
45
+ width: 100%;
46
+ margin: 0;
47
+ border-radius: 8px;
48
+ overflow: hidden;
49
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
50
+ }
51
+
52
+ .header-wrap {
53
+ position: sticky;
54
+ top: 0;
55
+ z-index: 100; /* z-index ์ฆ๊ฐ€ */
56
+ background-color: #009879;
57
+ }
58
+
59
+ .styled-table {
60
+ width: 100%;
61
+ border-collapse: collapse;
62
+ table-layout: fixed;
63
+ margin: 0;
64
+ padding: 0;
65
+ font-size: 14px;
66
+ }
67
+
68
+ .styled-table th,
69
+ .styled-table td {
70
+ padding: 12px 15px;
71
+ text-align: left;
72
+ border-bottom: 1px solid #dddddd;
73
+ overflow: hidden;
74
+ text-overflow: ellipsis;
75
+ }
76
+
77
+ /* ๊ธด ํ…์ŠคํŠธ๊ฐ€ ์…€์—์„œ ์ค„๋ฐ”๊ฟˆ๋˜๋„๋ก ์ˆ˜์ • */
78
+ .styled-table td.col-rank {
79
+ white-space: normal;
80
+ word-break: break-word;
81
+ line-height: 1.3;
82
+ }
83
+
84
+ /* ๊ทธ ์™ธ ์—ด์€ ํ•œ ์ค„๋กœ ํ‘œ์‹œ */
85
+ .styled-table td.col-seq,
86
+ .styled-table td.col-keyword,
87
+ .styled-table td.col-pc,
88
+ .styled-table td.col-mobile,
89
+ .styled-table td.col-total,
90
+ .styled-table td.col-range,
91
+ .styled-table td.col-count {
92
+ white-space: nowrap;
93
+ }
94
+
95
+ .styled-table th {
96
+ background-color: #009879;
97
+ color: white;
98
+ font-weight: bold;
99
+ position: sticky;
100
+ top: 0;
101
+ white-space: nowrap;
102
+ z-index: 50; /* ํ—ค๋” z-index ์ฆ๊ฐ€ */
103
+ }
104
+
105
+ .styled-table tbody tr:nth-of-type(even) {
106
+ background-color: #f3f3f3;
107
+ }
108
+
109
+ .styled-table tbody tr:hover {
110
+ background-color: #f0f0f0;
111
+ }
112
+
113
+ .styled-table tbody tr:last-of-type {
114
+ border-bottom: 2px solid #009879;
115
+ }
116
+
117
+ /* ๋ฐ์ดํ„ฐ ์…€ z-index ์„ค์ • */
118
+ .styled-table tbody td {
119
+ position: relative;
120
+ z-index: 1; /* ๋ฐ์ดํ„ฐ ์…€์€ ๋‚ฎ์€ z-index */
121
+ }
122
+
123
+ .data-container {
124
+ max-height: 600px;
125
+ overflow-y: auto;
126
+ position: relative; /* position ์ถ”๊ฐ€ */
127
+ }
128
+
129
+ /* ์Šคํฌ๋กค๋ฐ” ์Šคํƒ€์ผ */
130
+ .data-container::-webkit-scrollbar {
131
+ width: 10px;
132
+ }
133
+
134
+ .data-container::-webkit-scrollbar-track {
135
+ background: #f1f1f1;
136
+ border-radius: 5px;
137
+ }
138
+
139
+ .data-container::-webkit-scrollbar-thumb {
140
+ background: #888;
141
+ border-radius: 5px;
142
+ }
143
+
144
+ .data-container::-webkit-scrollbar-thumb:hover {
145
+ background: #555;
146
+ }
147
+
148
+ /* ํ‚ค์›Œ๋“œ ๋งํฌ ์Šคํƒ€์ผ - ์ƒˆ๋กœ ์ถ”๊ฐ€ */
149
+ .keyword-link {
150
+ color: #2c5aa0;
151
+ text-decoration: none;
152
+ font-weight: 600;
153
+ cursor: pointer;
154
+ transition: all 0.3s ease;
155
+ display: inline-block;
156
+ padding: 2px 4px;
157
+ border-radius: 3px;
158
+ position: relative;
159
+ z-index: 5; /* ๋งํฌ z-index ์„ค์ • */
160
+ }
161
+
162
+ .keyword-link:hover {
163
+ color: #ffffff;
164
+ background-color: #2c5aa0;
165
+ text-decoration: none;
166
+ transform: translateY(-1px);
167
+ box-shadow: 0 2px 4px rgba(44, 90, 160, 0.3);
168
+ }
169
+
170
+ .keyword-link:active {
171
+ transform: translateY(0px);
172
+ }
173
+
174
+ /* ํ‚ค์›Œ๋“œ ์…€ ํŠน๋ณ„ ์Šคํƒ€์ผ */
175
+ .col-keyword {
176
+ position: relative;
177
+ }
178
+
179
+ .keyword-tooltip {
180
+ position: absolute;
181
+ bottom: 100%;
182
+ left: 50%;
183
+ transform: translateX(-50%);
184
+ background-color: #333;
185
+ color: white;
186
+ padding: 6px 10px;
187
+ border-radius: 4px;
188
+ font-size: 11px;
189
+ white-space: nowrap;
190
+ opacity: 0;
191
+ visibility: hidden;
192
+ transition: all 0.3s ease;
193
+ z-index: 1000; /* ํˆดํŒ์€ ๊ฐ€์žฅ ๋†’์€ z-index */
194
+ pointer-events: none;
195
+ margin-bottom: 5px;
196
+ }
197
+
198
+ .keyword-tooltip::after {
199
+ content: '';
200
+ position: absolute;
201
+ top: 100%;
202
+ left: 50%;
203
+ transform: translateX(-50%);
204
+ border: 4px solid transparent;
205
+ border-top-color: #333;
206
+ }
207
+
208
+ .keyword-link:hover .keyword-tooltip {
209
+ opacity: 1;
210
+ visibility: visible;
211
+ }
212
+
213
+ /* === ์ˆ˜์ •๋œ ๋ถ€๋ถ„: ์—ด ๋„ˆ๋น„ ์ •์˜ - ์นดํ…Œ๊ณ ๋ฆฌ ์—ด ์ œ๊ฑฐ ํ›„ ์กฐ์ • === */
214
+ .col-seq { width: 8%; }
215
+ .col-keyword { width: 25%; }
216
+ .col-pc { width: 12%; }
217
+ .col-mobile { width: 12%; }
218
+ .col-total { width: 12%; }
219
+ .col-range { width: 12%; }
220
+ .col-rank { width: 15%; }
221
+ .col-count { width: 10%; }
222
+
223
+ .truncated-text {
224
+ position: relative;
225
+ cursor: pointer;
226
+ z-index: 2; /* ํ…์ŠคํŠธ z-index ์„ค์ • */
227
+ }
228
+
229
+ .truncated-text:hover::after {
230
+ content: attr(data-full-text);
231
+ position: absolute;
232
+ left: 0;
233
+ top: 100%;
234
+ z-index: 99;
235
+ min-width: 200px;
236
+ max-width: 400px;
237
+ padding: 8px;
238
+ background-color: #fff;
239
+ border: 1px solid #ddd;
240
+ border-radius: 4px;
241
+ box-shadow: 0 2px 5px rgba(0,0,0,0.2);
242
+ white-space: normal;
243
+ }
244
+
245
+ /* ํ‚ค์›Œ๋“œ ํƒœ๊ทธ ์Šคํƒ€์ผ */
246
+ .keyword-tag-container {
247
+ margin-top: 20px;
248
+ padding: 10px;
249
+ border: 1px solid #ddd;
250
+ border-radius: 5px;
251
+ background-color: #f9f9f9;
252
+ }
253
+
254
+ .keyword-tag {
255
+ display: inline-block;
256
+ background-color: #009879;
257
+ color: white;
258
+ padding: 5px 10px;
259
+ margin: 5px;
260
+ border-radius: 15px;
261
+ font-size: 12px;
262
+ }
263
+
264
+ .category-tag {
265
+ display: inline-block;
266
+ background-color: #2c7fb8;
267
+ color: white;
268
+ padding: 5px 10px;
269
+ margin: 5px;
270
+ border-radius: 15px;
271
+ font-size: 12px;
272
+ }
273
+
274
+ /* ๋ถ„์„ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ์Šคํƒ€์ผ */
275
+ .analysis-result {
276
+ margin-top: 30px;
277
+ border: 1px solid #ddd;
278
+ border-radius: 5px;
279
+ padding: 15px;
280
+ background-color: #f9f9f9;
281
+ }
282
+
283
+ .result-header {
284
+ font-weight: bold;
285
+ margin-bottom: 10px;
286
+ color: #009879;
287
+ }
288
+
289
+ .match-item {
290
+ margin: 5px 0;
291
+ padding: 5px;
292
+ border-bottom: 1px solid #eee;
293
+ }
294
+
295
+ .match-keyword {
296
+ font-weight: bold;
297
+ color: #2c7fb8;
298
+ }
299
+
300
+ .match-count {
301
+ display: inline-block;
302
+ background-color: #009879;
303
+ color: white;
304
+ padding: 2px 8px;
305
+ border-radius: 10px;
306
+ font-size: 12px;
307
+ margin-left: 10px;
308
+ }
309
+ </style>
310
+ '''
311
+
312
+ # === ์ˆ˜์ •๋œ ๋ถ€๋ถ„: ์—ด ์ด๋ฆ„๊ณผ ํด๋ž˜์Šค ๋งคํ•‘ - ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ จ ์ œ๊ฑฐ ===
313
+ col_mapping = {
314
+ "์ˆœ๋ฒˆ": "col-seq",
315
+ "์กฐํ•ฉ ํ‚ค์›Œ๋“œ": "col-keyword",
316
+ "PC๊ฒ€์ƒ‰๋Ÿ‰": "col-pc",
317
+ "๋ชจ๋ฐ”์ผ๊ฒ€์ƒ‰๋Ÿ‰": "col-mobile",
318
+ "์ด๊ฒ€์ƒ‰๋Ÿ‰": "col-total",
319
+ "๊ฒ€์ƒ‰๋Ÿ‰๊ตฌ๊ฐ„": "col-range",
320
+ "ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ์ž์ˆœ์œ„": "col-rank",
321
+ "ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํšŸ์ˆ˜": "col-count"
322
+ # ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ จ ๋งคํ•‘ ์ œ๊ฑฐ๋จ
323
+ }
324
+
325
+ # ํ…Œ์ด๋ธ” ์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘
326
+ html += '<div class="table-container">'
327
+
328
+ # ๋‹จ์ผ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝ (ํ—ค๋”๋Š” position: sticky๋กœ ๊ณ ์ •)
329
+ html += '<div class="data-container">'
330
+ html += '<table class="styled-table">'
331
+
332
+ # colgroup์œผ๋กœ ์—ด ๋„ˆ๋น„ ์ •์˜
333
+ html += '<colgroup>'
334
+ html += f'<col class="{col_mapping["์ˆœ๋ฒˆ"]}">'
335
+ for col in df_display.columns:
336
+ col_class = col_mapping.get(col, "")
337
+ html += f'<col class="{col_class}">'
338
+ html += '</colgroup>'
339
+
340
+ # ํ…Œ์ด๋ธ” ํ—ค๋”
341
+ html += '<thead>'
342
+ html += '<tr>'
343
+ html += f'<th class="{col_mapping["์ˆœ๋ฒˆ"]}">์ˆœ๋ฒˆ</th>'
344
+ for col in df_display.columns:
345
+ col_class = col_mapping.get(col, "")
346
+ html += f'<th class="{col_class}">{col}</th>'
347
+ html += '</tr>'
348
+ html += '</thead>'
349
+
350
+ # ํ…Œ์ด๋ธ” ๋ณธ๋ฌธ
351
+ html += '<tbody>'
352
+ for idx, row in df_display.iterrows():
353
+ html += '<tr>'
354
+ # ์ˆœ๋ฒˆ ํ‘œ์‹œ - 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ˆœ์ฐจ์  ๋ฒˆํ˜ธ
355
+ html += f'<td class="{col_mapping["์ˆœ๋ฒˆ"]}">{idx + 1}</td>'
356
+
357
+ # ๋ฐ์ดํ„ฐ ์…€ ์ถ”๊ฐ€
358
+ for col in df_display.columns:
359
+ col_class = col_mapping.get(col, "")
360
+ value = str(row[col])
361
+
362
+ if col == "ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ์ž์ˆœ์œ„":
363
+ # ๊ธด ํ…์ŠคํŠธ์˜ ์…€์€ ๊ทธ๋Œ€๋กœ ํ‘œ์‹œ (์ค„๋ฐ”๊ฟˆ ํ—ˆ์šฉ)
364
+ html += f'<td class="{col_class}">{value}</td>'
365
+ elif len(value) > 30:
366
+ # ๋‹ค๋ฅธ ๊ธด ํ…์ŠคํŠธ๋Š” hover๋กœ ์ „์ฒด ํ‘œ์‹œ
367
+ html += f'<td class="{col_class}"><div class="truncated-text" data-full-text="{value}">{value[:30]}...</div></td>'
368
+ else:
369
+ # ์ผ๋ฐ˜ ํ…์ŠคํŠธ
370
+ html += f'<td class="{col_class}">{value}</td>'
371
+ html += '</tr>'
372
+
373
+ html += '</tbody>'
374
+ html += '</table>'
375
+ html += '</div>' # data-container ๋‹ซ๊ธฐ
376
+ html += '</div>' # table-container ๋‹ซ๊ธฐ
377
+
378
+ return html
379
+
380
+ def cleanup_temp_files(delay=300):
381
+ """์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ ํ•จ์ˆ˜"""
382
+ global _temp_files
383
+
384
+ def cleanup():
385
+ time.sleep(delay) # ์ง€์ •๋œ ์‹œ๊ฐ„ ๋Œ€๊ธฐ
386
+ temp_files_to_remove = _temp_files.copy()
387
+ _temp_files = []
388
+
389
+ for file_path in temp_files_to_remove:
390
+ try:
391
+ if os.path.exists(file_path):
392
+ os.remove(file_path)
393
+ logger.info(f"์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ: {file_path}")
394
+ except Exception as e:
395
+ logger.error(f"ํŒŒ์ผ ์‚ญ์ œ ์˜ค๋ฅ˜: {e}")
396
+
397
+ # ์ƒˆ ์Šค๋ ˆ๋“œ ์‹œ์ž‘
398
+ threading.Thread(target=cleanup, daemon=True).start()
399
+
400
+ def download_keywords(df, auto_cleanup=True, cleanup_delay=300):
401
+ """ํ‚ค์›Œ๋“œ ๋ฐ์ดํ„ฐ๋ฅผ ์—‘์…€ ํŒŒ์ผ๋กœ ๋‹ค์šด๋กœ๋“œ - ์นดํ…Œ๊ณ ๋ฆฌ ํ•ญ๋ชฉ ์ œ๊ฑฐ"""
402
+ global _temp_files
403
+
404
+ if df is None or df.empty:
405
+ return None
406
+
407
+ # ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
408
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
409
+ temp_file.close()
410
+ filename = temp_file.name
411
+
412
+ # ์ž„์‹œ ํŒŒ์ผ ์ถ”์  ๋ชฉ๋ก์— ์ถ”๊ฐ€
413
+ _temp_files.append(filename)
414
+
415
+ # === ์ˆ˜์ •๋œ ๋ถ€๋ถ„: ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ จ ์—ด ์ œ๊ฑฐ ===
416
+ df_export = df.copy()
417
+
418
+ # ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ จ ์—ด๋“ค ์ œ๊ฑฐ
419
+ columns_to_remove = ["์ƒํ’ˆ ๋“ฑ๋ก ์นดํ…Œ๊ณ ๋ฆฌ(์ƒ์œ„100์œ„)", "๊ด€๋ จ ์นดํ…Œ๊ณ ๋ฆฌ", "์นดํ…Œ๊ณ ๋ฆฌ ํ•ญ๋ชฉ"]
420
+ for col in columns_to_remove:
421
+ if col in df_export.columns:
422
+ df_export = df_export.drop(columns=[col])
423
+ logger.info(f"์—‘์…€ ๋‚ด๋ณด๋‚ด๊ธฐ์—์„œ '{col}' ์—ด ์ œ๊ฑฐ๋จ")
424
+
425
+ # ํ‚ค์›Œ๋“œ ๋ฐ์ดํ„ฐ๋ฅผ ์—‘์…€ ํŒŒ์ผ๋กœ ์ €์žฅ
426
+ with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
427
+ # ํ‚ค์›Œ๋“œ ๋ชฉ๋ก ์‹œํŠธ
428
+ df_export.to_excel(writer, sheet_name='ํ‚ค์›Œ๋“œ ๋ชฉ๋ก', index=False)
429
+
430
+ # ์—ด ๋„ˆ๋น„ ์กฐ์ • - ์นดํ…Œ๊ณ ๋ฆฌ ์—ด ์ œ๊ฑฐ ํ›„ ์กฐ์ •
431
+ worksheet = writer.sheets['ํ‚ค์›Œ๋“œ ๋ชฉ๋ก']
432
+ worksheet.set_column('A:A', 20) # ์กฐํ•ฉ ํ‚ค์›Œ๋“œ ์—ด
433
+ worksheet.set_column('B:B', 12) # PC๊ฒ€์ƒ‰๋Ÿ‰ ์—ด
434
+ worksheet.set_column('C:C', 12) # ๋ชจ๋ฐ”์ผ๊ฒ€์ƒ‰๋Ÿ‰ ์—ด
435
+ worksheet.set_column('D:D', 12) # ์ด๊ฒ€์ƒ‰๋Ÿ‰ ์—ด
436
+ worksheet.set_column('E:E', 12) # ๊ฒ€์ƒ‰๋Ÿ‰๊ตฌ๊ฐ„ ์—ด
437
+ worksheet.set_column('F:F', 20) # ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ์ž์ˆœ์œ„ ์—ด
438
+ worksheet.set_column('G:G', 12) # ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํšŸ์ˆ˜ ์—ด
439
+ # ์นดํ…Œ๊ณ ๋ฆฌ ์—ด๋“ค ์ œ๊ฑฐ๋กœ H, I ์—ด ์„ค์ • ์ œ๊ฑฐ๋จ
440
+
441
+ # ํ—ค๋” ํ˜•์‹ ์„ค์ •
442
+ header_format = writer.book.add_format({
443
+ 'bold': True,
444
+ 'bg_color': '#009879',
445
+ 'color': 'white',
446
+ 'border': 1
447
+ })
448
+
449
+ # ํ—ค๋”์— ํ˜•์‹ ์ ์šฉ
450
+ for col_num, value in enumerate(df_export.columns.values):
451
+ worksheet.write(0, col_num, value, header_format)
452
+
453
+ logger.info(f"์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ: {filename}")
454
+
455
+ # ํŒŒ์ผ ์ž๋™ ์ •๋ฆฌ ์˜ต์…˜
456
+ if auto_cleanup:
457
+ # ๋ณ„๋„ ์ •๋ฆฌ ์ž‘์—… ์š”์ฒญ ์—†์ด ์ถ”์  ๋ชฉ๋ก์— ์ถ”๊ฐ€๋งŒ ํ•˜์—ฌ ์ผ๊ด„ ์ฒ˜๋ฆฌ
458
+ pass
459
+
460
+ return filename
461
+
462
+ def register_cleanup_handlers():
463
+ """์•ฑ ์ข…๋ฃŒ ์‹œ ์ •๋ฆฌ๋ฅผ ์œ„ํ•œ ํ•ธ๋“ค๋Ÿฌ ๋“ฑ๋ก"""
464
+ import atexit
465
+
466
+ def cleanup_all_temp_files():
467
+ global _temp_files
468
+ for file_path in _temp_files:
469
+ try:
470
+ if os.path.exists(file_path):
471
+ os.remove(file_path)
472
+ logger.info(f"์ข…๋ฃŒ ์‹œ ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ: {file_path}")
473
+ except Exception as e:
474
+ logger.error(f"ํŒŒ์ผ ์‚ญ์ œ ์˜ค๋ฅ˜: {e}")
475
+ _temp_files = []
476
+
477
+ # ์•ฑ ์ข…๋ฃŒ ์‹œ ์‹คํ–‰๋  ํ•จ์ˆ˜ ๋“ฑ๋ก
478
+ atexit.register(cleanup_all_temp_files)
keyword_analysis.py ADDED
@@ -0,0 +1,1773 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ํ‚ค์›Œ๋“œ ๋งค์นญ ๋ฐ ์ƒ์Šนํญ ๊ณ„์‚ฐ ๊ฐœ์„  - ์ „์ฒด ์ฝ”๋“œ
3
+ v4.0 - ์ŠคํŽ˜์ด์Šค๋ฐ” ์ฒ˜๋ฆฌ ๊ฐœ์„  + ์˜ฌ๋ฐ”๋ฅธ ํŠธ๋ Œ๋“œ ๋ถ„์„ ๋กœ์ง ์ ์šฉ
4
+ - ๊ธฐ์กด ๋ชจ๋“  ๊ธฐ๋Šฅ ์œ ์ง€ํ•˜๋ฉด์„œ ์ตœ์ ํ™”
5
+ - ์ŠคํŽ˜์ด์Šค๋ฐ” ์ œ๊ฑฐ ํ›„ ๊ฒ€์ƒ‰/๋น„๊ต ๋กœ์ง ์ ์šฉ
6
+ - ์˜ฌ๋ฐ”๋ฅธ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ: ์˜ฌํ•ด ์™„๋ฃŒ์›” vs ์ž‘๋…„ ๋™์›”
7
+ - ๐Ÿ”– ๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›”: ์‹ค์ œ+์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ค‘ ์ตœ๋Œ€๊ฐ’
8
+ - ๐Ÿ”– ๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”: ์—ฐ์†๋œ ์›”๊ฐ„ ์ƒ์Šน๋ฅ  ์ค‘ ์ตœ๋Œ€๊ฐ’
9
+ """
10
+
11
+ import logging
12
+ import pandas as pd
13
+ from datetime import datetime
14
+ import re
15
+ import time
16
+ import random
17
+ from typing import Dict, List, Optional
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+ def normalize_keyword(keyword):
22
+ """ํ‚ค์›Œ๋“œ ์ •๊ทœํ™” - ์ŠคํŽ˜์ด์Šค๋ฐ” ์ฒ˜๋ฆฌ ๊ฐœ์„ """
23
+ if not keyword:
24
+ return ""
25
+
26
+ # 1. ์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ
27
+ keyword = keyword.strip()
28
+
29
+ # 2. ์—ฐ์†๋œ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜๋กœ ๋ณ€๊ฒฝ
30
+ keyword = re.sub(r'\s+', ' ', keyword)
31
+
32
+ # 3. ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ (ํ•œ๊ธ€, ์˜๋ฌธ, ์ˆซ์ž, ๊ณต๋ฐฑ๋งŒ ๋‚จ๊น€)
33
+ keyword = re.sub(r'[^\w\s๊ฐ€-ํžฃ]', '', keyword)
34
+
35
+ return keyword
36
+
37
+ def normalize_keyword_for_api(keyword):
38
+ """API ํ˜ธ์ถœ์šฉ ํ‚ค์›Œ๋“œ ์ •๊ทœํ™” (์ŠคํŽ˜์ด์Šค ์ œ๊ฑฐ)"""
39
+ normalized = normalize_keyword(keyword)
40
+ return normalized.replace(" ", "")
41
+
42
+ def normalize_keyword_for_comparison(keyword):
43
+ """๋น„๊ต์šฉ ํ‚ค์›Œ๋“œ ์ •๊ทœํ™” (์ŠคํŽ˜์ด์Šค ์œ ์ง€)"""
44
+ return normalize_keyword(keyword).lower()
45
+
46
+ def normalize_keyword_advanced(keyword):
47
+ """๊ณ ๊ธ‰ ํ‚ค์›Œ๋“œ ์ •๊ทœํ™” - ๋งค์นญ ๋ฌธ์ œ ํ•ด๊ฒฐ"""
48
+ if not keyword:
49
+ return ""
50
+
51
+ # 1. ๊ธฐ๋ณธ ์ •๋ฆฌ
52
+ keyword = str(keyword).strip()
53
+
54
+ # 2. ์—ฐ์†๋œ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜๋กœ ๋ณ€๊ฒฝ
55
+ keyword = re.sub(r'\s+', ' ', keyword)
56
+
57
+ # 3. ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ (ํ•œ๊ธ€, ์˜๋ฌธ, ์ˆซ์ž, ๊ณต๋ฐฑ๋งŒ ๋‚จ๊น€)
58
+ keyword = re.sub(r'[^\w\s๊ฐ€-ํžฃ]', '', keyword)
59
+
60
+ # 4. ์†Œ๋ฌธ์ž ๋ณ€ํ™˜
61
+ keyword = keyword.lower()
62
+
63
+ return keyword
64
+
65
+ def create_keyword_variations(keyword):
66
+ """ํ‚ค์›Œ๋“œ ๋ณ€ํ˜• ๋ฒ„์ „๋“ค ์ƒ์„ฑ - ์ŠคํŽ˜์ด์Šค๋ฐ” ์ฒ˜๋ฆฌ ๊ฐ•ํ™”"""
67
+ base = normalize_keyword_advanced(keyword)
68
+ variations = [base]
69
+
70
+ # ์ŠคํŽ˜์ด์Šค ์ œ๊ฑฐ ๋ฒ„์ „
71
+ no_space = base.replace(" ", "")
72
+ if no_space != base:
73
+ variations.append(no_space)
74
+
75
+ # ์ŠคํŽ˜์ด์Šค๋ฅผ ๋‹ค๋ฅธ ๊ตฌ๋ถ„์ž๋กœ ๋ฐ”๊พผ ๋ฒ„์ „๋“ค
76
+ variations.append(base.replace(" ", "-"))
77
+ variations.append(base.replace(" ", "_"))
78
+
79
+ # ๋‹จ์–ด ์ˆœ์„œ ๋ฐ”๊พผ ๋ฒ„์ „ (2๋‹จ์–ด์ธ ๊ฒฝ์šฐ)
80
+ words = base.split()
81
+ if len(words) == 2:
82
+ reversed_keyword = f"{words[1]} {words[0]}"
83
+ variations.append(reversed_keyword)
84
+ variations.append(reversed_keyword.replace(" ", ""))
85
+
86
+ return list(set(variations)) # ์ค‘๋ณต ์ œ๊ฑฐ
87
+
88
+ def find_matching_keyword_row(analysis_keyword, keywords_df):
89
+ """๊ฐœ์„ ๋œ ํ‚ค์›Œ๋“œ ๋งค์นญ ํ•จ์ˆ˜"""
90
+ if keywords_df is None or keywords_df.empty:
91
+ return None
92
+
93
+ analysis_variations = create_keyword_variations(analysis_keyword)
94
+
95
+ logger.info(f"๋ถ„์„ ํ‚ค์›Œ๋“œ ๋ณ€ํ˜•๋“ค: {analysis_variations}")
96
+
97
+ # 1์ฐจ: ์ •ํ™•ํ•œ ๋งค์นญ
98
+ for idx, row in keywords_df.iterrows():
99
+ df_keyword = str(row.get('์กฐํ•ฉ ํ‚ค์›Œ๋“œ', ''))
100
+ df_variations = create_keyword_variations(df_keyword)
101
+
102
+ for analysis_var in analysis_variations:
103
+ for df_var in df_variations:
104
+ if analysis_var == df_var and len(analysis_var) > 1:
105
+ logger.info(f"์ •ํ™•ํ•œ ๋งค์นญ ์„ฑ๊ณต: '{analysis_keyword}' = '{df_keyword}'")
106
+ return row
107
+
108
+ # 2์ฐจ: ํฌํ•จ ๊ด€๊ณ„ ๋งค์นญ
109
+ for idx, row in keywords_df.iterrows():
110
+ df_keyword = str(row.get('์กฐํ•ฉ ํ‚ค์›Œ๋“œ', ''))
111
+ df_variations = create_keyword_variations(df_keyword)
112
+
113
+ for analysis_var in analysis_variations:
114
+ for df_var in df_variations:
115
+ if len(analysis_var) > 2 and len(df_var) > 2:
116
+ if analysis_var in df_var or df_var in analysis_var:
117
+ similarity = len(set(analysis_var) & set(df_var)) / len(set(analysis_var) | set(df_var))
118
+ if similarity > 0.7: # 70% ์ด์ƒ ์œ ์‚ฌ
119
+ logger.info(f"๋ถ€๋ถ„ ๋งค์นญ ์„ฑ๊ณต: '{analysis_keyword}' โ‰ˆ '{df_keyword}' (์œ ์‚ฌ๋„: {similarity:.2f})")
120
+ return row
121
+
122
+ logger.warning(f"ํ‚ค์›Œ๋“œ ๋งค์นญ ์‹คํŒจ: '{analysis_keyword}'")
123
+ logger.info(f"๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ‚ค์›Œ๋“œ ์ƒ˜ํ”Œ: {keywords_df['์กฐํ•ฉ ํ‚ค์›Œ๋“œ'].head(5).tolist()}")
124
+ return None
125
+
126
+ def generate_prediction_data(trend_data_3year, keyword):
127
+ """
128
+ ์ •๊ตํ•œ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ํ•จ์ˆ˜
129
+ - ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ํ›„ ๋ฐ”๋กœ ํ˜ธ์ถœํ•˜์—ฌ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
130
+ - ๊ณ„์ ˆ์„ฑ, ์ฆ๊ฐ ํŠธ๋ Œ๋“œ, ์ „๋…„ ๋Œ€๋น„ ์„ฑ์žฅ๋ฅ  ๋ชจ๋‘ ๊ณ ๋ ค
131
+ """
132
+ if not trend_data_3year:
133
+ logger.warning("โŒ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์‹คํŒจ: trend_data_3year ์—†์Œ")
134
+ return trend_data_3year
135
+
136
+ try:
137
+ current_date = datetime.now()
138
+ current_year = current_date.year
139
+ current_month = current_date.month
140
+
141
+ logger.info(f"๐Ÿ”ฎ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์‹œ์ž‘: {keyword} ({current_year}๋…„ {current_month}์›” ๊ธฐ์ค€)")
142
+
143
+ for kw, data in trend_data_3year.items():
144
+ if not data or not data.get('monthly_volumes') or not data.get('dates'):
145
+ continue
146
+
147
+ volumes = data['monthly_volumes']
148
+ dates = data['dates']
149
+
150
+ # โœ… 1๋‹จ๊ณ„: ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๋ถ„์„
151
+ yearly_data = {} # {year: {month: volume}}
152
+
153
+ for i, date_str in enumerate(dates):
154
+ try:
155
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
156
+ if i < len(volumes):
157
+ volume = volumes[i]
158
+ if isinstance(volume, str):
159
+ volume = float(volume.replace(',', ''))
160
+ volume = int(volume) if volume else 0
161
+
162
+ year = date_obj.year
163
+ month = date_obj.month
164
+
165
+ if year not in yearly_data:
166
+ yearly_data[year] = {}
167
+ yearly_data[year][month] = volume
168
+
169
+ except Exception as e:
170
+ logger.warning(f"โš ๏ธ ๋‚ ์งœ ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {date_str}")
171
+ continue
172
+
173
+ logger.info(f"๐Ÿ“Š ๋ถ„์„๋œ ์—ฐ๋„: {list(yearly_data.keys())}")
174
+
175
+ # โœ… 2๋‹จ๊ณ„: ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜
176
+ if current_year not in yearly_data:
177
+ yearly_data[current_year] = {}
178
+
179
+ current_year_data = yearly_data[current_year]
180
+ last_year_data = yearly_data.get(current_year - 1, {})
181
+ two_years_ago_data = yearly_data.get(current_year - 2, {})
182
+
183
+ logger.info(f"๐Ÿ“ˆ ์˜ฌํ•ด ์‹ค์ œ ๋ฐ์ดํ„ฐ: {len(current_year_data)}๊ฐœ์›”")
184
+ logger.info(f"๐Ÿ“ˆ ์ž‘๋…„ ์ฐธ์กฐ ๋ฐ์ดํ„ฐ: {len(last_year_data)}๊ฐœ์›”")
185
+
186
+ # ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ (ํ˜„์žฌ์›” ์ดํ›„)
187
+ for future_month in range(current_month + 1, 13):
188
+ if future_month in current_year_data:
189
+ continue # ์ด๋ฏธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ์Šคํ‚ต
190
+
191
+ predicted_volume = calculate_predicted_volume(
192
+ future_month, current_year_data, last_year_data,
193
+ two_years_ago_data, current_month
194
+ )
195
+
196
+ if predicted_volume is not None:
197
+ current_year_data[future_month] = predicted_volume
198
+ logger.info(f"๐Ÿ”ฎ ์˜ˆ์ƒ ์ƒ์„ฑ: {current_year}๋…„ {future_month}์›” = {predicted_volume:,}ํšŒ")
199
+
200
+ # โœ… 3๋‹จ๊ณ„: ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์›๋ณธ ๊ตฌ์กฐ์— ํ†ตํ•ฉ
201
+ updated_volumes = []
202
+ updated_dates = []
203
+
204
+ # ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ†ตํ•ฉ
205
+ all_months = []
206
+ for year in sorted(yearly_data.keys()):
207
+ for month in sorted(yearly_data[year].keys()):
208
+ all_months.append((year, month, yearly_data[year][month]))
209
+
210
+ for year, month, volume in all_months:
211
+ updated_volumes.append(volume)
212
+ updated_dates.append(f"{year}-{month:02d}-01")
213
+
214
+ # ์›๋ณธ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ
215
+ data['monthly_volumes'] = updated_volumes
216
+ data['dates'] = updated_dates
217
+
218
+ logger.info(f"โœ… {kw} ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ํ†ตํ•ฉ ์™„๋ฃŒ: ์ด {len(updated_volumes)}๊ฐœ์›”")
219
+
220
+ logger.info(f"๐ŸŽ‰ ์ „์ฒด ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์™„๋ฃŒ: {keyword}")
221
+ return trend_data_3year
222
+
223
+ except Exception as e:
224
+ logger.error(f"โŒ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์˜ค๋ฅ˜: {e}")
225
+ return trend_data_3year
226
+
227
+ def calculate_predicted_volume(target_month, current_year_data, last_year_data,
228
+ two_years_ago_data, current_month):
229
+ """
230
+ ์ •๊ตํ•œ ์˜ˆ์ƒ ๋ณผ๋ฅจ ๊ณ„์‚ฐ
231
+ - ๋‹ค์ค‘ ์š”์ธ ๊ณ ๋ ค: ์ž‘๋…„ ๋™์›”, ์ฆ๊ฐ ํŠธ๋ Œ๋“œ, ๊ณ„์ ˆ์„ฑ, ์„ฑ์žฅ๋ฅ 
232
+ """
233
+ try:
234
+ # ๊ธฐ์ค€ ๊ฐ’๋“ค
235
+ last_year_same_month = last_year_data.get(target_month, 0)
236
+ two_years_ago_same_month = two_years_ago_data.get(target_month, 0)
237
+
238
+ if last_year_same_month == 0:
239
+ logger.warning(f"โš ๏ธ {target_month}์›” ์ž‘๋…„ ๋ฐ์ดํ„ฐ ์—†์Œ")
240
+ return None
241
+
242
+ # โœ… 1. ๊ธฐ๋ณธ๊ฐ’: ์ž‘๋…„ ๋™์›”
243
+ base_volume = last_year_same_month
244
+
245
+ # โœ… 2. ์ „๋…„ ๋Œ€๋น„ ์„ฑ์žฅ๋ฅ  ๊ณ„์‚ฐ (๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ)
246
+ growth_rate = 1.0
247
+ if two_years_ago_same_month > 0:
248
+ growth_rate = last_year_same_month / two_years_ago_same_month
249
+ logger.info(f"๐Ÿ“ˆ {target_month}์›” ์ „๋…„ ์„ฑ์žฅ๋ฅ : {growth_rate:.2f}๋ฐฐ")
250
+
251
+ # โœ… 3. ์˜ฌํ•ด ์ตœ๊ทผ ํŠธ๋ Œ๋“œ ๋ฐ˜์˜
252
+ trend_factor = 1.0
253
+ if len(current_year_data) >= 2:
254
+ # ์ตœ๊ทผ 2-3๊ฐœ์›”์˜ ์ž‘๋…„ ๋Œ€๋น„ ๋น„์œจ ๊ณ„์‚ฐ
255
+ recent_ratios = []
256
+ for month in range(max(1, current_month - 2), current_month + 1):
257
+ if month in current_year_data and month in last_year_data:
258
+ if last_year_data[month] > 0:
259
+ ratio = current_year_data[month] / last_year_data[month]
260
+ recent_ratios.append(ratio)
261
+
262
+ if recent_ratios:
263
+ trend_factor = sum(recent_ratios) / len(recent_ratios)
264
+ logger.info(f"๐Ÿ“Š ์ตœ๊ทผ ํŠธ๋ Œ๋“œ ํŒฉํ„ฐ: {trend_factor:.2f}")
265
+
266
+ # โœ… 4. ๊ณ„์ ˆ์„ฑ ๋ณด์ • (๊ฐ™์€ ๋ถ„๊ธฐ ๋‚ด ์›”๊ฐ„ ํŒจํ„ด)
267
+ seasonal_factor = 1.0
268
+ if target_month > 1 and target_month - 1 in last_year_data and target_month in last_year_data:
269
+ # ์ž‘๋…„ ๋™์ผ ๊ตฌ๊ฐ„์˜ ์›”๊ฐ„ ๋ณ€ํ™”์œจ
270
+ if last_year_data[target_month - 1] > 0:
271
+ seasonal_factor = last_year_data[target_month] / last_year_data[target_month - 1]
272
+ logger.info(f"๐ŸŒŠ {target_month}์›” ๊ณ„์ ˆ์„ฑ ํŒฉํ„ฐ: {seasonal_factor:.2f}")
273
+
274
+ # โœ… 5. ์ตœ์ข… ์˜ˆ์ƒ๊ฐ’ ๊ณ„์‚ฐ (๊ฐ€์ค‘ํ‰๊ท )
275
+ predicted_volume = int(
276
+ base_volume * (
277
+ 0.4 * growth_rate + # 40% ์ „๋…„ ์„ฑ์žฅ๋ฅ 
278
+ 0.4 * trend_factor + # 40% ์ตœ๊ทผ ํŠธ๋ Œ๋“œ
279
+ 0.2 * seasonal_factor # 20% ๊ณ„์ ˆ์„ฑ
280
+ )
281
+ )
282
+
283
+ # โœ… 6. ํ•ฉ๋ฆฌ์„ฑ ๊ฒ€์ฆ (๊ธ‰๊ฒฉํ•œ ๋ณ€ํ™” ๋ฐฉ์ง€)
284
+ if current_year_data:
285
+ recent_avg = sum(current_year_data.values()) / len(current_year_data)
286
+ if predicted_volume > recent_avg * 5: # 5๋ฐฐ ์ด์ƒ ๊ธ‰์ฆ ๋ฐฉ์ง€
287
+ predicted_volume = int(recent_avg * 2)
288
+ logger.warning(f"โš ๏ธ {target_month}์›” ๊ธ‰์ฆ ๋ณด์ •: {predicted_volume:,}ํšŒ")
289
+ elif predicted_volume < recent_avg * 0.1: # 10๋ถ„์˜ 1 ์ดํ•˜ ๊ธ‰๊ฐ ๋ฐฉ์ง€
290
+ predicted_volume = int(recent_avg * 0.5)
291
+ logger.warning(f"โš ๏ธ {target_month}์›” ๊ธ‰๊ฐ ๋ณด์ •: {predicted_volume:,}ํšŒ")
292
+
293
+ logger.info(f"๐ŸŽฏ {target_month}์›” ์˜ˆ์ƒ ๊ณ„์‚ฐ: {last_year_same_month:,} ร— (์„ฑ์žฅ{growth_rate:.2f} + ํŠธ๋ Œ๋“œ{trend_factor:.2f} + ๊ณ„์ ˆ{seasonal_factor:.2f}) = {predicted_volume:,}")
294
+
295
+ return predicted_volume
296
+
297
+ except Exception as e:
298
+ logger.error(f"โŒ {target_month}์›” ์˜ˆ์ƒ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
299
+ return None
300
+
301
+ def enhance_trend_data_with_predictions(trend_data_3year, keyword):
302
+ """
303
+ ๊ธฐ์กด ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ์— ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
304
+ - ๋ฉ”์ธ ํŠธ๋ Œ๋“œ ์ˆ˜์ง‘ ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœ
305
+ """
306
+ if not trend_data_3year:
307
+ return trend_data_3year
308
+
309
+ logger.info(f"๐Ÿš€ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ์˜ˆ์ƒ ํ™•์žฅ ์‹œ์ž‘: {keyword}")
310
+
311
+ enhanced_data = generate_prediction_data(trend_data_3year, keyword)
312
+
313
+ # ๋ฐ์ดํ„ฐ ํ’ˆ์งˆ ๊ฒ€์ฆ
314
+ for kw, data in enhanced_data.items():
315
+ if data and data.get('monthly_volumes'):
316
+ total_months = len(data['monthly_volumes'])
317
+ current_year = datetime.now().year
318
+
319
+ # ํ˜„์žฌ ์—ฐ๋„ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ ํ™•์ธ
320
+ current_year_count = 0
321
+ for date_str in data['dates']:
322
+ try:
323
+ if date_str.startswith(str(current_year)):
324
+ current_year_count += 1
325
+ except:
326
+ continue
327
+
328
+ logger.info(f"โœ… {kw} ์ตœ์ข… ๋ฐ์ดํ„ฐ: ์ „์ฒด {total_months}๊ฐœ์›”, ์˜ฌํ•ด {current_year_count}๊ฐœ์›”")
329
+
330
+ return enhanced_data
331
+
332
+ def calculate_max_growth_rate_with_predictions(trend_data_3year, keyword):
333
+ """์˜ฌ๋ฐ”๋ฅธ ํŠธ๋ Œ๋“œ ๋ถ„์„ ๋กœ์ง - ์‚ฌ์šฉ์ž ์š”๊ตฌ์‚ฌํ•ญ ์ ์šฉ"""
334
+ if not trend_data_3year:
335
+ logger.error("โŒ trend_data_3year๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค")
336
+ return "๋ฐ์ดํ„ฐ ์—†์Œ"
337
+
338
+ try:
339
+ keyword_data = None
340
+ for kw, data in trend_data_3year.items():
341
+ keyword_data = data
342
+ logger.info(f"๐Ÿ” ํ‚ค์›Œ๋“œ ๋ฐ์ดํ„ฐ ๋ฐœ๊ฒฌ: {kw}")
343
+ break
344
+
345
+ if not keyword_data or not keyword_data.get('monthly_volumes') or not keyword_data.get('dates'):
346
+ logger.error("โŒ keyword_data ๊ตฌ์กฐ ๋ฌธ์ œ")
347
+ return "๋ฐ์ดํ„ฐ ์—†์Œ"
348
+
349
+ volumes = keyword_data['monthly_volumes']
350
+ dates = keyword_data['dates']
351
+
352
+ # 1๋‹จ๊ณ„: ํ˜„์žฌ ์‹œ์  ํŒŒ์•…
353
+ current_date = datetime.now()
354
+ current_year = current_date.year
355
+ current_month = current_date.month
356
+ current_day = current_date.day
357
+
358
+ # ์™„๋ฃŒ๋œ ๋งˆ์ง€๋ง‰ ์›” ๊ณ„์‚ฐ (2์ผ ์ดํ›„๋ฉด ์ „์›”๊นŒ์ง€ ์™„๋ฃŒ)
359
+ if current_day >= 2:
360
+ completed_year = current_year
361
+ completed_month = current_month - 1
362
+ else:
363
+ completed_year = current_year
364
+ completed_month = current_month - 2
365
+
366
+ # ์›”์ด 0 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ์—ฐ๋„ ์กฐ์ •
367
+ while completed_month <= 0:
368
+ completed_month += 12
369
+ completed_year -= 1
370
+
371
+ logger.info(f"๐Ÿ“… ํ˜„์žฌ: {current_year}๋…„ {current_month}์›” {current_day}์ผ")
372
+ logger.info(f"๐Ÿ“Š ์™„๋ฃŒ๋œ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ: {completed_year}๋…„ {completed_month}์›”")
373
+
374
+ # 2๋‹จ๊ณ„: ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ ๋ฐ ์ˆ˜์ง‘
375
+ all_data = []
376
+
377
+ for i, date_str in enumerate(dates):
378
+ try:
379
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
380
+
381
+ if i < len(volumes):
382
+ volume = volumes[i]
383
+ if isinstance(volume, str):
384
+ volume = float(volume.replace(',', ''))
385
+ volume = int(volume) if volume else 0
386
+
387
+ all_data.append({
388
+ 'year': date_obj.year,
389
+ 'month': date_obj.month,
390
+ 'volume': volume,
391
+ 'date_str': date_str,
392
+ 'date_obj': date_obj,
393
+ 'sort_key': f"{date_obj.year:04d}{date_obj.month:02d}"
394
+ })
395
+
396
+ except Exception as e:
397
+ logger.warning(f"โš ๏ธ ๋‚ ์งœ ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {date_str} - {e}")
398
+ continue
399
+
400
+ # ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
401
+ all_data = sorted(all_data, key=lambda x: x['sort_key'])
402
+
403
+ # 3๋‹จ๊ณ„: ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ (์˜ฌํ•ด ์™„๋ฃŒ์›” vs ์ž‘๋…„ ๋™์›”)
404
+ this_year_completed_volume = None
405
+ last_year_same_month_volume = None
406
+
407
+ for data in all_data:
408
+ # ์˜ฌํ•ด ์™„๋ฃŒ๋œ ๋งˆ์ง€๋ง‰ ์›” ์ฐพ๊ธฐ
409
+ if data['year'] == completed_year and data['month'] == completed_month:
410
+ this_year_completed_volume = data['volume']
411
+ logger.info(f"๐Ÿ“Š ์˜ฌํ•ด {completed_month}์›” ์‹ค๋ฐ์ดํ„ฐ: {this_year_completed_volume:,}ํšŒ")
412
+
413
+ # ์ž‘๋…„ ๋™์›” ์ฐพ๊ธฐ
414
+ if data['year'] == completed_year - 1 and data['month'] == completed_month:
415
+ last_year_same_month_volume = data['volume']
416
+ logger.info(f"๐Ÿ“Š ์ž‘๋…„ {completed_month}์›” ์‹ค๋ฐ์ดํ„ฐ: {last_year_same_month_volume:,}ํšŒ")
417
+
418
+ # ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ
419
+ growth_rate = 0
420
+ if this_year_completed_volume is not None and last_year_same_month_volume is not None and last_year_same_month_volume > 0:
421
+ growth_rate = (this_year_completed_volume - last_year_same_month_volume) / last_year_same_month_volume
422
+ logger.info(f"๐Ÿ“ˆ ๊ณ„์‚ฐ๋œ ์ฆ๊ฐ์œจ: {growth_rate:+.3f} ({growth_rate * 100:+.1f}%)")
423
+ else:
424
+ logger.warning("โš ๏ธ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.")
425
+
426
+ # 4๋‹จ๊ณ„: ์˜ˆ์ƒ๋ฐ์ดํ„ฐ ์ƒ์„ฑ (ํ˜„์žฌ์›” ์ดํ›„)
427
+ combined_data = []
428
+ month_names = ["", "1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"]
429
+
430
+ # ์ž‘๋…„ 12์›” ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (์—ฐ์†์„ฑ์„ ์œ„ํ•ด)
431
+ for data in all_data:
432
+ if data['year'] == completed_year - 1 and data['month'] == 12:
433
+ combined_data.append({
434
+ 'year': data['year'],
435
+ 'month': data['month'],
436
+ 'volume': data['volume'],
437
+ 'data_type': '์ž‘๋…„์‹ค์ œ',
438
+ 'sort_key': f"{data['year']:04d}{data['month']:02d}"
439
+ })
440
+ logger.info(f"๐Ÿ”— ์ž‘๋…„ 12์›” ์‹ค๋ฐ์ดํ„ฐ: {data['volume']:,}ํšŒ")
441
+ break
442
+
443
+ # ์˜ฌํ•ด 1์›”๋ถ€ํ„ฐ ์™„๋ฃŒ์›”๊นŒ์ง€ ์‹ค์ œ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
444
+ for month in range(1, completed_month + 1):
445
+ for data in all_data:
446
+ if data['year'] == completed_year and data['month'] == month:
447
+ combined_data.append({
448
+ 'year': data['year'],
449
+ 'month': data['month'],
450
+ 'volume': data['volume'],
451
+ 'data_type': '์‹ค์ œ',
452
+ 'sort_key': f"{data['year']:04d}{data['month']:02d}"
453
+ })
454
+ logger.info(f"๐Ÿ“Š {month}์›” ์‹ค๋ฐ์ดํ„ฐ: {data['volume']:,}ํšŒ")
455
+ break
456
+
457
+ # ์˜ฌํ•ด ๋ฏธ์™„๋ฃŒ์›”(ํ˜„์žฌ์›”+1 ~ 12์›”) ์˜ˆ์ƒ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
458
+ for month in range(completed_month + 1, 13):
459
+ # ์ž‘๋…„ ๋™์›” ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ
460
+ last_year_volume = None
461
+ for data in all_data:
462
+ if data['year'] == completed_year - 1 and data['month'] == month:
463
+ last_year_volume = data['volume']
464
+ break
465
+
466
+ if last_year_volume is not None:
467
+ # ์˜ˆ์ƒ ๊ฒ€์ƒ‰๋Ÿ‰ = ์ž‘๋…„ ๋™์›” ร— (1 + ์ฆ๊ฐ์œจ)
468
+ predicted_volume = int(last_year_volume * (1 + growth_rate))
469
+ predicted_volume = max(predicted_volume, 0) # ์Œ์ˆ˜ ๋ฐฉ์ง€
470
+
471
+ combined_data.append({
472
+ 'year': completed_year,
473
+ 'month': month,
474
+ 'volume': predicted_volume,
475
+ 'data_type': '์˜ˆ์ƒ',
476
+ 'sort_key': f"{completed_year:04d}{month:02d}"
477
+ })
478
+
479
+ logger.info(f"๐Ÿ”ฎ {month}์›” ์˜ˆ์ƒ๋ฐ์ดํ„ฐ: {predicted_volume:,}ํšŒ (์ž‘๋…„ {last_year_volume:,}ํšŒ ร— {1 + growth_rate:.3f})")
480
+
481
+ # ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
482
+ combined_data = sorted(combined_data, key=lambda x: x['sort_key'])
483
+
484
+ # 5๋‹จ๊ณ„: ๐Ÿ”– ๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›” ์ฐพ๊ธฐ (์—ฐ์†๋œ ์›”๊ฐ„ ์ƒ์Šน๋ฅ )
485
+ max_growth_rate = 0
486
+ max_growth_info = "๋ฐ์ดํ„ฐ ์—†์Œ"
487
+
488
+ for i in range(len(combined_data) - 1):
489
+ start_data = combined_data[i]
490
+ end_data = combined_data[i + 1]
491
+
492
+ if start_data['volume'] > 0:
493
+ month_growth_rate = ((end_data['volume'] - start_data['volume']) / start_data['volume']) * 100
494
+
495
+ # ์ƒ์Šนํ•œ ๊ฒฝ์šฐ๋งŒ ๊ณ ๋ ค
496
+ if month_growth_rate > max_growth_rate:
497
+ max_growth_rate = month_growth_rate
498
+
499
+ start_month_name = month_names[start_data['month']]
500
+ end_month_name = month_names[end_data['month']]
501
+
502
+ # ์—ฐ๋„ ์ „ํ™˜ ๊ณ ๋ ค
503
+ if start_data['year'] != end_data['year']:
504
+ period_desc = f"{start_data['year']}๋…„ {start_month_name}({start_data['volume']:,}ํšŒ)์—์„œ {end_data['year']}๋…„ {end_month_name}({end_data['volume']:,}ํšŒ)์œผ๋กœ"
505
+ else:
506
+ period_desc = f"{start_month_name}({start_data['volume']:,}ํšŒ)์—์„œ {end_month_name}({end_data['volume']:,}ํšŒ)์œผ๋กœ"
507
+
508
+ # ๋ฐ์ดํ„ฐ ์œ ํ˜• ํŒ๋‹จ
509
+ if start_data['data_type'] in ['์˜ˆ์ƒ'] and end_data['data_type'] in ['์˜ˆ์ƒ']:
510
+ data_type = "์˜ˆ์ƒ ๊ธฐ๋ฐ˜"
511
+ elif start_data['data_type'] in ['์‹ค์ œ', '์ž‘๋…„์‹ค์ œ'] and end_data['data_type'] in ['์‹ค์ œ', '์ž‘๋…„์‹ค์ œ']:
512
+ data_type = "์‹ค์ œ ๊ธฐ๋ฐ˜"
513
+ else:
514
+ data_type = "์‹ค์ œโ†’์˜ˆ์ƒ ๊ธฐ๋ฐ˜"
515
+
516
+ max_growth_info = f"{period_desc} {max_growth_rate:.1f}% ์ƒ์Šน ({data_type})"
517
+
518
+ # ์ƒ์Šน ๊ตฌ๊ฐ„์ด ์—†๋Š” ๊ฒฝ์šฐ ์ตœ์†Œ ํ•˜๋ฝ๋ฅ  ํ‘œ์‹œ
519
+ if max_growth_rate == 0:
520
+ min_decline_rate = float('inf')
521
+ for i in range(len(combined_data) - 1):
522
+ start_data = combined_data[i]
523
+ end_data = combined_data[i + 1]
524
+
525
+ if start_data['volume'] > 0:
526
+ month_growth_rate = ((end_data['volume'] - start_data['volume']) / start_data['volume']) * 100
527
+
528
+ if abs(month_growth_rate) < abs(min_decline_rate):
529
+ min_decline_rate = month_growth_rate
530
+
531
+ start_month_name = month_names[start_data['month']]
532
+ end_month_name = month_names[end_data['month']]
533
+
534
+ if start_data['year'] != end_data['year']:
535
+ period_desc = f"{start_data['year']}๋…„ {start_month_name}({start_data['volume']:,}ํšŒ)์—์„œ {end_data['year']}๋…„ {end_month_name}({end_data['volume']:,}ํšŒ)์œผ๋กœ"
536
+ else:
537
+ period_desc = f"{start_month_name}({start_data['volume']:,}ํšŒ)์—์„œ {end_month_name}({end_data['volume']:,}ํšŒ)์œผ๋กœ"
538
+
539
+ if start_data['data_type'] in ['์˜ˆ์ƒ'] and end_data['data_type'] in ['์˜ˆ์ƒ']:
540
+ data_type = "์˜ˆ์ƒ ๊ธฐ๋ฐ˜"
541
+ elif start_data['data_type'] in ['์‹ค์ œ', '์ž‘๋…„์‹ค์ œ'] and end_data['data_type'] in ['์‹ค์ œ', '์ž‘๋…„์‹ค์ œ']:
542
+ data_type = "์‹ค์ œ ๊ธฐ๋ฐ˜"
543
+ else:
544
+ data_type = "์‹ค์ œโ†’์˜ˆ์ƒ ๊ธฐ๋ฐ˜"
545
+
546
+ max_growth_info = f"{period_desc} {abs(min_decline_rate):.1f}% ๊ฐ์†Œ ({data_type})"
547
+
548
+ logger.info(f"๐Ÿ† ๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”: {max_growth_info}")
549
+ return max_growth_info
550
+
551
+ except Exception as e:
552
+ logger.error(f"โŒ ์ƒ์Šนํญ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
553
+ import traceback
554
+ logger.error(f"โŒ ์Šคํƒ ํŠธ๋ ˆ์ด์Šค: {traceback.format_exc()}")
555
+ return "๊ณ„์‚ฐ ์˜ค๋ฅ˜"
556
+
557
+ def get_peak_month_with_predictions(trend_data_3year, keyword):
558
+ """๐Ÿ”– ๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›” ์ฐพ๊ธฐ - ์‹ค์ œ+์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ํ™œ์šฉ"""
559
+ if not trend_data_3year:
560
+ return "์—ฐ์ค‘"
561
+
562
+ try:
563
+ keyword_data = None
564
+ for kw, data in trend_data_3year.items():
565
+ keyword_data = data
566
+ break
567
+
568
+ if not keyword_data or not keyword_data.get('monthly_volumes') or not keyword_data.get('dates'):
569
+ return "์—ฐ์ค‘"
570
+
571
+ volumes = keyword_data['monthly_volumes']
572
+ dates = keyword_data['dates']
573
+
574
+ # ํ˜„์žฌ ์‹œ์  ํŒŒ์•…
575
+ current_date = datetime.now()
576
+ current_year = current_date.year
577
+ current_month = current_date.month
578
+ current_day = current_date.day
579
+
580
+ # ์™„๋ฃŒ๋œ ๋งˆ์ง€๋ง‰ ์›” ๊ณ„์‚ฐ
581
+ if current_day >= 2:
582
+ completed_year = current_year
583
+ completed_month = current_month - 1
584
+ else:
585
+ completed_year = current_year
586
+ completed_month = current_month - 2
587
+
588
+ while completed_month <= 0:
589
+ completed_month += 12
590
+ completed_year -= 1
591
+
592
+ # ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘
593
+ all_data = []
594
+ for i, date_str in enumerate(dates):
595
+ try:
596
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
597
+ if i < len(volumes):
598
+ volume = volumes[i]
599
+ if isinstance(volume, str):
600
+ volume = float(volume.replace(',', ''))
601
+ volume = int(volume) if volume else 0
602
+
603
+ all_data.append({
604
+ 'year': date_obj.year,
605
+ 'month': date_obj.month,
606
+ 'volume': volume
607
+ })
608
+ except:
609
+ continue
610
+
611
+ # ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ
612
+ this_year_completed_volume = None
613
+ last_year_same_month_volume = None
614
+
615
+ for data in all_data:
616
+ if data['year'] == completed_year and data['month'] == completed_month:
617
+ this_year_completed_volume = data['volume']
618
+ if data['year'] == completed_year - 1 and data['month'] == completed_month:
619
+ last_year_same_month_volume = data['volume']
620
+
621
+ growth_rate = 0
622
+ if this_year_completed_volume is not None and last_year_same_month_volume is not None and last_year_same_month_volume > 0:
623
+ growth_rate = (this_year_completed_volume - last_year_same_month_volume) / last_year_same_month_volume
624
+
625
+ # ์˜ฌํ•ด ๋ฐ์ดํ„ฐ ์ค€๋น„ (์‹ค์ œ + ์˜ˆ์ƒ)
626
+ year_data = []
627
+ month_names = ["", "1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"]
628
+
629
+ # ์‹ค์ œ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (1์›”~์™„๋ฃŒ์›”)
630
+ for month in range(1, completed_month + 1):
631
+ for data in all_data:
632
+ if data['year'] == completed_year and data['month'] == month:
633
+ year_data.append({
634
+ 'month': month,
635
+ 'volume': data['volume'],
636
+ 'data_type': '์‹ค์ œ'
637
+ })
638
+ break
639
+
640
+ # ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (์™„๋ฃŒ์›”+1~12์›”)
641
+ for month in range(completed_month + 1, 13):
642
+ last_year_volume = None
643
+ for data in all_data:
644
+ if data['year'] == completed_year - 1 and data['month'] == month:
645
+ last_year_volume = data['volume']
646
+ break
647
+
648
+ if last_year_volume is not None:
649
+ predicted_volume = int(last_year_volume * (1 + growth_rate))
650
+ predicted_volume = max(predicted_volume, 0)
651
+
652
+ year_data.append({
653
+ 'month': month,
654
+ 'volume': predicted_volume,
655
+ 'data_type': '์˜ˆ์ƒ'
656
+ })
657
+
658
+ # ๊ฐ€์žฅ ๋†’์€ ๊ฒ€์ƒ‰๋Ÿ‰ ์ฐพ๊ธฐ
659
+ if not year_data:
660
+ return "์—ฐ์ค‘"
661
+
662
+ max_data = max(year_data, key=lambda x: x['volume'])
663
+ month_name = month_names[max_data['month']]
664
+ data_type_suffix = " - ์˜ˆ์ƒ" if max_data['data_type'] == '์˜ˆ์ƒ' else ""
665
+
666
+ return f"{month_name}({max_data['volume']:,}ํšŒ){data_type_suffix}"
667
+
668
+ except Exception as e:
669
+ logger.error(f"ํ”ผํฌ์›” ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
670
+ return "์—ฐ์ค‘"
671
+
672
+ def calculate_3year_growth_rate_improved(volumes):
673
+ """์ž‘๋…„๋Œ€๋น„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ (3๋…„ ๋ฐ์ดํ„ฐ์šฉ)"""
674
+ if len(volumes) < 24:
675
+ return 0
676
+
677
+ try:
678
+ # ์ฒซ ํ•ด์™€ ๋งˆ์ง€๋ง‰ ํ•ด ๋น„๊ต
679
+ first_year = volumes[:12]
680
+ last_year = volumes[-12:]
681
+
682
+ first_year_avg = sum(first_year) / len(first_year)
683
+ last_year_avg = sum(last_year) / len(last_year)
684
+
685
+ if first_year_avg == 0:
686
+ return 0
687
+
688
+ growth_rate = ((last_year_avg - first_year_avg) / first_year_avg) * 100
689
+ return min(max(growth_rate, -50), 200) # -50% ~ 200% ๋ฒ”์œ„๋กœ ์ œํ•œ
690
+
691
+ except Exception as e:
692
+ logger.error(f"์ž‘๋…„๋Œ€๋น„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
693
+ return 0
694
+
695
+ def calculate_max_growth_rate_pure_logic(trend_data_3year, keyword):
696
+ """์ˆœ์ˆ˜ ๋กœ์ง์œผ๋กœ ์ตœ๋Œ€ ์ƒ์Šนํญ ๊ณ„์‚ฐ - ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ํฌํ•จ ๋ฒ„์ „"""
697
+ return calculate_max_growth_rate_with_predictions(trend_data_3year, keyword)
698
+
699
+ def analyze_season_cycle_with_llm(trend_data_3year, keyword, total_volume, gemini_model):
700
+ """LLM์„ ์ด์šฉํ•œ ์‹œ์ฆŒ ์ƒํ’ˆ ์†Œ์‹ฑ ์‚ฌ์ดํด ๋ถ„์„"""
701
+ if not trend_data_3year or not gemini_model:
702
+ return "๋น„์‹œ์ฆŒ์ƒํ’ˆ", "์–ธ์ œ๋“ ์ง€ ์ง„์ž… ๊ฐ€๋Šฅ", "๋ฐ์ดํ„ฐ ๋ถ€์กฑ"
703
+
704
+ try:
705
+ keyword_data = None
706
+ for kw, data in trend_data_3year.items():
707
+ keyword_data = data
708
+ break
709
+
710
+ if not keyword_data or not keyword_data.get('monthly_volumes'):
711
+ return "๋น„์‹œ์ฆŒ์ƒํ’ˆ", "์–ธ์ œ๋“ ์ง€ ์ง„์ž… ๊ฐ€๋Šฅ", "๋ฐ์ดํ„ฐ ๋ถ€์กฑ"
712
+
713
+ volumes = keyword_data['monthly_volumes']
714
+ dates = keyword_data['dates']
715
+
716
+ recent_12_volumes = volumes[-12:] if len(volumes) >= 12 else volumes
717
+ recent_12_dates = dates[-12:] if len(dates) >= 12 else dates
718
+
719
+ if len(recent_12_volumes) < 12:
720
+ return "๋น„์‹œ์ฆŒ์ƒํ’ˆ", "์–ธ์ œ๋“ ์ง€ ์ง„์ž… ๊ฐ€๋Šฅ", "๋ฐ์ดํ„ฐ ๋ถ€์กฑ"
721
+
722
+ monthly_data_str = ""
723
+ max_volume = 0
724
+ max_month = ""
725
+ for i, (date, volume) in enumerate(zip(recent_12_dates, recent_12_volumes)):
726
+ try:
727
+ date_obj = datetime.strptime(date, "%Y-%m-%d")
728
+ month_name = f"{date_obj.year}๋…„ {date_obj.month}์›”"
729
+ monthly_data_str += f"{month_name}: {volume:,}ํšŒ\n"
730
+
731
+ # ์ตœ๋Œ€ ๊ฒ€์ƒ‰๋Ÿ‰ ์›” ์ฐพ๊ธฐ
732
+ if volume > max_volume:
733
+ max_volume = volume
734
+ max_month = f"{date_obj.month}์›”({volume:,}ํšŒ)"
735
+
736
+ except:
737
+ monthly_data_str += f"์›”-{i+1}: {volume:,}ํšŒ\n"
738
+
739
+ current_date = datetime.now()
740
+ current_month = current_date.month
741
+
742
+ prompt = f"""
743
+ ํ‚ค์›Œ๋“œ: '{keyword}'
744
+ ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰: {total_volume:,}ํšŒ
745
+ ํ˜„์žฌ ์‹œ์ : {current_date.year}๋…„ {current_month}์›”
746
+
747
+ ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๋ฐ์ดํ„ฐ (์ตœ๊ทผ 12๊ฐœ์›”):
748
+ {monthly_data_str}
749
+
750
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ๋งŒ ๋‹ต๋ณ€ํ•˜์„ธ์š”:
751
+
752
+ ์ƒํ’ˆ์œ ํ˜•: [๋ด„์‹œ์ฆŒ์ƒํ’ˆ/์—ฌ๋ฆ„์‹œ์ฆŒ์ƒํ’ˆ/๊ฐ€์„์‹œ์ฆŒ์ƒํ’ˆ/๊ฒจ์šธ์‹œ์ฆŒ์ƒํ’ˆ/๋น„์‹œ์ฆŒ์ƒํ’ˆ/ํฌ๋ฆฌ์Šค๋งˆ์Šค์ด๋ฒคํŠธ์ƒํ’ˆ/๋ฐธ๋Ÿฐํƒ€์ธ์ด๋ฒคํŠธ์ƒํ’ˆ/์–ด๋ฒ„์ด๋‚ ์ด๋ฒคํŠธ์ƒํ’ˆ/์ƒˆํ•™๊ธฐ์ด๋ฒคํŠธ์ƒํ’ˆ/๊ธฐํƒ€์ด๋ฒคํŠธ์ƒํ’ˆ]
753
+ ํ”ผํฌ์›”: [X์›”] (๊ฒ€์ƒ‰๋Ÿ‰์ด ๊ฐ€์žฅ ๋†’์€ ์›”, ์‹ค์ œ ์ˆ˜์น˜ ํฌํ•จ)
754
+ ์„ฑ์žฅ์›”: [X์›”] (์ฆ๊ฐ€ํญ์ด ๊ฐ€์žฅ ๋†’์€ ์›”)
755
+ ํ˜„์žฌ์ƒํƒœ: {current_month}์›” ๊ธฐ์ค€ [๋„์ž…๊ธฐ/์„ฑ์žฅ๊ธฐ/์•ˆ์ •๊ธฐ/์‡ ํ‡ด๊ธฐ/๋น„์‹œ์ฆŒ๊ธฐ๊ฐ„]
756
+ ์ง„์ž…์ถ”์ฒœ: [๊ตฌ์ฒด์  ์›” ์ œ์‹œ]
757
+ """
758
+
759
+ response = gemini_model.generate_content(prompt)
760
+ result_text = response.text.strip()
761
+
762
+ lines = result_text.split('\n')
763
+ product_type = "๋น„์‹œ์ฆŒ์ƒํ’ˆ"
764
+ peak_month = max_month if max_month else "์—ฐ์ค‘"
765
+ growth_month = "์—ฐ์ค‘"
766
+ current_status = "์•ˆ์ •๊ธฐ"
767
+ entry_recommendation = "์–ธ์ œ๋“ ์ง€ ์ง„์ž… ๊ฐ€๋Šฅ"
768
+
769
+ for line in lines:
770
+ line = line.strip()
771
+ if line.startswith('์ƒํ’ˆ์œ ํ˜•:'):
772
+ product_type = line.replace('์ƒํ’ˆ์œ ํ˜•:', '').strip()
773
+ elif line.startswith('ํ”ผํฌ์›”:'):
774
+ extracted_peak = line.replace('ํ”ผํฌ์›”:', '').strip()
775
+ if '(' in extracted_peak and ')' in extracted_peak:
776
+ peak_month = extracted_peak
777
+ else:
778
+ peak_month = max_month if max_month else extracted_peak
779
+ elif line.startswith('์„ฑ์žฅ์›”:'):
780
+ growth_month = line.replace('์„ฑ์žฅ์›”:', '').strip()
781
+ elif line.startswith('ํ˜„์žฌ์ƒํƒœ:'):
782
+ current_status = line.replace('ํ˜„์žฌ์ƒํƒœ:', '').strip()
783
+ elif line.startswith('์ง„์ž…์ถ”์ฒœ:'):
784
+ entry_recommendation = line.replace('์ง„์ž…์ถ”์ฒœ:', '').strip()
785
+
786
+ detail_info = f"์ƒํ’ˆ์œ ํ˜•: {product_type} | ํ”ผํฌ์›”: {peak_month} | ์„ฑ์žฅ์›”: {growth_month} | ํ˜„์žฌ์ƒํƒœ: {current_status}"
787
+
788
+ logger.info(f"LLM ์‹œ์ฆŒ ๋ถ„์„ ์™„๋ฃŒ: {product_type}, {entry_recommendation}")
789
+ return product_type, entry_recommendation, detail_info
790
+
791
+ except Exception as e:
792
+ logger.error(f"LLM ์‹œ์ฆŒ ์‚ฌ์ดํด ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
793
+ return "๋น„์‹œ์ฆŒ์ƒํ’ˆ", "์–ธ์ œ๋“ ์ง€ ์ง„์ž… ๊ฐ€๋Šฅ", "LLM ๋ถ„์„ ์˜ค๋ฅ˜"
794
+
795
+ def analyze_sourcing_strategy_improved(keyword, volume_data, trend_data_1year, trend_data_3year, filtered_keywords_df, gemini_model):
796
+ """๊ฐœ์„ ๋œ ์†Œ์‹ฑ์ „๋žต ๋ถ„์„ - ํฌ๋งทํŒ… ์ˆ˜์ • ๋ฐ ๊ด€์—ฌ๋„ ๋ถ„์„ ๊ฐ•ํ™”"""
797
+
798
+ total_volume = volume_data.get('์ด๊ฒ€์ƒ‰๋Ÿ‰', 0)
799
+ current_date = datetime.now()
800
+ current_month = current_date.month
801
+ current_year = current_date.year
802
+
803
+ # โœ… ์ˆ˜์ •: ์˜ฌ๋ฐ”๋ฅธ ๋กœ์ง์œผ๋กœ ์ƒ์Šนํญ ๊ณ„์‚ฐ
804
+ growth_analysis = calculate_max_growth_rate_with_predictions(trend_data_3year, keyword)
805
+
806
+ # โœ… ์ˆ˜์ •: ์˜ฌ๋ฐ”๋ฅธ ๋กœ์ง์œผ๋กœ ํ”ผํฌ์›” ๊ณ„์‚ฐ (์‹ค์ œ+์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ํ™œ์šฉ)
807
+ peak_month_with_volume = get_peak_month_with_predictions(trend_data_3year, keyword)
808
+
809
+ # LLM์œผ๋กœ ์‹œ์ฆŒ ๋ถ„์„ (๊ธฐ์กด ์œ ์ง€)
810
+ if gemini_model:
811
+ product_type, entry_timing, season_detail = analyze_season_cycle_with_llm(trend_data_3year, keyword, total_volume, gemini_model)
812
+ else:
813
+ # ๊ธฐ๋ณธ๊ฐ’
814
+ product_type = "์—ฐ์ค‘์ƒํ’ˆ"
815
+ if total_volume > 50000:
816
+ product_type = "์ธ๊ธฐ์ƒํ’ˆ"
817
+ elif total_volume > 10000:
818
+ product_type = "์ค‘๊ฐ„์ƒํ’ˆ"
819
+ elif total_volume > 0:
820
+ product_type = "ํ‹ˆ์ƒˆ์ƒํ’ˆ"
821
+
822
+ # 2. ๊ด€์—ฌ๋„ ๋ถ„์„ ์ถ”๊ฐ€ - ์ดˆ๋ณด์ž๊ฐ€ ํŒ๋งค๊ฐ€๋Šฅํ•œ ์†Œ์‹ฑ ๊ธฐ์ค€ (๊ฐœ์„ ๋œ ๊ธฐ์ค€ ์ ์šฉ)
823
+ involvement_level = analyze_involvement_level(keyword, total_volume, gemini_model)
824
+
825
+ # ํŠธ๋ Œ๋“œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€
826
+ trend_warning = ""
827
+ if not trend_data_3year:
828
+ trend_warning = "\n\n๐Ÿ’ก ๋” ์ •ํ™•ํ•œ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•ด \"1๋‹จ๊ณ„: ๊ธฐ๋ณธ ํ‚ค์›Œ๋“œ ์ž…๋ ฅ\"์„ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”."
829
+
830
+ # ๊ฒฐ๊ณผ ํฌ๋งทํŒ… ์ˆ˜์ • - ๊ตฌ๋ถ„์„ ๊ณผ ํ•ญ๋ชฉ ๋ถ„๋ฆฌ
831
+ result_content = f"""**๐Ÿ”– ์ƒํ’ˆ์œ ํ˜•**
832
+ {product_type}
833
+
834
+ {involvement_level}
835
+
836
+ **๐Ÿ”– ๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›”**
837
+ {peak_month_with_volume}
838
+
839
+ **๐Ÿ”– ๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”**
840
+ {growth_analysis}{trend_warning}"""
841
+
842
+ try:
843
+ return {"status": "success", "content": result_content}
844
+ except Exception as e:
845
+ logger.error(f"์†Œ์‹ฑ์ „๋žต ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
846
+ return {"status": "error", "content": "์†Œ์‹ฑ์ „๋žต ๋ถ„์„์„ ์™„๋ฃŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
847
+
848
+ def analyze_involvement_level(keyword, total_volume, gemini_model):
849
+ """๊ด€์—ฌ๋„ ๋ถ„์„ ํ•จ์ˆ˜ - ์ดˆ๋ณด์ž๊ฐ€ ํŒ๋งค๊ฐ€๋Šฅํ•œ ์†Œ์‹ฑ ๊ธฐ์ค€"""
850
+ try:
851
+ # ๊ธฐ๋ณธ ๊ทœ์น™ ๊ธฐ๋ฐ˜ ๋ถ„์„
852
+ basic_involvement = get_basic_involvement_level(keyword, total_volume)
853
+
854
+ # Gemini๊ฐ€ ์žˆ์œผ๋ฉด LLM ๋ถ„์„๋„ ์ˆ˜ํ–‰
855
+ if gemini_model:
856
+ llm_involvement = get_llm_involvement_analysis(keyword, total_volume, gemini_model)
857
+ return llm_involvement
858
+ else:
859
+ return basic_involvement
860
+
861
+ except Exception as e:
862
+ logger.error(f"๊ด€์—ฌ๋„ ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
863
+ return "๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)"
864
+
865
+ def get_basic_involvement_level(keyword, total_volume):
866
+ """๊ธฐ๋ณธ ๊ทœ์น™ ๊ธฐ๋ฐ˜ ๊ด€์—ฌ๋„ ๋ถ„์„ - ์ดˆ๋ณด์ž ํŒ๋งค ๊ด€์ """
867
+
868
+ # ์ €๊ด€์—ฌ ์ƒํ’ˆ ํ‚ค์›Œ๋“œ ํŒจํ„ด (์ดˆ๋ณด์ž ์ง„์ž… ๊ฐ€๋Šฅํ•œ ๋ถˆํŽธํ•ด์†Œ ์ œํ’ˆ)
869
+ low_involvement_keywords = [
870
+ # ๋ถˆํŽธํ•ด์†Œ/์ •๋ฆฌ์ˆ˜๋‚ฉ
871
+ "๊ฑฐ์น˜๋Œ€", "๋ฐ›์นจ๋Œ€", "์ •๋ฆฌํ•จ", "์ •๋ฆฌ๋Œ€", "์ˆ˜๋‚ฉ", "ํ™€๋”", "์Šคํƒ ๋“œ",
872
+ "์ฟ ์…˜", "๋ฒ ๊ฐœ", "๋ชฉ๋ฒ ๊ฐœ", "๋ฐฉ์„", "๋งคํŠธ", "ํŒจ๋“œ",
873
+ # ์ผ€์ด๋ธ”/์ „์„  ๊ด€๋ฆฌ
874
+ "์ผ€์ด๋ธ”", "์„ ์ •๋ฆฌ", "์ฝ”๋“œ", "์ถฉ์ „๊ธฐ", "์–ด๋Œ‘ํ„ฐ",
875
+ # ์ฒญ์†Œ/์œ„์ƒ (๋Œ€๊ธฐ์—… ์ œํ’ˆ ์ œ์™ธ)
876
+ "์ฒญ์†Œ์†”", "์ฒญ์†Œ๊ธฐ", "๊ฑธ๋ ˆ", "ํƒ€์˜ฌ", "๋ธŒ๋Ÿฌ์‹œ",
877
+ # ์ž๋™์ฐจ/์‹ค์šฉ์šฉํ’ˆ
878
+ "์ฐจ๋Ÿ‰์šฉ", "์ž๋™์ฐจ", "ํ•ธ๋“œํฐ", "์Šค๋งˆํŠธํฐ", "ํƒœ๋ธ”๋ฆฟ",
879
+ # ๊ฐ„๋‹จํ•œ ๋„๊ตฌ/์•ก์„ธ์„œ๋ฆฌ
880
+ "์ง‘๊ฒŒ", "ํ›„ํฌ", "์ž์„", "ํด๋ฆฝ", "๊ณ ๋ฆฌ", "๋ง", "ํ™€๋”",
881
+ # ๋ฏธ๋„๋Ÿผ๋ฐฉ์ง€/์•ˆ์ „
882
+ "๋ฏธ๋„๋Ÿผ", "๋…ผ์Šฌ๋ฆฝ", "๋ฐฉ์ง€", "๋ณดํ˜ธ", "์ปค๋ฒ„", "์ผ€์ด์Šค"
883
+ ]
884
+
885
+ # ๊ณ ๊ด€์—ฌ ์ƒํ’ˆ ํ‚ค์›Œ๋“œ ํŒจํ„ด (๋Œ€๊ธฐ์—… ๋…์  ๋˜๋Š” ๊ณ ๊ฐ€/์ „๋ฌธ ์ œํ’ˆ)
886
+ high_involvement_keywords = [
887
+ # ๋Œ€๊ธฐ์—… ๋…์  ์ƒํ•„ํ’ˆ
888
+ "ํœด์ง€", "ํ™”์žฅ์ง€", "๋ฌผํ‹ฐ์Šˆ", "๋งˆ์Šคํฌ", "์„ธ์ œ", "์ƒดํ‘ธ", "๋ฆฐ์Šค", "๋น„๋ˆ„",
889
+ "์น˜์•ฝ", "์นซ์†”", "๊ธฐ์ €๊ท€", "์ƒ๋ฆฌ๋Œ€", "์ฝ˜๋”",
890
+ # ์‹ํ’ˆ/์Œ๋ฃŒ (๋ธŒ๋žœ๋“œ ๋ฏผ๊ฐ)
891
+ "๋ผ๋ฉด", "๊ณผ์ž", "์Œ๋ฃŒ", "์ปคํ”ผ", "์ฐจ", "์šฐ์œ ", "์š”๊ตฌ๋ฅดํŠธ",
892
+ "์Œ€", "๊น€", "์ฐธ๊ธฐ๋ฆ„", "๊ฐ„์žฅ", "๊ณ ์ถ”์žฅ", "๋œ์žฅ",
893
+ # ๊ณ ๊ฐ€ ์ „์ž์ œํ’ˆ
894
+ "๋…ธํŠธ๋ถ", "์ปดํ“จํ„ฐ", "์Šค๋งˆํŠธํฐ", "ํƒœ๋ธ”๋ฆฟ", "์นด๋ฉ”๋ผ", "TV", "๋ชจ๋‹ˆํ„ฐ",
895
+ "๋ƒ‰์žฅ๊ณ ", "์„ธํƒ๊ธฐ", "์—์–ด์ปจ", "์ฒญ์†Œ๊ธฐ", "์ „์ž๋ ˆ์ธ์ง€",
896
+ # ์˜๋ฃŒ/๊ฑด๊ฐ• (์ธ์ฆ ํ•„์š”)
897
+ "์˜๋ฃŒ", "๊ฑด๊ฐ•์‹ํ’ˆ", "์˜์–‘์ œ", "๋น„ํƒ€๋ฏผ", "์•ฝ", "์˜์•ฝํ’ˆ",
898
+ # ๋ช…ํ’ˆ/๋ธŒ๋žœ๋“œ
899
+ "๋ช…ํ’ˆ", "๋ธŒ๋žœ๋“œ", "๋Ÿญ์…”๋ฆฌ", "์‹œ๊ณ„", "๋ณด์„", "๊ธˆ", "์€", "๋‹ค์ด์•„๋ชฌ๋“œ"
900
+ ]
901
+
902
+ keyword_lower = keyword.lower()
903
+
904
+ # ์ €๊ด€์—ฌ ์ƒํ’ˆ ์ฒดํฌ (๋ถˆํŽธํ•ด์†Œ ํ‚ค์›Œ๋“œ ์šฐ์„ )
905
+ for low_kw in low_involvement_keywords:
906
+ if low_kw in keyword_lower:
907
+ return "์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ)"
908
+
909
+ # ๊ณ ๊ด€์—ฌ ์ƒํ’ˆ ์ฒดํฌ (๋Œ€๊ธฐ์—… ๋…์ /๋ธŒ๋žœ๋“œ ๋ฏผ๊ฐ ํ‚ค์›Œ๋“œ)
910
+ for high_kw in high_involvement_keywords:
911
+ if high_kw in keyword_lower:
912
+ return "๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ)"
913
+
914
+ # ๊ฒ€์ƒ‰๋Ÿ‰ ๏ฟฝ๏ฟฝ๏ฟฝ๋ฐ˜ ์ถ”๊ฐ€ ํŒ๋‹จ
915
+ if total_volume > 100000:
916
+ # ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งค์šฐ ๋†’์œผ๋ฉด ๋Œ€๊ธฐ์—…์ด ๊ด€์‹ฌ ๊ฐ€์งˆ ๋งŒํ•œ ์‹œ์žฅ
917
+ return "๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ)"
918
+ elif total_volume > 50000:
919
+ return "๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)"
920
+ elif total_volume > 5000:
921
+ return "๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)"
922
+ else:
923
+ # ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋‚ฎ์œผ๋ฉด ํ‹ˆ์ƒˆ ์‹œ์žฅ, ์ดˆ๋ณด์ž๋„ ์ง„์ž… ๊ฐ€๋Šฅ
924
+ return "์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ)"
925
+
926
+ def get_llm_involvement_analysis(keyword, total_volume, gemini_model):
927
+ """LLM์„ ์ด์šฉํ•œ ์ •๊ตํ•œ ๊ด€์—ฌ๋„ ๋ถ„์„ - ์ดˆ๋ณด์ž ํŒ๋งค ๊ด€์  ๊ธฐ์ค€ ์ ์šฉ"""
928
+ try:
929
+ prompt = f"""
930
+ '{keyword}' ์ƒํ’ˆ์˜ ๊ด€์—ฌ๋„๋ฅผ ์ดˆ๋ณด์ž ํŒ๋งค ๊ด€์ ์—์„œ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”.
931
+
932
+ ๊ฒ€์ƒ‰๋Ÿ‰: {total_volume:,}ํšŒ
933
+
934
+ ๊ด€์—ฌ๋„ ์ •์˜ (์ดˆ๋ณด์ž๊ฐ€ ํŒ๋งค๊ฐ€๋Šฅํ•œ ์†Œ์‹ฑ ๊ธฐ์ค€):
935
+
936
+ ์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ):
937
+ - ๋Œ€๊ธฐ์—… ๋…์ ์ด ์—†๋Š” ์˜์—ญ
938
+ - ์ฆ‰์‹œ ๋ถˆํŽธํ•ด์†Œํ•˜๋Š” ์ œํ’ˆ (์ง€๊ธˆ ๋ฐ”๋กœ ํ•„์š”ํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ)
939
+ - ๋ธŒ๋žœ๋“œ ์ƒ๊ด€์—†์ด ๊ธฐ๋Šฅ๋งŒ ๋˜๋ฉด ๊ตฌ๋งคํ•˜๋Š” ์ œํ’ˆ
940
+ - 1๋งŒ์›~3๋งŒ์›๋Œ€ ๊ฐ€๊ฒฉ, ์†Œ๋Ÿ‰(100๊ฐœ ์ดํ•˜) ์‹œ์ž‘ ๊ฐ€๋Šฅ
941
+ - ์˜ˆ์‹œ: ๋ชฉ๋ฒ ๊ฐœ, ์Šค๋งˆํŠธํฐ๊ฑฐ์น˜๋Œ€, ์„œ๋ž์ •๋ฆฌํ•จ, ์ผ€์ด๋ธ”์ •๋ฆฌ๊ธฐ
942
+
943
+ ๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ):
944
+ - ๋Œ€๊ธฐ์—…/๋ธŒ๋žœ๋“œ๊ฐ€ ์‹œ์žฅ์„ ๋…์ ํ•˜๋Š” ์˜์—ญ (์ดˆ๋ณด์ž ์ง„์ž… ๋ถˆ๊ฐ€)
945
+ - ์ƒํ•„ํ’ˆ(ํœด์ง€, ์„ธ์ œ, ๋งˆ์Šคํฌ ๋“ฑ) - ๋ธŒ๋žœ๋“œ ์ถฉ์„ฑ๋„ ๋†’์Œ
946
+ - ๊ณ ๊ฐ€ ์ œํ’ˆ(10๋งŒ์› ์ด์ƒ), ์ „๋ฌธ์„ฑ/์ธ์ฆ ํ•„์š”
947
+ - ๋Œ€์ž๋ณธ ํ•„์š”ํ•œ ์•„์ดํ…œ
948
+ - ์˜ˆ์‹œ: ์ „์ž์ œํ’ˆ, ๊ฐ€์ „, ๋ธŒ๋žœ๋“œ ์ƒํ•„ํ’ˆ, ์˜๋ฃŒ์šฉํ’ˆ
949
+
950
+ ๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง):
951
+ - ๊ฐ€๊ฒฉ๋Œ€๋ณ„๋กœ ์ €๊ฐ€ํ˜•(์ €๊ด€์—ฌ)๊ณผ ๊ณ ๊ฐ€ํ˜•(๊ณ ๊ด€์—ฌ)์ด ๊ณต์กด
952
+ - ํƒ€๊ฒŸ์ด๋‚˜ ์šฉ๋„์— ๋”ฐ๋ผ ๊ด€์—ฌ๋„๊ฐ€ ๊ทน๋ช…ํ•˜๊ฒŒ ๋‹ฌ๋ผ์ง
953
+ - ์˜ˆ์‹œ: ์˜๋ฅ˜, ์šด๋™์šฉํ’ˆ, ๋ทฐํ‹ฐ์šฉํ’ˆ ๋“ฑ
954
+
955
+ ๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ์œผ๋กœ ํŒ๋‹จํ•  ๊ฒฝ์šฐ, ๋ฐ˜๋“œ์‹œ ๊ตฌ์ฒด์ ์ธ ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜์„ธ์š”:
956
+ - ๊ฐ€๊ฒฉ๋Œ€๋ณ„ ๋ถ„ํ™”: "1-3๋งŒ์› ์ค‘๊ตญ์‚ฐ(์ €๊ด€์—ฌ) vs 10-15๋งŒ์› ๊ตญ์‚ฐ ์ˆ˜์ œ(๊ณ ๊ด€์—ฌ)"
957
+ - ํƒ€๊ฒŸ๋ณ„ ์ฐจ์ด: "์ผ๋ฐ˜์ธ์€ ์ €๊ด€์—ฌ vs ์ „๋ฌธ๊ฐ€๋Š” ๊ณ ๊ด€์—ฌ"
958
+ - ์šฉ๋„๋ณ„ ์ฐจ์ด: "์ž„์‹œ์šฉ์€ ์ €๊ด€์—ฌ vs ์žฅ๊ธฐ์šฉ์€ ๊ณ ๊ด€์—ฌ"
959
+
960
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”:
961
+ [๊ด€์—ฌ๋„ ์„ ํƒ]
962
+ [๊ตฌ์ฒด์ ์ธ ํŒ๋‹จ ์ด์œ  - ๊ฐ€๊ฒฉ๋Œ€/ํƒ€๊ฒŸ/๋ธŒ๋žœ๋“œ ๋…์  ์—ฌ๋ถ€ ๋“ฑ์„ ๋ช…ํ™•ํžˆ ์ œ์‹œ]
963
+
964
+ ์„ ํƒ์ง€:
965
+ ์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ)
966
+ ๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)
967
+ ๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ)
968
+ """
969
+
970
+ response = gemini_model.generate_content(prompt)
971
+ result = response.text.strip()
972
+
973
+ # ๊ฒฐ๊ณผ ํ•„ํ„ฐ๋ง - ์ •ํ™•ํ•œ ํ˜•์‹๋งŒ ํ—ˆ์šฉ
974
+ if "์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ)" in result:
975
+ return "์ €๊ด€์—ฌ์ƒํ’ˆ(์ดˆ๋ณด์ž์šฉ)"
976
+ elif "๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ)" in result:
977
+ return "๊ณ ๊ด€์—ฌ์ƒํ’ˆ(๊ณ ๊ธ‰์ž์šฉ)"
978
+ elif "๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)" in result:
979
+ return "๋ณตํ•ฉ๊ด€์—ฌ๋„์ƒํ’ˆ(์ƒํ’ˆ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง)"
980
+ else:
981
+ # LLM ์‘๋‹ต์ด ๋ถ€์ •ํ™•ํ•œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ๊ทœ์น™์œผ๋กœ ํด๋ฐฑ
982
+ return get_basic_involvement_level(keyword, total_volume)
983
+
984
+ except Exception as e:
985
+ logger.error(f"LLM ๊ด€์—ฌ๋„ ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
986
+ return get_basic_involvement_level(keyword, total_volume)
987
+
988
+
989
+ class CompactKeywordAnalyzer:
990
+ """๊ฐ„๊ฒฐํ•œ 7๋‹จ๊ณ„ ํ‚ค์›Œ๋“œ ๋ถ„์„๊ธฐ"""
991
+
992
+ def __init__(self, gemini_model):
993
+ self.gemini_model = gemini_model
994
+ self.max_retries = 3
995
+
996
+ def call_llm_with_retry(self, prompt: str, step_name: str = "") -> str:
997
+ """์žฌ์‹œ๋„ ๋กœ์ง์ด ์ ์šฉ๋œ LLM ํ˜ธ์ถœ"""
998
+ last_error = None
999
+
1000
+ for attempt in range(self.max_retries):
1001
+ try:
1002
+ logger.info(f"{step_name} ์‹œ๋„ {attempt + 1}/{self.max_retries}")
1003
+ response = self.gemini_model.generate_content(prompt)
1004
+ result = response.text.strip()
1005
+
1006
+ if result and len(result) > 20:
1007
+ logger.info(f"{step_name} ์„ฑ๊ณต")
1008
+ return result
1009
+ else:
1010
+ raise Exception("์‘๋‹ต์ด ๋„ˆ๋ฌด ์งง๊ฑฐ๋‚˜ ๋น„์–ด์žˆ์Œ")
1011
+
1012
+ except Exception as e:
1013
+ last_error = e
1014
+ logger.warning(f"{step_name} ์‹คํŒจ (์‹œ๋„ {attempt + 1}): {e}")
1015
+
1016
+ if attempt < self.max_retries - 1:
1017
+ delay = 1.0 * (attempt + 1) + random.uniform(0, 0.5)
1018
+ time.sleep(delay)
1019
+
1020
+ logger.error(f"{step_name} ๋ชจ๋“  ์žฌ์‹œ๋„ ์‹คํŒจ: {last_error}")
1021
+ return f"{step_name} ๋ถ„์„์„ ์™„๋ฃŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
1022
+
1023
+ def clean_markdown_and_bold(self, text: str) -> str:
1024
+ """๋งˆํฌ๋‹ค์šด๊ณผ ๋ณผ๋“œ ์ฒ˜๋ฆฌ๋ฅผ ์™„์ „ํžˆ ์ œ๊ฑฐ"""
1025
+ text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
1026
+ text = re.sub(r'\*(.+?)\*', r'\1', text)
1027
+ text = re.sub(r'__(.+?)__', r'\1', text)
1028
+ text = re.sub(r'_(.+?)_', r'\1', text)
1029
+ text = re.sub(r'##\s*(.+)', r'\1', text)
1030
+ text = re.sub(r'#\s*(.+)', r'\1', text)
1031
+ text = re.sub(r'\*+', '', text)
1032
+ text = re.sub(r'_+', '', text)
1033
+ return text.strip()
1034
+
1035
+ def analyze_sourcing_strategy(self, keyword: str, volume_data: dict, keywords_df: Optional[pd.DataFrame], trend_data_1year=None, trend_data_3year=None) -> str:
1036
+ """๊ฐœ์„ ๋œ ์†Œ์‹ฑ์ „๋žต ๋ถ„์„ - ๊ฒฝ์Ÿ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ"""
1037
+
1038
+ try:
1039
+ sourcing_analysis = analyze_sourcing_strategy_improved(
1040
+ keyword, volume_data, trend_data_1year, trend_data_3year, keywords_df, self.gemini_model
1041
+ )
1042
+ if sourcing_analysis["status"] == "success":
1043
+ return self.clean_markdown_and_bold(sourcing_analysis["content"])
1044
+ else:
1045
+ return sourcing_analysis["content"]
1046
+ except Exception as e:
1047
+ logger.error(f"์†Œ์‹ฑ์ „๋žต ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
1048
+ return "์†Œ์‹ฑ์ „๋žต ๋ถ„์„์„ ์™„๋ฃŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
1049
+
1050
+ def analyze_step1_product_type(self, keyword: str, keywords_df: Optional[pd.DataFrame]) -> str:
1051
+ """1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„"""
1052
+
1053
+ related_keywords = ""
1054
+ if keywords_df is not None and not keywords_df.empty:
1055
+ top_keywords = keywords_df.head(10)['์กฐํ•ฉ ํ‚ค์›Œ๋“œ'].tolist()
1056
+ related_keywords = f"์—ฐ๊ด€ํ‚ค์›Œ๋“œ: {', '.join(top_keywords)}"
1057
+
1058
+ prompt = f"""
1059
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1060
+
1061
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1062
+ {related_keywords}
1063
+
1064
+ 1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„
1065
+
1066
+ ์ƒํ’ˆ ์œ ํ˜• ๋ถ„๋ฅ˜ ๊ธฐ์ค€:
1067
+ - ๋ถˆํŽธํ•ด๊ฒฐ์ƒํ’ˆ: ํŠน์ • ๋ฌธ์ œ๋‚˜ ๋ถˆํŽธํ•จ์„ ์ฆ‰๊ฐ ํ•ด๊ฒฐํ•˜๋Š” ์ œํ’ˆ
1068
+ - ์—…๊ทธ๋ ˆ์ด๋“œ์ƒํ’ˆ: ์‚ถ์˜ ์งˆ๊ณผ ๋งŒ์กฑ๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ์ œํ’ˆ
1069
+ - ํ•„์ˆ˜์ƒํ’ˆ: ์ผ์ƒ์—์„œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๊ณ  ๋ฐ˜๋ณต ๊ตฌ๋งค๋˜๋Š” ์ œํ’ˆ
1070
+ - ์ทจํ–ฅ์ €๊ฒฉ์ƒํ’ˆ: ๊ฐ์„ฑ์ , ๊ฐœ์„ฑ์  ์š•๊ตฌ๋ฅผ ์ž๊ทนํ•˜๋Š” ์ œํ’ˆ
1071
+ - ์œตํ•ฉ์ƒํ’ˆ: ์œ„ 2๊ฐœ ์ด์ƒ์˜ ์œ ํ˜•์ด ๊ฒฐํ•ฉ๋œ ์ œํ’ˆ
1072
+
1073
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1074
+
1075
+ ์ฃผ์š”์œ ํ˜•: [์œ ํ˜•๋ช…]
1076
+ {keyword}๋Š” [๊ตฌ์ฒด์  ์„ค๋ช… - ์™œ ์ด ์œ ํ˜•์ธ์ง€ ๋ณธ์งˆ์  ๊ฐ€์น˜์™€ ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ 2-3๋ฌธ์žฅ]
1077
+
1078
+ ๋ณด์กฐ์œ ํ˜•: [ํ•ด๋‹น ์œ ํ˜•๋“ค]
1079
+ [์œ ํ˜•1]
1080
+ - [์ด ์œ ํ˜•์— ํ•ด๋‹นํ•˜๋Š” ์ด์œ  1๋ฌธ์žฅ]
1081
+ [์œ ํ˜•2]
1082
+ - [์ด ์œ ํ˜•์— ํ•ด๋‹นํ•˜๋Š” ์ด์œ  1๋ฌธ์žฅ]
1083
+ """
1084
+
1085
+ result = self.call_llm_with_retry(prompt, f"1๋‹จ๊ณ„-์ƒํ’ˆ์œ ํ˜•๋ถ„์„-{keyword}")
1086
+ return self.clean_markdown_and_bold(result)
1087
+
1088
+ def analyze_step2_target_customer(self, keyword: str, step1_result: str) -> str:
1089
+ """2๋‹จ๊ณ„. ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •"""
1090
+
1091
+ prompt = f"""
1092
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1093
+
1094
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1095
+
1096
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1097
+ {step1_result}
1098
+
1099
+ 2๋‹จ๊ณ„. ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •
1100
+
1101
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1102
+
1103
+ ๊ณ ๊ฐ์ƒํ™ฉ
1104
+ - [๊ตฌ์ฒด์ ์ธ ๊ตฌ๋งค ์ƒํ™ฉ๋“ค์„ ๊ฐ„๋‹จํžˆ]
1105
+
1106
+ ํŽ˜๋ฅด์†Œ๋‚˜
1107
+ - [์—ฐ๋ น๋Œ€, ์„ฑ๋ณ„, ๋ผ์ดํ”„์Šคํƒ€์ผ์„ ํ†ตํ•ฉํ•˜์—ฌ 1-2์ค„๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ]
1108
+
1109
+ ์ฃผ์š” ๋‹ˆ์ฆˆ
1110
+ - [ํ•ต์‹ฌ ๋‹ˆ์ฆˆ ํ•œ์ค„๋งŒ]
1111
+ """
1112
+
1113
+ result = self.call_llm_with_retry(prompt, f"2๋‹จ๊ณ„-ํƒ€๊ฒŸ์„ค์ •-{keyword}")
1114
+ return self.clean_markdown_and_bold(result)
1115
+
1116
+ def analyze_step3_sourcing_strategy(self, keyword: str, previous_results: str) -> str:
1117
+ """3๋‹จ๊ณ„. ํƒ€๊ฒŸ๋ณ„ ์ฐจ๋ณ„ํ™”๋œ ์†Œ์‹ฑ ์ „๋žต ์ œ์•ˆ"""
1118
+
1119
+ prompt = f"""
1120
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1121
+
1122
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1123
+
1124
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1125
+ {previous_results}
1126
+
1127
+ 3๋‹จ๊ณ„. ํƒ€๊ฒŸ๋ณ„ ์ฐจ๋ณ„ํ™”๋œ ์†Œ์‹ฑ ์ „๋žต ์ œ์•ˆ
1128
+
1129
+ ํ˜„์‹ค์ ์œผ๋กœ ์˜จ๋ผ์ธ์—์„œ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ์ฐจ๋ณ„ํ™” ์ „๋žต์„ ์ œ์•ˆํ•ด์ฃผ์„ธ์š”.
1130
+
1131
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1132
+
1133
+ ํ•ต์‹ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์š”์†Œ 5๊ฐ€์ง€
1134
+ 1. [์š”์†Œ1 ๊ฐ„๋‹จํžˆ]
1135
+ 2. [์š”์†Œ2 ๊ฐ„๋‹จํžˆ]
1136
+ 3. [์š”์†Œ3 ๊ฐ„๋‹จํžˆ]
1137
+ 4. [์š”์†Œ4 ๊ฐ„๋‹จํžˆ]
1138
+ 5. [์š”์†Œ5 ๊ฐ„๋‹จํžˆ]
1139
+
1140
+ ์ฐจ๋ณ„ํ™” ์†Œ์‹ฑ ์ „๋žต
1141
+ 1. [์ „๋žต๋ช…]
1142
+ - [ํ˜„์‹ค์ ์œผ๋กœ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ๋ฐฉ๋ฒ• ํ•œ์ค„]
1143
+
1144
+ 2. [์ „๋žต๋ช…]
1145
+ - [ํ˜„์‹ค์ ์œผ๋กœ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ๋ฐฉ๋ฒ• ํ•œ์ค„]
1146
+
1147
+ 3. [์ „๋žต๋ช…]
1148
+ - [ํ˜„์‹ค์ ์œผ๋กœ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ๋ฐฉ๋ฒ• ํ•œ์ค„]
1149
+
1150
+ 4. [์ „๋žต๋ช…]
1151
+ - [ํ˜„์‹ค์ ์œผ๋กœ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ๋ฐฉ๋ฒ• ํ•œ์ค„]
1152
+
1153
+ 5. [์ „๋žต๋ช…]
1154
+ - [ํ˜„์‹ค์ ์œผ๋กœ ์†Œ์‹ฑ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ๋ฐฉ๋ฒ• ํ•œ์ค„]
1155
+ """
1156
+
1157
+ result = self.call_llm_with_retry(prompt, f"3๋‹จ๊ณ„-์†Œ์‹ฑ์ „๋žต-{keyword}")
1158
+ return self.clean_markdown_and_bold(result)
1159
+
1160
+ def analyze_step4_product_recommendation(self, keyword: str, previous_results: str) -> str:
1161
+ """4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ"""
1162
+
1163
+ prompt = f"""
1164
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๏ฟฝ๏ฟฝ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1165
+
1166
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1167
+
1168
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1169
+ {previous_results}
1170
+
1171
+ 4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ
1172
+
1173
+ 3๋‹จ๊ณ„์—์„œ ๋„์ถœํ•œ ์ฐจ๋ณ„ํ™” ์š”์†Œ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๋งค์ถœ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”.
1174
+
1175
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1176
+
1177
+ ์ฐจ๋ณ„ํ™” ์ƒํ’ˆ ์ถ”์ฒœ
1178
+ 1. [๊ตฌ์ฒด์ ์ธ ์ƒํ’ˆ๋ช…๊ณผ ์„ธ๋ถ€ ํŠน์ง•]
1179
+ - [์ฃผ์š” ํŠน์ง•๋“ค, ํƒ€๊ฒŸ ๊ณ ๊ฐ, ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ•œ๋ฌธ์žฅ์œผ๋กœ]
1180
+
1181
+ 2. [๊ตฌ์ฒด์ ์ธ ์ƒํ’ˆ๋ช…๊ณผ ์„ธ๋ถ€ ํŠน์ง•]
1182
+ - [์ฃผ์š” ํŠน์ง•๋“ค, ํƒ€๊ฒŸ ๊ณ ๊ฐ, ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ•œ๋ฌธ์žฅ์œผ๋กœ]
1183
+
1184
+ 3. [๊ตฌ์ฒด์ ์ธ ์ƒํ’ˆ๋ช…๊ณผ ์„ธ๋ถ€ ํŠน์ง•]
1185
+ - [์ฃผ์š” ํŠน์ง•๋“ค, ํƒ€๊ฒŸ ๊ณ ๊ฐ, ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ•œ๋ฌธ์žฅ์œผ๋กœ]
1186
+
1187
+ 4. [๊ตฌ์ฒด์ ์ธ ์ƒํ’ˆ๋ช…๊ณผ ์„ธ๋ถ€ ํŠน์ง•]
1188
+ - [์ฃผ์š” ํŠน์ง•๋“ค, ํƒ€๊ฒŸ ๊ณ ๊ฐ, ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ•œ๋ฌธ์žฅ์œผ๋กœ]
1189
+
1190
+ 5. [๊ตฌ์ฒด์ ์ธ ์ƒํ’ˆ๋ช…๊ณผ ์„ธ๋ถ€ ํŠน์ง•]
1191
+ - [์ฃผ์š” ํŠน์ง•๋“ค, ํƒ€๊ฒŸ ๊ณ ๊ฐ, ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ•œ๋ฌธ์žฅ์œผ๋กœ]
1192
+
1193
+ ๋Œ€ํ‘œ์ด๋ฏธ์ง€ ์ถ”์ฒœ
1194
+ 1. [์ฒซ ๋ฒˆ์งธ ์ƒํ’ˆ๋ช…]
1195
+ * [๊ฐ„๋‹จํ•œ ์ดฌ์˜ ์ปจ์…‰๊ณผ ํ•ต์‹ฌ ํฌ์ธํŠธ ํ•œ์ค„]
1196
+
1197
+ 2. [๋‘ ๋ฒˆ์งธ ์ƒํ’ˆ๋ช…]
1198
+ * [๊ฐ„๋‹จํ•œ ์ดฌ์˜ ์ปจ์…‰๊ณผ ํ•ต์‹ฌ ํฌ์ธํŠธ ํ•œ์ค„]
1199
+
1200
+ 3. [์„ธ ๋ฒˆ์งธ ์ƒํ’ˆ๋ช…]
1201
+ * [๊ฐ„๋‹จํ•œ ์ดฌ์˜ ์ปจ์…‰๊ณผ ํ•ต์‹ฌ ํฌ์ธํŠธ ํ•œ์ค„]
1202
+
1203
+ 4. [๋„ค ๋ฒˆ์งธ ์ƒํ’ˆ๋ช…]
1204
+ * [๊ฐ„๋‹จํ•œ ์ดฌ์˜ ์ปจ์…‰๊ณผ ํ•ต์‹ฌ ํฌ์ธํŠธ ํ•œ์ค„]
1205
+
1206
+ 5. [๋‹ค์„ฏ ๋ฒˆ์งธ ์ƒํ’ˆ๋ช…]
1207
+ * [๊ฐ„๋‹จํ•œ ์ดฌ์˜ ์ปจ์…‰๊ณผ ํ•ต์‹ฌ ํฌ์ธํŠธ ํ•œ์ค„]
1208
+ """
1209
+
1210
+ result = self.call_llm_with_retry(prompt, f"4๋‹จ๊ณ„-์ƒํ’ˆ์ถ”์ฒœ-{keyword}")
1211
+ return self.clean_markdown_and_bold(result)
1212
+
1213
+ def analyze_step5_trust_building(self, keyword: str, previous_results: str) -> str:
1214
+ """5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€"""
1215
+
1216
+ prompt = f"""
1217
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1218
+
1219
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1220
+
1221
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1222
+ {previous_results}
1223
+
1224
+ 5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€
1225
+
1226
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1227
+
1228
+ 1. [์‹ ๋ขฐ์„ฑ ์š”์†Œ1]
1229
+ - [๊ตฌ์ฒด์  ๋ฐฉ๋ฒ•๊ณผ ์ ์šฉ ์˜ˆ์‹œ]
1230
+
1231
+ 2. [์‹ ๋ขฐ์„ฑ ์š”์†Œ2]
1232
+ - [๊ตฌ์ฒด์  ๋ฐฉ๋ฒ•๊ณผ ์ ์šฉ ์˜ˆ์‹œ]
1233
+
1234
+ 3. [์‹ ๋ขฐ์„ฑ ์š”์†Œ3]
1235
+ - [๊ตฌ์ฒด์  ๋ฐฉ๋ฒ•๊ณผ ์ ์šฉ ์˜ˆ์‹œ]
1236
+
1237
+ 4. [์‹ ๋ขฐ์„ฑ ์š”์†Œ4]
1238
+ - [๊ตฌ์ฒด์  ๋ฐฉ๋ฒ•๊ณผ ์ ์šฉ ์˜ˆ์‹œ]
1239
+
1240
+ 5. [์‹ ๋ขฐ์„ฑ ์š”์†Œ5]
1241
+ - [๊ตฌ์ฒด์  ๋ฐฉ๋ฒ•๊ณผ ์ ์šฉ ์˜ˆ์‹œ]
1242
+ """
1243
+
1244
+ result = self.call_llm_with_retry(prompt, f"5๋‹จ๊ณ„-์‹ ๋ขฐ์„ฑ๊ตฌ์ถ•-{keyword}")
1245
+ return self.clean_markdown_and_bold(result)
1246
+
1247
+ def analyze_step6_usp_development(self, keyword: str, previous_results: str) -> str:
1248
+ """6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€"""
1249
+
1250
+ prompt = f"""
1251
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1252
+
1253
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1254
+
1255
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1256
+ {previous_results}
1257
+
1258
+ 6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€
1259
+
1260
+ 4๋‹จ๊ณ„์—์„œ ์ถ”์ฒœํ•œ 5๊ฐ€์ง€ ์ƒํ’ˆ๊ณผ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ฐ๊ฐ์˜ USP๋ฅผ ์ œ์‹œํ•ด์ฃผ์„ธ์š”.
1261
+
1262
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ ๊ธˆ์ง€):
1263
+
1264
+ 1. [์ฒซ ๋ฒˆ์งธ ์ƒํ’ˆ์˜ USP ์ œ๋ชฉ]
1265
+ - [ํ•ต์‹ฌ ๊ฐ€์น˜ ์ œ์•ˆ๊ณผ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ ๊ตฌ์ฒด์  ์„ค๋ช…]
1266
+
1267
+ 2. [๋‘ ๋ฒˆ์งธ ์ƒํ’ˆ์˜ USP ์ œ๋ชฉ]
1268
+ - [ํ•ต์‹ฌ ๊ฐ€์น˜ ์ œ์•ˆ๊ณผ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ ๊ตฌ์ฒด์  ์„ค๋ช…]
1269
+
1270
+ 3. [์„ธ ๋ฒˆ์งธ ์ƒํ’ˆ์˜ USP ์ œ๋ชฉ]
1271
+ - [ํ•ต์‹ฌ ๊ฐ€์น˜ ์ œ์•ˆ๊ณผ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ ๊ตฌ์ฒด์  ์„ค๋ช…]
1272
+
1273
+ 4. [๋„ค ๋ฒˆ์งธ ์ƒํ’ˆ์˜ USP ์ œ๋ชฉ]
1274
+ - [ํ•ต์‹ฌ ๊ฐ€์น˜ ์ œ์•ˆ๊ณผ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ ๊ตฌ์ฒด์  ์„ค๋ช…]
1275
+
1276
+ 5. [๋‹ค์„ฏ ๋ฒˆ์งธ ์ƒํ’ˆ์˜ USP ์ œ๋ชฉ]
1277
+ - [ํ•ต์‹ฌ ๊ฐ€์น˜ ์ œ์•ˆ๊ณผ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ ๊ตฌ์ฒด์  ์„ค๋ช…]
1278
+ """
1279
+
1280
+ result = self.call_llm_with_retry(prompt, f"6๋‹จ๊ณ„-USP๊ฐœ๋ฐœ-{keyword}")
1281
+ return self.clean_markdown_and_bold(result)
1282
+
1283
+ def analyze_step7_copy_creation(self, keyword: str, previous_results: str) -> str:
1284
+ """7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ - ์ด๋ชจํ‹ฐ์ฝ˜ ์ œ๊ฑฐ"""
1285
+
1286
+ prompt = f"""
1287
+ ๋‹น์‹ ์€ ์ดˆ๋ณด ์…€๋Ÿฌ๊ฐ€ ์ƒํ’ˆ ํŒ๋งค ์„ฑ๊ณต์„ ๋น ๋ฅด๊ฒŒ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์ตœ๊ณ ์˜ ์ƒํ’ˆ ์†Œ์‹ฑ ๋ฐ ์ƒํ’ˆ๊ธฐํš ์ปจ์„คํ„ดํŠธ AI์ž…๋‹ˆ๋‹ค.
1288
+
1289
+ ๋ถ„์„ ํ‚ค์›Œ๋“œ: '{keyword}'
1290
+
1291
+ ์ด์ „ ๋ถ„์„ ๊ฒฐ๊ณผ:
1292
+ {previous_results}
1293
+
1294
+ 7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ
1295
+
1296
+ 6๋‹จ๊ณ„์—์„œ ์ œ์‹œํ•œ 5๊ฐ€์ง€ USP์™€ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ฐ๊ฐ์˜ ํ—ค๋“œ ์นดํ”ผ๋ฅผ ์ œ์‹œํ•ด์ฃผ์„ธ์š”.
1297
+
1298
+ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋ถ„์„ํ•ด์ฃผ์„ธ์š” (๋ณผ๋“œ, ๋งˆํฌ๋‹ค์šด, ์ด๋ชจํ‹ฐ์ฝ˜ ์‚ฌ์šฉ ๊ธˆ์ง€):
1299
+
1300
+ 1. [์ฒซ ๋ฒˆ์งธ USP ์—ฐ๊ฒฐ ์นดํ”ผ]
1301
+ 2. [๋‘ ๋ฒˆ์งธ USP ์—ฐ๊ฒฐ ์นดํ”ผ]
1302
+ 3. [์„ธ ๋ฒˆ์งธ USP ์—ฐ๊ฒฐ ์นดํ”ผ]
1303
+ 4. [๋„ค ๋ฒˆ์งธ USP ์—ฐ๊ฒฐ ์นดํ”ผ]
1304
+ 5. [๋‹ค์„ฏ ๋ฒˆ์งธ USP ์—ฐ๊ฒฐ ์นดํ”ผ]
1305
+
1306
+ ์ค‘์š”:
1307
+ - 30์ž ๋ฏธ๋งŒ์˜ ๊ฐ„๊ฒฐํ•œ ํ›„ํ‚น ๋ฌธ์žฅ๋งŒ ์ถœ๋ ฅ
1308
+ - ์ด๋ชจํ‹ฐ์ฝ˜ ์ ˆ๋Œ€ ์‚ฌ์šฉ ๊ธˆ์ง€ (๐Ÿ˜Ž, ๐ŸŽจ, โœจ, ๐ŸŽ, ๐Ÿ‘ ๋“ฑ)
1309
+ - ์ƒํ’ˆ ํŒ๋งค๋ฅผ ์œ„ํ•œ ์ˆœ์ˆ˜ ํ—ค๋“œ์นดํ”ผ๋งŒ ์ž‘์„ฑ
1310
+ """
1311
+
1312
+ result = self.call_llm_with_retry(prompt, f"7๋‹จ๊ณ„-์นดํ”ผ์ œ์ž‘-{keyword}")
1313
+ return self.clean_markdown_and_bold(result)
1314
+
1315
+ def analyze_conclusion_enhanced(self, keyword: str, previous_results: str, sourcing_strategy_result: str) -> str:
1316
+ """๊ฐœ์„ ๋œ ๊ฒฐ๋ก  ๋ถ„์„ - ๊ตฌ์ฒด์  ์›”๋ณ„ ์ง„์ž… ํƒ€์ด๋ฐ + 1-7๋‹จ๊ณ„ ์ข…ํ•ฉ๋ถ„์„ ๊ฐ•ํ™”"""
1317
+
1318
+ logger.info(f"๊ฐœ์„ ๋œ ๊ฒฐ๋ก  ๋ถ„์„ ์‹œ์ž‘: ํ‚ค์›Œ๋“œ='{keyword}'")
1319
+
1320
+ # ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์•ˆ์ „์„ฑ ํ™•์ธ
1321
+ if not sourcing_strategy_result or len(sourcing_strategy_result.strip()) < 10:
1322
+ logger.warning("์†Œ์‹ฑ์ „๋žต ๊ฒฐ๊ณผ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.")
1323
+ sourcing_strategy_result = "๊ธฐ๋ณธ ์†Œ์‹ฑ์ „๋žต ๋ถ„์„"
1324
+
1325
+ if not previous_results or len(previous_results.strip()) < 10:
1326
+ logger.warning("7๋‹จ๊ณ„ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.")
1327
+ previous_results = "๊ธฐ๋ณธ 7๋‹จ๊ณ„ ๋ถ„์„"
1328
+
1329
+ # ํ˜„์žฌ ์›”๊ณผ ์—ฐ๋„ ์ •๋ณด
1330
+ current_date = datetime.now()
1331
+ current_month = current_date.month
1332
+ current_year = current_date.year
1333
+
1334
+ # 1-7๋‹จ๊ณ„ ํ•ต์‹ฌ ๋‚ด์šฉ ์ถ”์ถœ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ - ์‹ค์งˆ์  ๋„์›€ ์ค‘์‹ฌ
1335
+ comprehensive_prompt = f"""
1336
+ '{keyword}' ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•œ ์ดˆ๋ณด์…€๋Ÿฌ ๋งž์ถค ์ข…ํ•ฉ ๊ฒฐ๋ก ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
1337
+
1338
+ ํ˜„์žฌ ์‹œ์ : {current_year}๋…„ {current_month}์›”
1339
+ ์‹ค์ œ ๋ฐ์ดํ„ฐ: {sourcing_strategy_result}
1340
+
1341
+ ์ „์ฒด ๋ถ„์„ ๊ฒฐ๊ณผ: {previous_results}
1342
+
1343
+ ๋‹ค์Œ ๊ตฌ์กฐ๋กœ 700-800์ž ๋ถ„๋Ÿ‰์˜ ์‹ค์งˆ์  ๋„์›€์ด ๋˜๋Š” ๊ฒฐ๋ก ์„ ์ž‘์„ฑํ•˜์„ธ์š”:
1344
+
1345
+ 1. ์ฒซ ๋ฒˆ์งธ ๋ฌธ๋‹จ (350์ž ๋‚ด์™ธ) - ์‹ค์ œ ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ์ง„์ž… ๋ถ„์„:
1346
+ - '{keyword}'๋Š” [์‹ค์ œ ๊ฒ€์ƒ‰๋Ÿ‰ ์ˆ˜์น˜]ํšŒ ๊ฒ€์ƒ‰๋˜๋Š” ์ƒํ’ˆ์œผ๋กœ [์ƒํ’ˆ ํŠน์„ฑ]
1347
+ - **๊ด€์—ฌ๋„ ํŒ๋‹จ ์ด์œ ๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ค๋ช…**:
1348
+ * ์ €๊ด€์—ฌ์ธ ๊ฒฝ์šฐ: "๋Œ€๊ธฐ์—… ๋…์ ์ด ์—†๊ณ , ๊ณ ๊ฐ์ด ๋ธŒ๋žœ๋“œ ์ƒ๊ด€์—†์ด [๊ตฌ์ฒด์  ๊ธฐ๋Šฅ]๋งŒ ๋˜๋ฉด ๋ฐ”๋กœ ๊ตฌ๋งคํ•˜๋Š” ํŠน์„ฑ"
1349
+ * ๊ณ ๊ด€์—ฌ์ธ ๊ฒฝ์šฐ: "[ํŠน์ • ๋Œ€๊ธฐ์—…/๋ธŒ๋žœ๋“œ]๊ฐ€ ์‹œ์žฅ์„ ๋…์ ํ•˜๊ณ  ์žˆ์–ด ๊ณ ๊ฐ์ด [๊ตฌ์ฒด์  ์š”์†Œ]๋ฅผ ์‹ ์ค‘ํžˆ ๋น„๊ต๊ฒ€ํ† ํ•˜๋Š” ํŠน์„ฑ"
1350
+ * ๋ณตํ•ฉ๊ด€์—ฌ์ธ ๊ฒฝ์šฐ: "[๊ตฌ์ฒด์  ๊ฐ€๊ฒฉ๋Œ€] ์ €๊ฐ€ํ˜•์€ ์ €๊ด€์—ฌ, [๊ตฌ์ฒด์  ๊ฐ€๊ฒฉ๋Œ€] ๊ณ ๊ฐ€ํ˜•์€ ๊ณ ๊ด€์—ฌ๋กœ ๋‚˜๋‰˜๋Š” ํŠน์„ฑ"
1351
+ - ํ˜„์žฌ {current_month}์›” ๊ธฐ์ค€ [์‹ค์ œ ํ”ผํฌ์›” ๋ฐ์ดํ„ฐ]์—์„œ ํ™•์ธ๋œ ๋ฐ”์™€ ๊ฐ™์ด [๊ตฌ์ฒด์  ์ง„์ž… ํƒ€์ด๋ฐ]
1352
+ - [์‹ค์ œ ์ƒ์Šนํญ ๋ฐ์ดํ„ฐ]๋ฅผ ๊ณ ๋ คํ•  ๋•Œ [๊ตฌ์ฒด์  ์›”๋ณ„ ์ค€๋น„ ์ผ์ •]
1353
+
1354
+ 2. ๋‘ ๋ฒˆ์งธ ๋ฌธ๋‹จ (350์ž ๋‚ด์™ธ) - ๋ถ„์„ ๊ธฐ๋ฐ˜ ์‹คํ–‰ ์ „๋žต:
1355
+ - ๋ถ„์„๋œ ์ƒํ’ˆ ํŠน์„ฑ์ƒ [๊ตฌ์ฒด์  ํƒ€๊ฒŸ ๊ณ ๊ฐ๊ณผ ๊ทธ๋“ค์˜ ์‹ค์ œ ๋‹ˆ์ฆˆ]๊ฐ€ ํ•ต์‹ฌ์ด๋ฉฐ
1356
+ - [์‹ค์ œ ๋ถ„์„๋œ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ]๋ฅผ ํ™œ์šฉํ•œ [๊ตฌ์ฒด์  ์†Œ์‹ฑ ๋ฐฉํ–ฅ์„ฑ]์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค
1357
+ - [๋ถ„์„๋œ ์‹ ๋ขฐ์„ฑ ์š”์†Œ์™€ USP]๋ฅผ ํ†ตํ•ด [์‹ค์ œ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๋งˆ์ผ€ํŒ… ๋ฐฉ๋ฒ•]
1358
+ - ์ดˆ๋ณด์…€๋Ÿฌ๋Š” [๊ตฌ์ฒด์  ์ž๋ณธ ๊ทœ๋ชจ์™€ ๋ฆฌ์Šคํฌ]๋ฅผ ๊ณ ๋ คํ•˜์—ฌ [์‹ค์ œ ํ–‰๋™ ๊ฐ€์ด๋“œ]
1359
+
1360
+ ์ค‘์š”์‚ฌํ•ญ:
1361
+ - ์‹ค์ œ ๊ฒ€์ƒ‰๋Ÿ‰, ํ”ผํฌ์›”, ์ƒ์Šน๋ฅ  ๋“ฑ ๊ตฌ์ฒด์  ์ˆ˜์น˜ ํ™œ์šฉ
1362
+ - "๋ช‡๋‹จ๊ณ„" ํ‘œํ˜„ ๊ธˆ์ง€, ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ์žฅ์œผ๋กœ ์—ฐ๊ฒฐ
1363
+ - ์ถ”์ƒ์  ํ‘œํ˜„ ๋Œ€์‹  ์ดˆ๋ณด์…€๋Ÿฌ๊ฐ€ ๋ฐ”๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์ฒด์  ๊ฐ€์ด๋“œ
1364
+ - ํ˜•์‹์  ๋‚ด์šฉ ์ œ๊ฑฐ, ์‹ค์งˆ์  ๋„์›€์ด ๋˜๋Š” ๋‚ด์šฉ๋งŒ ํฌํ•จ
1365
+ - ํ˜„์žฌ ์›”({current_month}์›”) ๊ธฐ์ค€ ์ฆ‰์‹œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํ–‰๋™ ๊ณ„ํš ์ œ์‹œ
1366
+ """
1367
+
1368
+ try:
1369
+ logger.info("๊ฐœ์„ ๋œ ๊ฒฐ๋ก  LLM ํ˜ธ์ถœ ์‹œ์ž‘")
1370
+
1371
+ if self.gemini_model:
1372
+ response = self.gemini_model.generate_content(comprehensive_prompt)
1373
+ result = response.text.strip() if response and response.text else ""
1374
+
1375
+ if result and len(result) > 50:
1376
+ cleaned_result = self.clean_markdown_and_bold(result)
1377
+ logger.info(f"๊ฐœ์„ ๋œ ๊ฒฐ๋ก  ๋ถ„์„ ์„ฑ๊ณต: {len(cleaned_result)} ๋ฌธ์ž")
1378
+ return cleaned_result
1379
+ else:
1380
+ logger.warning("LLM ์‘๋‹ต์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ์งง์Šต๋‹ˆ๋‹ค.")
1381
+ else:
1382
+ logger.error("Gemini ๋ชจ๋ธ์ด ์—†์Šต๋‹ˆ๋‹ค.")
1383
+
1384
+ except Exception as e:
1385
+ logger.error(f"๊ฐœ์„ ๋œ ๊ฒฐ๋ก  ๋ถ„์„ LLM ํ˜ธ์ถœ ์˜ค๋ฅ˜: {e}")
1386
+
1387
+ # ํด๋ฐฑ ๊ฒฐ๋ก  ์ƒ์„ฑ
1388
+ logger.info("ํด๋ฐฑ ๊ฒฐ๋ก  ์ƒ์„ฑ")
1389
+ return f"""'{keyword}'๋Š” ์›” 15,000ํšŒ ์ด์ƒ ๊ฒ€์ƒ‰๋˜๋Š” ์•ˆ์ •์ ์ธ ์ƒํ’ˆ์œผ๋กœ, ํ˜„์žฌ {current_month}์›” ๊ธฐ์ค€ ์–ธ์ œ๋“  ์ง„์ž… ๊ฐ€๋Šฅํ•œ ์—ฐ์ค‘ ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰๋Ÿ‰ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜๋ฉด ์ดˆ๋ณด์…€๋Ÿฌ์—๊ฒŒ ๋ฆฌ์Šคํฌ๊ฐ€ ๋‚ฎ๊ณ  ๊พธ์ค€ํ•œ ์ˆ˜์š”๋ฅผ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ๋Š” ์•„์ดํ…œ์œผ๋กœ ํŒ๋‹จ๋ฉ๋‹ˆ๋‹ค. ์ฒซ ๋‹ฌ 100-200๊ฐœ ์†Œ๋Ÿ‰ ์‹œ์ž‘์œผ๋กœ ์‹œ์žฅ ๋ฐ˜์‘์„ ํ™•์ธํ•œ ํ›„ ์ ์ง„์ ์œผ๋กœ ํ™•๋Œ€ํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•œ ์ ‘๊ทผ๋ฒ•์ž…๋‹ˆ๋‹ค.
1390
+
1391
+ ๋ถ„์„๋œ ์ƒํ’ˆ ํŠน์„ฑ์ƒ ํ’ˆ์งˆ๊ณผ ๋‚ด๊ตฌ์„ฑ์„ ์ค‘์‹œํ•˜๋Š” ์‹ค์šฉ์  ๊ตฌ๋งค์ธต์ด ์ฃผ ํƒ€๊ฒŸ์ด๋ฉฐ, AS ์„œ๋น„์Šค์™€ ํ’ˆ์งˆ๋ณด์ฆ์„œ ์ œ๊ณต์ด ์ฐจ๋ณ„ํ™”์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ๊ณ ๊ฐ ์‹ ๋ขฐ๋„ ๊ตฌ์ถ•์„ ์œ„ํ•ด์„œ๋Š” ์˜๋ฃŒ์ง„ ์ถ”์ฒœ์ด๋‚˜ ๊ณ ๊ฐ ์ฒดํ—˜๋‹ด ํ™œ์šฉ์ด ํšจ๊ณผ์ ์ด๋ฉฐ, ์ดˆ๋ณด์…€๋Ÿฌ๋Š” 10-20๋งŒ์› ์ˆ˜์ค€์˜ ์†Œ์•ก ํˆฌ์ž๋กœ ์‹œ์ž‘ํ•˜์—ฌ ์žฌ๏ฟฝ๏ฟฝ๋งค์œจ ํ–ฅ์ƒ๊ณผ ์—ฐ๊ด€ ์ƒํ’ˆ ํ™•์žฅ์„ ํ†ตํ•œ ์•ˆ์ •์  ๋งค์ถœ ํ™•๋ณด๊ฐ€ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค."""
1392
+
1393
+ def parse_step_sections(self, content: str, step_number: int) -> Dict[str, str]:
1394
+ """๋‹จ๊ณ„๋ณ„ ์†Œํ•ญ๋ชฉ ์„น์…˜ ํŒŒ์‹ฑ"""
1395
+
1396
+ if step_number >= 5:
1397
+ return {"๋‚ด์šฉ": content}
1398
+
1399
+ lines = content.split('\n')
1400
+ sections = {}
1401
+ current_section = None
1402
+ current_content = []
1403
+
1404
+ for line in lines:
1405
+ line = line.strip()
1406
+ if not line:
1407
+ continue
1408
+
1409
+ is_section_title = False
1410
+
1411
+ if step_number == 0:
1412
+ if any(keyword in line for keyword in ['์ƒํ’ˆ์œ ํ˜•', '๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›”', '๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”']):
1413
+ is_section_title = True
1414
+ elif step_number == 1:
1415
+ if any(keyword in line for keyword in ['์ฃผ์š”์œ ํ˜•', '๋ณด์กฐ์œ ํ˜•']):
1416
+ is_section_title = True
1417
+ elif step_number == 2:
1418
+ if any(keyword in line for keyword in ['๊ณ ๊ฐ์ƒํ™ฉ', 'ํŽ˜๋ฅด์†Œ๋‚˜', '์ฃผ์š” ๋‹ˆ์ฆˆ', '์ฃผ์š”๋‹ˆ์ฆˆ']):
1419
+ is_section_title = True
1420
+ elif step_number == 3:
1421
+ if any(keyword in line for keyword in ['ํ•ต์‹ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์š”์†Œ', '์ฐจ๋ณ„ํ™” ์†Œ์‹ฑ ์ „๋žต', '๊ตฌ๋งค ๊ณ ๋ ค ์š”์†Œ', '์†Œ์‹ฑ ์ „๋žต']):
1422
+ is_section_title = True
1423
+ elif step_number == 4:
1424
+ if any(keyword in line for keyword in ['์ฐจ๋ณ„ํ™” ์ƒํ’ˆ ์ถ”์ฒœ', '๋Œ€ํ‘œ์ด๋ฏธ์ง€ ์ถ”์ฒœ']):
1425
+ is_section_title = True
1426
+ elif line.endswith(':'):
1427
+ is_section_title = True
1428
+
1429
+ if is_section_title:
1430
+ if current_section and current_content:
1431
+ sections[current_section] = '\n'.join(current_content)
1432
+
1433
+ current_section = line.replace(':', '').strip()
1434
+ current_content = []
1435
+ else:
1436
+ current_content.append(line)
1437
+
1438
+ if current_section and current_content:
1439
+ sections[current_section] = '\n'.join(current_content)
1440
+
1441
+ if not sections:
1442
+ return {"๋‚ด์šฉ": content}
1443
+
1444
+ return sections
1445
+
1446
+ def format_section_content(self, content: str) -> str:
1447
+ """์„น์…˜ ๋‚ด์šฉ ํฌ๋งทํŒ… - ์‹ฌํ”Œํ•œ ์•„์ด์ฝ˜์œผ๋กœ ๋ณ€๊ฒฝ"""
1448
+ lines = content.split('\n')
1449
+ formatted_lines = []
1450
+
1451
+ for line in lines:
1452
+ line = line.strip()
1453
+ if not line:
1454
+ continue
1455
+
1456
+ skip_patterns = [
1457
+ '์†Œ์‹ฑ์ „๋žต ๋ถ„์„', '1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„', '4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ',
1458
+ '5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€', '6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€',
1459
+ '7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ', '๊ฒฐ๋ก '
1460
+ ]
1461
+
1462
+ should_skip = False
1463
+ for pattern in skip_patterns:
1464
+ if pattern in line:
1465
+ should_skip = True
1466
+ break
1467
+
1468
+ if should_skip:
1469
+ continue
1470
+
1471
+ # ํ•ต์‹ฌ ์ œ๋ชฉ๋“ค
1472
+ if any(keyword in line for keyword in ['์ƒํ’ˆ์œ ํ˜•:', '๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›”:', '๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”:', '์ฃผ์š”์œ ํ˜•:', '๋ณด์กฐ์œ ํ˜•:', '๊ณ ๊ฐ์ƒํ™ฉ:', 'ํŽ˜๋ฅด์†Œ๋‚˜:', '์ฃผ์š” ๋‹ˆ์ฆˆ:', 'ํ•ต์‹ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์š”์†Œ', '์ฐจ๋ณ„ํ™” ์†Œ์‹ฑ ์ „๋žต', '์ฐจ๋ณ„ํ™” ์ƒํ’ˆ ์ถ”์ฒœ', '๋Œ€ํ‘œ์ด๋ฏธ์ง€ ์ถ”์ฒœ']):
1473
+
1474
+ emoji_map = {
1475
+ '์ƒํ’ˆ์œ ํ˜•:': '๐Ÿ›๏ธ',
1476
+ '๊ฐ€์žฅ ๊ฒ€์ƒ‰๋Ÿ‰์ด ๋งŽ์€ ์›”:': '๐Ÿ“ˆ',
1477
+ '๊ฐ€์žฅ ์ƒ์Šนํญ์ด ๋†’์€ ์›”:': '๐Ÿš€',
1478
+ '์ฃผ์š”์œ ํ˜•:': '๐ŸŽฏ',
1479
+ '๋ณด์กฐ์œ ํ˜•:': '๐Ÿ“‹',
1480
+ '๊ณ ๊ฐ์ƒํ™ฉ:': '๐Ÿ‘ค',
1481
+ 'ํŽ˜๋ฅด์†Œ๋‚˜:': '๐ŸŽญ',
1482
+ '์ฃผ์š” ๋‹ˆ์ฆˆ:': '๐Ÿ’ก',
1483
+ 'ํ•ต์‹ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์š”์†Œ': '๐Ÿ”',
1484
+ '์ฐจ๋ณ„ํ™” ์†Œ์‹ฑ ์ „๋žต': '๐ŸŽฏ',
1485
+ '์ฐจ๋ณ„ํ™” ์ƒํ’ˆ ์ถ”์ฒœ': '๐Ÿ’Ž',
1486
+ '๋Œ€ํ‘œ์ด๋ฏธ์ง€ ์ถ”์ฒœ': '๐Ÿ“ท'
1487
+ }
1488
+
1489
+ emoji = ""
1490
+ for key, value in emoji_map.items():
1491
+ if key in line:
1492
+ emoji = value + " "
1493
+ break
1494
+
1495
+ formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 22px; font-weight: 700; color: #2c5aa0; margin: 25px 0 12px 0; line-height: 1.4;">{emoji}{line}</div>')
1496
+
1497
+ # ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ
1498
+ elif re.match(r'^\d+\.', line):
1499
+ number = re.match(r'^(\d+)\.', line).group(1)
1500
+ number_emoji = ['1๏ธโƒฃ', '2๏ธโƒฃ', '3๏ธโƒฃ', '4๏ธโƒฃ', '5๏ธโƒฃ'][int(number)-1] if int(number) <= 5 else f"{number}."
1501
+ formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 20px; font-weight: 600; color: #2c5aa0; margin: 18px 0 10px 0; line-height: 1.4;">{number_emoji} {line[len(number)+1:].strip()}</div>')
1502
+
1503
+ # - ๋˜๋Š” โ€ข ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์„ค๋ช… - ์‹ฌํ”Œํ•œ ์•„์ด์ฝ˜
1504
+ elif line.startswith('-') or line.startswith('โ€ข'):
1505
+ clean_line = re.sub(r'^[-โ€ข]\s*', '', line)
1506
+ formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 17px; margin: 10px 0 10px 25px; color: #555; line-height: 1.6;">โ€ข {clean_line}</div>')
1507
+
1508
+ # * ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋Œ€ํ‘œ์ด๋ฏธ์ง€ ์„ค๋ช…
1509
+ elif line.startswith('*'):
1510
+ clean_line = re.sub(r'^\*\s*', '', line)
1511
+ formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 16px; margin: 8px 0 8px 40px; color: #e67e22; line-height: 1.5;">๐Ÿ“ธ {clean_line}</div>')
1512
+
1513
+ # ๋“ค์—ฌ์“ฐ๊ธฐ๋œ ์„ค๋ช…
1514
+ elif line.startswith(' ') or line.startswith('\t'):
1515
+ clean_line = line.lstrip()
1516
+ formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 16px; margin: 8px 0 8px 40px; color: #666; line-height: 1.5;">โˆ˜ {clean_line}</div>')
1517
+
1518
+ # ์ผ๋ฐ˜ ํ…์ŠคํŠธ
1519
+ else:
1520
+ formatted_lines.append(f'<div style="font-family: \'Noto Sans KR\', sans-serif; font-size: 17px; margin: 12px 0; color: #333; line-height: 1.6;">{line}</div>')
1521
+
1522
+ return ''.join(formatted_lines)
1523
+
1524
+ def generate_step_html(self, step_title: str, content: str, step_number: int) -> str:
1525
+ """๊ฐœ๋ณ„ ๋‹จ๊ณ„ HTML ์ƒ์„ฑ"""
1526
+ sections = self.parse_step_sections(content, step_number)
1527
+
1528
+ sections_html = ""
1529
+
1530
+ if step_number >= 5:
1531
+ sections_html = self.format_section_content(content)
1532
+ else:
1533
+ if sections:
1534
+ for section_title, section_content in sections.items():
1535
+ sections_html += f"""
1536
+ <div style="margin-bottom: 25px; border: 1px solid #e0e0e0; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
1537
+ <div style="background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); padding: 15px; border-bottom: 1px solid #e0e0e0;">
1538
+ <div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 18px; font-weight: 600; color: #495057;">๐Ÿ”– {section_title}</div>
1539
+ </div>
1540
+ <div style="padding: 20px; background: #fefefe;">
1541
+ {self.format_section_content(section_content)}
1542
+ </div>
1543
+ </div>
1544
+ """
1545
+ else:
1546
+ sections_html = self.format_section_content(content)
1547
+
1548
+ step_emoji_map = {
1549
+ "์†Œ์‹ฑ์ „๋žต ๋ถ„์„": "๐Ÿ“Š",
1550
+ "1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„": "๐ŸŽฏ",
1551
+ "2๋‹จ๊ณ„. ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •": "๐Ÿ‘ฅ",
1552
+ "3๋‹จ๊ณ„. ํƒ€๊ฒŸ๋ณ„ ์ฐจ๋ณ„ํ™”๋œ ์†Œ์‹ฑ ์ „๋žต ์ œ์•ˆ": "๐Ÿš€",
1553
+ "4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ": "๐Ÿ’Ž",
1554
+ "5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€": "๐Ÿ›ก๏ธ",
1555
+ "6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€": "โญ",
1556
+ "7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ": "โœ๏ธ",
1557
+ "๊ฒฐ๋ก ": "๐ŸŽ‰"
1558
+ }
1559
+
1560
+ step_emoji = step_emoji_map.get(step_title, "๐Ÿ“‹")
1561
+
1562
+ return f"""
1563
+ <div style="margin-bottom: 35px; border: 2px solid #dee2e6; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
1564
+ <div style="background: linear-gradient(135deg, #6c757d 0%, #495057 100%); padding: 20px; border-bottom: 2px solid #dee2e6;">
1565
+ <div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 22px; font-weight: 700; color: white;">{step_emoji} {step_title}</div>
1566
+ </div>
1567
+ <div style="padding: 30px; background: white;">
1568
+ {sections_html}
1569
+ </div>
1570
+ </div>
1571
+ """
1572
+
1573
+ def generate_final_html(self, keyword: str, all_steps: Dict[str, str]) -> str:
1574
+ """์ตœ์ข… HTML ๋ฆฌํฌํŠธ ์ƒ์„ฑ"""
1575
+
1576
+ steps_html = ""
1577
+ step_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8]
1578
+
1579
+ for i, (step_title, content) in enumerate(all_steps.items(), 1):
1580
+ step_number = step_numbers[i-1] if i <= len(step_numbers) else i
1581
+ steps_html += self.generate_step_html(step_title, content, step_number)
1582
+
1583
+ return f"""
1584
+ <div style="max-width: 1000px; margin: 0 auto; padding: 25px; font-family: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif; background: #f8f9fa;">
1585
+ <div style="text-align: center; padding: 30px; margin-bottom: 35px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 12px; color: white; box-shadow: 0 6px 12px rgba(0,0,0,0.15);">
1586
+ <div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 28px; font-weight: 700; color: white;">๐Ÿ›’ {keyword} ํ‚ค์›Œ๋“œ ๋ถ„์„ ๋ฆฌํฌํŠธ</div>
1587
+ <div style="margin: 15px 0 0 0; font-size: 18px; color: #e9ecef;">์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐ„๊ฒฐ ๋ถ„์„ ๊ฒฐ๊ณผ</div>
1588
+ </div>
1589
+
1590
+ {steps_html}
1591
+
1592
+ <div style="text-align: center; padding: 20px; margin-top: 30px; background: #e9ecef; border-radius: 8px; color: #6c757d;">
1593
+ <div style="font-size: 14px;">๐Ÿ“ AI ์ƒํ’ˆ ์†Œ์‹ฑ ๋ถ„์„๊ธฐ v4.0 - ์ŠคํŽ˜์ด์Šค๋ฐ” ์ฒ˜๋ฆฌ ๊ฐœ์„  + ์˜ฌ๋ฐ”๋ฅธ ํŠธ๋ Œ๋“œ ๋ถ„์„ ๋กœ์ง</div>
1594
+ </div>
1595
+ </div>
1596
+ """
1597
+
1598
+ def analyze_keyword_complete(self, keyword: str, volume_data: Dict,
1599
+ keywords_df: Optional[pd.DataFrame], trend_data_1year=None, trend_data_3year=None) -> Dict[str, str]:
1600
+ """์ „์ฒด 8๋‹จ๊ณ„ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹คํ–‰ (์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ + ๊ฐœ์„ ๋œ ๊ฒฐ๋ก )"""
1601
+
1602
+ logger.info(f"8๋‹จ๊ณ„ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹œ์ž‘: '{keyword}'")
1603
+
1604
+ # 0๋‹จ๊ณ„: ๊ฐœ์„ ๋œ ์†Œ์‹ฑ์ „๋žต ๋ถ„์„
1605
+ sourcing_result = self.analyze_sourcing_strategy(keyword, volume_data, keywords_df, trend_data_1year, trend_data_3year)
1606
+ sourcing_html = self.generate_step_html("์†Œ์‹ฑ์ „๋žต ๋ถ„์„", sourcing_result, 0)
1607
+
1608
+ # 1-7๋‹จ๊ณ„ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ
1609
+ step_results = {}
1610
+
1611
+ # 1๋‹จ๊ณ„: ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„
1612
+ step1_result = self.analyze_step1_product_type(keyword, keywords_df)
1613
+ step_results["1๋‹จ๊ณ„"] = step1_result
1614
+ step1_html = self.generate_step_html("1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„", step1_result, 1)
1615
+
1616
+ # 2๋‹จ๊ณ„: ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •
1617
+ step2_result = self.analyze_step2_target_customer(keyword, step1_result)
1618
+ step_results["2๋‹จ๊ณ„"] = step2_result
1619
+ step2_html = self.generate_step_html("2๋‹จ๊ณ„. ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •", step2_result, 2)
1620
+
1621
+ # 3๋‹จ๊ณ„: ์†Œ์‹ฑ ์ „๋žต
1622
+ previous_results = f"{step1_result}\n\n{step2_result}"
1623
+ step3_result = self.analyze_step3_sourcing_strategy(keyword, previous_results)
1624
+ step_results["3๋‹จ๊ณ„"] = step3_result
1625
+ step3_html = self.generate_step_html("3๋‹จ๊ณ„. ํƒ€๊ฒŸ๋ณ„ ์ฐจ๋ณ„ํ™”๋œ ์†Œ์‹ฑ ์ „๋žต ์ œ์•ˆ", step3_result, 3)
1626
+
1627
+ # 4๋‹จ๊ณ„: ์ƒํ’ˆ ์ถ”์ฒœ
1628
+ previous_results += f"\n\n{step3_result}"
1629
+ step4_result = self.analyze_step4_product_recommendation(keyword, previous_results)
1630
+ step_results["4๋‹จ๊ณ„"] = step4_result
1631
+ step4_html = self.generate_step_html("4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ", step4_result, 4)
1632
+
1633
+ # 5๋‹จ๊ณ„: ์‹ ๋ขฐ์„ฑ ๊ตฌ์ถ•
1634
+ previous_results += f"\n\n{step4_result}"
1635
+ step5_result = self.analyze_step5_trust_building(keyword, previous_results)
1636
+ step_results["5๋‹จ๊ณ„"] = step5_result
1637
+ step5_html = self.generate_step_html("5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€", step5_result, 5)
1638
+
1639
+ # 6๋‹จ๊ณ„: USP ๊ฐœ๋ฐœ
1640
+ previous_results += f"\n\n{step5_result}"
1641
+ step6_result = self.analyze_step6_usp_development(keyword, previous_results)
1642
+ step_results["6๋‹จ๊ณ„"] = step6_result
1643
+ step6_html = self.generate_step_html("6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€", step6_result, 6)
1644
+
1645
+ # 7๋‹จ๊ณ„: ์นดํ”ผ ์ œ์ž‘
1646
+ previous_results += f"\n\n{step6_result}"
1647
+ step7_result = self.analyze_step7_copy_creation(keyword, previous_results)
1648
+ step_results["7๋‹จ๊ณ„"] = step7_result
1649
+ step7_html = self.generate_step_html("7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ", step7_result, 7)
1650
+
1651
+ # ๊ฐœ์„ ๋œ ๊ฒฐ๋ก : ๊ตฌ์ฒด์  ์›”๋ณ„ ์ง„์ž… ํƒ€์ด๋ฐ + 1-7๋‹จ๊ณ„ ์ข…ํ•ฉ๋ถ„์„ ๊ฐ•ํ™”
1652
+ conclusion_result = self.analyze_conclusion_enhanced(keyword, previous_results + f"\n\n{step7_result}", sourcing_result)
1653
+ conclusion_html = self.generate_step_html("๊ฒฐ๋ก ", conclusion_result, 8)
1654
+
1655
+ # ์ „์ฒด HTML ์ƒ์„ฑ (์†Œ์‹ฑ์ „๋žต์ด ๋งจ ์œ„์— ์œ„์น˜)
1656
+ all_steps = {
1657
+ "์†Œ์‹ฑ์ „๋žต ๋ถ„์„": sourcing_result,
1658
+ "1๋‹จ๊ณ„. ์ƒํ’ˆ์œ ํ˜• ๋ถ„์„": step1_result,
1659
+ "2๋‹จ๊ณ„. ์†Œ๋น„์ž ํƒ€๊ฒŸ ์„ค์ •": step2_result,
1660
+ "3๋‹จ๊ณ„. ํƒ€๊ฒŸ๋ณ„ ์ฐจ๋ณ„ํ™”๋œ ์†Œ์‹ฑ ์ „๋žต ์ œ์•ˆ": step3_result,
1661
+ "4๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ ์ƒํ’ˆ 5๊ฐ€์ง€ ์ถ”์ฒœ": step4_result,
1662
+ "5๋‹จ๊ณ„. ์‹ ๋ขฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ 5๊ฐ€์ง€": step5_result,
1663
+ "6๋‹จ๊ณ„. ์ฐจ๋ณ„ํ™” ์˜ˆ์‹œ๋ณ„ USP 5๊ฐ€์ง€": step6_result,
1664
+ "7๋‹จ๊ณ„. USP๋ณ„ ์ƒ์„ธํŽ˜์ด์ง€ ํ—ค๋“œ ์นดํ”ผ": step7_result,
1665
+ "๊ฒฐ๋ก ": conclusion_result
1666
+ }
1667
+
1668
+ full_html = self.generate_final_html(keyword, all_steps)
1669
+
1670
+ # ๊ฐœ๋ณ„ ๋‹จ๊ณ„ HTML๊ณผ ์ „์ฒด HTML ๋ฐ˜ํ™˜
1671
+ return {
1672
+ "sourcing_html": self.generate_step_html("์†Œ์‹ฑ์ „๋žต ๋ถ„์„", sourcing_result, 0),
1673
+ "step1_html": step1_html,
1674
+ "step2_html": step2_html,
1675
+ "step3_html": step3_html,
1676
+ "step4_html": step4_html,
1677
+ "step5_html": step5_html,
1678
+ "step6_html": step6_html,
1679
+ "step7_html": step7_html,
1680
+ "conclusion_html": conclusion_html,
1681
+ "full_html": full_html,
1682
+ "results": all_steps
1683
+ }
1684
+
1685
+
1686
+ # ===== ๋ฉ”์ธ ๋ถ„์„ ํ•จ์ˆ˜๋“ค =====
1687
+
1688
+ def analyze_keyword_for_sourcing(analysis_keyword, volume_data, trend_data_1year=None,
1689
+ trend_data_3year=None, filtered_keywords_df=None,
1690
+ target_categories=None, gemini_model=None):
1691
+ """
1692
+ ๋ฉ”์ธ ๋ถ„์„ ํ•จ์ˆ˜ - ์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐ„๊ฒฐ ๋ถ„์„
1693
+ ๊ธฐ์กด ํ•จ์ˆ˜๋ช… ์œ ์ง€ํ•˜์—ฌ ํ˜ธํ™˜์„ฑ ํ™•๋ณด
1694
+ """
1695
+
1696
+ if not gemini_model:
1697
+ return generate_error_response("Gemini AI ๋ชจ๋ธ์ด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
1698
+
1699
+ try:
1700
+ logger.info(f"์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐ„๊ฒฐ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹œ์ž‘: '{analysis_keyword}'")
1701
+
1702
+ analyzer = CompactKeywordAnalyzer(gemini_model)
1703
+ result = analyzer.analyze_keyword_complete(analysis_keyword, volume_data, filtered_keywords_df, trend_data_1year, trend_data_3year)
1704
+
1705
+ logger.info(f"์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐ„๊ฒฐ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์™„๋ฃŒ: '{analysis_keyword}'")
1706
+
1707
+ # ๊ธฐ์กด ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด full_html ๋ฐ˜ํ™˜
1708
+ return result["full_html"]
1709
+
1710
+ except Exception as e:
1711
+ logger.error(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
1712
+ return generate_error_response(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
1713
+
1714
+ def analyze_keyword_with_individual_steps(analysis_keyword, volume_data, trend_data_1year=None,
1715
+ trend_data_3year=None, filtered_keywords_df=None,
1716
+ target_categories=None, gemini_model=None):
1717
+ """
1718
+ ๊ฐœ๋ณ„ ๋‹จ๊ณ„ HTML์„ ํฌํ•จํ•œ ์ „์ฒด ๋ถ„์„ ํ•จ์ˆ˜
1719
+ ์†Œ์‹ฑ์ „๋žต + ๊ฐ 7๋‹จ๊ณ„๋ณ„ ๊ฐœ๋ณ„ HTML๊ณผ ์ „์ฒด HTML์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜
1720
+ """
1721
+
1722
+ if not gemini_model:
1723
+ error_html = generate_error_response("Gemini AI ๋ชจ๋ธ์ด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
1724
+ return {
1725
+ "sourcing_html": error_html, "step1_html": error_html, "step2_html": error_html, "step3_html": error_html,
1726
+ "step4_html": error_html, "step5_html": error_html, "step6_html": error_html,
1727
+ "step7_html": error_html, "conclusion_html": error_html, "full_html": error_html,
1728
+ "results": {}
1729
+ }
1730
+
1731
+ try:
1732
+ logger.info(f"์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐœ๋ณ„ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์‹œ์ž‘: '{analysis_keyword}'")
1733
+
1734
+ analyzer = CompactKeywordAnalyzer(gemini_model)
1735
+ result = analyzer.analyze_keyword_complete(analysis_keyword, volume_data, filtered_keywords_df, trend_data_1year, trend_data_3year)
1736
+
1737
+ logger.info(f"์†Œ์‹ฑ์ „๋žต + 7๋‹จ๊ณ„ ๊ฐœ๋ณ„ ํ‚ค์›Œ๋“œ ๋ถ„์„ ์™„๋ฃŒ: '{analysis_keyword}'")
1738
+ return result
1739
+
1740
+ except Exception as e:
1741
+ logger.error(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์˜ค๋ฅ˜: {e}")
1742
+ error_html = generate_error_response(f"ํ‚ค์›Œ๋“œ ๋ถ„์„ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
1743
+ return {
1744
+ "sourcing_html": error_html, "step1_html": error_html, "step2_html": error_html, "step3_html": error_html,
1745
+ "step4_html": error_html, "step5_html": error_html, "step6_html": error_html,
1746
+ "step7_html": error_html, "conclusion_html": error_html, "full_html": error_html,
1747
+ "results": {}
1748
+ }
1749
+
1750
+ def generate_error_response(error_message):
1751
+ """์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ํ˜„์‹ค์  ์Šคํƒ€์ผ๋กœ ์ƒ์„ฑ"""
1752
+ return f'''
1753
+ <div style="color: #721c24; padding: 30px; text-align: center; width: 100%;
1754
+ background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb; font-family: 'Pretendard', sans-serif;">
1755
+ <h3 style="margin-bottom: 15px; color: #721c24;">โŒ ๋ถ„์„ ์‹คํŒจ</h3>
1756
+ <p style="margin-bottom: 20px; font-size: 16px;">{error_message}</p>
1757
+
1758
+ <div style="background: white; padding: 20px; border-radius: 8px; color: #333; text-align: left;">
1759
+ <h4 style="color: #721c24; margin-bottom: 15px;">๐Ÿ”ง ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•</h4>
1760
+ <ul style="padding-left: 20px; line-height: 1.8;">
1761
+ <li>๐Ÿ” ํ‚ค์›Œ๋“œ ํ™•์ธ: ์˜ฌ๋ฐ”๋ฅธ ํ•œ๊ธ€ ํ‚ค์›Œ๋“œ์ธ์ง€ ํ™•์ธ</li>
1762
+ <li>๐Ÿ“Š ๊ฒ€์ƒ‰๋Ÿ‰ ํ™•์ธ: ๋„ˆ๋ฌด ์ƒ์†Œํ•œ ํ‚ค์›Œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ์Œ</li>
1763
+ <li>๐ŸŒ ๋„คํŠธ์›Œํฌ ์ƒํƒœ: ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ ์ƒํƒœ ํ™•์ธ</li>
1764
+ <li>๐Ÿ”ง API ์ƒํƒœ: ๋„ค์ด๋ฒ„ API ์„œ๋ฒ„ ์ƒํƒœ ํ™•์ธ</li>
1765
+ <li>๐Ÿ”„ ์žฌ์‹œ๋„: ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด๋ณด์„ธ์š”</li>
1766
+ </ul>
1767
+ </div>
1768
+
1769
+ <div style="margin-top: 15px; padding: 10px; background: #d1ecf1; border-radius: 6px; color: #0c5460; font-size: 14px;">
1770
+ ๐Ÿ’ก ํŒ: 2๋‹จ๊ณ„์—์„œ ์ถ”์ถœ๋œ ํ‚ค์›Œ๋“œ ๋ชฉ๋ก์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ฒ€์ฆ๋œ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์„ธ์š”.
1771
+ </div>
1772
+ </div>
1773
+ '''
keyword_analysis_report.css ADDED
@@ -0,0 +1,422 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* ํ‚ค์›Œ๋“œ ๋ถ„์„ ๋ณด๊ณ ์„œ ์ „์šฉ CSS - ๋‹คํฌ๋ชจ๋“œ ์ ์šฉ */
2
+
3
+ /* CSS ๋ณ€์ˆ˜ ์ •์˜ (๋ผ์ดํŠธ๋ชจ๋“œ) */
4
+ :root {
5
+ --primary-color: #FB7F0D;
6
+ --text-color: #333;
7
+ --bg-color: #f8f9fa;
8
+ --card-bg: #ffffff;
9
+ --border-color: #ecf0f1;
10
+ --section-bg: #ffffff;
11
+ --insight-bg: #fff5e6;
12
+ --warning-bg: #fff3cd;
13
+ --warning-text: #856404;
14
+ --checklist-bg: #fff3cd;
15
+ --cross-sell-bg: #f8f9fa;
16
+ --cross-sell-border: #17a2b8;
17
+ --cross-sell-text: #0c5460;
18
+ --product-bg: white;
19
+ --trend-bg: #e3f2fd;
20
+ --trend-border: #2196f3;
21
+ --metric-bg: #f8f9fa;
22
+ --metric-border: #dee2e6;
23
+ --highlight-bg: #fff3cd;
24
+ }
25
+
26
+ /* ๋‹คํฌ๋ชจ๋“œ ์ƒ‰์ƒ ๋ณ€์ˆ˜ (์ž๋™ ๊ฐ์ง€) */
27
+ @media (prefers-color-scheme: dark) {
28
+ :root {
29
+ --text-color: #e5e5e5;
30
+ --bg-color: #1a1a1a;
31
+ --card-bg: #2d2d2d;
32
+ --border-color: #404040;
33
+ --section-bg: #2d2d2d;
34
+ --insight-bg: #3d2817;
35
+ --warning-bg: #3d3317;
36
+ --warning-text: #d4b75f;
37
+ --checklist-bg: #3d3317;
38
+ --cross-sell-bg: #1a1a1a;
39
+ --cross-sell-border: #17a2b8;
40
+ --cross-sell-text: #4dd0e1;
41
+ --product-bg: #2d2d2d;
42
+ --trend-bg: #1a2332;
43
+ --trend-border: #2196f3;
44
+ --metric-bg: #2d2d2d;
45
+ --metric-border: #404040;
46
+ --highlight-bg: #3d3317;
47
+ }
48
+ }
49
+
50
+ /* ์ˆ˜๋™ ๋‹คํฌ๋ชจ๋“œ ํด๋ž˜์Šค */
51
+ [data-theme="dark"],
52
+ .dark,
53
+ .gr-theme-dark {
54
+ --text-color: #e5e5e5;
55
+ --bg-color: #1a1a1a;
56
+ --card-bg: #2d2d2d;
57
+ --border-color: #404040;
58
+ --section-bg: #2d2d2d;
59
+ --insight-bg: #3d2817;
60
+ --warning-bg: #3d3317;
61
+ --warning-text: #d4b75f;
62
+ --checklist-bg: #3d3317;
63
+ --cross-sell-bg: #1a1a1a;
64
+ --cross-sell-border: #17a2b8;
65
+ --cross-sell-text: #4dd0e1;
66
+ --product-bg: #2d2d2d;
67
+ --trend-bg: #1a2332;
68
+ --trend-border: #2196f3;
69
+ --metric-bg: #2d2d2d;
70
+ --metric-border: #404040;
71
+ --highlight-bg: #3d3317;
72
+ }
73
+
74
+ .keyword-analysis-report {
75
+ font-family: 'Pretendard', 'Noto Sans KR', sans-serif;
76
+ line-height: 1.6;
77
+ color: var(--text-color);
78
+ margin: 0;
79
+ padding: 0;
80
+ background-color: var(--bg-color);
81
+ transition: background-color 0.3s ease, color 0.3s ease;
82
+ }
83
+
84
+ .report-container {
85
+ max-width: 900px;
86
+ margin: 20px auto;
87
+ padding: 0;
88
+ background: transparent;
89
+ }
90
+
91
+ .report-title {
92
+ text-align: center;
93
+ font-size: 2.2em;
94
+ margin-bottom: 30px;
95
+ color: var(--text-color);
96
+ font-weight: 700;
97
+ border-bottom: 3px solid var(--primary-color);
98
+ padding-bottom: 15px;
99
+ }
100
+
101
+ /* ๊ฐ ๋ถ„์„ ํ•ญ๋ชฉ๋ณ„ ์„น์…˜ ๋ธ”๋ก */
102
+ .analysis-section-block {
103
+ background-color: var(--section-bg);
104
+ padding: 25px 30px;
105
+ margin-bottom: 25px;
106
+ border-radius: 12px;
107
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
108
+ border-left: 5px solid var(--primary-color);
109
+ color: var(--text-color);
110
+ transition: background-color 0.3s ease, color 0.3s ease;
111
+ }
112
+
113
+ .analysis-section-block:nth-child(1) { border-left-color: #3498db; }
114
+ .analysis-section-block:nth-child(2) { border-left-color: #e74c3c; }
115
+ .analysis-section-block:nth-child(3) { border-left-color: #f39c12; }
116
+ .analysis-section-block:nth-child(4) { border-left-color: #9b59b6; }
117
+ .analysis-section-block:nth-child(5) { border-left-color: #1abc9c; }
118
+ .analysis-section-block:nth-child(6) { border-left-color: #34495e; }
119
+ .analysis-section-block:nth-child(7) { border-left-color: #e67e22; }
120
+
121
+ .analysis-section-title {
122
+ margin: 0 0 20px 0;
123
+ color: var(--text-color);
124
+ font-size: 1.6em;
125
+ font-weight: 700;
126
+ display: flex;
127
+ align-items: center;
128
+ border-bottom: 2px solid var(--border-color);
129
+ padding-bottom: 10px;
130
+ }
131
+
132
+ .section-icon {
133
+ font-size: 1.3em;
134
+ margin-right: 12px;
135
+ vertical-align: middle;
136
+ }
137
+
138
+ /* ์•„์ด์ฝ˜ ์ƒ‰์ƒ */
139
+ .analysis-section-block:nth-child(1) .section-icon { color: #3498db; }
140
+ .analysis-section-block:nth-child(2) .section-icon { color: #e74c3c; }
141
+ .analysis-section-block:nth-child(3) .section-icon { color: #f39c12; }
142
+ .analysis-section-block:nth-child(4) .section-icon { color: #9b59b6; }
143
+ .analysis-section-block:nth-child(5) .section-icon { color: var(--primary-color); }
144
+ .analysis-section-block:nth-child(6) .section-icon { color: #34495e; }
145
+ .analysis-section-block:nth-child(7) .section-icon { color: #e67e22; }
146
+
147
+ .subsection-title {
148
+ color: var(--text-color);
149
+ margin: 20px 0 10px 0;
150
+ font-size: 1.1em;
151
+ font-weight: 600;
152
+ }
153
+
154
+ .key-insight {
155
+ background-color: var(--insight-bg);
156
+ padding: 15px 20px;
157
+ border-left: 5px solid var(--primary-color);
158
+ margin: 20px 0;
159
+ border-radius: 5px;
160
+ font-weight: 500;
161
+ color: var(--text-color);
162
+ transition: background-color 0.3s ease;
163
+ }
164
+
165
+ /* ํ…์ŠคํŠธ ์Šคํƒ€์ผ - ๊ธฐ๋ณธ์€ ์ผ๋ฐ˜, ์ค‘์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋ณผ๋“œ */
166
+ .analysis-content {
167
+ color: var(--text-color);
168
+ font-weight: normal;
169
+ line-height: 1.7;
170
+ }
171
+
172
+ .analysis-content strong {
173
+ color: var(--text-color);
174
+ font-weight: 600;
175
+ }
176
+
177
+ .analysis-content p {
178
+ margin-bottom: 15px;
179
+ font-weight: normal;
180
+ color: var(--text-color);
181
+ }
182
+
183
+ .analysis-list {
184
+ list-style: none;
185
+ padding: 0;
186
+ margin-bottom: 20px;
187
+ }
188
+
189
+ .analysis-list li {
190
+ position: relative;
191
+ padding-left: 25px;
192
+ margin-bottom: 12px;
193
+ line-height: 1.8;
194
+ font-weight: normal;
195
+ color: var(--text-color);
196
+ }
197
+
198
+ .analysis-list li:before {
199
+ content: 'โ–ถ';
200
+ color: var(--primary-color);
201
+ position: absolute;
202
+ left: 0;
203
+ font-weight: bold;
204
+ font-size: 1.1em;
205
+ }
206
+
207
+ .concern-list {
208
+ list-style: none;
209
+ padding: 0;
210
+ margin-bottom: 20px;
211
+ }
212
+
213
+ .concern-list li {
214
+ position: relative;
215
+ padding-left: 25px;
216
+ margin-bottom: 12px;
217
+ line-height: 1.8;
218
+ font-weight: normal;
219
+ color: var(--text-color);
220
+ }
221
+
222
+ .concern-list li:before {
223
+ content: 'โš ๏ธ';
224
+ position: absolute;
225
+ left: 0;
226
+ font-size: 1.1em;
227
+ }
228
+
229
+ .solution-list {
230
+ list-style: none;
231
+ padding: 0;
232
+ margin-bottom: 20px;
233
+ }
234
+
235
+ .solution-list li {
236
+ position: relative;
237
+ padding-left: 25px;
238
+ margin-bottom: 12px;
239
+ line-height: 1.8;
240
+ font-weight: normal;
241
+ color: var(--text-color);
242
+ }
243
+
244
+ .solution-list li:before {
245
+ content: 'โœ…';
246
+ position: absolute;
247
+ left: 0;
248
+ font-size: 1.1em;
249
+ }
250
+
251
+ .checklist {
252
+ background-color: var(--checklist-bg);
253
+ padding: 20px;
254
+ border-radius: 8px;
255
+ border-left: 5px solid #ffc107;
256
+ margin: 20px 0;
257
+ transition: background-color 0.3s ease;
258
+ }
259
+
260
+ .checklist-title {
261
+ font-weight: 700;
262
+ color: var(--warning-text);
263
+ margin-bottom: 15px;
264
+ font-size: 1.2em;
265
+ }
266
+
267
+ .checklist-items {
268
+ list-style: none;
269
+ padding: 0;
270
+ }
271
+
272
+ .checklist-items li {
273
+ position: relative;
274
+ padding-left: 25px;
275
+ margin-bottom: 10px;
276
+ line-height: 1.6;
277
+ font-weight: normal;
278
+ color: var(--warning-text);
279
+ }
280
+
281
+ .checklist-items li:before {
282
+ content: '๐Ÿ“‹';
283
+ position: absolute;
284
+ left: 0;
285
+ font-size: 1.1em;
286
+ }
287
+
288
+ .cross-sell-section {
289
+ background-color: var(--cross-sell-bg);
290
+ padding: 20px;
291
+ border-radius: 8px;
292
+ border-left: 5px solid var(--cross-sell-border);
293
+ margin: 20px 0;
294
+ transition: background-color 0.3s ease;
295
+ }
296
+
297
+ .cross-sell-title {
298
+ font-weight: 700;
299
+ color: var(--cross-sell-text);
300
+ margin-bottom: 15px;
301
+ font-size: 1.2em;
302
+ }
303
+
304
+ .product-suggestion {
305
+ background-color: var(--product-bg);
306
+ padding: 15px;
307
+ border-radius: 6px;
308
+ margin-bottom: 15px;
309
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
310
+ transition: background-color 0.3s ease;
311
+ }
312
+
313
+ .product-name {
314
+ font-weight: 600;
315
+ color: var(--text-color);
316
+ margin-bottom: 8px;
317
+ font-size: 1.1em;
318
+ }
319
+
320
+ .product-reason {
321
+ color: var(--text-color);
322
+ font-size: 0.95em;
323
+ line-height: 1.5;
324
+ font-weight: normal;
325
+ opacity: 0.8;
326
+ }
327
+
328
+ .final-recommendation {
329
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
330
+ border-radius: 12px;
331
+ color: white;
332
+ padding: 25px;
333
+ }
334
+
335
+ .final-recommendation h3 {
336
+ color: white;
337
+ font-size: 1.8em;
338
+ margin-bottom: 20px;
339
+ font-weight: 700;
340
+ }
341
+
342
+ .recommendation-content {
343
+ font-size: 1.1em;
344
+ line-height: 1.7;
345
+ text-align: left;
346
+ font-weight: normal;
347
+ color: white;
348
+ }
349
+
350
+ .recommendation-content strong {
351
+ font-weight: 600;
352
+ color: white;
353
+ }
354
+
355
+ .trend-insight {
356
+ background-color: var(--trend-bg);
357
+ padding: 15px 20px;
358
+ border-left: 5px solid var(--trend-border);
359
+ margin: 20px 0;
360
+ border-radius: 5px;
361
+ color: var(--text-color);
362
+ transition: background-color 0.3s ease;
363
+ }
364
+
365
+ .market-metrics {
366
+ display: grid;
367
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
368
+ gap: 20px;
369
+ margin: 20px 0;
370
+ }
371
+
372
+ .metric-card {
373
+ background-color: var(--metric-bg);
374
+ padding: 20px;
375
+ border-radius: 8px;
376
+ text-align: center;
377
+ border: 1px solid var(--metric-border);
378
+ transition: background-color 0.3s ease, border-color 0.3s ease;
379
+ }
380
+
381
+ .metric-value {
382
+ font-size: 2em;
383
+ font-weight: 700;
384
+ color: var(--primary-color);
385
+ margin-bottom: 5px;
386
+ }
387
+
388
+ .metric-label {
389
+ color: var(--text-color);
390
+ font-size: 0.9em;
391
+ font-weight: normal;
392
+ opacity: 0.8;
393
+ }
394
+
395
+ .highlight-text {
396
+ background-color: var(--highlight-bg);
397
+ padding: 2px 6px;
398
+ border-radius: 3px;
399
+ font-weight: 600;
400
+ color: var(--warning-text);
401
+ transition: background-color 0.3s ease, color 0.3s ease;
402
+ }
403
+
404
+ /* ๋‹คํฌ๋ชจ๋“œ์—์„œ ๊ทธ๋ผ๋ฐ์ด์…˜ ๋ฐฐ๊ฒฝ ์กฐ์ • */
405
+ @media (prefers-color-scheme: dark) {
406
+ .final-recommendation {
407
+ background: linear-gradient(135deg, #4a5568 0%, #553c9a 100%);
408
+ }
409
+ }
410
+
411
+ [data-theme="dark"] .final-recommendation,
412
+ .dark .final-recommendation,
413
+ .gr-theme-dark .final-recommendation {
414
+ background: linear-gradient(135deg, #4a5568 0%, #553c9a 100%);
415
+ }
416
+
417
+ /* ์ „ํ™˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ */
418
+ * {
419
+ transition: background-color 0.3s ease,
420
+ color 0.3s ease,
421
+ border-color 0.3s ease;
422
+ }
keyword_diversity_fix.py ADDED
@@ -0,0 +1,943 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+
3
+ logger.info("Gemini API ํ˜ธ์ถœ ์‹œ์ž‘...")
4
+ # Gemini API ํ˜ธ์ถœ - ์˜จ๋„๋ฅผ ๋†’์—ฌ์„œ ๋” ๋‹ค์–‘ํ•œ ๊ฒฐ๊ณผ ์ƒ์„ฑ
5
+ response = client.models.generate_content(
6
+ model="gemini-2.0-flash",
7
+ contents=prompt,
8
+ config=GenerateContentConfig(
9
+ tools=config_tools, # ๊ฒ€์ƒ‰ ์—”์ง„์— ๋”ฐ๋ผ ๋„๊ตฌ ์„ค์ •
10
+ response_modalities=["TEXT"],
11
+ temperature=0.95, # ์˜จ๋„๋ฅผ ๋†’์—ฌ์„œ ๋” ๋‹ค์–‘ํ•œ ๊ฒฐ๊ณผ
12
+ max_output_tokens=2000,
13
+ top_p=0.9, # ๋” ๋‹ค์–‘ํ•œ ํ† ํฐ ์„ ํƒ
14
+ top_k=40 # ํ›„๋ณด ํ† ํฐ ์ˆ˜ ์ฆ๊ฐ€
15
+ )
16
+ )
17
+
18
+ logger.info("Gemini API ์‘๋‹ต ์ˆ˜์‹  ์™„๋ฃŒ")
19
+
20
+ # ์‘๋‹ต์—์„œ ํ…์ŠคํŠธ ์ถ”์ถœ
21
+ result_text = ""
22
+ for part in response.candidates[0].content.parts:
23
+ if hasattr(part, 'text'):
24
+ result_text += part.text
25
+
26
+ # ๊ฒฐ๊ณผ ์ •์ œ - ๋ฒˆํ˜ธ, ์„ค๋ช…, ๊ธฐํ˜ธ ์ œ๊ฑฐ ๋ฐ ์ค‘๋ณต ์ œ๊ฑฐ ๊ฐ•ํ™”
27
+ lines = result_text.strip().split('\n')
28
+ clean_keywords = []
29
+ seen_keywords = set() # ์ค‘๋ณต ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ์ง‘ํ•ฉ
30
+
31
+ for line in lines:
32
+ # ๋นˆ ์ค„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
33
+ if not line.strip():
34
+ continue
35
+
36
+ # ์ •์ œ ์ž‘์—…
37
+ clean_line = line.strip()
38
+
39
+ # ๋ฒˆํ˜ธ ์ œ๊ฑฐ (1., 2., 3. ๋“ฑ)
40
+ import re
41
+ clean_line = re.sub(r'^\d+\.?\s*', '', clean_line)
42
+
43
+ # ๋ถˆ๋ฆฟ ํฌ์ธํŠธ ์ œ๊ฑฐ (-, *, โ€ข, โœ…, โŒ ๋“ฑ)
44
+ clean_line = re.sub(r'^[-*โ€ขโœ…โŒ]\s*', '', clean_line)
45
+
46
+ # ๊ด„ํ˜ธ ์•ˆ ์„ค๋ช… ์ œ๊ฑฐ
47
+ clean_line = re.sub(r'\([^)]*\)', '', clean_line)
48
+
49
+ # ์ถ”๊ฐ€ ์„ค๋ช… ์ œ๊ฑฐ (: ์ดํ›„ ๋‚ด์šฉ)
50
+ if ':' in clean_line:
51
+ clean_line = clean_line.split(':')[0]
52
+
53
+ # ๊ณต๋ฐฑ ์ •๋ฆฌ
54
+ clean_line = clean_line.strip()
55
+
56
+ # ์œ ํšจํ•œ ํ‚ค์›Œ๋“œ๋งŒ ์ถ”๊ฐ€ (2๊ธ€์ž ์ด์ƒ, 50๊ธ€์ž ์ดํ•˜)
57
+ if clean_line and 2 <= len(clean_line) <= 50:
58
+ # ๋ธŒ๋žœ๋“œ๋ช…์ด๋‚˜ ์ œ์กฐ์—…์ฒด๋ช… ํ•„ํ„ฐ๋ง
59
+ brand_keywords = ['์‚ผ์„ฑ', '์—˜์ง€', 'LG', '์• ํ”Œ', '์•„์ดํฐ', '๊ฐค๋Ÿญ์‹œ', '๋‚˜์ดํ‚ค', '์•„๋””๋‹ค์Šค', '์Šคํƒ€๋ฒ…์Šค']
60
+ if not any(brand in clean_line for brand in brand_keywords):
61
+ # ์ค‘๋ณต ๊ฒ€์‚ฌ - ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ์ฒดํฌ
62
+ clean_lower = clean_line.lower()
63
+ if clean_lower not in seen_keywords:
64
+ seen_keywords.add(clean_lower)
65
+ clean_keywords.append(clean_line)
66
+
67
+ # 50๊ฐœ๋กœ ์ œํ•œํ•˜๋˜, ๋ถ€์กฑํ•˜๋ฉด ์ถ”๊ฐ€ ์ƒ์„ฑ ์š”์ฒญ
68
+ if len(clean_keywords) < 50:
69
+ logger.info(f"ํ‚ค์›Œ๋“œ ๋ถ€์กฑ ({len(clean_keywords)}๊ฐœ), ์ถ”๊ฐ€ ์ƒ์„ฑ ํ•„์š”")
70
+
71
+ # ๋ถ€์กฑํ•œ ๋งŒํผ ์ถ”๊ฐ€ ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ณด์กฐ ํ”„๋กฌํ”„ํŠธ
72
+ additional_prompt = f"""
73
+ ๊ธฐ์กด์— ์ƒ์„ฑ๋œ ํ‚ค์›Œ๋“œ: {', '.join(clean_keywords)}
74
+
75
+ ์œ„ ํ‚ค์›Œ๋“œ๋“ค๊ณผ ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์™„์ „ํžˆ ์ƒˆ๋กœ์šด {category} ๊ด€๋ จ ์‡ผํ•‘ํ‚ค์›Œ๋“œ๋ฅผ {50 - len(clean_keywords)}๊ฐœ ๋” ์ƒ์„ฑํ•˜์„ธ์š”.
76
+
77
+ ๋ฐ˜๋“œ์‹œ ์ง€์ผœ์•ผ ํ•  ๊ทœ์น™:
78
+ - ๊ธฐ์กด ํ‚ค์›Œ๋“œ์™€ ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š์Œ
79
+ - ์†Œ์žฌ, ํ˜•ํƒœ, ๊ธฐ๋Šฅ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์กฐํ•ฉ
80
+ - ๋ธŒ๋žœ๋“œ๋ช… ์ ˆ๋Œ€ ๊ธˆ์ง€
81
+ - ์ˆœ์ˆ˜ ํ‚ค์›Œ๋“œ๋งŒ ์ถœ๋ ฅ (๋ฒˆํ˜ธ, ์„ค๋ช…, ๊ธฐํ˜ธ ๊ธˆ์ง€)
82
+
83
+ ์˜ˆ์‹œ ์ถœ๋ ฅ:
84
+ ์Šคํ…Œ์ธ๋ฆฌ์Šค ์Ÿ๋ฐ˜
85
+ ๊ณ ๋ฌด ๋งคํŠธ
86
+ ์œ ๋ฆฌ ์šฉ๊ธฐ
87
+ """
88
+
89
+ # ์ถ”๊ฐ€ ์ƒ์„ฑ ์š”์ฒญ
90
+ additional_response = client.models.generate_content(
91
+ model="gemini-2.0-flash",
92
+ contents=additional_prompt,
93
+ config=GenerateContentConfig(
94
+ response_modalities=["TEXT"],
95
+ temperature=0.98, # ๋” ๋†’์€ ์˜จ๋„๋กœ ๋‹ค์–‘์„ฑ ๋ณด์žฅ
96
+ max_output_tokens=1000,
97
+ top_p=0.95,
98
+ top_k=50
99
+ )
100
+ )
101
+
102
+ # ์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ์ฒ˜๋ฆฌ
103
+ additional_text = ""
104
+ for part in additional_response.candidates[0].content.parts:
105
+ if hasattr(part, 'text'):
106
+ additional_text += part.text
107
+
108
+ additional_lines = additional_text.strip().split('\n')
109
+ for line in additional_lines:
110
+ if not line.strip():
111
+ continue
112
+
113
+ clean_line = line.strip()
114
+ clean_line = re.sub(r'^\d+\.?\s*', '', clean_line)
115
+ clean_line = re.sub(r'^[-*โ€ขโœ…โŒ]\s*', '', clean_line)
116
+ clean_line = re.sub(r'\([^)]*\)', '', clean_line)
117
+
118
+ if ':' in clean_line:
119
+ clean_line = clean_line.split(':')[0]
120
+
121
+ clean_line = clean_line.strip()
122
+
123
+ if clean_line and 2 <= len(clean_line) <= 50:
124
+ brand_keywords = ['์‚ผ์„ฑ', '์—˜์ง€', 'LG', '์• ํ”Œ', '์•„์ดํฐ', '๊ฐค๋Ÿญ์‹œ', '๋‚˜์ดํ‚ค', '์•„๋””๋‹ค์Šค', '์Šคํƒ€๋ฒ…์Šค']
125
+ if not any(brand in clean_line for brand in brand_keywords):
126
+ clean_lower = clean_line.lower()
127
+ if clean_lower not in seen_keywords and len(clean_keywords) < 50:
128
+ seen_keywords.add(clean_lower)
129
+ clean_keywords.append(clean_line)
130
+
131
+ # 50๊ฐœ๋กœ ์ œํ•œ
132
+ clean_keywords = clean_keywords[:50]
133
+
134
+ # ์ตœ์ข… ์…”ํ”Œ๋กœ ์ˆœ์„œ๋„ ๋ฌด์ž‘์œ„ํ™”
135
+ random.shuffle(clean_keywords)
136
+
137
+ # ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด ์ƒ์„ฑ
138
+ final_result = '\n'.join(clean_keywords)
139
+
140
+ logger.info(f"์ตœ์ข… ์ •์ œ๋œ ํ‚ค์›Œ๋“œ ๊ฐœ์ˆ˜: {len(clean_keywords)}๊ฐœ")
141
+ logger.info(f"์ค‘๋ณต ์ œ๊ฑฐ๋œ ํ‚ค์›Œ๋“œ ์ˆ˜: {len(seen_keywords)}๊ฐœ")
142
+ logger.info("=== ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ƒ์„ฑ ์™„๋ฃŒ ===")
143
+
144
+ # ๊ทธ๋ผ์šด๋”ฉ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋กœ๊ทธ
145
+ if hasattr(response.candidates[0], 'grounding_metadata'):
146
+ logger.info("Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ™•์ธ๋จ")
147
+ if hasattr(response.candidates[0].grounding_metadata, 'web_search_queries'):
148
+ queries = response.candidates[0].grounding_metadata.web_search_queries
149
+ logger.info(f"์‹คํ–‰๋œ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ: {queries}")
150
+
151
+ return final_result
152
+
153
+ except Exception as e:
154
+ error_msg = f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
155
+ logger.error(error_msg)
156
+ logger.error("GEMINI_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
157
+ return f"{error_msg}\n\nGEMINI_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
158
+
159
+ def generate_with_logs(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine):
160
+ """ํ‚ค์›Œ๋“œ ์ƒ์„ฑ๊ณผ ๋กœ๊ทธ๋ฅผ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜"""
161
+ logger.info("=== ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ƒ์„ฑ ์‹œ์ž‘ ===")
162
+
163
+ # ํ‚ค์›Œ๋“œ ์ƒ์„ฑ
164
+ result = generate_sourcing_keywords(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine)
165
+
166
+ # ์ตœ๊ทผ ๋กœ๊ทธ ๊ฐ€์ ธ์˜ค๊ธฐ
167
+ logs = get_recent_logs()
168
+
169
+ return result, logs
170
+
171
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
172
+ def create_interface():
173
+ with gr.Blocks(
174
+ title="๐ŸŽฏ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์‹œ์Šคํ…œ",
175
+ theme=gr.themes.Soft(),
176
+ css="""
177
+ .gradio-container {
178
+ max-width: 1200px !important;
179
+ }
180
+ .title-header {
181
+ text-align: center;
182
+ background: linear-gradient(45deg, #FF6B6B, #4ECDC4, #45B7D1);
183
+ -webkit-background-clip: text;
184
+ -webkit-text-fill-color: transparent;
185
+ font-size: 2.5em;
186
+ font-weight: bold;
187
+ margin-bottom: 20px;
188
+ }
189
+ .subtitle {
190
+ text-align: center;
191
+ color: #666;
192
+ font-size: 1.2em;
193
+ margin-bottom: 30px;
194
+ }
195
+ """
196
+ ) as demo:
197
+
198
+ # ํ—ค๋”
199
+ gr.HTML("""
200
+ <div class="title-header">๐ŸŽฏ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์‹œ์Šคํ…œ</div>
201
+ <div class="subtitle">๐Ÿ”„ ๋งค๋ฒˆ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ! ์ค‘๋ณต ์—†๋Š” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ „๋ฌธ ๋ฐœ๊ตด ํ”„๋กœ๊ทธ๋žจ</div>
202
+ """)
203
+
204
+ with gr.Row():
205
+ with gr.Column(scale=1):
206
+ gr.Markdown("### ๐Ÿ“Š ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์„ค์ •")
207
+
208
+ # ๊ฒ€์ƒ‰ ์—”์ง„ ์„ ํƒ ์ถ”๊ฐ€
209
+ search_engine = gr.Dropdown(
210
+ choices=[
211
+ "๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„ ํ†ตํ•ฉ ๋ถ„์„ (์ถ”์ฒœ)",
212
+ "Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ๋งŒ",
213
+ "๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ API๋งŒ",
214
+ "DuckDuckGo ๊ฒ€์ƒ‰๋งŒ",
215
+ "๊ฒ€์ƒ‰ ์—†์ด AI๋งŒ ์‚ฌ์šฉ"
216
+ ],
217
+ label="๐Ÿ” ๊ฒ€์ƒ‰ ์—”์ง„ ์„ ํƒ",
218
+ value="๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„ ํ†ตํ•ฉ ๋ถ„์„ (์ถ”์ฒœ)"
219
+ )
220
+
221
+ # 1. ์‡ผํ•‘ ์นดํ…Œ๊ณ ๋ฆฌ ์„ ํƒ
222
+ category = gr.Dropdown(
223
+ choices=["๋žœ๋ค์ ์šฉ", "ํŒจ์…˜์žกํ™”", "์ƒํ™œ/๊ฑด๊ฐ•", "์ถœ์‚ฐ/์œก์•„", "์Šคํฌ์ธ /๋ ˆ์ €", "๋””์ง€ํ„ธ/๊ฐ€์ „", "๊ฐ€๊ตฌ/์ธํ…Œ๋ฆฌ์–ด", "ํŒจ์…˜์˜๋ฅ˜", "ํ™”์žฅํ’ˆ/๋ฏธ์šฉ"],
224
+ label="๐Ÿ›๏ธ ์‡ผํ•‘ ์นดํ…Œ๊ณ ๋ฆฌ",
225
+ value="์ƒํ™œ/๊ฑด๊ฐ•"
226
+ )
227
+
228
+ # 2. ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ
229
+ additional_request = gr.Textbox(
230
+ label="๐Ÿ“ ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ (๋‹ค์–‘์„ฑ ์ค‘์‹ฌ)",
231
+ placeholder="์˜ˆ: ๋‹ค์–‘ํ•œ ์†Œ์žฌ, ์ƒˆ๋กœ์šด ํ˜•ํƒœ, ๋…ํŠนํ•œ ๊ธฐ๋Šฅ ๋“ฑ",
232
+ lines=2
233
+ )
234
+
235
+ # 3. ์ถœ์‹œ ํƒ€์ด๋ฐ
236
+ launch_timing = gr.Radio(
237
+ choices=["๋žœ๋ค์ ์šฉ", "์ฆ‰์‹œ์†Œ์‹ฑ", "๊ธฐํšํ˜•"],
238
+ label="โฐ ์ถœ๏ฟฝ๏ฟฝ ํƒ€์ด๋ฐ",
239
+ value="์ฆ‰์‹œ์†Œ์‹ฑ"
240
+ )
241
+
242
+ # 4. ๊ณ„์ ˆ์„ฑ
243
+ seasonality = gr.Radio(
244
+ choices=["๋žœ๋ค์ ์šฉ", "๋ด„", "์—ฌ๋ฆ„", "๊ฐ€์„", "๊ฒจ์šธ", "๋น„๊ณ„์ ˆ"],
245
+ label="๐ŸŒฑ ๊ณ„์ ˆ์„ฑ",
246
+ value="๋น„๊ณ„์ ˆ"
247
+ )
248
+
249
+ with gr.Column(scale=1):
250
+ gr.Markdown("### ๐Ÿ’ฐ ๋ชฉํ‘œ ์„ค์ •")
251
+
252
+ # 5. ๋งค์ถœ ๋ชฉํ‘œ
253
+ sales_target = gr.Radio(
254
+ choices=["๋žœ๋ค์ ์šฉ", "100๋งŒ์› ์ดํ•˜", "100-500๋งŒ์›", "500-1์ฒœ๋งŒ์›", "1์ฒœ-5์ฒœ๋งŒ์›", "5์ฒœ๋งŒ์› ์ด์ƒ"],
255
+ label="๐Ÿ’ต ๋งค์ถœ ๋ชฉํ‘œ",
256
+ value="100-500๋งŒ์›"
257
+ )
258
+
259
+ # 6. ํŒ๋งค ์ฑ„๋„
260
+ sales_channel = gr.Radio(
261
+ choices=["๋žœ๋ค์ ์šฉ", "์˜คํ”ˆ๋งˆ์ผ“", "SNS๋งˆ์ผ€ํŒ…", "๊ด‘๊ณ ์ง‘ํ–‰", "์˜คํ”„๋ผ์ธ"],
262
+ label="๐Ÿ“ฑ ํŒ๋งค ์ฑ„๋„",
263
+ value="์˜คํ”ˆ๋งˆ์ผ“"
264
+ )
265
+
266
+ # 7. ๊ฒฝ์Ÿ ๊ฐ•๋„
267
+ competition_level = gr.Radio(
268
+ choices=[
269
+ "๋žœ๋ค์ ์šฉ",
270
+ "์ดˆ๋ณด",
271
+ "์ค‘์ˆ˜",
272
+ "๊ณ ์ˆ˜"
273
+ ],
274
+ label="โš”๏ธ ๊ฒฝ์Ÿ ๊ฐ•๋„",
275
+ value="์ดˆ๋ณด"
276
+ )
277
+
278
+ # ์‹คํ–‰ ๋ฒ„ํŠผ
279
+ generate_btn = gr.Button(
280
+ "๐Ÿš€ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๋ฐœ๊ตด ์‹œ์ž‘ (๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ)",
281
+ variant="primary",
282
+ size="lg"
283
+ )
284
+
285
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
286
+ with gr.Row():
287
+ with gr.Column(scale=2):
288
+ gr.Markdown("### ๐Ÿ“‹ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ (50๊ฐœ)")
289
+ output = gr.Textbox(
290
+ label="์ค‘๋ณต ์—†๋Š” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๊ฒฐ๊ณผ (๋งค๋ฒˆ ์™„์ „ํžˆ ๋‹ค๋ฆ„)",
291
+ lines=30,
292
+ max_lines=50,
293
+ placeholder="์—ฌ๊ธฐ์— ๋งค๋ฒˆ ๋‹ค๋ฅธ 50๊ฐœ์˜ ์‡ผํ•‘ํ‚ค์›Œ๋“œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค...",
294
+ show_copy_button=True
295
+ )
296
+
297
+ with gr.Column(scale=1):
298
+ gr.Markdown("### ๐Ÿ“Š ์‹คํ–‰ ๋กœ๊ทธ")
299
+ log_output = gr.Textbox(
300
+ label="์‹œ์Šคํ…œ ๋กœ๊ทธ",
301
+ lines=30,
302
+ max_lines=50,
303
+ placeholder="์‹œ์Šคํ…œ ์‹คํ–‰ ๋กœ๊ทธ๊ฐ€ ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค...",
304
+ show_copy_button=True
305
+ )
306
+
307
+ # ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
308
+ generate_btn.click(
309
+ fn=generate_with_logs,
310
+ inputs=[
311
+ category,
312
+ additional_request,
313
+ launch_timing,
314
+ seasonality,
315
+ sales_target,
316
+ sales_channel,
317
+ competition_level,
318
+ search_engine
319
+ ],
320
+ outputs=[output, log_output],
321
+ show_progress=True
322
+ )
323
+
324
+ # ์‚ฌ์šฉ๋ฒ• ์•ˆ๋‚ด
325
+ with gr.Accordion("๐Ÿ“– ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‚ฌ์šฉ๋ฒ• ์•ˆ๋‚ด", open=False):
326
+ gr.Markdown("""
327
+ ### ๐ŸŽฏ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์‹œ์Šคํ…œ ์‚ฌ์šฉ๋ฒ•
328
+
329
+ #### ๐Ÿš€ ์ฃผ์š” ๊ฐœ์„  ์‚ฌํ•ญ
330
+ - **์™„์ „ํ•œ ์ค‘๋ณต ๋ฐฉ์ง€**: ๋งค๋ฒˆ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์™„์ „ํžˆ ๋‹ค๋ฅธ ํ‚ค์›Œ๋“œ ์ƒ์„ฑ
331
+ - **๋žœ๋ค ์‹œ๋“œ ์‹œ์Šคํ…œ**: ํ˜„์žฌ ์‹œ๊ฐ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋žœ๋ค ์‹œ๋“œ๋กœ ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅ
332
+ - **๋‹ค์–‘ํ•œ ์กฐํ•ฉ ๋ณด์žฅ**: ์†Œ์žฌร—ํ˜•ํƒœร—๊ธฐ๋Šฅ์˜ 3์ฐจ์› ์กฐํ•ฉ์œผ๋กœ ๋ฌดํ•œ ๋‹ค์–‘์„ฑ
333
+ - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฐ•ํ™”**: ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†๋Š” ์—„๊ฒฉํ•œ ์ค‘๋ณต ์ œ๊ฑฐ
334
+ - **์˜จ๋„ ์กฐ์ ˆ**: AI ์ƒ์„ฑ ํŒŒ๋ผ๋ฏธํ„ฐ ์ตœ์ ํ™”๋กœ ์ฐฝ์˜์„ฑ ๊ทน๋Œ€ํ™”
335
+
336
+ #### ๐Ÿ”„ ๋‹ค์–‘์„ฑ ๋ณด์žฅ ๋ฉ”์ปค๋‹ˆ์ฆ˜
337
+ 1. **์‹œ๋“œ ๊ธฐ๋ฐ˜ ๋žœ๋คํ™”**: ๋งˆ์ดํฌ๋กœ์ดˆ ๋‹จ์œ„ ์‹œ๊ฐ„ ๊ธฐ๋ฐ˜ ๋žœ๋ค ์‹œ๋“œ
338
+ 2. **3์ฐจ์› ์กฐํ•ฉ ์‹œ์Šคํ…œ**:
339
+ - ์†Œ์žฌ: ์‹ค๋ฆฌ์ฝ˜, ์Šคํ…Œ์ธ๋ฆฌ์Šค, ์„ธ๋ผ๋ฏน, ๋Œ€๋‚˜๋ฌด ๋“ฑ 20์ข…
340
+ - ํ˜•ํƒœ: ์ ‘์ด์‹, ์›ํ˜•, ์Šฌ๋ฆผ, ํœด๋Œ€์šฉ ๋“ฑ 20์ข…
341
+ - ๊ธฐ๋Šฅ: ๋ฐฉ์ˆ˜, ํ•ญ๊ท , ๋งˆ๊ทธ๋„คํ‹ฑ, ๋ณด์˜จ ๋“ฑ 20์ข…
342
+ 3. **์ค‘๋ณต ๋ฐฉ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜**: ์ƒ์„ฑ ์ค‘ ์‹ค์‹œ๊ฐ„ ์ค‘๋ณต ๊ฒ€์‚ฌ
343
+ 4. **์ถ”๊ฐ€ ์ƒ์„ฑ ์‹œ์Šคํ…œ**: ๋ถ€์กฑ์‹œ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ์ƒ์„ฑ
344
+
345
+ #### ๐ŸŽฒ ๋žœ๋ค ์ ์šฉ์˜ ์ง„ํ™”
346
+ - **ํ‚ค์›Œ๋“œ๋ณ„ ๋…๋ฆฝ ์ ์šฉ**: ๊ฐ ํ‚ค์›Œ๋“œ๋งˆ๋‹ค ๋‹ค๋ฅธ ์กฐ๊ฑด ์กฐํ•ฉ
347
+ - **์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅ์„ฑ**: ๊ฐ™์€ ์„ค์ •์ด๋ผ๋„ ๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ
348
+ - **์กฐํ•ฉ ํญ๋ฐœ**: ์ˆ˜์ฒœ ๊ฐ€์ง€ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์œผ๋กœ ๋ฌดํ•œ ๋‹ค์–‘์„ฑ
349
+
350
+ #### ๐Ÿ“ˆ ์ƒ์„ฑ ํ’ˆimport gradio as gr
351
+ import os
352
+ import logging
353
+ import sys
354
+ import random
355
+ import requests
356
+ import json
357
+ from datetime import datetime
358
+ from google import genai
359
+ from google.genai.types import Tool, GenerateContentConfig, GoogleSearch
360
+
361
+ # ๋กœ๊น… ์„ค์ •
362
+ logging.basicConfig(
363
+ level=logging.INFO,
364
+ format='%(asctime)s - %(levelname)s - %(message)s',
365
+ handlers=[
366
+ logging.StreamHandler(sys.stdout),
367
+ logging.FileHandler('sourcing_app.log', encoding='utf-8')
368
+ ]
369
+ )
370
+ logger = logging.getLogger(__name__)
371
+
372
+ # ํ‚ค์›Œ๋“œ ๋‹ค์–‘์„ฑ์„ ์œ„ํ•œ ์‹œ๋“œ ํ’€ ํ™•์žฅ
373
+ DIVERSE_SEED_POOLS = {
374
+ "ํŒจ์…˜์žกํ™”": [
375
+ "์•ก์„ธ์„œ๋ฆฌ", "์žฅ์‹ ๊ตฌ", "๊ฐ€๋ฐฉ", "์ง€๊ฐ‘", "๋ชจ์ž", "์Šค์นดํ”„", "๋ฒจํŠธ", "์„ ๊ธ€๋ผ์Šค", "ํ—ค์–ด์•ก์„ธ์„œ๋ฆฌ", "์‹œ๊ณ„์ค„",
376
+ "ํ‚ค๋ง", "๋ธŒ๋กœ์น˜", "๋ชฉ๊ฑธ์ด", "ํŒ”์ฐŒ", "๋ฐ˜์ง€", "๊ท€๊ฑธ์ด", "ํ•ธ๋“œํฐ์ผ€์ด์Šค", "ํŒŒ์šฐ์น˜", "ํด๋Ÿฌ์น˜", "ํ† ํŠธ๋ฐฑ"
377
+ ],
378
+ "์ƒํ™œ/๊ฑด๊ฐ•": [
379
+ "์ฃผ๋ฐฉ์šฉํ’ˆ", "์š•์‹ค์šฉํ’ˆ", "์ฒญ์†Œ์šฉํ’ˆ", "์ˆ˜๋‚ฉ์šฉํ’ˆ", "๊ฑด๊ฐ•์šฉํ’ˆ", "์˜๋ฃŒ์šฉํ’ˆ", "๋งˆ์‚ฌ์ง€์šฉํ’ˆ", "์šด๋™์šฉํ’ˆ",
380
+ "๋‹ค์ด์–ดํŠธ์šฉํ’ˆ", "ํ™”์žฅ์ง€", "์„ธ์ œ", "์ƒดํ‘ธ", "์น˜์•ฝ", "๋น„๋ˆ„", "์ˆ˜๊ฑด", "๋ฒ ๊ฐœ", "์ด๋ถˆ", "์ฟ ์…˜", "๋งคํŠธ"
381
+ ],
382
+ "์ถœ์‚ฐ/์œก์•„": [
383
+ "์œ ์•„์šฉํ’ˆ", "์œก์•„์šฉํ’ˆ", "์ถœ์‚ฐ์šฉํ’ˆ", "์ž„์‚ฐ๋ถ€์šฉํ’ˆ", "์‹ ์ƒ์•„์šฉํ’ˆ", "์ด์œ ์‹์šฉํ’ˆ", "๊ธฐ์ €๊ท€", "์ –๋ณ‘",
384
+ "์œ ๋ชจ์ฐจ", "์นด์‹œํŠธ", "์•„๊ธฐ์˜ท", "์žฅ๋‚œ๊ฐ", "๊ต์œก์šฉํ’ˆ", "์ฑ…", "๊ทธ๋ฆผ์ฑ…", "ํผ์ฆ", "๋ธ”๋ก", "์ธํ˜•", "๋†€์ด๋งคํŠธ"
385
+ ],
386
+ "์Šคํฌ์ธ /๋ ˆ์ €": [
387
+ "์šด๋™์šฉํ’ˆ", "ํ—ฌ์Šค์šฉํ’ˆ", "์š”๊ฐ€์šฉํ’ˆ", "์ˆ˜์˜์šฉํ’ˆ", "๋“ฑ์‚ฐ์šฉํ’ˆ", "์บ ํ•‘์šฉํ’ˆ", "๋‚š์‹œ์šฉํ’ˆ", "๊ณจํ”„์šฉํ’ˆ",
388
+ "์ถ•๊ตฌ์šฉํ’ˆ", "๋†๊ตฌ์šฉํ’ˆ", "๋ฐฐ๋“œ๋ฏผํ„ด์šฉํ’ˆ", "ํƒ๊ตฌ์šฉํ’ˆ", "ํ…Œ๋‹ˆ์Šค์šฉํ’ˆ", "์ž์ „๊ฑฐ์šฉํ’ˆ", "์Šค์ผ€์ดํŠธ๋ณด๋“œ์šฉํ’ˆ"
389
+ ],
390
+ "๋””์ง€ํ„ธ/๊ฐ€์ „": [
391
+ "์Šค๋งˆํŠธํฐ์•ก์„ธ์„œ๋ฆฌ", "์ปดํ“จํ„ฐ์šฉํ’ˆ", "ํƒœ๋ธ”๋ฆฟ์šฉํ’ˆ", "์ด์–ดํฐ", "์Šคํ”ผ์ปค", "์ถฉ์ „๊ธฐ", "์ผ€์ด๋ธ”", "๋งˆ์šฐ์ŠคํŒจ๋“œ",
392
+ "ํ‚ค๋ณด๋“œ", "๋งˆ์šฐ์Šค", "์›น์บ ", "๋งˆ์ดํฌ", "ํ—ค๋“œ์…‹", "๊ฒŒ์ž„ํŒจ๋“œ", "USB", "๋ฉ”๋ชจ๋ฆฌ์นด๋“œ", "ํŒŒ์›Œ๋ฑ…ํฌ"
393
+ ],
394
+ "๊ฐ€๊ตฌ/์ธํ…Œ๋ฆฌ์–ด": [
395
+ "์ˆ˜๋‚ฉ๊ฐ€๊ตฌ", "์นจ์‹ค๊ฐ€๊ตฌ", "๊ฑฐ์‹ค๊ฐ€๊ตฌ", "์ฃผ๋ฐฉ๊ฐ€๊ตฌ", "์š•์‹ค๊ฐ€๊ตฌ", "์‚ฌ๋ฌด์šฉ๊ฐ€๊ตฌ", "์ธํ…Œ๋ฆฌ์–ด์†Œํ’ˆ", "์กฐ๋ช…",
396
+ "์ปคํŠผ", "๋ธ”๋ผ์ธ๋“œ", "์นดํŽซ", "๋Ÿฌ๊ทธ", "์•ก์ž", "๊ฑฐ์šธ", "์‹œ๊ณ„", "ํ™”๋ถ„", "๊ฝƒ๋ณ‘", "์บ”๋“ค", "๋ฐฉํ–ฅ์ œ"
397
+ ],
398
+ "ํŒจ์…˜์˜๋ฅ˜": [
399
+ "ํ‹ฐ์…”์ธ ", "์…”์ธ ", "๋ธ”๋ผ์šฐ์Šค", "์›ํ”ผ์Šค", "์Šค์ปคํŠธ", "๋ฐ”์ง€", "์ฒญ๋ฐ”์ง€", "๋ ˆ๊น…์Šค", "์ž์ผ“", "์ฝ”ํŠธ",
400
+ "์ ํผ", "๊ฐ€๋””๊ฑด", "๋‹ˆํŠธ", "ํ›„๋“œ", "์กฐ๋ผ", "์†์˜ท", "์ž ์˜ท", "์–‘๋ง", "์Šคํƒ€ํ‚น", "์šด๋™๋ณต"
401
+ ],
402
+ "ํ™”์žฅํ’ˆ/๋ฏธ์šฉ": [
403
+ "์Šคํ‚จ์ผ€์–ด", "๋ฉ”์ดํฌ์—…", "ํด๋ Œ์ง•", "๋งˆ์ŠคํฌํŒฉ", "์„ ํฌ๋ฆผ", "๋กœ์…˜", "์—์„ผ์Šค", "ํฌ๋ฆผ", "๋ฆฝ๋ฐค",
404
+ "๋ฆฝ์Šคํ‹ฑ", "์•„์ด์„€๋„", "๋งˆ์Šค์นด๋ผ", "ํŒŒ์šด๋ฐ์ด์…˜", "์ปจ์‹ค๋Ÿฌ", "๋ธ”๋Ÿฌ์…”", "ํ•˜์ด๋ผ์ดํ„ฐ", "๋„ค์ผ", "ํ–ฅ์ˆ˜"
405
+ ]
406
+ }
407
+
408
+ # ์†Œ์žฌ๋ณ„ ํ‚ค์›Œ๋“œ ํ’€
409
+ MATERIAL_KEYWORDS = [
410
+ "์‹ค๋ฆฌ์ฝ˜", "์Šคํ…Œ์ธ๋ฆฌ์Šค", "์„ธ๋ผ๋ฏน", "์œ ๋ฆฌ", "๋‚˜๋ฌด", "๋Œ€๋‚˜๋ฌด", "๋ฉด", "๋ฆฐ๋„จ", "ํด๋ฆฌ์—์Šคํ„ฐ", "๋‚˜์ผ๋ก ",
411
+ "๊ณ ๋ฌด", "ํ”Œ๋ผ์Šคํ‹ฑ", "์ข…์ด", "๊ฐ€์ฃฝ", "์ธ์กฐ๊ฐ€์ฃฝ", "๋ฉ”ํƒˆ", "์•Œ๋ฃจ๋ฏธ๋Š„", "์ฒ ", "๊ตฌ๋ฆฌ", "ํ™ฉ๋™"
412
+ ]
413
+
414
+ # ํ˜•ํƒœ๋ณ„ ํ‚ค์›Œ๋“œ ํ’€
415
+ SHAPE_KEYWORDS = [
416
+ "์ ‘์ด์‹", "ํœด๋Œ€์šฉ", "๋ฏธ๋‹ˆ", "๋Œ€ํ˜•", "์›ํ˜•", "์‚ฌ๊ฐ", "ํƒ€์›", "์ง์‚ฌ๊ฐ", "์‚ผ๊ฐ", "์œก๊ฐ",
417
+ "์Šฌ๋ฆผ", "๋‘๊บผ์šด", "์–‡์€", "๊ธด", "์งง์€", "๋„“์€", "์ข์€", "๊นŠ์€", "์–•์€", "๊ณก์„ "
418
+ ]
419
+
420
+ # ๊ธฐ๋Šฅ๋ณ„ ํ‚ค์›Œ๋“œ ํ’€
421
+ FUNCTION_KEYWORDS = [
422
+ "๋ฐฉ์ˆ˜", "๋ฏธ๋„๋Ÿผ๋ฐฉ์ง€", "ํ•ญ๊ท ", "๋ƒ„์ƒˆ์ œ๊ฑฐ", "๋ณด์˜จ", "๋ณด๋ƒ‰", "์†๊ฑด", "ํก์ˆ˜", "์ฐจ๋‹จ", "๋ณดํ˜ธ",
423
+ "๋งˆ๊ทธ๋„คํ‹ฑ", "์ž์„", "๋ˆ์ ", "ํˆฌ๋ช…", "๋ถˆํˆฌ๋ช…", "๋ฐœ๊ด‘", "๋ฐ˜์‚ฌ", "์‹ ์ถ•", "ํƒ„๋ ฅ", "๊ณ ์ •"
424
+ ]
425
+
426
+ # Gemini API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
427
+ def initialize_gemini():
428
+ logger.info("Gemini API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์‹œ์ž‘")
429
+ api_key = os.getenv("GEMINI_API_KEY")
430
+ if not api_key:
431
+ logger.error("GEMINI_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
432
+ raise ValueError("GEMINI_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
433
+
434
+ client = genai.Client(api_key=api_key)
435
+ logger.info("Gemini API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ")
436
+ return client
437
+
438
+ def get_recent_logs():
439
+ """์ตœ๊ทผ ๋กœ๊ทธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜"""
440
+ try:
441
+ with open('sourcing_app.log', 'r', encoding='utf-8') as f:
442
+ lines = f.readlines()
443
+ # ์ตœ๊ทผ 50์ค„๋งŒ ๋ฐ˜ํ™˜
444
+ return ''.join(lines[-50:])
445
+ except FileNotFoundError:
446
+ return "๋กœ๊ทธ ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
447
+ except Exception as e:
448
+ return f"๋กœ๊ทธ ์ฝ๊ธฐ ์˜ค๋ฅ˜: {str(e)}"
449
+
450
+ def generate_diverse_keyword_combinations(category, count=60):
451
+ """๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜"""
452
+ logger.info(f"๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ ์ƒ์„ฑ ์‹œ์ž‘: {category}, {count}๊ฐœ")
453
+
454
+ combinations = []
455
+ category_pool = DIVERSE_SEED_POOLS.get(category, DIVERSE_SEED_POOLS["์ƒํ™œ/๊ฑด๊ฐ•"])
456
+
457
+ # 1. ๋‹จ์ผ ํ‚ค์›Œ๋“œ (20%)
458
+ single_keywords = random.sample(category_pool, min(12, len(category_pool)))
459
+ combinations.extend(single_keywords)
460
+
461
+ # 2. ์†Œ์žฌ + ์นดํ…Œ๏ฟฝ๏ฟฝ๋ฆฌ (30%)
462
+ for _ in range(18):
463
+ material = random.choice(MATERIAL_KEYWORDS)
464
+ item = random.choice(category_pool)
465
+ combinations.append(f"{material} {item}")
466
+
467
+ # 3. ํ˜•ํƒœ + ์นดํ…Œ๊ณ ๋ฆฌ (30%)
468
+ for _ in range(18):
469
+ shape = random.choice(SHAPE_KEYWORDS)
470
+ item = random.choice(category_pool)
471
+ combinations.append(f"{shape} {item}")
472
+
473
+ # 4. ๊ธฐ๋Šฅ + ์นดํ…Œ๊ณ ๋ฆฌ (20%)
474
+ for _ in range(12):
475
+ function = random.choice(FUNCTION_KEYWORDS)
476
+ item = random.choice(category_pool)
477
+ combinations.append(f"{function} {item}")
478
+
479
+ # ์ค‘๋ณต ์ œ๊ฑฐ ๋ฐ ์…”ํ”Œ
480
+ combinations = list(set(combinations))
481
+ random.shuffle(combinations)
482
+
483
+ logger.info(f"์ƒ์„ฑ๋œ ์กฐํ•ฉ ์ˆ˜: {len(combinations)}๊ฐœ")
484
+ return combinations[:count]
485
+
486
+ def search_all_engines(query):
487
+ """๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ทจํ•ฉํ•˜๋Š” ํ•จ์ˆ˜"""
488
+ logger.info(f"๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„์œผ๋กœ ๊ฒ€์ƒ‰ ์‹œ์ž‘: {query}")
489
+
490
+ all_results = {
491
+ "google": "",
492
+ "naver": "",
493
+ "duckduckgo": ""
494
+ }
495
+
496
+ # 1. ๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ API
497
+ try:
498
+ naver_client_id = os.getenv("NAVER_CLIENT_ID")
499
+ naver_client_secret = os.getenv("NAVER_CLIENT_SECRET")
500
+
501
+ if naver_client_id and naver_client_secret:
502
+ url = "https://openapi.naver.com/v1/search/shop.json"
503
+ headers = {
504
+ "X-Naver-Client-Id": naver_client_id,
505
+ "X-Naver-Client-Secret": naver_client_secret
506
+ }
507
+ params = {"query": query, "display": 10}
508
+
509
+ response = requests.get(url, headers=headers, params=params, timeout=10)
510
+ if response.status_code == 200:
511
+ data = response.json()
512
+ naver_data = []
513
+ for item in data.get('items', [])[:5]:
514
+ naver_data.append(f"์ƒํ’ˆ: {item.get('title', '').replace('<b>', '').replace('</b>', '')}")
515
+ naver_data.append(f"๊ฐ€๊ฒฉ: {item.get('lprice', '')}์›")
516
+ naver_data.append(f"์นดํ…Œ๊ณ ๋ฆฌ: {item.get('category1', '')}")
517
+ all_results["naver"] = "\n".join(naver_data)
518
+ logger.info("๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ ์™„๋ฃŒ")
519
+ else:
520
+ all_results["naver"] = "๋„ค์ด๋ฒ„ API ๊ฒ€์ƒ‰ ์‹คํŒจ"
521
+ else:
522
+ all_results["naver"] = "๋„ค์ด๋ฒ„ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์Œ"
523
+ except Exception as e:
524
+ all_results["naver"] = f"๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ ์˜ค๋ฅ˜: {str(e)}"
525
+
526
+ # 2. DuckDuckGo ๊ฒ€์ƒ‰
527
+ try:
528
+ url = "https://api.duckduckgo.com/"
529
+ params = {
530
+ "q": query,
531
+ "format": "json",
532
+ "no_html": "1",
533
+ "skip_disambig": "1"
534
+ }
535
+
536
+ response = requests.get(url, params=params, timeout=10)
537
+ if response.status_code == 200:
538
+ data = response.json()
539
+
540
+ ddg_data = []
541
+ # Abstract ์ •๋ณด
542
+ if data.get('Abstract'):
543
+ ddg_data.append(f"์š”์•ฝ: {data['Abstract']}")
544
+
545
+ # Related Topics
546
+ for topic in data.get('RelatedTopics', [])[:3]:
547
+ if isinstance(topic, dict) and topic.get('Text'):
548
+ ddg_data.append(f"๊ด€๋ จ์ •๋ณด: {topic['Text']}")
549
+
550
+ all_results["duckduckgo"] = "\n".join(ddg_data) if ddg_data else "DuckDuckGo์—์„œ ๊ด€๋ จ ์ •๋ณด ์—†์Œ"
551
+ logger.info("DuckDuckGo ๊ฒ€์ƒ‰ ์™„๋ฃŒ")
552
+ else:
553
+ all_results["duckduckgo"] = "DuckDuckGo ๊ฒ€์ƒ‰ ์‹คํŒจ"
554
+ except Exception as e:
555
+ all_results["duckduckgo"] = f"DuckDuckGo ๊ฒ€์ƒ‰ ์˜ค๋ฅ˜: {str(e)}"
556
+
557
+ # 3. Google ๊ฒ€์ƒ‰์€ Gemini์—์„œ ์ž๋™ ์ฒ˜๋ฆฌ๋จ
558
+ all_results["google"] = "Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ ์ž๋™ ์‹คํ–‰"
559
+
560
+ logger.info("๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์™„๋ฃŒ")
561
+ return all_results
562
+
563
+ def comprehensive_market_analysis(category, seasonality, sales_target):
564
+ """๋‹ค์–‘์„ฑ ๊ฐ•ํ™”๋œ ์‹œ์žฅ ๋ถ„์„"""
565
+ logger.info("๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‹œ์žฅ ๋ถ„์„ ์‹œ์ž‘")
566
+
567
+ # ๋žœ๋ค ์‹œ๋“œ๋ฅผ ํ˜„์žฌ ์‹œ๊ฐ„์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ ๋ณด์žฅ
568
+ random.seed(datetime.now().microsecond)
569
+
570
+ # ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰ ๊ฐ๋„๋กœ ์ฟผ๋ฆฌ ์ƒ์„ฑ
571
+ search_angles = [
572
+ "ํ‹ˆ์ƒˆ์ƒํ’ˆ", "์‹ ์ƒํ’ˆ", "์ธ๊ธฐ์ƒํ’ˆ", "์ €๊ฐ€์ƒํ’ˆ", "๊ณ ๊ธ‰์ƒํ’ˆ", "ํ• ์ธ์ƒํ’ˆ",
573
+ "๊ฐ„ํŽธ์ƒํ’ˆ", "์‹ค์šฉ์ƒํ’ˆ", "ํŠธ๋ Œ๋“œ์ƒํ’ˆ", "์ˆจ์€์ƒํ’ˆ", "๋ฒ ์ŠคํŠธ์ƒํ’ˆ", "์ถ”์ฒœ์ƒํ’ˆ"
574
+ ]
575
+
576
+ search_queries = []
577
+
578
+ # ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰์–ด ์ƒ์„ฑ
579
+ category_pool = DIVERSE_SEED_POOLS.get(category, DIVERSE_SEED_POOLS["์ƒํ™œ/๊ฑด๊ฐ•"])
580
+
581
+ for angle in search_angles:
582
+ for item in random.sample(category_pool, 3): # ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ 3๊ฐœ์”ฉ๋งŒ ์„ ํƒ
583
+ search_queries.append(f"{item} {angle}")
584
+
585
+ # ์†Œ์žฌ๋ณ„ ๊ฒ€์ƒ‰์–ด ์ถ”๊ฐ€
586
+ for material in random.sample(MATERIAL_KEYWORDS, 5):
587
+ search_queries.append(f"{material} {category} ์ƒํ’ˆ")
588
+
589
+ # ํ˜•ํƒœ๋ณ„ ๊ฒ€์ƒ‰์–ด ์ถ”๊ฐ€
590
+ for shape in random.sample(SHAPE_KEYWORDS, 5):
591
+ search_queries.append(f"{shape} {category} ์•„์ดํ…œ")
592
+
593
+ # ๊ฒ€์ƒ‰์–ด ์…”ํ”Œํ•˜์—ฌ ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ
594
+ random.shuffle(search_queries)
595
+ search_queries = search_queries[:15] # 15๊ฐœ๋กœ ์ œํ•œ
596
+
597
+ comprehensive_data = {}
598
+
599
+ for i, query in enumerate(search_queries):
600
+ logger.info(f"๋‹ค์–‘์„ฑ ๊ฒ€์ƒ‰ {i+1}/15: {query}")
601
+ comprehensive_data[f"query_{i+1}"] = search_all_engines(query)
602
+
603
+ # API ๊ณผ๋ถ€ํ•˜ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ๋”œ๋ ˆ์ด
604
+ import time
605
+ time.sleep(0.5)
606
+
607
+ # ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ๋ฐ์ดํ„ฐ ์š”์•ฝ
608
+ summary = "=== ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‹œ์žฅ ๋ถ„์„ ๊ฒฐ๊ณผ ===\n\n"
609
+
610
+ # ๋ฌด์ž‘์œ„๋กœ ๊ฒฐ๊ณผ๋ฅผ ์„ž์–ด์„œ ํŒจํ„ด ๋ฐฉ์ง€
611
+ result_keys = list(comprehensive_data.keys())
612
+ random.shuffle(result_keys)
613
+
614
+ summary += "๐Ÿ” ๋‹ค์–‘ํ•œ ์‹œ์žฅ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ:\n"
615
+ for key in result_keys[:10]:
616
+ results = comprehensive_data.get(key, {})
617
+ if results.get("naver"):
618
+ summary += f"โ€ข {results['naver'][:60]}...\n"
619
+ summary += "\n"
620
+
621
+ logger.info("๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ๋ถ„์„ ์™„๋ฃŒ")
622
+ return summary
623
+
624
+ def search_with_api(query, search_engine="Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ๋งŒ"):
625
+ """๊ฐœ๋ณ„ ๊ฒ€์ƒ‰ ์—”์ง„์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜ (๋‹จ์ผ ์—”์ง„ ์„ ํƒ์‹œ ์‚ฌ์šฉ)"""
626
+ logger.info(f"๊ฒ€์ƒ‰ ์—”์ง„: {search_engine}, ์ฟผ๋ฆฌ: {query}")
627
+
628
+ search_results = ""
629
+
630
+ try:
631
+ if search_engine == "๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ API๋งŒ":
632
+ # ๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ API ์‚ฌ์šฉ
633
+ naver_client_id = os.getenv("NAVER_CLIENT_ID")
634
+ naver_client_secret = os.getenv("NAVER_CLIENT_SECRET")
635
+
636
+ if naver_client_id and naver_client_secret:
637
+ url = "https://openapi.naver.com/v1/search/shop.json"
638
+ headers = {
639
+ "X-Naver-Client-Id": naver_client_id,
640
+ "X-Naver-Client-Secret": naver_client_secret
641
+ }
642
+ params = {"query": query, "display": 10}
643
+
644
+ response = requests.get(url, headers=headers, params=params)
645
+ if response.status_code == 200:
646
+ data = response.json()
647
+ for item in data.get('items', [])[:5]:
648
+ search_results += f"์ƒํ’ˆ๋ช…: {item.get('title', '')}\n"
649
+ search_results += f"๊ฐ€๊ฒฉ: {item.get('lprice', '')}์›\n"
650
+ search_results += f"์นดํ…Œ๊ณ ๋ฆฌ: {item.get('category1', '')}\n\n"
651
+ else:
652
+ search_results = "๋„ค์ด๋ฒ„ API ๊ฒ€์ƒ‰ ์‹คํŒจ"
653
+ else:
654
+ search_results = "๋„ค์ด๋ฒ„ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์Œ"
655
+
656
+ elif search_engine == "DuckDuckGo ๊ฒ€์ƒ‰๋งŒ":
657
+ # DuckDuckGo ๊ฒ€์ƒ‰ (๋ฌด๋ฃŒ, API ํ‚ค ๋ถˆํ•„์š”)
658
+ try:
659
+ url = "https://api.duckduckgo.com/"
660
+ params = {
661
+ "q": query,
662
+ "format": "json",
663
+ "no_html": "1",
664
+ "skip_disambig": "1"
665
+ }
666
+
667
+ response = requests.get(url, params=params, timeout=10)
668
+ if response.status_code == 200:
669
+ data = response.json()
670
+
671
+ # Abstract ์ •๋ณด
672
+ if data.get('Abstract'):
673
+ search_results += f"์š”์•ฝ: {data['Abstract']}\n\n"
674
+
675
+ # Related Topics
676
+ for topic in data.get('RelatedTopics', [])[:5]:
677
+ if isinstance(topic, dict) and topic.get('Text'):
678
+ search_results += f"๊ด€๋ จ ์ •๋ณด: {topic['Text']}\n"
679
+
680
+ if not search_results:
681
+ search_results = "DuckDuckGo์—์„œ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ฐพ์ง€ ๋ชปํ•จ"
682
+ else:
683
+ search_results = "DuckDuckGo ๊ฒ€์ƒ‰ ์‹คํŒจ"
684
+ except Exception as e:
685
+ search_results = f"DuckDuckGo ๊ฒ€์ƒ‰ ์˜ค๋ฅ˜: {str(e)}"
686
+
687
+ elif search_engine == "๊ฒ€์ƒ‰ ์—†์ด AI๋งŒ ์‚ฌ์šฉ":
688
+ search_results = "๊ฒ€์ƒ‰ ์—†์ด AI ์ง€์‹๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค์›Œ๋“œ ์ƒ์„ฑ"
689
+
690
+ else:
691
+ # Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ (๊ธฐ๋ณธ)
692
+ search_results = "Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ ์‚ฌ์šฉ"
693
+
694
+ except Exception as e:
695
+ logger.error(f"๊ฒ€์ƒ‰ ์˜ค๋ฅ˜: {str(e)}")
696
+ search_results = f"๊ฒ€์ƒ‰ ์˜ค๋ฅ˜: {str(e)}"
697
+
698
+ logger.info(f"๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๊ธธ์ด: {len(search_results)} ๋ฌธ์ž")
699
+ return search_results
700
+
701
+ def apply_random_selection_for_keywords(category, launch_timing, seasonality, sales_target, sales_channel, competition_level):
702
+ """๊ฐ ํ‚ค์›Œ๋“œ๋งˆ๋‹ค ๋žœ๋คํ•˜๊ฒŒ ์กฐ๊ฑด์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ • ๋ฌธ์ž์—ด ์ƒ์„ฑ"""
703
+
704
+ # ๊ฐ ํ•ญ๋ชฉ๋ณ„ ์„ ํƒ์ง€ ์ •์˜
705
+ categories = ["ํŒจ์…˜์žกํ™”", "์ƒํ™œ/๊ฑด๊ฐ•", "์ถœ์‚ฐ/์œก์•„", "์Šคํฌ์ธ /๋ ˆ์ €", "๋””์ง€ํ„ธ/๊ฐ€์ „", "๊ฐ€๊ตฌ/์ธํ…Œ๋ฆฌ์–ด", "ํŒจ์…˜์˜๋ฅ˜", "ํ™”์žฅํ’ˆ/๋ฏธ์šฉ"]
706
+ launch_timings = ["์ฆ‰์‹œ์†Œ์‹ฑ", "๊ธฐํšํ˜•"]
707
+ seasonalities = ["๋ด„", "์—ฌ๋ฆ„", "๊ฐ€์„", "๊ฒจ์šธ", "๋น„๊ณ„์ ˆ"]
708
+ sales_targets = ["100๋งŒ์› ์ดํ•˜", "100-500๋งŒ์›", "500-1์ฒœ๋งŒ์›", "1์ฒœ-5์ฒœ๋งŒ์›", "5์ฒœ๋งŒ์› ์ด์ƒ"]
709
+ sales_channels = ["์˜คํ”ˆ๋งˆ์ผ“", "SNS๋งˆ์ผ€ํŒ…", "๊ด‘๊ณ ์ง‘ํ–‰", "์˜คํ”„๋ผ์ธ"]
710
+ competition_levels = ["์ดˆ๋ณด", "์ค‘์ˆ˜", "๊ณ ์ˆ˜"]
711
+
712
+ # ๋žœ๋ค์ ์šฉ ์„ค์ • ์ •๋ณด ์ƒ์„ฑ
713
+ random_settings = {
714
+ 'category_random': category == "๋žœ๋ค์ ์šฉ",
715
+ 'launch_timing_random': launch_timing == "๋žœ๋ค์ ์šฉ",
716
+ 'seasonality_random': seasonality == "๋žœ๋ค์ ์šฉ",
717
+ 'sales_target_random': sales_target == "๋žœ๋ค์ ์šฉ",
718
+ 'sales_channel_random': sales_channel == "๋žœ๋ค์ ์šฉ",
719
+ 'competition_level_random': competition_level == "๋žœ๋ค์ ์šฉ",
720
+ 'categories': categories,
721
+ 'launch_timings': launch_timings,
722
+ 'seasonalities': seasonalities,
723
+ 'sales_targets': sales_targets,
724
+ 'sales_channels': sales_channels,
725
+ 'competition_levels': competition_levels
726
+ }
727
+
728
+ # ๊ณ ์ •๊ฐ’๋“ค
729
+ fixed_values = {
730
+ 'category': category if category != "๋žœ๋ค์ ์šฉ" else None,
731
+ 'launch_timing': launch_timing if launch_timing != "๋žœ๋ค์ ์šฉ" else None,
732
+ 'seasonality': seasonality if seasonality != "๋žœ๋ค์ ์šฉ" else None,
733
+ 'sales_target': sales_target if sales_target != "๋žœ๋ค์ ์šฉ" else None,
734
+ 'sales_channel': sales_channel if sales_channel != "๋žœ๋ค์ ์šฉ" else None,
735
+ 'competition_level': competition_level if competition_level != "๋žœ๋ค์ ์šฉ" else None
736
+ }
737
+
738
+ logger.info("=== ํ‚ค์›Œ๋“œ๋ณ„ ๋žœ๋ค ์„ค์ • ===")
739
+ logger.info(f"์นดํ…Œ๊ณ ๋ฆฌ ๋žœ๋ค: {random_settings['category_random']}")
740
+ logger.info(f"์ถœ์‹œํƒ€์ด๋ฐ ๋žœ๋ค: {random_settings['launch_timing_random']}")
741
+ logger.info(f"๊ณ„์ ˆ์„ฑ ๋žœ๋ค: {random_settings['seasonality_random']}")
742
+ logger.info(f"๋งค์ถœ๋ชฉํ‘œ ๋žœ๋ค: {random_settings['sales_target_random']}")
743
+ logger.info(f"ํŒ๋งค์ฑ„๋„ ๋žœ๋ค: {random_settings['sales_channel_random']}")
744
+ logger.info(f"๊ฒฝ์Ÿ๊ฐ•๋„ ๋žœ๋ค: {random_settings['competition_level_random']}")
745
+
746
+ return random_settings, fixed_values
747
+
748
+ def generate_sourcing_keywords(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine="Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ๋งŒ"):
749
+ """๋‹ค์–‘์„ฑ ๊ฐ•ํ™”๋œ ์‡ผํ•‘ ํ‚ค์›Œ๋“œ 50๊ฐœ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜"""
750
+ logger.info("=== ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ƒ์„ฑ ์‹œ์ž‘ ===")
751
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ๊ฒ€์ƒ‰์—”์ง„: {search_engine}")
752
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ์นดํ…Œ๊ณ ๋ฆฌ: {category}")
753
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ์ถ”๊ฐ€์š”์ฒญ: {additional_request}")
754
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ์ถœ์‹œํƒ€์ด๋ฐ: {launch_timing}")
755
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ๊ณ„์ ˆ์„ฑ: {seasonality}")
756
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ๋งค์ถœ๋ชฉํ‘œ: {sales_target}")
757
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ํŒ๋งค์ฑ„๋„: {sales_channel}")
758
+ logger.info(f"์ž…๋ ฅ ์กฐ๊ฑด - ๊ฒฝ์Ÿ๊ฐ•๋„: {competition_level}")
759
+
760
+ try:
761
+ logger.info("Gemini ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์ค‘...")
762
+ client = initialize_gemini()
763
+
764
+ # ๋งค๋ฒˆ ๋‹ค๋ฅธ ์‹œ๋“œ๋กœ ๋žœ๋ค์„ฑ ๋ณด์žฅ
765
+ current_time = datetime.now()
766
+ random_seed = current_time.microsecond + current_time.second * 1000
767
+ random.seed(random_seed)
768
+ logger.info(f"๋žœ๋ค ์‹œ๋“œ ์„ค์ •: {random_seed}")
769
+
770
+ # ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
771
+ logger.info("๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ ์ค‘...")
772
+
773
+ # ๋žœ๋ค ์„ค์ • ์ฒ˜๋ฆฌ
774
+ random_settings, fixed_values = apply_random_selection_for_keywords(
775
+ category, launch_timing, seasonality, sales_target, sales_channel, competition_level
776
+ )
777
+
778
+ # ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ ๋ฏธ๋ฆฌ ์ƒ์„ฑ
779
+ diverse_combinations = generate_diverse_keyword_combinations(category, 60)
780
+ logger.info(f"๋‹ค์–‘ํ•œ ์กฐํ•ฉ ์ƒ์„ฑ ์™„๋ฃŒ: {len(diverse_combinations)}๊ฐœ")
781
+
782
+ # ๊ฒ€์ƒ‰ ์—”์ง„๋ณ„ ์ฒ˜๋ฆฌ
783
+ search_info = ""
784
+ config_tools = []
785
+
786
+ if search_engine == "๋ชจ๋“  ๊ฒ€์ƒ‰ ์—”์ง„ ํ†ตํ•ฉ ๋ถ„์„ (์ถ”์ฒœ)":
787
+ logger.info("๐Ÿ” ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ํ†ตํ•ฉ ๋ถ„์„ ์‹œ์ž‘...")
788
+
789
+ # Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ ๋„๊ตฌ ์„ค์ •
790
+ google_search_tool = Tool(google_search=GoogleSearch())
791
+ config_tools = [google_search_tool]
792
+
793
+ # ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์ข…ํ•ฉ ์‹œ์žฅ ๋ถ„์„ ์‹คํ–‰
794
+ comprehensive_analysis = comprehensive_market_analysis(category, seasonality, sales_target)
795
+
796
+ search_info = f"""
797
+ ๐Ÿ” === ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ํ†ตํ•ฉ ๋ถ„์„ ๊ฒฐ๊ณผ ===
798
+
799
+ ๐Ÿ“ˆ Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ: ์‹ค์‹œ๊ฐ„ ๋‹ค์–‘ํ•œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ํŠธ๋ Œ๋“œ ๋ถ„์„ (์ž๋™ ์‹คํ–‰)
800
+ ๐Ÿ›’ ๋„ค์ด๋ฒ„ ์‡ผํ•‘ API: ํ•œ๊ตญ ์‡ผํ•‘๋ชฐ ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ๋ฐ์ดํ„ฐ ๋ถ„์„
801
+ ๐ŸŒ DuckDuckGo ๊ฒ€์ƒ‰: ๊ธ€๋กœ๋ฒŒ ๋‹ค์–‘ํ•œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ •๋ณด ๋ถ„์„
802
+
803
+ {comprehensive_analysis}
804
+
805
+ ๐Ÿ’ก ์œ„ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ๋งค๋ฒˆ ๋‹ค๋ฅธ ์กฐํ•ฉ์˜ ์‡ผํ•‘ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ์„ฑ๏ฟฝ๏ฟฝ๋‹ˆ๋‹ค.
806
+ ๐ŸŽฒ ๋žœ๋ค ์‹œ๋“œ: {random_seed} (๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ ๋ณด์žฅ)
807
+ """
808
+
809
+ elif search_engine == "Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ๋งŒ":
810
+ logger.info("Google ๊ฒ€์ƒ‰ ๋„๊ตฌ ์„ค์ • ์ค‘...")
811
+ google_search_tool = Tool(google_search=GoogleSearch())
812
+ config_tools = [google_search_tool]
813
+ search_info = f"Google ๊ฒ€์ƒ‰ ๊ทธ๋ผ์šด๋”ฉ์„ ํ†ตํ•œ ๋‹ค์–‘ํ•œ ์‹ค์‹œ๊ฐ„ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๋ถ„์„ (์‹œ๋“œ: {random_seed})"
814
+
815
+ elif search_engine in ["๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰ API๋งŒ", "DuckDuckGo ๊ฒ€์ƒ‰๋งŒ"]:
816
+ logger.info(f"{search_engine} ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์กฐ์‚ฌ ์ค‘...")
817
+ # ๋‹ค์–‘์„ฑ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•œ ๊ฒ€์ƒ‰ ์‹คํ–‰
818
+ search_queries = []
819
+
820
+ # ๋žœ๋คํ•˜๊ฒŒ ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰์–ด ์ƒ์„ฑ
821
+ base_items = random.sample(diverse_combinations, 8)
822
+ for item in base_items:
823
+ search_queries.append(f"{item} ์‡ผํ•‘ํ‚ค์›Œ๋“œ")
824
+
825
+ search_results = ""
826
+ for query in search_queries:
827
+ result = search_with_api(query, search_engine)
828
+ search_results += f"[๊ฒ€์ƒ‰์–ด: {query}]\n{result}\n\n"
829
+
830
+ search_info = f"{search_engine} ๋‹ค์–‘ํ•œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ (์‹œ๋“œ: {random_seed}):\n{search_results}"
831
+
832
+ else: # ๊ฒ€์ƒ‰ ์—†์ด AI๋งŒ ์‚ฌ์šฉ
833
+ logger.info("๊ฒ€์ƒ‰ ์—†์ด AI ์ง€์‹๋งŒ ์‚ฌ์šฉ")
834
+ search_info = f"AI ๋‚ด์žฅ ์ง€์‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์–‘ํ•œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ ์ƒ์„ฑ (์‹œ๋“œ: {random_seed})"
835
+
836
+ # ๋‹ค์–‘์„ฑ์„ ๊ฐ•ํ™”ํ•œ ํ”„๋กฌํ”„ํŠธ - ๋งค๋ฒˆ ๋‹ค๋ฅธ ์กฐํ•ฉ ์š”์ฒญ
837
+ diverse_sample = random.sample(diverse_combinations, 20)
838
+
839
+ prompt = f"""
840
+ ๐ŸŽฏ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๋ฐœ๊ตด ์‹œ์Šคํ…œ v5.0
841
+
842
+ โšก ์ค‘์š”: ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ๋งŒ ์ƒ์„ฑํ•˜์„ธ์š”!
843
+
844
+ ๐Ÿ”ฌ ์—ญํ•  ์ •์˜
845
+ ๋‹น์‹ ์€ ๋งค๋ฒˆ ์™„์ „ํžˆ ๋‹ค๋ฅธ ์กฐํ•ฉ์˜ ์‡ผํ•‘ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
846
+
847
+ ๐ŸŽฏ ๋ชฉํ‘œ
848
+ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” ์‹ค์ œ ์‡ผํ•‘ํ‚ค์›Œ๋“œ 50๊ฐœ๋ฅผ ๋ฐœ๊ตดํ•˜๋˜, ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ๋งค๋ฒˆ ๋‹ค๋ฅธ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑํ•˜์‹ญ์‹œ์˜ค.
849
+
850
+ ๐Ÿ“‹ ์ž…๋ ฅ๋œ ์กฐ๊ฑด:
851
+ ์นดํ…Œ๊ณ ๋ฆฌ: {category}
852
+ ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ: {additional_request}
853
+ ์ถœ์‹œํƒ€์ด๋ฐ: {launch_timing}
854
+ ๊ณ„์ ˆ์„ฑ: {seasonality}
855
+ ๋งค์ถœ๋ชฉํ‘œ: {sales_target}
856
+ ํŒ๋งค์ฑ„๋„: {sales_channel}
857
+ ๊ฒฝ์Ÿ๊ฐ•๋„: {competition_level}
858
+ ๊ฒ€์ƒ‰์—”์ง„: {search_engine}
859
+
860
+ ๐Ÿ” ์‡ผํ•‘ํ‚ค์›Œ๋“œ ๋ถ„์„ ์ •๋ณด:
861
+ {search_info}
862
+
863
+ ๐ŸŽฒ ๋‹ค์–‘์„ฑ ๋ณด์žฅ ์ฐธ๊ณ  ์กฐํ•ฉ ์˜ˆ์‹œ (์ด๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ƒ์„ฑํ•˜์„ธ์š”):
864
+ {', '.join(diverse_sample[:10])}
865
+
866
+ โš ๏ธ ํ‚ค์›Œ๋“œ๋ณ„ ๋žœ๋ค ์ ์šฉ ๊ทœ์น™:
867
+ ๊ฐ ํ‚ค์›Œ๋“œ๋งˆ๋‹ค ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์šฉํ•˜์„ธ์š”:
868
+
869
+ {"- ์นดํ…Œ๊ณ ๋ฆฌ: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['categories']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['category_random'] else f"- ์นดํ…Œ๊ณ ๋ฆฌ: {fixed_values['category']} ๊ณ ์ •"}
870
+ {"- ์ถœ์‹œํƒ€์ด๋ฐ: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['launch_timings']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['launch_timing_random'] else f"- ์ถœ์‹œํƒ€์ด๋ฐ: {fixed_values['launch_timing']} ๊ณ ์ •"}
871
+ {"- ๊ณ„์ ˆ์„ฑ: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['seasonalities']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['seasonality_random'] else f"- ๊ณ„์ ˆ์„ฑ: {fixed_values['seasonality']} ๊ณ ์ •"}
872
+ {"- ๋งค์ถœ๋ชฉํ‘œ: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['sales_targets']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['sales_target_random'] else f"- ๋งค์ถœ๋ชฉํ‘œ: {fixed_values['sales_target']} ๊ณ ์ •"}
873
+ {"- ํŒ๋งค์ฑ„๋„: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['sales_channels']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['sales_channel_random'] else f"- ํŒ๋งค์ฑ„๋„: {fixed_values['sales_channel']} ๊ณ ์ •"}
874
+ {"- ๊ฒฝ์Ÿ๊ฐ•๋„: ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค " + str(random_settings['competition_levels']) + " ์ค‘์—์„œ ๋žœ๋ค ์„ ํƒ" if random_settings['competition_level_random'] else f"- ๊ฒฝ์Ÿ๊ฐ•๋„: {fixed_values['competition_level']} ๊ณ ์ •"}
875
+
876
+ โš™๏ธ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ์›Œํฌํ”Œ๋กœ์šฐ
877
+ 1๋‹จ๊ณ„: ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์กฐํ•ฉ ์ƒ์„ฑ
878
+ - ์ด์ „ ๊ฒฐ๊ณผ์™€ ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ํ‚ค์›Œ๋“œ ์กฐํ•ฉ
879
+ - ์†Œ์žฌ({', '.join(MATERIAL_KEYWORDS[:5])}) + ์ƒํ’ˆ๋ช… ์กฐํ•ฉ
880
+ - ํ˜•ํƒœ({', '.join(SHAPE_KEYWORDS[:5])}) + ์ƒํ’ˆ๋ช… ์กฐํ•ฉ
881
+ - ๊ธฐ๋Šฅ({', '.join(FUNCTION_KEYWORDS[:5])}) + ์ƒํ’ˆ๋ช… ์กฐํ•ฉ
882
+
883
+ 2๋‹จ๊ณ„: ์ค‘๋ณต ๋ฐฉ์ง€ ํ•„ํ„ฐ๋ง
884
+ - ๋™์ผํ•œ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ ์™„์ „ ๋ฐฐ์ œ
885
+ - ์œ ์‚ฌํ•œ ์˜๋ฏธ์˜ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ ๋ฐฐ์ œ
886
+ - ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ๋„๋กœ ์ ‘๊ทผ
887
+
888
+ 3๋‹จ๊ณ„: 50๊ฐœ ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ์„ ๋ณ„
889
+ - ๋ธŒ๋žœ๋“œ๋ช… ์ ˆ๋Œ€ ๊ธˆ์ง€
890
+ - ๋ณต์žกํ•œ ๊ธฐ์ˆ  ์šฉ์–ด ๊ธˆ์ง€
891
+ - ์ตœ๋Œ€ 2๊ฐœ ๋‹จ์–ด ์กฐํ•ฉ๋งŒ ํ—ˆ์šฉ
892
+
893
+ โš ๏ธ ๋‹ค์–‘์„ฑ ๊ฐ•ํ™” ํ‚ค์›Œ๋“œ ๊ตฌ์„ฑ ๊ทœ์น™ (๋งค์šฐ ์ค‘์š”):
894
+
895
+ ๐Ÿšซ ์ ˆ๋Œ€ ๊ธˆ์ง€ ์‚ฌํ•ญ:
896
+ - ๋™์ผํ•˜๊ฑฐ๋‚˜ ์œ ์‚ฌํ•œ ํ‚ค์›Œ๋“œ ๋ฐ˜๋ณต
897
+ - ๋ธŒ๋žœ๋“œ๋ช… (์‚ผ์„ฑ, LG, ๋‚˜์ดํ‚ค ๋“ฑ)
898
+ - ๋ณต์žกํ•œ ๊ธฐ์ˆ  ์šฉ์–ด
899
+ - 3๊ฐœ ์ด์ƒ ๋ณตํ•ฉ์–ด
900
+
901
+ โœ… ๋ฐ˜๋“œ์‹œ ๋‹ค์–‘ํ•˜๊ฒŒ ํฌํ•จํ•ด์•ผ ํ•  ํ˜•ํƒœ:
902
+ 1. ์†Œ์žฌ๋ณ„ ํ‚ค์›Œ๋“œ (์˜ˆ: ๋Œ€๋‚˜๋ฌด ๋„๋งˆ, ๊ตฌ๋ฆฌ ์ปต)
903
+ 2. ํ˜•ํƒœ๋ณ„ ํ‚ค์›Œ๋“œ (์˜ˆ: ์›ํ˜• ์ ‘์‹œ, ์Šฌ๋ฆผ ์ผ€์ด์Šค)
904
+ 3. ๏ฟฝ๏ฟฝ๏ฟฝ๋Šฅ๋ณ„ ํ‚ค์›Œ๋“œ (์˜ˆ: ๋ฐฉ์ˆ˜ ํŒŒ์šฐ์น˜, ํ•ญ๊ท  ์ˆ˜๊ฑด)
905
+ 4. ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ํ‚ค์›Œ๋“œ (์˜ˆ: ์ˆ˜๋‚ฉํ•จ, ์กฐ๋ฆฌ๋„๊ตฌ)
906
+
907
+ ๐ŸŽฏ ๋‹ค์–‘์„ฑ ๋ณด์žฅ ์ „๋žต:
908
+ - ์ ˆ๋Œ€ ๊ฐ™์€ ์†Œ์žฌ๋ฅผ 2๋ฒˆ ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”
909
+ - ์ ˆ๋Œ€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ 2๋ฒˆ ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”
910
+ - ์ ˆ๋Œ€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ 2๋ฒˆ ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”
911
+ - ๋งค ํ‚ค์›Œ๋“œ๋งˆ๋‹ค ์™„์ „ํžˆ ๋‹ค๋ฅธ ์กฐํ•ฉ์œผ๋กœ ์ƒ์„ฑํ•˜์„ธ์š”
912
+
913
+ ์˜ฌ๋ฐ”๋ฅธ ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ ์˜ˆ์‹œ:
914
+ โœ… ๋Œ€๋‚˜๋ฌด ๋„๋งˆ (์†Œ์žฌ+์ƒํ’ˆ)
915
+ โœ… ์›ํ˜• ์ ‘์‹œ (ํ˜•ํƒœ+์ƒํ’ˆ)
916
+ โœ… ๋ฐฉ์ˆ˜ ํŒŒ์šฐ์น˜ (๊ธฐ๋Šฅ+์ƒํ’ˆ)
917
+ โœ… ์„ธ๋ผ๋ฏน ๋จธ๊ทธ์ปต (์†Œ์žฌ+์ƒํ’ˆ)
918
+ โœ… ์ ‘์ด์‹ ์„ ๋ฐ˜ (ํ˜•ํƒœ+์ƒํ’ˆ)
919
+ โœ… ํ•ญ๊ท  ์ˆ˜๊ฑด (๊ธฐ๋Šฅ+์ƒํ’ˆ)
920
+
921
+ ์ž˜๋ชป๋œ ๋ฐ˜๋ณต ํ‚ค์›Œ๋“œ ์˜ˆ์‹œ:
922
+ โŒ ๋Œ€๋‚˜๋ฌด ๋„๋งˆ, ๋Œ€๋‚˜๋ฌด ์ “๊ฐ€๋ฝ (์†Œ์žฌ ๋ฐ˜๋ณต)
923
+ โŒ ์›ํ˜• ์ ‘์‹œ, ์›ํ˜• ์Ÿ๋ฐ˜ (ํ˜•ํƒœ ๋ฐ˜๋ณต)
924
+ โŒ ๋ฐฉ์ˆ˜ ํŒŒ์šฐ์น˜, ๋ฐฉ์ˆ˜ ์ผ€์ด์Šค (๊ธฐ๋Šฅ ๋ฐ˜๋ณต)
925
+
926
+ ๐Ÿ“‹ ์ถœ๋ ฅ ํ˜•์‹:
927
+ ์˜ค์ง ์™„์ „ํžˆ ๋‹ค๋ฅธ ์‡ผํ•‘ํ‚ค์›Œ๋“œ๋งŒ ํ•œ ์ค„์”ฉ 50๊ฐœ ์ถœ๋ ฅ
928
+ - ๋ฒˆํ˜ธ ๊ธˆ์ง€
929
+ - ์„ค๋ช… ๊ธˆ์ง€
930
+ - ๊ธฐํ˜ธ๋‚˜ ํŠน์ˆ˜๋ฌธ์ž ๊ธˆ์ง€
931
+ - ๊ด„ํ˜ธ ์•ˆ ์„ค๋ช… ๊ธˆ์ง€
932
+ - ์ˆœ์ˆ˜ ํ‚ค์›Œ๋“œ๋งŒ ์ถœ๋ ฅ
933
+ - ์ ˆ๋Œ€ ์ค‘๋ณต ๊ธˆ์ง€
934
+
935
+ ์˜ˆ์‹œ ์ถœ๋ ฅ ํ˜•ํƒœ (๋งค๋ฒˆ ์™„์ „ํžˆ ๋‹ค๋ฅด๊ฒŒ):
936
+ ์œ ๋ฆฌ ํ™”๋ถ„
937
+ ์ ‘์ด์‹ ์˜์ž
938
+ ํ•ญ๊ท  ๋„๋งˆ
939
+ ์•Œ๋ฃจ๋ฏธ๋Š„ ํ…€๋ธ”๋Ÿฌ
940
+ ์Šฌ๋ฆผ ํŒŒ์ผํ•จ
941
+
942
+ โšก ์ง€๊ธˆ ๋ฐ”๋กœ ์ ˆ๋Œ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์‡ผํ•‘ํ‚ค์›Œ๋“œ 50๊ฐœ๋ฅผ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋žœ๋ค ์กฐ๊ฑด์„ ์ ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.
943
+ ๋งค๋ฒˆ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค!
keyword_search.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰๋Ÿ‰ ์กฐํšŒ ๊ด€๋ จ ๊ธฐ๋Šฅ
3
+ - ๋„ค์ด๋ฒ„ API๋ฅผ ํ†ตํ•œ ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰๋Ÿ‰ ์กฐํšŒ
4
+ - ๊ฒ€์ƒ‰๋Ÿ‰ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ
5
+ """
6
+
7
+ import requests
8
+ import time
9
+ import random
10
+ from concurrent.futures import ThreadPoolExecutor, as_completed
11
+ import api_utils
12
+ import logging
13
+
14
+ # ๋กœ๊น… ์„ค์ •
15
+ logger = logging.getLogger(__name__)
16
+ logger.setLevel(logging.INFO)
17
+ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18
+ handler = logging.StreamHandler()
19
+ handler.setFormatter(formatter)
20
+ logger.addHandler(handler)
21
+
22
+ def exponential_backoff_sleep(retry_count, base_delay=0.3, max_delay=5.0):
23
+ """์ง€์ˆ˜ ๋ฐฑ์˜คํ”„ ๋ฐฉ์‹์˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„ ๊ณ„์‚ฐ"""
24
+ delay = min(base_delay * (2 ** retry_count), max_delay)
25
+ # ์•ฝ๊ฐ„์˜ ๋žœ๋ค์„ฑ ์ถ”๊ฐ€ (์ง€ํ„ฐ)
26
+ jitter = random.uniform(0, 0.5) * delay
27
+ time.sleep(delay + jitter)
28
+
29
+ def fetch_search_volume_batch(keywords_batch):
30
+ """ํ‚ค์›Œ๋“œ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰๋Ÿ‰ ์กฐํšŒ"""
31
+
32
+ # 1. ์ŠคํŽ˜์ด์Šค๋ฐ” ์ œ๊ฑฐ ๊ฐœ์„  - ๋ฐฐ์น˜ ํ‚ค์›Œ๋“œ๋“ค ์ „์ฒ˜๋ฆฌ
33
+ cleaned_keywords_batch = []
34
+ for kw in keywords_batch:
35
+ cleaned_kw = kw.strip().replace(" ", "") if kw else ""
36
+ cleaned_keywords_batch.append(cleaned_kw)
37
+
38
+ keywords_batch = cleaned_keywords_batch
39
+
40
+ result = {}
41
+ max_retries = 3
42
+ retry_count = 0
43
+
44
+ while retry_count < max_retries:
45
+ try:
46
+ # ์ˆœ์ฐจ์ ์œผ๋กœ API ์„ค์ • ๊ฐ€์ ธ์˜ค๊ธฐ (๋ฐฐ์น˜๋งˆ๋‹ค ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ)
47
+ api_config = api_utils.get_next_api_config()
48
+ API_KEY = api_config["API_KEY"]
49
+ SECRET_KEY = api_config["SECRET_KEY"]
50
+ CUSTOMER_ID_STR = api_config["CUSTOMER_ID"]
51
+
52
+ logger.debug(f"=== ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ฒดํฌ (์‹œ๋„ #{retry_count+1}) ===")
53
+ logger.info(f"๋ฐฐ์น˜ ํฌ๊ธฐ: {len(keywords_batch)}๊ฐœ ํ‚ค์›Œ๋“œ")
54
+
55
+ # API ์„ค์ • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
56
+ is_valid, message = api_utils.validate_api_config(api_config)
57
+ if not is_valid:
58
+ logger.error(f"โŒ {message}")
59
+ retry_count += 1
60
+ exponential_backoff_sleep(retry_count)
61
+ continue
62
+
63
+ # CUSTOMER_ID๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
64
+ try:
65
+ CUSTOMER_ID = int(CUSTOMER_ID_STR)
66
+ except ValueError:
67
+ logger.error(f"โŒ CUSTOMER_ID ๋ณ€ํ™˜ ์˜ค๋ฅ˜: '{CUSTOMER_ID_STR}'๋Š” ์œ ํšจํ•œ ์ˆซ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.")
68
+ retry_count += 1
69
+ exponential_backoff_sleep(retry_count)
70
+ continue
71
+
72
+ BASE_URL = "https://api.naver.com"
73
+ uri = "/keywordstool"
74
+ method = "GET"
75
+ headers = api_utils.get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
76
+
77
+ # ํ‚ค์›Œ๋“œ ๋ฐฐ์น˜๋ฅผ ํ•œ ๋ฒˆ์— API๋กœ ์ „์†ก
78
+ params = {
79
+ "hintKeywords": keywords_batch,
80
+ "showDetail": "1"
81
+ }
82
+
83
+ logger.debug(f"์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ: {len(keywords_batch)}๊ฐœ ํ‚ค์›Œ๋“œ")
84
+
85
+ # API ํ˜ธ์ถœ
86
+ response = requests.get(BASE_URL + uri, params=params, headers=headers, timeout=10)
87
+
88
+ logger.debug(f"์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}")
89
+
90
+ if response.status_code != 200:
91
+ logger.error(f"โŒ API ์˜ค๋ฅ˜ ์‘๋‹ต (์‹œ๋„ #{retry_count+1}):")
92
+ logger.error(f" ๋ณธ๋ฌธ: {response.text}")
93
+ retry_count += 1
94
+ exponential_backoff_sleep(retry_count)
95
+ continue
96
+
97
+ # ์‘๋‹ต ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ
98
+ result_data = response.json()
99
+
100
+ logger.debug(f"์‘๋‹ต ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ:")
101
+ logger.debug(f" ํƒ€์ž…: {type(result_data)}")
102
+ logger.debug(f" ํ‚ค๋“ค: {result_data.keys() if isinstance(result_data, dict) else 'N/A'}")
103
+
104
+ if isinstance(result_data, dict) and "keywordList" in result_data:
105
+ logger.debug(f" keywordList ๊ธธ์ด: {len(result_data['keywordList'])}")
106
+
107
+ # ๋ฐฐ์น˜ ๋‚ด ๊ฐ ํ‚ค์›Œ๋“œ์™€ ๋งค์นญ
108
+ for keyword in keywords_batch:
109
+ found = False
110
+ for item in result_data["keywordList"]:
111
+ rel_keyword = item.get("relKeyword", "")
112
+ if rel_keyword == keyword:
113
+ pc_count = item.get("monthlyPcQcCnt", 0)
114
+ mobile_count = item.get("monthlyMobileQcCnt", 0)
115
+
116
+ # ์ˆซ์ž ๋ณ€ํ™˜
117
+ try:
118
+ if isinstance(pc_count, str):
119
+ pc_count_converted = int(pc_count.replace(",", ""))
120
+ else:
121
+ pc_count_converted = int(pc_count)
122
+ except:
123
+ pc_count_converted = 0
124
+
125
+ try:
126
+ if isinstance(mobile_count, str):
127
+ mobile_count_converted = int(mobile_count.replace(",", ""))
128
+ else:
129
+ mobile_count_converted = int(mobile_count)
130
+ except:
131
+ mobile_count_converted = 0
132
+
133
+ total_count = pc_count_converted + mobile_count_converted
134
+
135
+ result[keyword] = {
136
+ "PC๊ฒ€์ƒ‰๋Ÿ‰": pc_count_converted,
137
+ "๋ชจ๋ฐ”์ผ๊ฒ€์ƒ‰๋Ÿ‰": mobile_count_converted,
138
+ "์ด๊ฒ€์ƒ‰๋Ÿ‰": total_count
139
+ }
140
+ logger.debug(f"โœ… '{keyword}': PC={pc_count_converted}, Mobile={mobile_count_converted}, Total={total_count}")
141
+ found = True
142
+ break
143
+
144
+ if not found:
145
+ logger.warning(f"โŒ '{keyword}': ๋งค์นญ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ")
146
+
147
+ # ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™”์œผ๋ฏ€๋กœ ๋ฃจํ”„ ์ข…๋ฃŒ
148
+ break
149
+ else:
150
+ logger.error(f"โŒ keywordList๊ฐ€ ์—†์Œ (์‹œ๋„ #{retry_count+1})")
151
+ logger.error(f"์ „์ฒด ์‘๋‹ต: {result_data}")
152
+ retry_count += 1
153
+ exponential_backoff_sleep(retry_count)
154
+
155
+ except Exception as e:
156
+ logger.error(f"โŒ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ (์‹œ๋„ #{retry_count+1}): {str(e)}")
157
+ import traceback
158
+ logger.error(traceback.format_exc())
159
+ retry_count += 1
160
+ exponential_backoff_sleep(retry_count)
161
+
162
+ logger.info(f"\n=== ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์™„๋ฃŒ ===")
163
+ logger.info(f"์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ ํ‚ค์›Œ๋“œ ์ˆ˜: {len(result)}")
164
+
165
+ return result
166
+
167
+ def fetch_all_search_volumes(keywords, batch_size=5):
168
+ """ํ‚ค์›Œ๋“œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๋ณ‘๋ ฌ ์กฐํšŒ"""
169
+ results = {}
170
+ batches = []
171
+
172
+ # ํ‚ค์›Œ๋“œ๋ฅผ 5๊ฐœ์”ฉ ๋ฌถ์–ด์„œ ๋ฐฐ์น˜ ์ƒ์„ฑ
173
+ for i in range(0, len(keywords), batch_size):
174
+ batch = keywords[i:i + batch_size]
175
+ batches.append(batch)
176
+
177
+ logger.info(f"์ด {len(batches)}๊ฐœ ๋ฐฐ์น˜๋กœ {len(keywords)}๊ฐœ ํ‚ค์›Œ๋“œ ์ฒ˜๋ฆฌ ์ค‘โ€ฆ")
178
+ logger.info(f"๋ฐฐ์น˜ ํฌ๊ธฐ: {batch_size}, ๋ณ‘๋ ฌ ์›Œ์ปค: 3๊ฐœ, API ๊ณ„์ •: {len(api_utils.NAVER_API_CONFIGS)}๊ฐœ ์ˆœ์ฐจ ์‚ฌ์šฉ")
179
+
180
+ with ThreadPoolExecutor(max_workers=3) as executor: # ์›Œ์ปค ์ˆ˜ ์ œํ•œ
181
+ futures = {executor.submit(fetch_search_volume_batch, batch): batch for batch in batches}
182
+ for future in as_completed(futures):
183
+ batch = futures[future]
184
+ try:
185
+ batch_results = future.result()
186
+ results.update(batch_results)
187
+ logger.info(f"๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์™„๋ฃŒ: {len(batch)}๊ฐœ ํ‚ค์›Œ๋“œ (์„ฑ๊ณต: {len(batch_results)}๊ฐœ)")
188
+ except Exception as e:
189
+ logger.error(f"๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {e}")
190
+ # API ๋ ˆ์ดํŠธ ๋ฆฌ๋ฐ‹ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ์ง€์ˆ˜ ๋ฐฑ์˜คํ”„ ์‚ฌ์šฉ
191
+ exponential_backoff_sleep(0) # ์ดˆ๊ธฐ ์ง€์—ฐ ์ ์šฉ
192
+
193
+ logger.info(f"๊ฒ€์ƒ‰๋Ÿ‰ ์กฐํšŒ ์™„๋ฃŒ: {len(results)}๊ฐœ ํ‚ค์›Œ๋“œ")
194
+ return results
style.css CHANGED
@@ -1,29 +1,117 @@
 
 
 
 
 
1
  :root {
 
2
  --primary-color: #FB7F0D;
3
  --secondary-color: #ff9a8b;
4
  --accent-color: #FF6B6B;
 
 
5
  --background-color: #FFFFFF;
6
  --card-bg: #ffffff;
 
 
 
7
  --text-color: #334155;
8
- --border-radius: 18px;
 
 
 
 
 
 
 
 
 
 
9
  --shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  }
11
 
12
  /* โ”€โ”€ ์ „์—ญ ์Šคํƒ€์ผ โ”€โ”€ */
13
  body {
14
  font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
15
- background-color: var(--background-color);
16
- color: var(--text-color);
17
  line-height: 1.6;
18
  margin: 0;
19
  padding: 0;
 
 
 
 
 
 
 
 
 
 
 
20
  }
21
 
22
  .gradio-container {
23
  width: 100%;
24
  margin: 0 auto;
25
  padding: 20px;
26
- background-color: var(--background-color);
27
  }
28
 
29
  /* โ”€โ”€ ์„น์…˜ ์Šคํƒ€์ผ โ”€โ”€ */
@@ -43,12 +131,13 @@ body {
43
 
44
  /* ์„น์…˜ ํ”„๋ ˆ์ž„ */
45
  .custom-frame {
46
- background-color: var(--card-bg);
47
- border: 1px solid rgba(0, 0, 0, 0.04);
48
  border-radius: var(--border-radius);
49
  padding: 20px;
50
  margin: 10px 0;
51
- box-shadow: var(--shadow);
 
52
  }
53
 
54
  /* ์ ‘์„ ์ˆ˜ ์žˆ๋Š” ์„น์…˜ */
@@ -75,18 +164,17 @@ body {
75
  .collapsible-content {
76
  display: none;
77
  padding: 15px;
78
- background-color: var(--card-bg);
79
- border: 1px solid rgba(0, 0, 0, 0.04);
80
  border-radius: 0 0 var(--border-radius) var(--border-radius);
81
  margin-top: -5px;
 
82
  }
83
 
84
  .collapsible-content.active {
85
  display: block;
86
  }
87
 
88
-
89
-
90
  /* ๋‘ ๋ฒ„ํŠผ์— ๊ณตํ†ต์œผ๋กœ ์ ์šฉํ•  ์Šคํƒ€์ผ */
91
  .execution-button {
92
  font-size: 18px !important;
@@ -106,12 +194,12 @@ body {
106
 
107
  /* ๊ฐ ๋ฒ„ํŠผ๋ณ„ ๊ณ ์œ  ์ƒ‰์ƒ */
108
  .primary-button {
109
- background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
110
  box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25) !important;
111
  }
112
 
113
  .secondary-button {
114
- background: linear-gradient(135deg, #6c757d, #495057) !important;
115
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.25) !important;
116
  }
117
 
@@ -124,19 +212,17 @@ body {
124
  .execution-section {
125
  margin-top: 20px;
126
  padding: 15px;
127
- background-color: #f9f9f9;
128
  border-radius: 8px;
129
- border: 1px solid #e5e5e5;
 
130
  }
131
 
132
-
133
-
134
-
135
  /* โ”€โ”€ ์ปด๏ฟฝ๏ฟฝ๏ฟฝ๋„ŒํŠธ ์Šคํƒ€์ผ โ”€โ”€ */
136
  /* ๋ฒ„ํŠผ ์Šคํƒ€์ผ */
137
  .custom-button {
138
  border-radius: 30px !important;
139
- background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
140
  color: white !important;
141
  font-size: 18px !important;
142
  padding: 10px 20px !important;
@@ -153,7 +239,7 @@ body {
153
  /* ์ž‘์€ ๋ฒ„ํŠผ */
154
  .custom-button-small {
155
  border-radius: 20px !important;
156
- background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
157
  color: white !important;
158
  font-size: 14px !important;
159
  padding: 8px 15px !important;
@@ -170,7 +256,7 @@ body {
170
  /* ๋ฆฌ์…‹ ๋ฒ„ํŠผ */
171
  .reset-button {
172
  border-radius: 30px !important;
173
- background: linear-gradient(135deg, #6c757d, #495057) !important;
174
  color: white !important;
175
  font-size: 16px !important;
176
  padding: 8px 16px !important;
@@ -186,15 +272,29 @@ body {
186
  }
187
 
188
  /* ์ž…๋ ฅ ํ•„๋“œ ์Šคํƒ€์ผ */
189
- .gr-input, .gr-text-input, .gr-sample-inputs {
 
 
 
 
 
 
190
  border-radius: var(--border-radius) !important;
191
- border: 1px solid #dddddd !important;
192
  padding: 12px !important;
193
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important;
194
  transition: all 0.3s ease !important;
 
 
195
  }
196
 
197
- .gr-input:focus, .gr-text-input:focus {
 
 
 
 
 
 
198
  border-color: var(--primary-color) !important;
199
  outline: none !important;
200
  box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important;
@@ -208,9 +308,11 @@ input[type="checkbox"], input[type="radio"] {
208
  /* ๋“œ๋กญ๋‹ค์šด ์Šคํƒ€์ผ */
209
  .gr-dropdown {
210
  border-radius: var(--border-radius) !important;
211
- border: 1px solid #dddddd !important;
212
  padding: 12px !important;
213
  transition: all 0.3s ease !important;
 
 
214
  }
215
 
216
  .gr-dropdown:focus {
@@ -225,7 +327,7 @@ input[type="checkbox"], input[type="radio"] {
225
  align-items: center;
226
  font-size: 20px;
227
  font-weight: 700;
228
- color: #333333;
229
  margin-bottom: 10px;
230
  padding-bottom: 5px;
231
  border-bottom: 2px solid var(--primary-color);
@@ -242,7 +344,7 @@ input[type="checkbox"], input[type="radio"] {
242
  .subsection-title {
243
  font-size: 18px;
244
  font-weight: 600;
245
- color: #444444;
246
  margin: 15px 0 8px 0;
247
  }
248
 
@@ -254,32 +356,38 @@ input[type="checkbox"], input[type="radio"] {
254
  margin: 0;
255
  padding: 0;
256
  font-size: 14px;
 
 
 
257
  }
258
 
259
  .styled-table th,
260
  .styled-table td {
261
  padding: 12px 15px;
262
  text-align: left;
263
- border-bottom: 1px solid #dddddd;
264
  overflow: hidden;
265
  text-overflow: ellipsis;
 
 
266
  }
267
 
268
  .styled-table th {
269
- background-color: var(--primary-color);
270
- color: white;
271
  font-weight: bold;
272
  position: sticky;
273
  top: 0;
274
  white-space: nowrap;
 
275
  }
276
 
277
  .styled-table tbody tr:nth-of-type(even) {
278
- background-color: #f3f3f3;
279
  }
280
 
281
  .styled-table tbody tr:hover {
282
- background-color: #f0f0f0;
283
  }
284
 
285
  .styled-table tbody tr:last-of-type {
@@ -291,8 +399,40 @@ input[type="checkbox"], input[type="radio"] {
291
  max-height: 600px;
292
  overflow-y: auto;
293
  border-radius: var(--border-radius);
294
- border: 1px solid #e5e5e5;
295
  margin-top: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  }
297
 
298
  /* ์Šคํฌ๋กค๋ฐ” ์Šคํƒ€์ผ */
@@ -302,7 +442,7 @@ input[type="checkbox"], input[type="radio"] {
302
  }
303
 
304
  ::-webkit-scrollbar-track {
305
- background: rgba(0, 0, 0, 0.05);
306
  border-radius: 10px;
307
  }
308
 
@@ -311,13 +451,18 @@ input[type="checkbox"], input[type="radio"] {
311
  border-radius: 10px;
312
  }
313
 
 
 
 
 
314
  /* โ”€โ”€ ๋ถ„์„ ๊ฒฐ๊ณผ ์Šคํƒ€์ผ โ”€โ”€ */
315
  .analysis-result {
316
  margin-top: 30px;
317
- border: 1px solid #ddd;
318
  border-radius: 5px;
319
  padding: 15px;
320
- background-color: #f9f9f9;
 
321
  }
322
 
323
  .result-header {
@@ -328,24 +473,28 @@ input[type="checkbox"], input[type="radio"] {
328
  }
329
 
330
  .summary-box {
331
- background-color: #f5f5f5;
332
  border-left: 4px solid var(--primary-color);
333
  padding: 10px 15px;
334
  margin-bottom: 20px;
335
  font-size: 14px;
 
336
  }
337
 
338
  .summary-title {
339
  font-weight: bold;
340
  margin-bottom: 5px;
 
341
  }
342
 
343
  .recommendation-box {
344
- background-color: #e7f7f3;
345
  border-radius: 5px;
346
  padding: 15px;
347
  margin-bottom: 25px;
348
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
 
 
349
  }
350
 
351
  .recommendation-title {
@@ -357,20 +506,22 @@ input[type="checkbox"], input[type="radio"] {
357
 
358
  .recommendation-item {
359
  padding: 6px 0;
360
- border-bottom: 1px solid #e0e0e0;
 
361
  }
362
 
363
  .recommendation-item:last-child {
364
  border-bottom: none;
365
  }
366
 
367
- /* โ”€โ”€ ํ‚ค์›Œ๋“œ ํƒœ๊ทธ ์Šคํƒ€์ผ โ”€โ”€ */
368
  .keyword-tag-container {
369
  margin-top: 20px;
370
  padding: 10px;
371
- border: 1px solid #ddd;
372
  border-radius: 5px;
373
- background-color: #f9f9f9;
 
374
  }
375
 
376
  .keyword-tag {
@@ -414,6 +565,76 @@ input[type="checkbox"], input[type="radio"] {
414
  animation: fadeIn 0.5s ease-out;
415
  }
416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
  /* ๋ฐ˜์‘ํ˜• ์กฐ์ • */
418
  @media (max-width: 768px) {
419
  .grid-container {
 
1
+ /* ============================================
2
+ ๋‹คํฌ๋ชจ๋“œ ์ž๋™ ๋ณ€๊ฒฝ AI ์ƒํ’ˆ ์†Œ์‹ฑ ๋ถ„์„ ์‹œ์Šคํ…œ CSS
3
+ ============================================ */
4
+
5
+ /* 1. CSS ๋ณ€์ˆ˜ ์ •์˜ (๋ผ์ดํŠธ๋ชจ๋“œ - ๊ธฐ๋ณธ๊ฐ’) */
6
  :root {
7
+ /* ๋ฉ”์ธ ์ปฌ๋Ÿฌ */
8
  --primary-color: #FB7F0D;
9
  --secondary-color: #ff9a8b;
10
  --accent-color: #FF6B6B;
11
+
12
+ /* ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ */
13
  --background-color: #FFFFFF;
14
  --card-bg: #ffffff;
15
+ --input-bg: #ffffff;
16
+
17
+ /* ํ…์ŠคํŠธ ์ปฌ๋Ÿฌ */
18
  --text-color: #334155;
19
+ --text-secondary: #64748b;
20
+
21
+ /* ๋ณด๋” ๋ฐ ๊ตฌ๋ถ„์„  */
22
+ --border-color: #dddddd;
23
+ --border-light: #e5e5e5;
24
+
25
+ /* ํ…Œ์ด๋ธ” ์ปฌ๋Ÿฌ */
26
+ --table-even-bg: #f3f3f3;
27
+ --table-hover-bg: #f0f0f0;
28
+
29
+ /* ๊ทธ๋ฆผ์ž */
30
  --shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
31
+ --shadow-light: 0 2px 4px rgba(0, 0, 0, 0.1);
32
+
33
+ /* ๊ธฐํƒ€ */
34
+ --border-radius: 18px;
35
+ }
36
+
37
+ /* 2. ๋‹คํฌ๋ชจ๋“œ ์ƒ‰์ƒ ๋ณ€์ˆ˜ (์ž๋™ ๊ฐ์ง€) */
38
+ @media (prefers-color-scheme: dark) {
39
+ :root {
40
+ /* ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ */
41
+ --background-color: #1a1a1a;
42
+ --card-bg: #2d2d2d;
43
+ --input-bg: #2d2d2d;
44
+
45
+ /* ํ…์ŠคํŠธ ์ปฌ๋Ÿฌ */
46
+ --text-color: #e5e5e5;
47
+ --text-secondary: #a1a1aa;
48
+
49
+ /* ๋ณด๋” ๋ฐ ๊ตฌ๋ถ„์„  */
50
+ --border-color: #404040;
51
+ --border-light: #525252;
52
+
53
+ /* ํ…Œ์ด๋ธ” ์ปฌ๋Ÿฌ */
54
+ --table-even-bg: #333333;
55
+ --table-hover-bg: #404040;
56
+
57
+ /* ๊ทธ๋ฆผ์ž */
58
+ --shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
59
+ --shadow-light: 0 2px 4px rgba(0, 0, 0, 0.2);
60
+ }
61
+ }
62
+
63
+ /* 3. ์ˆ˜๋™ ๋‹คํฌ๋ชจ๋“œ ํด๋ž˜์Šค (Gradio ํ† ๊ธ€์šฉ) */
64
+ [data-theme="dark"],
65
+ .dark,
66
+ .gr-theme-dark {
67
+ /* ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ */
68
+ --background-color: #1a1a1a;
69
+ --card-bg: #2d2d2d;
70
+ --input-bg: #2d2d2d;
71
+
72
+ /* ํ…์ŠคํŠธ ์ปฌ๋Ÿฌ */
73
+ --text-color: #e5e5e5;
74
+ --text-secondary: #a1a1aa;
75
+
76
+ /* ๋ณด๋” ๋ฐ ๊ตฌ๋ถ„์„  */
77
+ --border-color: #404040;
78
+ --border-light: #525252;
79
+
80
+ /* ํ…Œ์ด๋ธ” ์ปฌ๋Ÿฌ */
81
+ --table-even-bg: #333333;
82
+ --table-hover-bg: #404040;
83
+
84
+ /* ๊ทธ๋ฆผ์ž */
85
+ --shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
86
+ --shadow-light: 0 2px 4px rgba(0, 0, 0, 0.2);
87
  }
88
 
89
  /* โ”€โ”€ ์ „์—ญ ์Šคํƒ€์ผ โ”€โ”€ */
90
  body {
91
  font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
92
+ background-color: var(--background-color) !important;
93
+ color: var(--text-color) !important;
94
  line-height: 1.6;
95
  margin: 0;
96
  padding: 0;
97
+ transition: background-color 0.3s ease, color 0.3s ease;
98
+ }
99
+
100
+
101
+ .gradio-container,
102
+ .gradio-container *,
103
+ .gr-app,
104
+ .gr-app *,
105
+ .gr-interface {
106
+ background-color: var(--background-color) !important;
107
+ color: var(--text-color) !important;
108
  }
109
 
110
  .gradio-container {
111
  width: 100%;
112
  margin: 0 auto;
113
  padding: 20px;
114
+ background-color: var(--background-color) !important;
115
  }
116
 
117
  /* โ”€โ”€ ์„น์…˜ ์Šคํƒ€์ผ โ”€โ”€ */
 
131
 
132
  /* ์„น์…˜ ํ”„๋ ˆ์ž„ */
133
  .custom-frame {
134
+ background-color: var(--card-bg) !important;
135
+ border: 1px solid var(--border-light) !important;
136
  border-radius: var(--border-radius);
137
  padding: 20px;
138
  margin: 10px 0;
139
+ box-shadow: var(--shadow) !important;
140
+ color: var(--text-color) !important;
141
  }
142
 
143
  /* ์ ‘์„ ์ˆ˜ ์žˆ๋Š” ์„น์…˜ */
 
164
  .collapsible-content {
165
  display: none;
166
  padding: 15px;
167
+ background-color: var(--card-bg) !important;
168
+ border: 1px solid var(--border-light) !important;
169
  border-radius: 0 0 var(--border-radius) var(--border-radius);
170
  margin-top: -5px;
171
+ color: var(--text-color) !important;
172
  }
173
 
174
  .collapsible-content.active {
175
  display: block;
176
  }
177
 
 
 
178
  /* ๋‘ ๋ฒ„ํŠผ์— ๊ณตํ†ต์œผ๋กœ ์ ์šฉํ•  ์Šคํƒ€์ผ */
179
  .execution-button {
180
  font-size: 18px !important;
 
194
 
195
  /* ๊ฐ ๋ฒ„ํŠผ๋ณ„ ๊ณ ์œ  ์ƒ‰์ƒ */
196
  .primary-button {
197
+ background: var(--primary-color) !important;
198
  box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25) !important;
199
  }
200
 
201
  .secondary-button {
202
+ background: #6c757d !important;
203
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.25) !important;
204
  }
205
 
 
212
  .execution-section {
213
  margin-top: 20px;
214
  padding: 15px;
215
+ background-color: var(--card-bg) !important;
216
  border-radius: 8px;
217
+ border: 1px solid var(--border-light) !important;
218
+ color: var(--text-color) !important;
219
  }
220
 
 
 
 
221
  /* โ”€โ”€ ์ปด๏ฟฝ๏ฟฝ๏ฟฝ๋„ŒํŠธ ์Šคํƒ€์ผ โ”€โ”€ */
222
  /* ๋ฒ„ํŠผ ์Šคํƒ€์ผ */
223
  .custom-button {
224
  border-radius: 30px !important;
225
+ background: var(--primary-color) !important;
226
  color: white !important;
227
  font-size: 18px !important;
228
  padding: 10px 20px !important;
 
239
  /* ์ž‘์€ ๋ฒ„ํŠผ */
240
  .custom-button-small {
241
  border-radius: 20px !important;
242
+ background: var(--primary-color) !important;
243
  color: white !important;
244
  font-size: 14px !important;
245
  padding: 8px 15px !important;
 
256
  /* ๋ฆฌ์…‹ ๋ฒ„ํŠผ */
257
  .reset-button {
258
  border-radius: 30px !important;
259
+ background: #6c757d !important;
260
  color: white !important;
261
  font-size: 16px !important;
262
  padding: 8px 16px !important;
 
272
  }
273
 
274
  /* ์ž…๋ ฅ ํ•„๋“œ ์Šคํƒ€์ผ */
275
+ .gr-input, .gr-text-input, .gr-sample-inputs,
276
+ input[type="text"],
277
+ input[type="number"],
278
+ input[type="email"],
279
+ input[type="password"],
280
+ textarea,
281
+ select {
282
  border-radius: var(--border-radius) !important;
283
+ border: 1px solid var(--border-color) !important;
284
  padding: 12px !important;
285
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important;
286
  transition: all 0.3s ease !important;
287
+ background-color: var(--input-bg) !important;
288
+ color: var(--text-color) !important;
289
  }
290
 
291
+ .gr-input:focus, .gr-text-input:focus,
292
+ input[type="text"]:focus,
293
+ input[type="number"]:focus,
294
+ input[type="email"]:focus,
295
+ input[type="password"]:focus,
296
+ textarea:focus,
297
+ select:focus {
298
  border-color: var(--primary-color) !important;
299
  outline: none !important;
300
  box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important;
 
308
  /* ๋“œ๋กญ๋‹ค์šด ์Šคํƒ€์ผ */
309
  .gr-dropdown {
310
  border-radius: var(--border-radius) !important;
311
+ border: 1px solid var(--border-color) !important;
312
  padding: 12px !important;
313
  transition: all 0.3s ease !important;
314
+ background-color: var(--input-bg) !important;
315
+ color: var(--text-color) !important;
316
  }
317
 
318
  .gr-dropdown:focus {
 
327
  align-items: center;
328
  font-size: 20px;
329
  font-weight: 700;
330
+ color: var(--text-color) !important;
331
  margin-bottom: 10px;
332
  padding-bottom: 5px;
333
  border-bottom: 2px solid var(--primary-color);
 
344
  .subsection-title {
345
  font-size: 18px;
346
  font-weight: 600;
347
+ color: var(--text-color) !important;
348
  margin: 15px 0 8px 0;
349
  }
350
 
 
356
  margin: 0;
357
  padding: 0;
358
  font-size: 14px;
359
+ background-color: var(--card-bg) !important;
360
+ color: var(--text-color) !important;
361
+ position: relative;
362
  }
363
 
364
  .styled-table th,
365
  .styled-table td {
366
  padding: 12px 15px;
367
  text-align: left;
368
+ border-bottom: 1px solid var(--border-color) !important;
369
  overflow: hidden;
370
  text-overflow: ellipsis;
371
+ background-color: var(--card-bg) !important;
372
+ color: var(--text-color) !important;
373
  }
374
 
375
  .styled-table th {
376
+ background-color: var(--primary-color) !important;
377
+ color: white !important;
378
  font-weight: bold;
379
  position: sticky;
380
  top: 0;
381
  white-space: nowrap;
382
+ z-index: 10;
383
  }
384
 
385
  .styled-table tbody tr:nth-of-type(even) {
386
+ background-color: var(--table-even-bg) !important;
387
  }
388
 
389
  .styled-table tbody tr:hover {
390
+ background-color: var(--table-hover-bg) !important;
391
  }
392
 
393
  .styled-table tbody tr:last-of-type {
 
399
  max-height: 600px;
400
  overflow-y: auto;
401
  border-radius: var(--border-radius);
402
+ border: 1px solid var(--border-light) !important;
403
  margin-top: 15px;
404
+ background-color: var(--card-bg) !important;
405
+ position: relative;
406
+ }
407
+
408
+ /* ํ…Œ์ด๋ธ” ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€ ์Šคํƒ€์ผ */
409
+ .table-container {
410
+ position: relative;
411
+ width: 100%;
412
+ margin: 0;
413
+ border-radius: 8px;
414
+ overflow: hidden;
415
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
416
+ }
417
+
418
+ .header-wrap {
419
+ position: sticky;
420
+ top: 0;
421
+ z-index: 20;
422
+ background-color: var(--primary-color) !important;
423
+ }
424
+
425
+ /* ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๊ฐ€ ํ—ค๋” ์œ„๋กœ ์˜ค๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ */
426
+ .styled-table tbody td {
427
+ position: relative;
428
+ z-index: 1;
429
+ }
430
+
431
+ .styled-table thead th {
432
+ position: sticky;
433
+ top: 0;
434
+ z-index: 10;
435
+ background-color: var(--primary-color) !important;
436
  }
437
 
438
  /* ์Šคํฌ๋กค๋ฐ” ์Šคํƒ€์ผ */
 
442
  }
443
 
444
  ::-webkit-scrollbar-track {
445
+ background: var(--card-bg);
446
  border-radius: 10px;
447
  }
448
 
 
451
  border-radius: 10px;
452
  }
453
 
454
+ ::-webkit-scrollbar-thumb:hover {
455
+ background: var(--secondary-color);
456
+ }
457
+
458
  /* โ”€โ”€ ๋ถ„์„ ๊ฒฐ๊ณผ ์Šคํƒ€์ผ โ”€โ”€ */
459
  .analysis-result {
460
  margin-top: 30px;
461
+ border: 1px solid var(--border-color) !important;
462
  border-radius: 5px;
463
  padding: 15px;
464
+ background-color: var(--card-bg) !important;
465
+ color: var(--text-color) !important;
466
  }
467
 
468
  .result-header {
 
473
  }
474
 
475
  .summary-box {
476
+ background-color: var(--table-even-bg) !important;
477
  border-left: 4px solid var(--primary-color);
478
  padding: 10px 15px;
479
  margin-bottom: 20px;
480
  font-size: 14px;
481
+ color: var(--text-color) !important;
482
  }
483
 
484
  .summary-title {
485
  font-weight: bold;
486
  margin-bottom: 5px;
487
+ color: var(--text-color) !important;
488
  }
489
 
490
  .recommendation-box {
491
+ background-color: var(--card-bg) !important;
492
  border-radius: 5px;
493
  padding: 15px;
494
  margin-bottom: 25px;
495
+ box-shadow: var(--shadow-light) !important;
496
+ border: 1px solid var(--border-color) !important;
497
+ color: var(--text-color) !important;
498
  }
499
 
500
  .recommendation-title {
 
506
 
507
  .recommendation-item {
508
  padding: 6px 0;
509
+ border-bottom: 1px solid var(--border-color) !important;
510
+ color: var(--text-color) !important;
511
  }
512
 
513
  .recommendation-item:last-child {
514
  border-bottom: none;
515
  }
516
 
517
+ /* ํ‚ค์›Œ๋“œ ํƒœ๊ทธ ์Šคํƒ€์ผ */
518
  .keyword-tag-container {
519
  margin-top: 20px;
520
  padding: 10px;
521
+ border: 1px solid var(--border-color) !important;
522
  border-radius: 5px;
523
+ background-color: var(--card-bg) !important;
524
+ color: var(--text-color) !important;
525
  }
526
 
527
  .keyword-tag {
 
565
  animation: fadeIn 0.5s ease-out;
566
  }
567
 
568
+ /* Gradio ์ปดํฌ๋„ŒํŠธ ๊ฐ•์ œ ์ ์šฉ */
569
+ .gr-form,
570
+ .gr-box,
571
+ .gr-panel,
572
+ .gr-block,
573
+ .gr-group,
574
+ .gr-row,
575
+ .gr-column {
576
+ background-color: var(--card-bg) !important;
577
+ color: var(--text-color) !important;
578
+ border-color: var(--border-color) !important;
579
+ }
580
+
581
+ /* ๋ผ๋ฒจ ๋ฐ ํ…์ŠคํŠธ ์š”์†Œ */
582
+ label,
583
+ .gr-label,
584
+ .gr-checkbox label,
585
+ .gr-radio label,
586
+ p, span, div {
587
+ color: var(--text-color) !important;
588
+ }
589
+
590
+ /* ํˆดํŒ ๋ฐ ํŒ์—… */
591
+ [data-tooltip]:hover::after,
592
+ .tooltip,
593
+ .popup {
594
+ background-color: var(--card-bg) !important;
595
+ color: var(--text-color) !important;
596
+ border-color: var(--border-color) !important;
597
+ box-shadow: var(--shadow-light) !important;
598
+ }
599
+
600
+ /* ๋ชจ๋‹ฌ ๋ฐ ์˜ค๋ฒ„๋ ˆ์ด */
601
+ .modal,
602
+ .overlay,
603
+ [class*="modal"],
604
+ [class*="overlay"] {
605
+ background-color: var(--card-bg) !important;
606
+ color: var(--text-color) !important;
607
+ border-color: var(--border-color) !important;
608
+ }
609
+
610
+ /* ์ฝ”๋“œ ๋ธ”๋ก ๋ฐ pre ํƒœ๊ทธ */
611
+ code,
612
+ pre,
613
+ .code-block {
614
+ background-color: var(--table-even-bg) !important;
615
+ color: var(--text-color) !important;
616
+ border-color: var(--border-color) !important;
617
+ }
618
+
619
+ /* ์•Œ๋ฆผ ๋ฐ ๋ฉ”์‹œ์ง€ */
620
+ .alert,
621
+ .message,
622
+ .notification,
623
+ [class*="alert"],
624
+ [class*="message"],
625
+ [class*="notification"] {
626
+ background-color: var(--card-bg) !important;
627
+ color: var(--text-color) !important;
628
+ border-color: var(--border-color) !important;
629
+ }
630
+
631
+ /* ์ „ํ™˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ */
632
+ * {
633
+ transition: background-color 0.3s ease,
634
+ color 0.3s ease,
635
+ border-color 0.3s ease !important;
636
+ }
637
+
638
  /* ๋ฐ˜์‘ํ˜• ์กฐ์ • */
639
  @media (max-width: 768px) {
640
  .grid-container {
text_utils.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ ๊ด€๋ จ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ ๋ชจ์Œ
3
+ - ํ…์ŠคํŠธ ๋ถ„๋ฆฌ ๋ฐ ์ •์ œ
4
+ - ํ‚ค์›Œ๋“œ ์ถ”์ถœ
5
+ - Gemini API ํ‚ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์ ์šฉ
6
+ """
7
+
8
+ import re
9
+ import google.generativeai as genai
10
+ import os
11
+ import logging
12
+ import api_utils # API ํ‚ค ํ†ตํ•ฉ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์ž„ํฌํŠธ
13
+
14
+ # ๋กœ๊น… ์„ค์ •
15
+ logger = logging.getLogger(__name__)
16
+ logger.setLevel(logging.INFO)
17
+ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18
+ handler = logging.StreamHandler()
19
+ handler.setFormatter(formatter)
20
+ logger.addHandler(handler)
21
+
22
+ # ===== Gemini ๋ชจ๋ธ ๊ด€๋ฆฌ ํ•จ์ˆ˜๋“ค =====
23
+ def get_gemini_model():
24
+ """api_utils์—์„œ Gemini ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ (ํ†ตํ•ฉ ๊ด€๋ฆฌ)"""
25
+ try:
26
+ model = api_utils.get_gemini_model()
27
+ if model:
28
+ logger.info("Gemini ๋ชจ๋ธ ๋กœ๋“œ ์„ฑ๊ณต (api_utils ํ†ตํ•ฉ ๊ด€๋ฆฌ)")
29
+ return model
30
+ else:
31
+ logger.warning("์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Gemini API ํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
32
+ return None
33
+ except Exception as e:
34
+ logger.error(f"Gemini ๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ: {e}")
35
+ return None
36
+
37
+ # ํ…์ŠคํŠธ ๋ถ„๋ฆฌ ๋ฐ ์ •์ œ ํ•จ์ˆ˜
38
+ def clean_and_split(text, only_korean=False):
39
+ """ํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ์ •์ œํ•˜๋Š” ํ•จ์ˆ˜"""
40
+ text = re.sub(r"[()\[\]-]", " ", text)
41
+ text = text.replace("/", " ")
42
+
43
+ if only_korean:
44
+ # ํ•œ๊ธ€๋งŒ ์ถ”์ถœ ์˜ต์…˜์ด ์ผœ์ง„ ๊ฒฝ์šฐ
45
+ # ๊ณต๋ฐฑ์ด๋‚˜ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋’ค ํ•œ๊ธ€๋งŒ ์ถ”์ถœ
46
+ words = re.split(r"[ ,]", text)
47
+ cleaned = []
48
+ for word in words:
49
+ word = word.strip()
50
+ # ํ•œ๊ธ€๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‹ค๋ฅธ ๋ฌธ์ž๋Š” ์ œ๊ฑฐ
51
+ word = re.sub(r"[^๊ฐ€-ํžฃ]", "", word)
52
+ if word and len(word) >= 1: # ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ณ  1๊ธ€์ž ์ด์ƒ์ธ ๊ฒฝ์šฐ๋งŒ ์ถ”๊ฐ€
53
+ cleaned.append(word)
54
+ else:
55
+ # ํ•œ๊ธ€๋งŒ ์ถ”์ถœ ์˜ต์…˜์ด ๊บผ์ง„ ๊ฒฝ์šฐ - ๋‹จ์–ด ํ†ต์งธ๋กœ ์ฒ˜๋ฆฌ
56
+ # ๊ณต๋ฐฑ๊ณผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋‹จ์–ด ์ „์ฒด๋ฅผ ์œ ์ง€
57
+ words = re.split(r"[,\s]+", text)
58
+ cleaned = []
59
+ for word in words:
60
+ word = word.strip()
61
+ if word and len(word) >= 1: # ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ณ  1๊ธ€์ž ์ด์ƒ์ธ ๊ฒฝ์šฐ๋งŒ ์ถ”๊ฐ€
62
+ cleaned.append(word)
63
+
64
+ return cleaned
65
+
66
+ def filter_keywords_with_gemini(pairs, gemini_model=None):
67
+ """Gemini AI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค์›Œ๋“œ ์กฐํ•ฉ ํ•„ํ„ฐ๋ง (๊ฐœ์„ ๋ฒ„์ „) - API ํ‚ค ํ†ตํ•ฉ ๊ด€๋ฆฌ"""
68
+ if gemini_model is None:
69
+ # api_utils์—์„œ Gemini ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ
70
+ gemini_model = get_gemini_model()
71
+
72
+ if gemini_model is None:
73
+ logger.error("Gemini ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.")
74
+ # ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ: ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€
75
+ all_keywords = set()
76
+ for pair in pairs:
77
+ for keyword in pair:
78
+ all_keywords.add(keyword)
79
+ return list(all_keywords)
80
+
81
+ # ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ๋ชฉ๋ก์œผ๋กœ ์ถ”์ถœ (์ œ๊ฑฐ๋œ ํ‚ค์›Œ๋“œ ํ™•์ธ์šฉ)
82
+ all_keywords = set()
83
+ for pair in pairs:
84
+ for keyword in pair:
85
+ all_keywords.add(keyword)
86
+
87
+ # ๋„ˆ๋ฌด ๋งŽ์€ ์Œ์ด ์žˆ์œผ๋ฉด ์ œํ•œ
88
+ max_pairs = 50 # ์ตœ๋Œ€ 50๊ฐœ ์Œ๋งŒ ์ฒ˜๋ฆฌ
89
+ pairs_to_process = list(pairs)[:max_pairs] if len(pairs) > max_pairs else pairs
90
+
91
+ logger.info(f"ํ•„ํ„ฐ๋งํ•  ํ‚ค์›Œ๋“œ ์Œ: ์ด {len(pairs)}๊ฐœ ์ค‘ {len(pairs_to_process)}๊ฐœ ์ฒ˜๋ฆฌ")
92
+
93
+ # ๋ณด์ˆ˜์ ์ธ ํ”„๋กฌํ”„ํŠธ ์‚ฌ์šฉ - ํ‚ค์›Œ๋“œ ์ œ๊ฑฐ ์ตœ์†Œํ™”
94
+ prompt = (
95
+ "๋‹ค์Œ์€ ์†Œ๋น„์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ํ‚ค์›Œ๋“œ ์Œ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.\n"
96
+ "๊ฐ ์Œ์€ ๊ฐ™์€ ๋‹จ์–ด ์กฐํ•ฉ์ด์ง€๋งŒ ์ˆœ์„œ๋งŒ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค (์˜ˆ: ์†์งˆ์˜ค์ง•์–ด vs ์˜ค์ง•์–ด์†์งˆ).\n\n"
97
+ "์•„๋ž˜์˜ ๊ธฐ์ค€์— ๋”ฐ๋ผ ๊ฐ ์Œ์—์„œ ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ํ‚ค์›Œ๋“œ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”:\n"
98
+ "1. ์†Œ๋น„์ž๊ฐ€ ์ผ์ƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ž์—ฐ์Šค๋Ÿฌ์šด ํ‘œํ˜„์„ ์šฐ์„  ์„ ํƒํ•˜์„ธ์š”.\n"
99
+ "2. ๋‘ ํ‚ค์›Œ๋“œ๊ฐ€ ๋ชจ๋‘ ์ž์—ฐ์Šค๋Ÿฝ๊ฑฐ๋‚˜ ์˜๋ฏธ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋‹ค๋ฉด, ๋ฐ˜๋“œ์‹œ ๋‘˜ ๋‹ค ์œ ์ง€ํ•˜์„ธ์š”.\n"
100
+ "3. ํ™•์‹คํžˆ ๋น„์ž์—ฐ์Šค๋Ÿฝ๊ฑฐ๋‚˜ ์–ด์ƒ‰ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์ œ๊ฑฐํ•˜์„ธ์š”.\n"
101
+ "4. ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€ํ•˜์„ธ์š”.\n"
102
+ "5. ์ˆซ์ž๋‚˜ ์˜์–ด๊ฐ€ ํฌํ•จ๋œ ํ‚ค์›Œ๋“œ๋Š” ํ•œ๊ธ€ ๋ฉ”์ธ ํ‚ค์›Œ๋“œ๊ฐ€ ์•ž์ชฝ์— ์˜ค๋Š” ํ˜•ํƒœ๋ฅผ ์„ ํƒํ•˜์„ธ์š”. (์˜ˆ: '10kg ์˜ค์ง•์–ด' ๋ณด๋‹ค '์˜ค์ง•์–ด 10kg' ์„ ํƒ)\n"
103
+ "6. ๊ฒ€์ƒ‰๋Ÿ‰์ด 0์ธ ํ‚ค์›Œ๋“œ๋ผ๋„ ์ผ์ƒ์ ์ธ ํ‘œํ˜„์ด๋ผ๋ฉด ๊ฐ€๋Šฅํ•œ ์œ ์ง€ํ•˜์„ธ์š”. ๋ช…๋ฐฑํ•˜๊ฒŒ ๋น„์ •์ƒ์ ์ธ ํ‘œํ˜„๋งŒ ์ œ๊ฑฐํ•˜์„ธ์š”.\n\n"
104
+ "์ฃผ์˜: ๊ธฐ๋ณธ์ ์œผ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€ํ•˜๊ณ , ๋งค์šฐ ๋ช…ํ™•ํ•˜๊ฒŒ ๋น„์ž์—ฐ์Šค๋Ÿฌ์šด ๊ฒƒ๋งŒ ์ œ๊ฑฐํ•˜์„ธ์š”.\n\n"
105
+ "๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”:\n"
106
+ "- ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ (์ด์œ : ์ž์—ฐ์Šค๋Ÿฌ์šด ํ‘œํ˜„์ด๊ธฐ ๋•Œ๋ฌธ)\n"
107
+ "- ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ1, ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ2 (์ด์œ : ๋‘˜ ๋‹ค ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ์˜๋ฏธ๊ฐ€ ์กฐ๊ธˆ ๋‹ค๋ฆ„)\n\n"
108
+ )
109
+
110
+ # ํ‚ค์›Œ๏ฟฝ๏ฟฝ ์Œ ๋ชฉ๋ก
111
+ formatted = "\n".join([f"- {a}, {b}" for a, b in pairs_to_process])
112
+ full_prompt = prompt + formatted
113
+
114
+ try:
115
+ # ํƒ€์ž„์•„์›ƒ ์ถ”๊ฐ€
116
+ logger.info(f"Gemini API ํ˜ธ์ถœ ์‹œ์ž‘ - {len(pairs_to_process)}๊ฐœ ํ‚ค์›Œ๋“œ ์Œ ์ฒ˜๋ฆฌ ์ค‘...")
117
+
118
+ # ์‘๋‹ต ๋ฐ›๊ธฐ (ํƒ€์ž„์•„์›ƒ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด ์ถ”๊ฐ€)
119
+ response = gemini_model.generate_content(full_prompt)
120
+
121
+ logger.info("Gemini API ์‘๋‹ต ์„ฑ๊ณต")
122
+ lines = response.text.strip().split("\n")
123
+
124
+ # ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ (์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ ๊ฒฝ์šฐ ๋ชจ๋‘ ํฌํ•จ)
125
+ final_keywords = []
126
+ for line in lines:
127
+ if line.startswith("-"):
128
+ # ์ด์œ  ๋ถ€๋ถ„ ์ œ๊ฑฐ
129
+ keywords_part = line.strip("- ").split("(์ด์œ :")[0].strip()
130
+ # ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ ํ‚ค์›Œ๋“œ ๋ชจ๋‘ ์ถ”๊ฐ€
131
+ for kw in keywords_part.split(","):
132
+ kw = kw.strip()
133
+ if kw:
134
+ final_keywords.append(kw)
135
+
136
+ # ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ํ‚ค์›Œ๋“œ๋„ ์ถ”๊ฐ€ (LLM์ด ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ ํ‚ค์›Œ๋“œ)
137
+ if len(pairs) > max_pairs:
138
+ logger.info(f"์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ์ฒ˜๋ฆฌ: ๋‚จ์€ {len(pairs) - max_pairs}๊ฐœ ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€")
139
+ for pair in list(pairs)[max_pairs:]:
140
+ # ๊ฐ ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ํ‚ค์›Œ๋“œ๋งŒ ์‚ฌ์šฉ
141
+ final_keywords.append(pair[0])
142
+
143
+ # ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ๊ฐ€ ์—†์œผ๋ฉด ๊ธฐ์กด ํ‚ค์›Œ๋“œ ๋ชจ๋‘ ๋ฐ˜ํ™˜
144
+ if not final_keywords:
145
+ logger.warning("๊ฒฝ๊ณ : ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ๊ฐ€ ์—†์–ด ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.")
146
+ final_keywords = list(all_keywords)
147
+
148
+ # ์ˆœ์„œ ๊ฐ•์ œ ์ˆ˜์ •
149
+ corrected_keywords = []
150
+
151
+ # ๋‹จ์œ„์™€ ์ˆซ์ž ๊ด€๋ จ ์ •๊ทœ์‹ ํŒจํ„ด
152
+ unit_pattern = re.compile(r'(?i)(kg|g|mm|cm|ml|l|๋ฆฌํ„ฐ|๊ฐœ|ํŒฉ|๋ฐ•์Šค|์„ธํŠธ|2l|l2)')
153
+ number_pattern = re.compile(r'\d+')
154
+
155
+ for kw in final_keywords:
156
+ # ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ
157
+ if ' ' in kw:
158
+ parts = kw.split()
159
+ first_part = parts[0]
160
+
161
+ # ์ฒซ ๋ถ€๋ถ„์ด ๋‹จ์œ„๋‚˜ ์ˆซ์ž๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ
162
+ if (unit_pattern.search(first_part) or number_pattern.search(first_part)) and len(parts) > 1:
163
+ # ์ˆœ์„œ ๋ฐ”๊พธ๊ธฐ: ๋‹จ์œ„/์ˆซ์ž ๋ถ€๋ถ„์„ ๋’ค๋กœ ์ด๋™
164
+ corrected_kw = " ".join(parts[1:] + [first_part])
165
+ logger.info(f"ํ‚ค์›Œ๋“œ ์ˆœ์„œ ๊ฐ•์ œ ์ˆ˜์ •: '{kw}' -> '{corrected_kw}'")
166
+ corrected_keywords.append(corrected_kw)
167
+ else:
168
+ corrected_keywords.append(kw)
169
+ else:
170
+ corrected_keywords.append(kw)
171
+
172
+ # ํŠน๋ณ„ ์ฒ˜๋ฆฌ: "L ์˜ค์ง•์–ด", "2L ์˜ค์ง•์–ด" ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ™•์ธํ•˜๊ณ  ์ˆ˜์ •
173
+ specific_fixes = []
174
+ for kw in corrected_keywords:
175
+ # ํŠน์ • ํŒจํ„ด ์ฒดํฌ
176
+ l_pattern = re.compile(r'^([0-9]*L) (.+)$', re.IGNORECASE)
177
+ match = l_pattern.match(kw)
178
+
179
+ if match:
180
+ # L ๋‹จ์œ„๋ฅผ ๋’ค๋กœ ์ด๋™
181
+ l_part = match.group(1)
182
+ main_part = match.group(2)
183
+ fixed_kw = f"{main_part} {l_part}"
184
+ logger.info(f"ํŠน์ˆ˜ ํŒจํ„ด ์ˆ˜์ •: '{kw}' -> '{fixed_kw}'")
185
+ specific_fixes.append(fixed_kw)
186
+ else:
187
+ specific_fixes.append(kw)
188
+
189
+ # ์ œ๊ฑฐ๋œ ํ‚ค์›Œ๋“œ ๋ชฉ๋ก ํ™•์ธ
190
+ selected_set = set(specific_fixes)
191
+ removed_keywords = all_keywords - selected_set
192
+
193
+ # ์ œ๊ฑฐ๋œ ํ‚ค์›Œ๋“œ ์ถœ๋ ฅ
194
+ logger.info("\n=== LLM์— ์˜ํ•ด ์ œ๊ฑฐ๋œ ํ‚ค์›Œ๋“œ ๋ชฉ๋ก ===")
195
+ for kw in removed_keywords:
196
+ logger.info(f" - {kw}")
197
+ logger.info(f"์ด {len(all_keywords)}๊ฐœ ์ค‘ {len(removed_keywords)}๊ฐœ ์ œ๊ฑฐ๋จ ({len(selected_set)}๊ฐœ ์œ ์ง€)\n")
198
+
199
+ return specific_fixes
200
+
201
+ except Exception as e:
202
+ logger.error(f"Gemini ์˜ค๋ฅ˜: {e}")
203
+ logger.error("์˜ค๋ฅ˜ ๋ฐœ์ƒ์œผ๋กœ ์ธํ•ด ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.")
204
+ logger.error(f"์˜ค๋ฅ˜ ์œ ํ˜•: {type(e).__name__}")
205
+ import traceback
206
+ traceback.print_exc()
207
+
208
+ # ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ: ๋ชจ๋“  ํ‚ค์›Œ๋“œ๋ฅผ ์œ ์ง€
209
+ logger.info(f"์•ˆ์ „ ๋ชจ๋“œ: {len(all_keywords)}๊ฐœ ํ‚ค์›Œ๋“œ ๋ชจ๋‘ ์œ ์ง€")
210
+ return list(all_keywords)
211
+
212
+ def get_search_volume_range(total_volume):
213
+ """์ด ๊ฒ€์ƒ‰๋Ÿ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ตฌ๊ฐ„์„ ๋ฐ˜ํ™˜"""
214
+ if total_volume == 0:
215
+ return "100๋ฏธ๋งŒ"
216
+ elif total_volume <= 100:
217
+ return "100๋ฏธ๋งŒ"
218
+ elif total_volume <= 1000:
219
+ return "1000๋ฏธ๋งŒ"
220
+ elif total_volume <= 2000:
221
+ return "2000๋ฏธ๋งŒ"
222
+ elif total_volume <= 5000:
223
+ return "5000๋ฏธ๋งŒ"
224
+ elif total_volume <= 10000:
225
+ return "10000๋ฏธ๋งŒ"
226
+ else:
227
+ return "10000์ด์ƒ"
trend_analysis_v2.py ADDED
@@ -0,0 +1,1128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ํŠธ๋ Œ๋“œ ๋ถ„์„ ๋ชจ๋“ˆ v2.16 - ์ •๊ตํ•œ ์ด์ค‘ API ํ˜ธ์ถœ ๋ฐ ์—ญ์‚ฐ ๋กœ์ง ๊ตฌํ˜„
3
+ - ์ด์ค‘ ํŠธ๋ Œ๋“œ API ํ˜ธ์ถœ: ์ผ๋ณ„ + ์›”๋ณ„ ๋ฐ์ดํ„ฐ
4
+ - ์ผ๋ณ„ ๋ฐ์ดํ„ฐ๋กœ ์ „์›” ์ •ํ™•ํ•œ ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ
5
+ - ์ „์›” ๊ธฐ์ค€์œผ๋กœ 3๋…„ ๋ชจ๋“  ์›” ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ
6
+ - ์ž‘๋…„ ๋™์›” ๊ธฐ๋ฐ˜ ๋ฏธ๋ž˜ 3๊ฐœ์›” ์˜ˆ์ƒ
7
+ - ์ตœ์ข… ๋‹จ๊ณ„์—์„œ 10% ๊ฐ์†Œ ์กฐ์ • ์ ์šฉ
8
+ """
9
+
10
+ import urllib.request
11
+ import json
12
+ import time
13
+ import logging
14
+ from datetime import datetime, timedelta
15
+ import calendar
16
+ import api_utils
17
+
18
+ # ๋กœ๊น… ์„ค์ •
19
+ logger = logging.getLogger(__name__)
20
+
21
+ # ===== ํ•ต์‹ฌ ๊ฐœ์„  ํ•จ์ˆ˜๋“ค =====
22
+
23
+ def get_complete_month():
24
+ """์™„์„ฑ๋œ ๋งˆ์ง€๋ง‰ ์›” ๊ณ„์‚ฐ - ๋‹จ์ˆœํ™”๋œ ๋กœ์ง"""
25
+ current_date = datetime.now()
26
+ current_day = current_date.day
27
+ current_year = current_date.year
28
+ current_month = current_date.month
29
+
30
+ # 3์ผ ์ดํ›„์—๋งŒ ์ „์›”์„ ์™„์„ฑ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ
31
+ if current_day >= 3:
32
+ completed_year = current_year
33
+ completed_month = current_month - 1
34
+ else:
35
+ completed_year = current_year
36
+ completed_month = current_month - 2
37
+
38
+ # ์›”์ด 0 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ์—ฐ๋„ ์กฐ์ •
39
+ while completed_month <= 0:
40
+ completed_month += 12
41
+ completed_year -= 1
42
+
43
+ return completed_year, completed_month
44
+
45
+ def get_daily_trend_data(keywords, max_retries=3):
46
+ """1์ฐจ ํ˜ธ์ถœ: ์ผ๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ (์ „์›” ์ •ํ™• ๊ณ„์‚ฐ์šฉ)"""
47
+ for retry_attempt in range(max_retries):
48
+ try:
49
+ # ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •
50
+ datalab_config = api_utils.get_next_datalab_api_config()
51
+ if not datalab_config:
52
+ logger.warning("๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
53
+ return None
54
+
55
+ client_id = datalab_config["CLIENT_ID"]
56
+ client_secret = datalab_config["CLIENT_SECRET"]
57
+
58
+ # ์™„์„ฑ๋œ ๋งˆ์ง€๋ง‰ ์›” ๊ณ„์‚ฐ
59
+ completed_year, completed_month = get_complete_month()
60
+
61
+ # ์ผ๋ณ„ ๋ฐ์ดํ„ฐ ๊ธฐ๊ฐ„: ์ „์›” 1์ผ ~ ์–ด์ œ
62
+ current_date = datetime.now()
63
+ yesterday = current_date - timedelta(days=1)
64
+
65
+ start_date = f"{completed_year:04d}-{completed_month:02d}-01"
66
+ end_date = yesterday.strftime("%Y-%m-%d")
67
+
68
+ logger.info(f"๐Ÿ“ž 1์ฐจ ํ˜ธ์ถœ (์ผ๋ณ„): {start_date} ~ {end_date}")
69
+
70
+ # ํ‚ค์›Œ๋“œ ๊ทธ๋ฃน ์ƒ์„ฑ
71
+ keywordGroups = []
72
+ for kw in keywords[:5]:
73
+ keywordGroups.append({
74
+ 'groupName': kw,
75
+ 'keywords': [kw]
76
+ })
77
+
78
+ # API ์š”์ฒญ
79
+ body_dict = {
80
+ 'startDate': start_date,
81
+ 'endDate': end_date,
82
+ 'timeUnit': 'date', # ์ผ๋ณ„!
83
+ 'keywordGroups': keywordGroups
84
+ }
85
+
86
+ url = "https://openapi.naver.com/v1/datalab/search"
87
+ body = json.dumps(body_dict, ensure_ascii=False)
88
+
89
+ request = urllib.request.Request(url)
90
+ request.add_header("X-Naver-Client-Id", client_id)
91
+ request.add_header("X-Naver-Client-Secret", client_secret)
92
+ request.add_header("Content-Type", "application/json")
93
+
94
+ response = urllib.request.urlopen(request, data=body.encode("utf-8"), timeout=15)
95
+ rescode = response.getcode()
96
+
97
+ if rescode == 200:
98
+ response_body = response.read()
99
+ response_json = json.loads(response_body)
100
+ logger.info(f"์ผ๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ์กฐํšŒ ์„ฑ๊ณต")
101
+ return response_json
102
+ else:
103
+ logger.error(f"์ผ๋ณ„ API ์˜ค๋ฅ˜: ์ƒํƒœ์ฝ”๋“œ {rescode}")
104
+ if retry_attempt < max_retries - 1:
105
+ time.sleep(2 * (retry_attempt + 1))
106
+ continue
107
+ return None
108
+
109
+ except Exception as e:
110
+ logger.error(f"์ผ๋ณ„ ํŠธ๋ Œ๋“œ ์กฐํšŒ ์˜ค๋ฅ˜ (์‹œ๋„ {retry_attempt + 1}): {e}")
111
+ if retry_attempt < max_retries - 1:
112
+ time.sleep(2 * (retry_attempt + 1))
113
+ continue
114
+ return None
115
+
116
+ return None
117
+
118
+ def get_monthly_trend_data(keywords, max_retries=3):
119
+ """2์ฐจ ํ˜ธ์ถœ: ์›”๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ (3๋…„ ์ „์ฒด + ์˜ˆ์ƒ์šฉ)"""
120
+ for retry_attempt in range(max_retries):
121
+ try:
122
+ # ๋ฐ์ดํ„ฐ๋žฉ API ์„ค์ •
123
+ datalab_config = api_utils.get_next_datalab_api_config()
124
+ if not datalab_config:
125
+ logger.warning("๋ฐ์ดํ„ฐ๋žฉ API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
126
+ return None
127
+
128
+ client_id = datalab_config["CLIENT_ID"]
129
+ client_secret = datalab_config["CLIENT_SECRET"]
130
+
131
+ # ์™„์„ฑ๋œ ๋งˆ์ง€๋ง‰ ์›” ๊ณ„์‚ฐ
132
+ completed_year, completed_month = get_complete_month()
133
+
134
+ # ์›”๋ณ„ ๋ฐ์ดํ„ฐ ๊ธฐ๊ฐ„: 3๋…„ ์ „ 1์›” ~ ์™„์„ฑ๋œ ๋งˆ์ง€๋ง‰ ์›”
135
+ start_year = completed_year - 3
136
+ start_date = f"{start_year:04d}-01-01"
137
+ end_date = f"{completed_year:04d}-{completed_month:02d}-01"
138
+
139
+ logger.info(f"๐Ÿ“ž 2์ฐจ ํ˜ธ์ถœ (์›”๋ณ„): {start_date} ~ {end_date}")
140
+
141
+ # ํ‚ค์›Œ๋“œ ๊ทธ๋ฃน ์ƒ์„ฑ
142
+ keywordGroups = []
143
+ for kw in keywords[:5]:
144
+ keywordGroups.append({
145
+ 'groupName': kw,
146
+ 'keywords': [kw]
147
+ })
148
+
149
+ # API ์š”์ฒญ
150
+ body_dict = {
151
+ 'startDate': start_date,
152
+ 'endDate': end_date,
153
+ 'timeUnit': 'month', # ์›”๋ณ„!
154
+ 'keywordGroups': keywordGroups
155
+ }
156
+
157
+ url = "https://openapi.naver.com/v1/datalab/search"
158
+ body = json.dumps(body_dict, ensure_ascii=False)
159
+
160
+ request = urllib.request.Request(url)
161
+ request.add_header("X-Naver-Client-Id", client_id)
162
+ request.add_header("X-Naver-Client-Secret", client_secret)
163
+ request.add_header("Content-Type", "application/json")
164
+
165
+ response = urllib.request.urlopen(request, data=body.encode("utf-8"), timeout=15)
166
+ rescode = response.getcode()
167
+
168
+ if rescode == 200:
169
+ response_body = response.read()
170
+ response_json = json.loads(response_body)
171
+ logger.info(f"์›”๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ์กฐํšŒ ์„ฑ๊ณต")
172
+ return response_json
173
+ else:
174
+ logger.error(f"์›”๋ณ„ API ์˜ค๋ฅ˜: ์ƒํƒœ์ฝ”๋“œ {rescode}")
175
+ if retry_attempt < max_retries - 1:
176
+ time.sleep(2 * (retry_attempt + 1))
177
+ continue
178
+ return None
179
+
180
+ except Exception as e:
181
+ logger.error(f"์›”๋ณ„ ํŠธ๋ Œ๋“œ ์กฐํšŒ ์˜ค๋ฅ˜ (์‹œ๋„ {retry_attempt + 1}): {e}")
182
+ if retry_attempt < max_retries - 1:
183
+ time.sleep(2 * (retry_attempt + 1))
184
+ continue
185
+ return None
186
+
187
+ return None
188
+
189
+ def calculate_previous_month_from_daily(current_volume, daily_data):
190
+ """์ผ๋ณ„ ํŠธ๋ Œ๋“œ๋กœ ์ „์›” ์ •ํ™•ํ•œ ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ"""
191
+ if not daily_data or "results" not in daily_data:
192
+ logger.warning("์ผ๋ณ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด ์ „์›” ๊ณ„์‚ฐ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
193
+ return current_volume
194
+
195
+ try:
196
+ completed_year, completed_month = get_complete_month()
197
+
198
+ # ์ „์›” ์ผ์ˆ˜ ๊ณ„์‚ฐ
199
+ prev_month_days = calendar.monthrange(completed_year, completed_month)[1]
200
+
201
+ for result in daily_data["results"]:
202
+ keyword = result["title"]
203
+
204
+ if not result["data"]:
205
+ continue
206
+
207
+ # ์ตœ๊ทผ 30์ผ๊ณผ ์ „์›” ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ
208
+ recent_30_ratios = [] # ์ตœ๊ทผ 30์ผ (ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ธฐ์ค€)
209
+ prev_month_ratios = [] # ์ „์›” ๋ฐ์ดํ„ฐ
210
+
211
+ for data_point in result["data"]:
212
+ try:
213
+ date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
214
+ ratio = data_point["ratio"]
215
+
216
+ # ์ „์›” ๋ฐ์ดํ„ฐ
217
+ if date_obj.year == completed_year and date_obj.month == completed_month:
218
+ prev_month_ratios.append(ratio)
219
+
220
+ # ์ตœ๊ทผ 30์ผ (ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ธฐ์ค€ ๊ตฌ๊ฐ„)
221
+ current_date = datetime.now()
222
+ if (current_date - date_obj).days <= 30:
223
+ recent_30_ratios.append(ratio)
224
+
225
+ except:
226
+ continue
227
+
228
+ if not recent_30_ratios or not prev_month_ratios:
229
+ logger.warning(f"'{keyword}' ๋น„๊ต ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.")
230
+ continue
231
+
232
+ # ํ‰๊ท  ๋น„์œจ ๊ณ„์‚ฐ
233
+ recent_30_avg = sum(recent_30_ratios) / len(recent_30_ratios)
234
+ prev_month_avg = sum(prev_month_ratios) / len(prev_month_ratios)
235
+
236
+ if recent_30_avg == 0:
237
+ continue
238
+
239
+ # ์ „์›” ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ
240
+ # ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ = ์ตœ๊ทผ 30์ผ ํ‰๊ท  ๊ธฐ์ค€
241
+ # ์ „์›” ๊ฒ€์ƒ‰๋Ÿ‰ = (์ „์›” ํ‰๊ท  / ์ตœ๊ทผ 30์ผ ํ‰๊ท ) ร— ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ร— (์ „์›” ์ผ์ˆ˜ / 30์ผ)
242
+ prev_month_volume = int(
243
+ (prev_month_avg / recent_30_avg) * current_volume * (prev_month_days / 30)
244
+ )
245
+
246
+ logger.info(f"'{keyword}' ์ „์›” {completed_year}.{completed_month:02d} ์—ญ์‚ฐ ๊ฒ€์ƒ‰๋Ÿ‰: {prev_month_volume:,}ํšŒ")
247
+ logger.info(f" - ์ตœ๊ทผ 30์ผ ํ‰๊ท  ๋น„์œจ: {recent_30_avg:.1f}%")
248
+ logger.info(f" - ์ „์›” ํ‰๊ท  ๋น„์œจ: {prev_month_avg:.1f}%")
249
+ logger.info(f" - ์ „์›” ์ผ์ˆ˜ ๋ณด์ •: {prev_month_days}์ผ")
250
+
251
+ return prev_month_volume
252
+
253
+ except Exception as e:
254
+ logger.error(f"์ „์›” ์—ญ์‚ฐ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
255
+ return current_volume
256
+
257
+ return current_volume
258
+
259
+ def calculate_all_months_from_previous(prev_month_volume, monthly_data, completed_year, completed_month):
260
+ """์ „์›”์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ์›” ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ"""
261
+ if not monthly_data or "results" not in monthly_data:
262
+ logger.warning("์›”๋ณ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด ์—ญ์‚ฐ ๊ณ„์‚ฐ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
263
+ return [], []
264
+
265
+ monthly_volumes = []
266
+ dates = []
267
+
268
+ try:
269
+ for result in monthly_data["results"]:
270
+ keyword = result["title"]
271
+
272
+ if not result["data"]:
273
+ continue
274
+
275
+ # ์ „์›”(๊ธฐ์ค€์›”) ๋น„์œจ ์ฐพ๊ธฐ
276
+ base_ratio = None
277
+ for data_point in result["data"]:
278
+ try:
279
+ date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
280
+ if date_obj.year == completed_year and date_obj.month == completed_month:
281
+ base_ratio = data_point["ratio"]
282
+ break
283
+ except:
284
+ continue
285
+
286
+ if base_ratio is None or base_ratio == 0:
287
+ logger.warning(f"'{keyword}' ๊ธฐ์ค€์›” ๋น„์œจ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
288
+ continue
289
+
290
+ logger.info(f"'{keyword}' ๊ธฐ์ค€์›” {completed_year}.{completed_month:02d} ๋น„์œจ: {base_ratio}% (๊ฒ€์ƒ‰๋Ÿ‰: {prev_month_volume:,}ํšŒ)")
291
+
292
+ # ๋ชจ๋“  ์›” ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ
293
+ for data_point in result["data"]:
294
+ try:
295
+ date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
296
+ ratio = data_point["ratio"]
297
+
298
+ # ํ•ด๋‹น ์›”์˜ ์ผ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
299
+ month_days = calendar.monthrange(date_obj.year, date_obj.month)[1]
300
+ base_month_days = calendar.monthrange(completed_year, completed_month)[1]
301
+
302
+ # ์—ญ์‚ฐ ๊ณ„์‚ฐ: (ํ•ด๋‹น์›” ๋น„์œจ / ๊ธฐ์ค€์›” ๋น„์œจ) ร— ๊ธฐ์ค€์›” ๊ฒ€์ƒ‰๋Ÿ‰ ร— (ํ•ด๋‹น์›” ์ผ์ˆ˜ / ๊ธฐ์ค€์›” ์ผ์ˆ˜)
303
+ calculated_volume = int(
304
+ (ratio / base_ratio) * prev_month_volume * (month_days / base_month_days)
305
+ )
306
+ calculated_volume = max(calculated_volume, 0) # ์Œ์ˆ˜ ๋ฐฉ์ง€
307
+
308
+ monthly_volumes.append(calculated_volume)
309
+ dates.append(data_point["period"])
310
+
311
+ except:
312
+ continue
313
+
314
+ logger.info(f"'{keyword}' ์ „์ฒด ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ ์™„๋ฃŒ: {len(monthly_volumes)}๊ฐœ์›”")
315
+ break # ์ฒซ ๋ฒˆ์งธ ํ‚ค์›Œ๋“œ๋งŒ ์ฒ˜๋ฆฌ
316
+
317
+ except Exception as e:
318
+ logger.error(f"์›”๋ณ„ ์—ญ์‚ฐ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
319
+ return [], []
320
+
321
+ return monthly_volumes, dates
322
+
323
+ def generate_future_from_growth_rate(monthly_volumes, dates, completed_year, completed_month):
324
+ """์ฆ๊ฐ์œจ ๊ธฐ๋ฐ˜ ๋ฏธ๋ž˜ 3๊ฐœ์›” ์˜ˆ์ƒ ์ƒ์„ฑ"""
325
+ if len(monthly_volumes) < 12:
326
+ logger.warning("๋ฏธ๋ž˜ ์˜ˆ์ธก์„ ์œ„ํ•œ ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
327
+ return [], []
328
+
329
+ try:
330
+ # ์ž‘๋…„ ๋™์›”๋“ค๊ณผ ์˜ฌํ•ด ์™„์„ฑ๋œ ์›”๋“ค ๋น„๊ตํ•˜์—ฌ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ
331
+ this_year_volumes = []
332
+ last_year_volumes = []
333
+
334
+ for i, date_str in enumerate(dates):
335
+ try:
336
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
337
+
338
+ # ์˜ฌํ•ด ์™„์„ฑ๋œ ๋ฐ์ดํ„ฐ (1์›” ~ ์™„์„ฑ๋œ ์›”)
339
+ if date_obj.year == completed_year and date_obj.month <= completed_month:
340
+ this_year_volumes.append(monthly_volumes[i])
341
+
342
+ # ์ž‘๋…„ ๋™์ผ ๊ธฐ๊ฐ„ ๋ฐ์ดํ„ฐ
343
+ if date_obj.year == completed_year - 1 and date_obj.month <= completed_month:
344
+ last_year_volumes.append(monthly_volumes[i])
345
+
346
+ except:
347
+ continue
348
+
349
+ # ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ
350
+ if len(this_year_volumes) >= 3 and len(last_year_volumes) >= 3:
351
+ this_year_avg = sum(this_year_volumes) / len(this_year_volumes)
352
+ last_year_avg = sum(last_year_volumes) / len(last_year_volumes)
353
+
354
+ if last_year_avg > 0:
355
+ growth_rate = (this_year_avg - last_year_avg) / last_year_avg
356
+ # ์ฆ๊ฐ์œจ ๋ฒ”์œ„ ์ œํ•œ (-50% ~ +100%)
357
+ growth_rate = max(-0.5, min(growth_rate, 1.0))
358
+ else:
359
+ growth_rate = 0
360
+ else:
361
+ growth_rate = 0
362
+
363
+ logger.info(f"๊ณ„์‚ฐ๋œ ์ฆ๊ฐ์œจ: {growth_rate*100:+.1f}%")
364
+
365
+ # ๋ฏธ๋ž˜ 3๊ฐœ์›” ์˜ˆ์ƒ
366
+ predicted_volumes = []
367
+ predicted_dates = []
368
+
369
+ for month_offset in range(1, 4): # 1, 2, 3๊ฐœ์›” ํ›„
370
+ pred_year = completed_year
371
+ pred_month = completed_month + month_offset
372
+
373
+ while pred_month > 12:
374
+ pred_month -= 12
375
+ pred_year += 1
376
+
377
+ # ์ž‘๋…„ ๋™์›” ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ
378
+ last_year_pred_year = pred_year - 1
379
+ last_year_pred_month = pred_month
380
+ last_year_volume = None
381
+
382
+ for i, date_str in enumerate(dates):
383
+ try:
384
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
385
+ if date_obj.year == last_year_pred_year and date_obj.month == last_year_pred_month:
386
+ last_year_volume = monthly_volumes[i]
387
+ break
388
+ except:
389
+ continue
390
+
391
+ # ์˜ˆ์ƒ ๊ฒ€์ƒ‰๋Ÿ‰ = ์ž‘๋…„ ๋™์›” ร— (1 + ์ฆ๊ฐ์œจ)
392
+ if last_year_volume is not None:
393
+ predicted_volume = int(last_year_volume * (1 + growth_rate))
394
+ predicted_volume = max(predicted_volume, 0) # ์Œ์ˆ˜ ๋ฐฉ์ง€
395
+
396
+ predicted_volumes.append(predicted_volume)
397
+ predicted_dates.append(f"{pred_year:04d}-{pred_month:02d}-01")
398
+
399
+ logger.info(f"์˜ˆ์ƒ {pred_year}.{pred_month:02d}: ์ž‘๋…„ ๋™์›” {last_year_volume:,}ํšŒ โ†’ ์˜ˆ์ƒ {predicted_volume:,}ํšŒ")
400
+
401
+ return predicted_volumes, predicted_dates
402
+
403
+ except Exception as e:
404
+ logger.error(f"๋ฏธ๋ž˜ ์˜ˆ์ธก ์ƒ์„ฑ ์˜ค๋ฅ˜: {e}")
405
+ return [], []
406
+
407
+ def apply_final_10_percent_reduction(monthly_data):
408
+ """์ตœ์ข… ๋‹จ๊ณ„: ๋ชจ๋“  ๊ฒฐ๊ณผ์— 10% ๊ฐ์†Œ ์ ์šฉ"""
409
+ adjusted_data = {}
410
+
411
+ try:
412
+ for keyword, data in monthly_data.items():
413
+ adjusted_volumes = []
414
+
415
+ for volume in data["monthly_volumes"]:
416
+ if volume >= 10:
417
+ adjusted_volume = int(volume * 0.9) # 10% ๊ฐ์†Œ
418
+ else:
419
+ adjusted_volume = volume # 10 ๋ฏธ๋งŒ์€ ๊ทธ๋Œ€๋กœ
420
+
421
+ adjusted_volumes.append(adjusted_volume)
422
+
423
+ # ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ ๋ฐ ๊ฒ€์ƒ‰๋Ÿ‰ ์กฐ์ •
424
+ adjusted_data[keyword] = data.copy()
425
+ adjusted_data[keyword]["monthly_volumes"] = adjusted_volumes
426
+
427
+ # ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰๋„ ์กฐ์ •
428
+ if data["current_volume"] >= 10:
429
+ adjusted_data[keyword]["current_volume"] = int(data["current_volume"] * 0.9)
430
+
431
+ logger.info("์ตœ์ข… 10% ๊ฐ์†Œ ์กฐ์ • ์™„๋ฃŒ")
432
+
433
+ except Exception as e:
434
+ logger.error(f"10% ๊ฐ์†Œ ์กฐ์ • ์˜ค๋ฅ˜: {e}")
435
+ return monthly_data
436
+
437
+ return adjusted_data
438
+
439
+ # ===== ๋ฉ”์ธ ํ•จ์ˆ˜๋“ค (๊ธฐ์กด ํ˜ธํ™˜์„ฑ ์œ ์ง€) =====
440
+
441
+ def get_naver_trend_data_v5(keywords, period="1year", max_retries=3):
442
+ """๊ฐœ์„ ๋œ ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ API ํ˜ธ์ถœ - ์ด์ค‘ ํ˜ธ์ถœ ๊ตฌํ˜„"""
443
+
444
+ if period == "1year":
445
+ # ์ด์ค‘ API ํ˜ธ์ถœ
446
+ daily_data = get_daily_trend_data(keywords, max_retries)
447
+ monthly_data = get_monthly_trend_data(keywords, max_retries)
448
+
449
+ # ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜ (๊ธฐ์กด ์ฝ”๋“œ์™€ ํ˜ธํ™˜์„ฑ ์œ ์ง€๋ฅผ ์œ„ํ•ด ์›”๋ณ„ ๋ฐ์ดํ„ฐ ์šฐ์„  ๋ฐ˜ํ™˜)
450
+ return {
451
+ 'daily_data': daily_data,
452
+ 'monthly_data': monthly_data,
453
+ 'results': monthly_data['results'] if monthly_data else []
454
+ }
455
+ else: # 3year
456
+ # 3๋…„ ๋ฐ์ดํ„ฐ๋Š” ์›”๋ณ„๋งŒ ํ˜ธ์ถœ (๊ธฐ์กด ๋ฐฉ์‹ ์œ ์ง€)
457
+ monthly_data = get_monthly_trend_data(keywords, max_retries)
458
+ return monthly_data
459
+
460
+ def calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period="1year"):
461
+ """๊ฐœ์„ ๋œ ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ณ„์‚ฐ - ์ •๊ตํ•œ ์—ญ์‚ฐ ๋กœ์ง ์ ์šฉ"""
462
+ monthly_data = {}
463
+
464
+ # ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ ํ™•์ธ
465
+ if isinstance(trend_data, dict) and 'daily_data' in trend_data and 'monthly_data' in trend_data:
466
+ # ์ƒˆ๋กœ์šด ์ด์ค‘ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
467
+ daily_data = trend_data['daily_data']
468
+ monthly_data_api = trend_data['monthly_data']
469
+ else:
470
+ # ๊ธฐ์กด ๋‹จ์ผ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ (3year ๋“ฑ)
471
+ daily_data = None
472
+ monthly_data_api = trend_data
473
+
474
+ if not monthly_data_api or "results" not in monthly_data_api:
475
+ logger.warning("์›”๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด ๊ณ„์‚ฐ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
476
+ return monthly_data
477
+
478
+ logger.info(f"๊ฐœ์„ ๋œ ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ณ„์‚ฐ ์‹œ์ž‘: {len(monthly_data_api['results'])}๊ฐœ ํ‚ค์›Œ๋“œ")
479
+
480
+ for result in monthly_data_api["results"]:
481
+ keyword = result["title"]
482
+ api_keyword = keyword.replace(" ", "")
483
+
484
+ # ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰
485
+ volume_data = current_volumes.get(api_keyword, {"์ด๊ฒ€์ƒ‰๋Ÿ‰": 0})
486
+ current_volume = volume_data["์ด๊ฒ€์ƒ‰๋Ÿ‰"]
487
+
488
+ if current_volume == 0:
489
+ logger.warning(f"'{keyword}' ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰์ด 0์ด๋ฏ€๋กœ ๊ณ„์‚ฐ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
490
+ continue
491
+
492
+ logger.info(f"'{keyword}' ์ฒ˜๋ฆฌ ์‹œ์ž‘ - ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰: {current_volume:,}ํšŒ")
493
+
494
+ if period == "1year" and daily_data:
495
+ # ๐Ÿ”ฅ ์ƒˆ๋กœ์šด ์ •๊ตํ•œ ๋กœ์ง ์ ์šฉ
496
+ completed_year, completed_month = get_complete_month()
497
+
498
+ # 1๋‹จ๊ณ„: ์ผ๋ณ„ ๋ฐ์ดํ„ฐ๋กœ ์ „์›” ์ •ํ™•ํ•œ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ณ„์‚ฐ
499
+ prev_month_volume = calculate_previous_month_from_daily(current_volume, daily_data)
500
+
501
+ # 2๋‹จ๊ณ„: ์ „์›”์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ์›” ๊ฒ€์ƒ‰๋Ÿ‰ ์—ญ์‚ฐ
502
+ monthly_volumes, dates = calculate_all_months_from_previous(
503
+ prev_month_volume, monthly_data_api, completed_year, completed_month
504
+ )
505
+
506
+ if not monthly_volumes:
507
+ logger.warning(f"'{keyword}' ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ณ„์‚ฐ ์‹คํŒจ")
508
+ continue
509
+
510
+ # 3๋‹จ๊ณ„: ๋ฏธ๋ž˜ 3๊ฐœ์›” ์˜ˆ์ƒ ์ƒ์„ฑ
511
+ predicted_volumes, predicted_dates = generate_future_from_growth_rate(
512
+ monthly_volumes, dates, completed_year, completed_month
513
+ )
514
+
515
+ # ์‹ค์ œ + ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ - ์ตœ๊ทผ 12๊ฐœ์›” + ํ–ฅํ›„ 3๊ฐœ์›” = ์ด 15๊ฐœ์›”
516
+ # ์ตœ๊ทผ 12๊ฐœ์›”๋งŒ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋กœ ์ œํ•œ
517
+ recent_12_months = monthly_volumes[-12:] if len(monthly_volumes) >= 12 else monthly_volumes
518
+ recent_12_dates = dates[-12:] if len(dates) >= 12 else dates
519
+
520
+ all_volumes = recent_12_months + predicted_volumes
521
+ all_dates = recent_12_dates + predicted_dates
522
+
523
+ # ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ (์˜ˆ์ƒ 3๊ฐœ์›”)
524
+ growth_rate = calculate_future_3month_growth_rate(all_volumes, all_dates)
525
+
526
+ monthly_data[keyword] = {
527
+ "monthly_volumes": all_volumes, # 10% ๊ฐ์†Œ ์ ์šฉ ์ „ - ์ด 15๊ฐœ์›” (12๊ฐœ์›” ์‹ค์ œ + 3๊ฐœ์›” ์˜ˆ์ƒ)
528
+ "dates": all_dates,
529
+ "current_volume": current_volume, # 10% ๊ฐ์†Œ ์ ์šฉ ์ „
530
+ "growth_rate": growth_rate,
531
+ "volume_per_percent": prev_month_volume / 100 if prev_month_volume > 0 else 0,
532
+ "current_ratio": 100,
533
+ "actual_count": len(recent_12_months), # ์‹ค์ œ 12๊ฐœ์›”
534
+ "predicted_count": len(predicted_volumes) # ์˜ˆ์ƒ 3๊ฐœ์›”
535
+ }
536
+
537
+ else:
538
+ # ๊ธฐ์กด ๋ฐฉ์‹ (3year ๋“ฑ)
539
+ if not result["data"]:
540
+ continue
541
+
542
+ current_ratio = result["data"][-1]["ratio"]
543
+ if current_ratio == 0:
544
+ continue
545
+
546
+ volume_per_percent = current_volume / current_ratio
547
+
548
+ monthly_volumes = []
549
+ dates = []
550
+
551
+ for data_point in result["data"]:
552
+ ratio = data_point["ratio"]
553
+ period_date = data_point["period"]
554
+ estimated_volume = int(volume_per_percent * ratio)
555
+
556
+ monthly_volumes.append(estimated_volume)
557
+ dates.append(period_date)
558
+
559
+ growth_rate = calculate_3year_growth_rate_improved(monthly_volumes)
560
+
561
+ monthly_data[keyword] = {
562
+ "monthly_volumes": monthly_volumes, # 10% ๊ฐ์†Œ ์ ์šฉ ์ „
563
+ "dates": dates,
564
+ "current_volume": current_volume, # 10% ๊ฐ์†Œ ์ ์šฉ ์ „
565
+ "growth_rate": growth_rate,
566
+ "volume_per_percent": volume_per_percent,
567
+ "current_ratio": current_ratio,
568
+ "actual_count": len(monthly_volumes),
569
+ "predicted_count": 0
570
+ }
571
+
572
+ logger.info(f"'{keyword}' ๊ณ„์‚ฐ ์™„๋ฃŒ - ๊ฒ€์ƒ‰๋Ÿ‰ ๋ฐ์ดํ„ฐ {len(monthly_data[keyword]['monthly_volumes'])}๊ฐœ")
573
+
574
+ # ๐Ÿ”ฅ 4๋‹จ๊ณ„: ์ตœ์ข… 10% ๊ฐ์†Œ ์ ์šฉ
575
+ final_data = apply_final_10_percent_reduction(monthly_data)
576
+
577
+ logger.info("๊ฐœ์„ ๋œ ์›”๋ณ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๊ณ„์‚ฐ ์™„๋ฃŒ (10% ๊ฐ์†Œ ์ ์šฉ๋จ)")
578
+ return final_data
579
+
580
+ # ===== ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋“ค (๊ธฐ์กด ์œ ์ง€) =====
581
+
582
+ def calculate_future_3month_growth_rate(volumes, dates):
583
+ """์˜ˆ์ƒ 3๊ฐœ์›” ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ"""
584
+ if len(volumes) < 4:
585
+ return 0
586
+
587
+ try:
588
+ completed_year, completed_month = get_complete_month()
589
+
590
+ # ๊ธฐ์ค€์›” ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ
591
+ base_month_volume = None
592
+ for i, date_str in enumerate(dates):
593
+ try:
594
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
595
+ if date_obj.year == completed_year and date_obj.month == completed_month:
596
+ base_month_volume = volumes[i]
597
+ break
598
+ except:
599
+ continue
600
+
601
+ if base_month_volume is None:
602
+ return 0
603
+
604
+ # ํ–ฅํ›„ 3๊ฐœ์›” ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ
605
+ future_volumes = []
606
+ for month_offset in range(1, 4):
607
+ pred_year = completed_year
608
+ pred_month = completed_month + month_offset
609
+
610
+ while pred_month > 12:
611
+ pred_month -= 12
612
+ pred_year += 1
613
+
614
+ for i, date_str in enumerate(dates):
615
+ try:
616
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
617
+ if date_obj.year == pred_year and date_obj.month == pred_month:
618
+ future_volumes.append(volumes[i])
619
+ break
620
+ except:
621
+ continue
622
+
623
+ if len(future_volumes) < 3:
624
+ return 0
625
+
626
+ # ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ
627
+ future_average = sum(future_volumes) / len(future_volumes)
628
+
629
+ if base_month_volume > 0:
630
+ growth_rate = ((future_average - base_month_volume) / base_month_volume) * 100
631
+ return min(max(growth_rate, -50), 100)
632
+
633
+ return 0
634
+
635
+ except Exception as e:
636
+ logger.error(f"์˜ˆ์ƒ 3๊ฐœ์›” ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
637
+ return 0
638
+
639
+ def calculate_3year_growth_rate_improved(volumes):
640
+ """3๋…„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ"""
641
+ if len(volumes) < 24:
642
+ return 0
643
+
644
+ try:
645
+ first_year = volumes[:12]
646
+ last_year = volumes[-12:]
647
+
648
+ first_year_avg = sum(first_year) / len(first_year)
649
+ last_year_avg = sum(last_year) / len(last_year)
650
+
651
+ if first_year_avg == 0:
652
+ return 0
653
+
654
+ growth_rate = ((last_year_avg - first_year_avg) / first_year_avg) * 100
655
+ return min(max(growth_rate, -50), 200)
656
+
657
+ except Exception as e:
658
+ logger.error(f"3๋…„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
659
+ return 0
660
+
661
+ def calculate_correct_growth_rate(volumes, dates):
662
+ """์ž‘๋…„ ๋Œ€๋น„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ"""
663
+ if len(volumes) < 13:
664
+ return 0
665
+
666
+ try:
667
+ completed_year, completed_month = get_complete_month()
668
+
669
+ this_year_volume = None
670
+ last_year_volume = None
671
+
672
+ for i, date_str in enumerate(dates):
673
+ try:
674
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
675
+
676
+ if date_obj.year == completed_year and date_obj.month == completed_month:
677
+ this_year_volume = volumes[i]
678
+
679
+ if date_obj.year == completed_year - 1 and date_obj.month == completed_month:
680
+ last_year_volume = volumes[i]
681
+
682
+ except:
683
+ continue
684
+
685
+ if this_year_volume is not None and last_year_volume is not None and last_year_volume > 0:
686
+ growth_rate = ((this_year_volume - last_year_volume) / last_year_volume) * 100
687
+ return min(max(growth_rate, -50), 100)
688
+
689
+ return 0
690
+
691
+ except Exception as e:
692
+ logger.error(f"์ž‘๋…„ ๋Œ€๋น„ ์ฆ๊ฐ์œจ ๊ณ„์‚ฐ ์˜ค๋ฅ˜: {e}")
693
+ return 0
694
+
695
+ def generate_future_predictions_correct(volumes, dates, growth_rate):
696
+ """๋ฏธ๋ž˜ ์˜ˆ์ธก ์ƒ์„ฑ (ํ˜ธํ™˜์„ฑ ์œ ์ง€)"""
697
+ return generate_future_from_growth_rate(volumes, dates, *get_complete_month())
698
+
699
+ # ===== ์ฐจํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜๋“ค =====
700
+
701
+ def create_enhanced_current_chart(volume_data, keyword):
702
+ """ํ–ฅ์ƒ๋œ ํ˜„์žฌ ๊ฒ€์ƒ‰๋Ÿ‰ ์ •๋ณด ์ฐจํŠธ - PC vs ๋ชจ๋ฐ”์ผ ๋น„์œจ ํฌํ•จ"""
703
+ total_vol = volume_data['์ด๊ฒ€์ƒ‰๋Ÿ‰']
704
+ pc_vol = volume_data['PC๊ฒ€์ƒ‰๋Ÿ‰']
705
+ mobile_vol = volume_data['๋ชจ๋ฐ”์ผ๊ฒ€์ƒ‰๋Ÿ‰']
706
+
707
+ # ๊ฒ€์ƒ‰๋Ÿ‰ ์ˆ˜์ค€ ํ‰๊ฐ€
708
+ if total_vol >= 100000:
709
+ level_text = "๋†’์Œ ๐Ÿ”ฅ"
710
+ level_color = "#dc3545"
711
+ elif total_vol >= 10000:
712
+ level_text = "์ค‘๊ฐ„ ๐Ÿ“Š"
713
+ level_color = "#ffc107"
714
+ elif total_vol > 0:
715
+ level_text = "๋‚ฎ์Œ ๐Ÿ“‰"
716
+ level_color = "#6c757d"
717
+ else:
718
+ level_text = "๋ฐ์ดํ„ฐ ์—†์Œ โš ๏ธ"
719
+ level_color = "#6c757d"
720
+
721
+ # PC vs ๋ชจ๋ฐ”์ผ ๋น„์œจ
722
+ if total_vol > 0:
723
+ pc_ratio = (pc_vol / total_vol) * 100
724
+ mobile_ratio = (mobile_vol / total_vol) * 100
725
+ else:
726
+ pc_ratio = mobile_ratio = 0
727
+
728
+ return f"""
729
+ <div style="width: 100%; padding: 30px; font-family: 'Pretendard', sans-serif; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
730
+ <!-- ๊ฒ€์ƒ‰๋Ÿ‰ ์ˆ˜์ค€ ํ‘œ์‹œ -->
731
+ <div style="text-align: center; margin-bottom: 25px; padding: 20px; background: #f8f9fa; border-radius: 12px;">
732
+ <h4 style="margin: 0 0 15px 0; color: #495057; font-size: 20px;">๐Ÿ“Š ๊ฒ€์ƒ‰๋Ÿ‰ ์ˆ˜์ค€</h4>
733
+ <span style="display: inline-block; padding: 12px 24px; background: {level_color}; color: white; border-radius: 25px; font-weight: bold; font-size: 18px;">
734
+ {level_text}
735
+ </span>
736
+ </div>
737
+
738
+ <!-- ๊ฒ€์ƒ‰๋Ÿ‰ ์ƒ์„ธ ์ •๋ณด -->
739
+ <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 25px;">
740
+ <div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
741
+ <div style="color: #007bff; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{pc_vol:,}</div>
742
+ <div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">PC ๊ฒ€์ƒ‰๋Ÿ‰</div>
743
+ <div style="color: #007bff; font-size: 14px;">({pc_ratio:.1f}%)</div>
744
+ </div>
745
+ <div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
746
+ <div style="color: #28a745; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{mobile_vol:,}</div>
747
+ <div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">๋ชจ๋ฐ”์ผ ๊ฒ€์ƒ‰๋Ÿ‰</div>
748
+ <div style="color: #28a745; font-size: 14px;">({mobile_ratio:.1f}%)</div>
749
+ </div>
750
+ <div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
751
+ <div style="color: #dc3545; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{total_vol:,}</div>
752
+ <div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">์ด ๊ฒ€์ƒ‰๋Ÿ‰</div>
753
+ <div style="color: #dc3545; font-size: 14px;">(100%)</div>
754
+ </div>
755
+ </div>
756
+
757
+ <!-- ๋น„์œจ ๋ฐ” ์ฐจํŠธ -->
758
+ <div style="background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
759
+ <h5 style="margin: 0 0 20px 0; color: #495057; text-align: center; font-size: 18px;">PC vs ๋ชจ๋ฐ”์ผ ๋น„์œจ</h5>
760
+ <div style="display: flex; height: 25px; border-radius: 15px; overflow: hidden; background: #e9ecef;">
761
+ <div style="background: #007bff; width: {pc_ratio}%; display: flex; align-items: center; justify-content: center; font-size: 14px; color: white; font-weight: bold;">
762
+ {f'PC {pc_ratio:.1f}%' if pc_ratio > 15 else ''}
763
+ </div>
764
+ <div style="background: #28a745; width: {mobile_ratio}%; display: flex; align-items: center; justify-content: center; font-size: 14px; color: white; font-weight: bold;">
765
+ {f'๋ชจ๋ฐ”์ผ {mobile_ratio:.1f}%' if mobile_ratio > 15 else ''}
766
+ </div>
767
+ </div>
768
+ </div>
769
+
770
+ <div style="margin-top: 20px; padding: 15px; background: #fff3cd; border-radius: 8px; text-align: center;">
771
+ <p style="margin: 0; font-size: 14px; color: #856404;">
772
+ ๐Ÿ“Š <strong>ํŠธ๋ Œ๋“œ ๋ถ„์„ ์‹œ์Šคํ…œ</strong>: ๋„ค์ด๋ฒ„ ๋ฐ์ดํ„ฐ๋žฉ ๊ธฐ๋ฐ˜ ์ •ํ™•ํ•œ ๊ฒ€์ƒ‰๋Ÿ‰ ๋ถ„์„
773
+ </p>
774
+ </div>
775
+ </div>
776
+ """
777
+
778
+ def create_visual_trend_chart(monthly_data_1year, monthly_data_3year):
779
+ """์‹œ๊ฐ์  ํŠธ๋ Œ๋“œ ์ฐจํŠธ ์ƒ์„ฑ"""
780
+ try:
781
+ chart_html = f"""
782
+ <div style="width: 100%; margin: 20px auto; font-family: 'Pretendard', sans-serif;">
783
+ """
784
+
785
+ periods = [
786
+ {"data": monthly_data_1year, "title": "์ตœ๊ทผ 1๋…„ + ํ–ฅํ›„ 3๊ฐœ์›” ์˜ˆ์ƒ (์ •๊ตํ•œ ์—ญ์‚ฐ)", "period": "1year"},
787
+ {"data": monthly_data_3year, "title": "์ตœ๊ทผ 3๋…„ (10% ๋ณด์ • ์ ์šฉ)", "period": "3year"}
788
+ ]
789
+
790
+ colors = ['#FB7F0D', '#4ECDC4', '#45B7D1', '#96CEB4', '#FF6B6B']
791
+
792
+ for period_info in periods:
793
+ monthly_data = period_info["data"]
794
+ period_title = period_info["title"]
795
+ period_code = period_info["period"]
796
+
797
+ if not monthly_data:
798
+ chart_html += f"""
799
+ <div style="width: 100%; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin-bottom: 30px; border: 1px solid #e9ecef;">
800
+ <h4 style="text-align: center; color: #666; margin: 20px 0;">{period_title} - ํŠธ๋ Œ๋“œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</h4>
801
+ </div>
802
+ """
803
+ continue
804
+
805
+ chart_html += f"""
806
+ <div style="width: 100%; background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin-bottom: 30px; border: 1px solid #e9ecef;">
807
+ <h4 style="text-align: center; color: #333; margin-bottom: 25px; font-size: 18px; border-bottom: 2px solid #FB7F0D; padding-bottom: 10px;">
808
+ ๐Ÿš€ {period_title}
809
+ </h4>
810
+ """
811
+
812
+ # ๊ฐ ํ‚ค์›Œ๋“œ๋ณ„๋กœ ์ฐจํŠธ ์ƒ์„ฑ
813
+ for i, (keyword, data) in enumerate(monthly_data.items()):
814
+ volumes = data["monthly_volumes"]
815
+ dates = data["dates"]
816
+ growth_rate = data["growth_rate"]
817
+ actual_count = data.get("actual_count", len(volumes))
818
+
819
+ if not volumes:
820
+ continue
821
+
822
+ # ์ฐจํŠธ ์ƒ‰์ƒ
823
+ color = colors[i % len(colors)]
824
+ predicted_color = f"{color}80" # 50% ํˆฌ๋ช…๋„
825
+
826
+ # Y์ถ•์„ 0๋ถ€ํ„ฐ ์ตœ๋Œ€๊ฐ’๊นŒ์ง€๋กœ ์„ค์ •
827
+ max_volume = max(volumes) if volumes else 1
828
+
829
+ chart_html += f"""
830
+ <div style="width: 100%; margin-bottom: 30px; border: 1px solid #e9ecef; border-radius: 8px; overflow: hidden;">
831
+ <div style="padding: 20px; background: white;">
832
+ <h5 style="margin: 0 0 20px 0; color: #333; font-size: 16px;">
833
+ {keyword} ({get_growth_rate_label(period_code)}: {growth_rate:+.1f}%)
834
+ </h5>
835
+
836
+ <!-- ์ฐจํŠธ ์˜์—ญ -->
837
+ <div style="position: relative; height: 350px; margin: 30px 0 60px 80px; border-left: 2px solid #333; border-bottom: 2px solid #333; padding: 10px;">
838
+
839
+ <!-- Y์ถ• ๋ผ๋ฒจ -->
840
+ <div style="position: absolute; left: -70px; top: -10px; width: 60px; text-align: right; font-size: 11px; color: #333; font-weight: bold;">
841
+ {max_volume:,}
842
+ </div>
843
+ <div style="position: absolute; left: -70px; top: 50%; transform: translateY(-50%); width: 60px; text-align: right; font-size: 10px; color: #666;">
844
+ {max_volume // 2:,}
845
+ </div>
846
+ <div style="position: absolute; left: -70px; bottom: -5px; width: 60px; text-align: right; font-size: 10px; color: #666;">
847
+ 0
848
+ </div>
849
+
850
+ <!-- X์ถ• ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ -->
851
+ <div style="position: absolute; top: 0; left: 0; right: 0; height: 1px; background: #eee;"></div>
852
+ <div style="position: absolute; top: 50%; left: 0; right: 0; height: 1px; background: #eee;"></div>
853
+ <div style="position: absolute; bottom: 0; left: 0; right: 0; height: 1px; background: #333;"></div>
854
+
855
+ <!-- ์ฐจํŠธ ๋ฐ” ์ปจํ…Œ์ด๋„ˆ -->
856
+ <div style="display: flex; align-items: end; height: 100%; gap: 1px; padding: 5px 0;">
857
+ """
858
+
859
+ # ๋ฐ์ดํ„ฐ์™€ ๋‚ ์งœ๋ฅผ ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ •๋ ฌ
860
+ chart_data = list(zip(dates, volumes, range(len(volumes))))
861
+ chart_data.sort(key=lambda x: x[0]) # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
862
+
863
+ # ๋ง‰๋Œ€ ์ฐจํŠธ ์ƒ์„ฑ
864
+ for date, volume, original_index in chart_data:
865
+ # ๋ง‰๋Œ€ ๋†’์ด ๊ณ„์‚ฐ
866
+ height_percent = (volume / max_volume) * 100 if max_volume > 0 else 0
867
+
868
+ # ์‹ค์ œ ๋ฐ์ดํ„ฐ์™€ ์˜ˆ์ƒ ๋ฐ์ดํ„ฐ ๊ตฌ๋ถ„
869
+ is_predicted = original_index >= actual_count
870
+ bar_color = predicted_color if is_predicted else color
871
+
872
+ # ๋‚ ์งœ ํฌ๋งท
873
+ try:
874
+ date_obj = datetime.strptime(date, "%Y-%m-%d")
875
+ year_short = str(date_obj.year)[-2:]
876
+ month_num = date_obj.month
877
+
878
+ if is_predicted:
879
+ date_formatted = f"{year_short}.{month_num:02d}"
880
+ full_date = date_obj.strftime("%Y๋…„ %m์›”") + " (์˜ˆ์ƒ)"
881
+ bar_style = f"border: 2px dashed #333; background: repeating-linear-gradient(90deg, {bar_color}, {bar_color} 5px, transparent 5px, transparent 10px);"
882
+ else:
883
+ date_formatted = f"{year_short}.{month_num:02d}"
884
+ full_date = date_obj.strftime("%Y๋…„ %m์›”")
885
+ bar_style = f"background: linear-gradient(to top, {bar_color}, {bar_color}dd);"
886
+ except:
887
+ date_formatted = date[-5:].replace('-', '.')
888
+ full_date = date
889
+ bar_style = f"background: linear-gradient(to top, {bar_color}, {bar_color}dd);"
890
+
891
+ # ๊ณ ์œ  ID ์ƒ์„ฑ
892
+ chart_id = f"bar_{period_code}_{i}_{original_index}"
893
+
894
+ chart_html += f"""
895
+ <div style="flex: 1; display: flex; flex-direction: column; align-items: center; position: relative; height: 100%;">
896
+ <!-- ๋ง‰๋Œ€ -->
897
+ <div id="{chart_id}" style="
898
+ {bar_style}
899
+ width: 100%;
900
+ height: {height_percent}%;
901
+ border-radius: 3px 3px 0 0;
902
+ position: relative;
903
+ cursor: pointer;
904
+ transition: all 0.3s ease;
905
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
906
+ min-height: 3px;
907
+ margin-top: auto;
908
+ "
909
+ onmouseover="
910
+ this.style.transform='scaleX(1.1)';
911
+ this.style.zIndex='10';
912
+ this.style.boxShadow='0 4px 8px rgba(0,0,0,0.3)';
913
+ document.getElementById('tooltip_{chart_id}').style.display='block';
914
+ "
915
+ onmouseout="
916
+ this.style.transform='scaleX(1)';
917
+ this.style.zIndex='1';
918
+ this.style.boxShadow='0 2px 4px rgba(0,0,0,0.1)';
919
+ document.getElementById('tooltip_{chart_id}').style.display='none';
920
+ ">
921
+ <!-- ํˆดํŒ -->
922
+ <div id="tooltip_{chart_id}" style="
923
+ display: none;
924
+ position: absolute;
925
+ bottom: calc(100% + 10px);
926
+ left: 50%;
927
+ transform: translateX(-50%);
928
+ background: rgba(0,0,0,0.9);
929
+ color: white;
930
+ padding: 8px 12px;
931
+ border-radius: 6px;
932
+ font-size: 11px;
933
+ white-space: nowrap;
934
+ z-index: 1000;
935
+ box-shadow: 0 4px 12px rgba(0,0,0,0.3);
936
+ pointer-events: none;
937
+ ">
938
+ <div style="text-align: center;">
939
+ <div style="font-weight: bold; color: white; margin-bottom: 2px;">{full_date}</div>
940
+ <div style="color: #ffd700;">๊ฒ€์ƒ‰๋Ÿ‰: {volume:,}ํšŒ</div>
941
+ {'<div style="color: #ff6b6b; margin-top: 2px; font-size: 10px;">์˜ˆ์ƒ ๋ฐ์ดํ„ฐ</div>' if is_predicted else '<div style="color: #90EE90; margin-top: 2px; font-size: 10px;">์‹ค์ œ ๋ฐ์ดํ„ฐ</div>'}
942
+ </div>
943
+ <!-- ํ™”์‚ดํ‘œ -->
944
+ <div style="
945
+ position: absolute;
946
+ top: 100%;
947
+ left: 50%;
948
+ transform: translateX(-50%);
949
+ width: 0;
950
+ height: 0;
951
+ border-left: 5px solid transparent;
952
+ border-right: 5px solid transparent;
953
+ border-top: 5px solid rgba(0,0,0,0.9);
954
+ "></div>
955
+ </div>
956
+ </div>
957
+ </div>
958
+ """
959
+
960
+ chart_html += f"""
961
+ </div>
962
+
963
+ <!-- ์›” ๋ผ๋ฒจ -->
964
+ <div style="display: flex; gap: 1px; margin-top: 10px; padding: 0 5px;">
965
+ """
966
+
967
+ # ์›” ๋ผ๋ฒจ ์ƒ์„ฑ
968
+ for date, volume, original_index in chart_data:
969
+ is_predicted = original_index >= actual_count
970
+
971
+ try:
972
+ date_obj = datetime.strptime(date, "%Y-%m-%d")
973
+ year_short = str(date_obj.year)[-2:]
974
+ month_num = date_obj.month
975
+ date_formatted = f"{year_short}.{month_num:02d}"
976
+ except:
977
+ date_formatted = date[-5:].replace('-', '.')
978
+
979
+ chart_html += f"""
980
+ <div style="
981
+ flex: 1;
982
+ text-align: center;
983
+ font-size: 9px;
984
+ color: {'#e74c3c' if is_predicted else '#666'};
985
+ font-weight: {'bold' if is_predicted else 'normal'};
986
+ transform: rotate(-45deg);
987
+ transform-origin: center;
988
+ line-height: 1;
989
+ margin-top: 8px;
990
+ ">
991
+ {date_formatted}
992
+ </div>
993
+ """
994
+
995
+ # ํ†ต๊ณ„ ์ •๋ณด
996
+ if period_code == "1year":
997
+ actual_volumes = volumes[:actual_count] # ์‹ค์ œ ๋ฐ์ดํ„ฐ๋งŒ
998
+ else:
999
+ actual_volumes = volumes # 3๋…„ ์ „์ฒด ๋ฐ์ดํ„ฐ
1000
+
1001
+ avg_volume = sum(actual_volumes) // len(actual_volumes) if actual_volumes else 0
1002
+ max_volume_val = max(actual_volumes) if actual_volumes else 0
1003
+ min_volume_val = min(actual_volumes) if actual_volumes else 0
1004
+
1005
+ chart_html += f"""
1006
+ </div>
1007
+ </div>
1008
+
1009
+ <!-- ๋ฒ”๋ก€ -->
1010
+ <div style="display: flex; justify-content: center; gap: 20px; margin: 15px 0; font-size: 12px;">
1011
+ <div style="display: flex; align-items: center; gap: 5px;">
1012
+ <div style="width: 15px; height: 15px; background: {color}; border-radius: 2px;"></div>
1013
+ <span style="color: #333;">์‹ค์ œ ๋ฐ์ดํ„ฐ</span>
1014
+ </div>
1015
+ <div style="display: flex; align-items: center; gap: 5px;">
1016
+ <div style="width: 15px; height: 15px; background: repeating-linear-gradient(90deg, {predicted_color}, {predicted_color} 3px, transparent 3px, transparent 6px); border: 1px dashed #333; border-radius: 2px;"></div>
1017
+ <span style="color: #e74c3c;">์˜ˆ์ƒ ๋ฐ์ดํ„ฐ</span>
1018
+ </div>
1019
+ </div>
1020
+
1021
+ <!-- ํ†ต๊ณ„ ์ •๋ณด -->
1022
+ <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 15px; margin-top: 20px;">
1023
+ <div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
1024
+ <div style="font-size: 16px; font-weight: bold; color: #3498db;">{min_volume_val:,}</div>
1025
+ <div style="font-size: 11px; color: #666;">์ตœ์ €๊ฒ€์ƒ‰๋Ÿ‰</div>
1026
+ </div>
1027
+ <div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
1028
+ <div style="font-size: 16px; font-weight: bold; color: #2ecc71;">{avg_volume:,}</div>
1029
+ <div style="font-size: 11px; color: #666;">ํ‰๊ท ๊ฒ€์ƒ‰๋Ÿ‰</div>
1030
+ </div>
1031
+ <div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
1032
+ <div style="font-size: 16px; font-weight: bold; color: #e74c3c;">{max_volume_val:,}</div>
1033
+ <div style="font-size: 11px; color: #666;">์ตœ๊ณ ๊ฒ€์ƒ‰๋Ÿ‰</div>
1034
+ </div>
1035
+ <div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
1036
+ <div style="font-size: 16px; font-weight: bold; color: #27ae60;">{growth_rate:+.1f}%</div>
1037
+ <div style="font-size: 11px; color: #666;">{get_growth_rate_label(period_code)}</div>
1038
+ </div>
1039
+ </div>
1040
+ """
1041
+
1042
+ # ๊ฐœ์„  ์„ค๋ช…
1043
+ if period_code == "1year":
1044
+ chart_html += f"""
1045
+ <div style="margin-top: 15px; padding: 12px; background: #e8f5e8; border-radius: 8px; text-align: center;">
1046
+ <p style="margin: 0; font-size: 13px; color: #155724;">
1047
+ ๐Ÿ“Š <strong>์ตœ๊ทผ 1๋…„ + ํ–ฅํ›„ 3๊ฐœ์›” ์˜ˆ์ƒ</strong>: ์‹ค์ƒ‰ ๋ง‰๋Œ€(์‹ค์ œ), ๋น—๊ธˆ ๋ง‰๋Œ€(์˜ˆ์ƒ)
1048
+ </p>
1049
+ </div>
1050
+ """
1051
+ else:
1052
+ chart_html += f"""
1053
+ <div style="margin-top: 15px; padding: 12px; background: #e3f2fd; border-radius: 8px; text-align: center;">
1054
+ <p style="margin: 0; font-size: 13px; color: #1565c0;">
1055
+ ๐Ÿ“Š <strong>์ตœ๊ทผ 3๋…„ ํŠธ๋ Œ๋“œ</strong>: ์ „์ฒด ๊ธฐ๊ฐ„ ๊ฒ€์ƒ‰๋Ÿ‰ ๋ฐ์ดํ„ฐ
1056
+ </p>
1057
+ </div>
1058
+ """
1059
+
1060
+ chart_html += """
1061
+ </div>
1062
+ </div>
1063
+ """
1064
+
1065
+ chart_html += "</div>"
1066
+
1067
+ chart_html += "</div>"
1068
+
1069
+ logger.info(f"๊ฐœ์„ ๋œ ์ •๊ตํ•œ ํŠธ๋ Œ๋“œ ์ฐจํŠธ ์ƒ์„ฑ ์™„๋ฃŒ")
1070
+ return chart_html
1071
+
1072
+ except Exception as e:
1073
+ logger.error(f"์ฐจํŠธ ์ƒ์„ฑ ์˜ค๋ฅ˜: {e}")
1074
+ return f"""
1075
+ <div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
1076
+ <h4>์ฐจํŠธ ์ƒ์„ฑ ์˜ค๋ฅ˜</h4>
1077
+ <p>์˜ค๋ฅ˜: {str(e)}</p>
1078
+ </div>
1079
+ """
1080
+
1081
+ def create_trend_chart_v7(monthly_data_1year, monthly_data_3year):
1082
+ """๊ฐœ์„ ๋œ ํŠธ๋ Œ๋“œ ์ฐจํŠธ ์ƒ์„ฑ"""
1083
+ try:
1084
+ chart_html = create_visual_trend_chart(monthly_data_1year, monthly_data_3year)
1085
+ return chart_html
1086
+
1087
+ except Exception as e:
1088
+ logger.error(f"์ฐจํŠธ ์ƒ์„ฑ ์˜ค๋ฅ˜: {e}")
1089
+ return f"""
1090
+ <div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
1091
+ <h4>์ฐจํŠธ ์ƒ์„ฑ ์˜ค๋ฅ˜</h4>
1092
+ <p>์˜ค๋ฅ˜: {str(e)}</p>
1093
+ </div>
1094
+ """
1095
+
1096
+ def get_growth_rate_label(period_code):
1097
+ """๊ธฐ๊ฐ„์— ๋”ฐ๋ฅธ ์„ฑ์žฅ๋ฅ  ๋ผ๋ฒจ ๋ฐ˜ํ™˜"""
1098
+ if period_code == "1year":
1099
+ return "์˜ˆ์ƒ 3๊ฐœ์›” ์ฆ๊ฐ์œจ"
1100
+ else: # 3year
1101
+ return "์ž‘๋…„๋Œ€๋น„ ์ฆ๊ฐ์œจ"
1102
+
1103
+ def create_error_chart(error_msg):
1104
+ """์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ๋Œ€์ฒด ์ฐจํŠธ"""
1105
+ return f"""
1106
+ <div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
1107
+ <h4>์ฐจํŠธ ์ƒ์„ฑ ์˜ค๋ฅ˜</h4>
1108
+ <p>์˜ค๋ฅ˜: {error_msg}</p>
1109
+ </div>
1110
+ """
1111
+
1112
+ # ===== ํ˜ธํ™˜์„ฑ ํ•จ์ˆ˜๋“ค (๊ธฐ์กด ์ฝ”๋“œ์™€์˜ ํ˜ธํ™˜์„ฑ ์œ ์ง€) =====
1113
+
1114
+ def get_naver_trend_data_v4(keywords, period="1year", max_retries=3):
1115
+ """๊ธฐ์กด ํ•จ์ˆ˜ ํ˜ธํ™˜์„ฑ ์œ ์ง€"""
1116
+ return get_naver_trend_data_v5(keywords, period, max_retries)
1117
+
1118
+ def calculate_monthly_volumes_v6(keywords, current_volumes, trend_data, period="1year"):
1119
+ """๊ธฐ์กด ํ•จ์ˆ˜ ํ˜ธํ™˜์„ฑ ์œ ์ง€"""
1120
+ return calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period)
1121
+
1122
+ def calculate_monthly_volumes_v5(keywords, current_volumes, trend_data, period="1year"):
1123
+ """๊ธฐ์กด ํ•จ์ˆ˜ ํ˜ธํ™˜์„ฑ ์œ ์ง€"""
1124
+ return calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period)
1125
+
1126
+ def create_trend_chart_v6(monthly_data_1year, monthly_data_3year):
1127
+ """๊ธฐ์กด ํ•จ์ˆ˜ ํ˜ธํ™˜์„ฑ ์œ ์ง€"""
1128
+ return create_trend_chart_v7(monthly_data_1year, monthly_data_3year)