Update app.py
Browse files
app.py
CHANGED
|
@@ -1,449 +1,197 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import base64
|
| 4 |
-
import
|
| 5 |
import traceback
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import anthropic
|
| 9 |
from selenium import webdriver
|
| 10 |
from selenium.webdriver.chrome.options import Options
|
|
|
|
| 11 |
from selenium.webdriver.common.by import By
|
| 12 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
| 13 |
from selenium.webdriver.support import expected_conditions as EC
|
| 14 |
-
from selenium.common.exceptions import TimeoutException,
|
| 15 |
-
from
|
| 16 |
-
import
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
print(".env ํ์ผ ๋ก๋ ์๋ฃ")
|
| 23 |
-
except ImportError:
|
| 24 |
-
print("python-dotenv๊ฐ ์ค์น๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ์๋์ผ๋ก ์ค์ ํด์ฃผ์ธ์.")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f".env ํ์ผ ๋ก๋ ์ค ์ค๋ฅ: {e}")
|
| 27 |
|
| 28 |
-
# Claude API
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
|
| 35 |
-
print("Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์ฑ๊ณต")
|
| 36 |
-
return client
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์คํจ: {e}")
|
| 39 |
-
return None
|
| 40 |
|
| 41 |
-
|
| 42 |
-
"""
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 77 |
-
options.add_argument("--memory-pressure-off")
|
| 78 |
-
options.add_argument("--max_old_space_size=4096")
|
| 79 |
-
|
| 80 |
-
# ์ด๋ฏธ์ง/CSS ๋นํ์ฑํ๋ก ๋ก๋ฉ ์๋ ํฅ์
|
| 81 |
-
prefs = {
|
| 82 |
-
"profile.managed_default_content_settings.images": 2, # ์ด๋ฏธ์ง ์ฐจ๋จ
|
| 83 |
-
"profile.default_content_setting_values.notifications": 2, # ์๋ฆผ ์ฐจ๋จ
|
| 84 |
-
}
|
| 85 |
-
options.add_experimental_option("prefs", prefs)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
| 90 |
-
options.add_experimental_option('useAutomationExtension', False)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
options.add_argument("--accept-lang=ko-KR,ko;q=0.9,en;q=0.8")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 103 |
-
from selenium.webdriver.chrome.service import Service
|
| 104 |
-
|
| 105 |
-
service = Service(ChromeDriverManager().install())
|
| 106 |
-
self.driver = webdriver.Chrome(service=service, options=options)
|
| 107 |
-
print("webdriver-manager๋ก ChromeDriver ์๋ ์ค์น ์ฑ๊ณต")
|
| 108 |
-
driver_created = True
|
| 109 |
-
|
| 110 |
-
except Exception as wm_error:
|
| 111 |
-
print(f"webdriver-manager ์คํจ: {wm_error}")
|
| 112 |
-
|
| 113 |
-
# ๋ฐฉ๋ฒ 2: ์์คํ
ChromeDriver ์ฌ์ฉ
|
| 114 |
-
if not driver_created:
|
| 115 |
-
try:
|
| 116 |
-
self.driver = webdriver.Chrome(options=options)
|
| 117 |
-
print("์์คํ
ChromeDriver ์ฌ์ฉ ์ฑ๊ณต")
|
| 118 |
-
driver_created = True
|
| 119 |
-
except Exception as sys_error:
|
| 120 |
-
print(f"์์คํ
ChromeDriver ์คํจ: {sys_error}")
|
| 121 |
-
|
| 122 |
-
if not driver_created:
|
| 123 |
-
print("๋ชจ๋ ChromeDriver ์ด๊ธฐํ ๋ฐฉ๋ฒ ์คํจ")
|
| 124 |
-
return False
|
| 125 |
-
|
| 126 |
-
# ํ์์์ ์ค์ (์๋ ์ต์ ํ)
|
| 127 |
-
self.driver.set_page_load_timeout(15) # ํ์ด์ง ๋ก๋ฉ ํ์์์ 15์ด
|
| 128 |
-
self.driver.implicitly_wait(2) # ์์ ๋๊ธฐ ์๊ฐ 2์ด๋ก ๋จ์ถ
|
| 129 |
-
|
| 130 |
-
# ๋ด ๊ฐ์ง ๋ฐฉ์ง ์คํฌ๋ฆฝํธ
|
| 131 |
-
try:
|
| 132 |
-
self.driver.execute_script("""
|
| 133 |
-
Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
|
| 134 |
-
Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]});
|
| 135 |
-
Object.defineProperty(navigator, 'languages', {get: () => ['ko-KR', 'ko', 'en']});
|
| 136 |
-
""")
|
| 137 |
-
except Exception as script_error:
|
| 138 |
-
print(f"๋ด ๊ฐ์ง ๋ฐฉ์ง ์คํฌ๋ฆฝํธ ์คํจ (๋ฌด์): {script_error}")
|
| 139 |
-
|
| 140 |
-
return True
|
| 141 |
-
|
| 142 |
-
except Exception as e:
|
| 143 |
-
print(f"๋๋ผ์ด๋ฒ ์ค์ ์ต์ข
์คํจ: {e}")
|
| 144 |
-
return False
|
| 145 |
-
|
| 146 |
-
def safe_find_element(self, selectors: list, timeout: int = 3):
|
| 147 |
-
"""์ด๊ณ ์ ์์ ์ฐพ๊ธฐ (ํ์์์ ๋จ์ถ)"""
|
| 148 |
-
for selector_type, selector in selectors:
|
| 149 |
try:
|
| 150 |
-
|
| 151 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 152 |
-
EC.presence_of_element_located((By.XPATH, selector))
|
| 153 |
-
)
|
| 154 |
-
elif selector_type == "css":
|
| 155 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 156 |
-
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
|
| 157 |
-
)
|
| 158 |
-
elif selector_type == "text":
|
| 159 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 160 |
-
EC.presence_of_element_located((By.XPATH, f"//*[contains(text(), '{selector}')]"))
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
if element:
|
| 164 |
-
return element
|
| 165 |
except:
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
"""์ฆ์ ํด๋ฆญ (๋๊ธฐ ์๊ฐ ์ต์ํ)"""
|
| 171 |
-
element = self.safe_find_element(selectors, timeout)
|
| 172 |
-
if element:
|
| 173 |
-
try:
|
| 174 |
-
# JavaScript ํด๋ฆญ ์ฐ์ ์๋ (๋ ๋น ๋ฆ)
|
| 175 |
-
self.driver.execute_script("arguments[0].click();", element)
|
| 176 |
-
return True
|
| 177 |
-
except:
|
| 178 |
-
try:
|
| 179 |
-
element.click()
|
| 180 |
-
return True
|
| 181 |
-
except:
|
| 182 |
-
return False
|
| 183 |
return False
|
| 184 |
-
|
| 185 |
-
def parallel_change_settings(self, region: str, period: str, category: str, progress_callback=None) -> dict:
|
| 186 |
-
"""๋ณ๋ ฌ๋ก ์ค์ ๋ณ๊ฒฝ ์๋ (์คํ์ )"""
|
| 187 |
-
results = {
|
| 188 |
-
'region': False,
|
| 189 |
-
'period': False,
|
| 190 |
-
'category': False,
|
| 191 |
-
'errors': []
|
| 192 |
-
}
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
if
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
results['period'] = True
|
| 211 |
|
| 212 |
-
|
| 213 |
-
if
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
results['category'] = True
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
return results
|
| 224 |
-
|
| 225 |
-
def change_region(self, target_region: str) -> bool:
|
| 226 |
-
"""์ด๊ณ ์ ์ง์ญ ๋ณ๊ฒฝ"""
|
| 227 |
-
try:
|
| 228 |
-
region_selectors = [
|
| 229 |
-
("xpath", "//button[@aria-label='๋ํ๋ฏผ๊ตญ, ์์น ์ ํ']"),
|
| 230 |
-
("xpath", "//button[contains(@aria-label, '์์น ์ ํ')]"),
|
| 231 |
-
("xpath", "//span[contains(text(), '๋ํ๋ฏผ๊ตญ')]//parent::button"),
|
| 232 |
-
]
|
| 233 |
-
|
| 234 |
-
if not self.safe_click(region_selectors):
|
| 235 |
-
return False
|
| 236 |
-
|
| 237 |
-
time.sleep(1) # ๋๊ธฐ ์๊ฐ ๋จ์ถ
|
| 238 |
-
|
| 239 |
-
region_mapping = {
|
| 240 |
-
"์ ์ธ๊ณ": ["์ ์ธ๊ณ", "Worldwide"],
|
| 241 |
-
"๋ฏธ๊ตญ": ["๋ฏธ๊ตญ", "United States"],
|
| 242 |
-
"์ผ๋ณธ": ["์ผ๋ณธ", "Japan"],
|
| 243 |
-
"์ค๊ตญ": ["์ค๊ตญ", "China"]
|
| 244 |
-
}
|
| 245 |
-
|
| 246 |
-
if target_region in region_mapping:
|
| 247 |
-
for region_text in region_mapping[target_region]:
|
| 248 |
-
region_option_selectors = [
|
| 249 |
-
("xpath", f"//span[contains(text(), '{region_text}')]"),
|
| 250 |
-
("xpath", f"//*[contains(text(), '{region_text}')]"),
|
| 251 |
-
]
|
| 252 |
-
|
| 253 |
-
if self.safe_click(region_option_selectors, timeout=2):
|
| 254 |
-
return True
|
| 255 |
-
|
| 256 |
-
return False
|
| 257 |
-
|
| 258 |
-
except Exception as e:
|
| 259 |
-
print(f"์ง์ญ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 260 |
-
return False
|
| 261 |
-
|
| 262 |
-
def change_time_period(self, target_period: str) -> bool:
|
| 263 |
-
"""์ด๊ณ ์ ๊ธฐ๊ฐ ๋ณ๊ฒฝ"""
|
| 264 |
-
try:
|
| 265 |
-
period_selectors = [
|
| 266 |
-
("xpath", "//button[contains(@aria-label, '๊ธฐ๊ฐ ์ ํ')]"),
|
| 267 |
-
("xpath", "//span[contains(text(), '์ง๋ 24์๊ฐ')]//parent::button"),
|
| 268 |
-
]
|
| 269 |
-
|
| 270 |
-
if not self.safe_click(period_selectors):
|
| 271 |
-
return False
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
"์ง๋ 1์๊ฐ": ["์ง๋ 1์๊ฐ"],
|
| 277 |
-
"์ง๋ 4์๊ฐ": ["์ง๋ 4์๊ฐ"],
|
| 278 |
-
"์ง๋ 1์ผ": ["์ง๋ 1์ผ"],
|
| 279 |
-
"์ง๋ 7์ผ": ["์ง๋ 7์ผ", "์ง๋ ์ฃผ"]
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
if target_period in period_mapping:
|
| 283 |
-
for period_text in period_mapping[target_period]:
|
| 284 |
-
period_option_selectors = [
|
| 285 |
-
("xpath", f"//span[contains(text(), '{period_text}')]"),
|
| 286 |
-
("xpath", f"//*[contains(text(), '{period_text}')]"),
|
| 287 |
-
]
|
| 288 |
-
|
| 289 |
-
if self.safe_click(period_option_selectors, timeout=2):
|
| 290 |
-
return True
|
| 291 |
-
|
| 292 |
-
return False
|
| 293 |
-
|
| 294 |
-
except Exception as e:
|
| 295 |
-
print(f"๊ธฐ๊ฐ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 296 |
-
return False
|
| 297 |
-
|
| 298 |
-
def change_category(self, target_category: str) -> bool:
|
| 299 |
-
"""์ด๊ณ ์ ์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ"""
|
| 300 |
-
try:
|
| 301 |
-
category_selectors = [
|
| 302 |
-
("xpath", "//button[contains(@aria-label, '์นดํ
๊ณ ๋ฆฌ ์ ํ')]"),
|
| 303 |
-
("xpath", "//span[contains(text(), '๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ')]//parent::button"),
|
| 304 |
-
]
|
| 305 |
-
|
| 306 |
-
if not self.safe_click(category_selectors):
|
| 307 |
-
return False
|
| 308 |
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
"์ํฐํ
์ธ๋จผํธ": ["์์ ๋ฐ ์ํฐํ
์ธ๋จผํธ", "์ํฐํ
์ธ๋จผํธ"],
|
| 317 |
-
"๋ด์ค": ["๋ด์ค"],
|
| 318 |
-
"๋น์ฆ๋์ค": ["๋น์ฆ๋์ค"]
|
| 319 |
-
}
|
| 320 |
-
|
| 321 |
-
if target_category in category_mapping:
|
| 322 |
-
for category_text in category_mapping[target_category]:
|
| 323 |
-
category_option_selectors = [
|
| 324 |
-
("xpath", f"//span[contains(text(), '{category_text}')]"),
|
| 325 |
-
("xpath", f"//*[contains(text(), '{category_text}')]"),
|
| 326 |
-
]
|
| 327 |
-
|
| 328 |
-
if self.safe_click(category_option_selectors, timeout=2):
|
| 329 |
-
return True
|
| 330 |
-
|
| 331 |
-
return False
|
| 332 |
-
|
| 333 |
-
except Exception as e:
|
| 334 |
-
print(f"์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ ์คํจ: {e}")
|
| 335 |
-
return False
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
error_msg = ""
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
# ๋๋ผ์ด๋ฒ ์ค์
|
| 346 |
-
if not self.setup_driver():
|
| 347 |
-
return None, False, "๋ธ๋ผ์ฐ์ ๋๋ผ์ด๋ฒ ์ค์ ์คํจ"
|
| 348 |
-
|
| 349 |
-
if progress_callback:
|
| 350 |
-
progress_callback("Google Trends ํ์ด์ง ์ ์ ์ค...")
|
| 351 |
-
|
| 352 |
-
# Google Trends ์ ์ (ํ๊ตญ์ด ์ง์ URL)
|
| 353 |
-
self.driver.get("https://trends.google.com/trending?geo=KR&hl=ko")
|
| 354 |
-
|
| 355 |
-
# ์ต์ํ์ ๋ก๋ฉ ๋๊ธฐ (1์ด๋ก ๋จ์ถ)
|
| 356 |
-
time.sleep(1)
|
| 357 |
-
|
| 358 |
-
if progress_callback:
|
| 359 |
-
progress_callback("ํ์ด์ง ๋ก๋ฉ ์๋ฃ, ์ค์ ๋ณ๊ฒฝ ์์...")
|
| 360 |
-
|
| 361 |
-
# ํ์ด์ง ์ค๋น ํ์ธ (๋น ๋ฅธ ์ฒดํฌ)
|
| 362 |
-
try:
|
| 363 |
-
WebDriverWait(self.driver, 5).until(
|
| 364 |
-
EC.presence_of_element_located((By.TAG_NAME, "button"))
|
| 365 |
-
)
|
| 366 |
-
except TimeoutException:
|
| 367 |
-
pass # ๊ณ์ ์งํ
|
| 368 |
-
|
| 369 |
-
# ์ค์ ๋ณ๊ฒฝ (๋ณ๋ ฌ ์ฒ๋ฆฌ)
|
| 370 |
-
settings_result = self.parallel_change_settings(region, period, category, progress_callback)
|
| 371 |
-
|
| 372 |
-
# ๊ฒฐ๊ณผ ํ์ธ
|
| 373 |
-
changes_made = []
|
| 374 |
-
if settings_result['region']:
|
| 375 |
-
changes_made.append(f"์ง์ญ: {region}")
|
| 376 |
-
if settings_result['period']:
|
| 377 |
-
changes_made.append(f"๊ธฐ๊ฐ: {period}")
|
| 378 |
-
if settings_result['category']:
|
| 379 |
-
changes_made.append(f"์นดํ
๊ณ ๋ฆฌ: {category}")
|
| 380 |
-
|
| 381 |
-
if settings_result['errors']:
|
| 382 |
-
error_msg = "\n".join(settings_result['errors'])
|
| 383 |
-
|
| 384 |
-
if progress_callback:
|
| 385 |
-
progress_callback("์ค์ ๋ณ๊ฒฝ ์๋ฃ, ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๋์ค...")
|
| 386 |
-
|
| 387 |
-
# ์ต์ข
๋ฐ์ดํฐ ๋ก๋ฉ ๋๊ธฐ (1์ด๋ก ๋จ์ถ)
|
| 388 |
-
time.sleep(1)
|
| 389 |
-
|
| 390 |
-
# ์คํฌ๋ฆฐ์ท ์ดฌ์ (ํ์ํ ์์ญ๋ง)
|
| 391 |
-
screenshot = self.driver.get_screenshot_as_png()
|
| 392 |
-
screenshot_b64 = base64.b64encode(screenshot).decode()
|
| 393 |
-
|
| 394 |
-
success_msg = f"์ค์ ์๋ฃ: {', '.join(changes_made) if changes_made else '๊ธฐ๋ณธ ์ค์ ์ฌ์ฉ'}"
|
| 395 |
|
| 396 |
-
|
|
|
|
|
|
|
| 397 |
|
| 398 |
-
|
| 399 |
-
error_msg = f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 400 |
-
return None, False, error_msg
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
|
| 406 |
-
def analyze_with_claude(
|
| 407 |
-
"""Claude API
|
| 408 |
try:
|
| 409 |
-
|
| 410 |
-
if not ANTHROPIC_API_KEY or ANTHROPIC_API_KEY == "your_api_key_here":
|
| 411 |
-
return """
|
| 412 |
-
<div style="color: red; padding: 20px; border: 1px solid red; border-radius: 5px;">
|
| 413 |
-
<h3>API ํค ์ค๋ฅ</h3>
|
| 414 |
-
<p><strong>ANTHROPIC_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค!</strong></p>
|
| 415 |
-
</div>
|
| 416 |
-
"""
|
| 417 |
|
| 418 |
-
#
|
| 419 |
-
|
|
|
|
|
|
|
| 420 |
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
<h3>Claude API ์ฐ๊ฒฐ ์คํจ</h3>
|
| 425 |
-
<p>API ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.</p>
|
| 426 |
-
</div>
|
| 427 |
-
"""
|
| 428 |
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
""
|
| 442 |
-
|
| 443 |
-
# Claude API ํธ์ถ (๋น ๋ฅธ ์ค์ )
|
| 444 |
-
message = claude_client.messages.create(
|
| 445 |
-
model="claude-3-5-sonnet-20241022",
|
| 446 |
-
max_tokens=1000, # ํ ํฐ ์ ์ค์ฌ์ ์๋ ํฅ์
|
| 447 |
messages=[
|
| 448 |
{
|
| 449 |
"role": "user",
|
|
@@ -457,7 +205,7 @@ HTML ํํ๋ก ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ณ , ์ด๋ชจ์ง๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์
|
|
| 457 |
"source": {
|
| 458 |
"type": "base64",
|
| 459 |
"media_type": "image/png",
|
| 460 |
-
"data":
|
| 461 |
}
|
| 462 |
}
|
| 463 |
]
|
|
@@ -465,377 +213,181 @@ HTML ํํ๋ก ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ณ , ์ด๋ชจ์ง๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์
|
|
| 465 |
]
|
| 466 |
)
|
| 467 |
|
| 468 |
-
return
|
| 469 |
|
| 470 |
except Exception as e:
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
<p><strong>์ค๋ฅ:</strong> {str(e)}</p>
|
| 475 |
-
</div>
|
| 476 |
-
"""
|
| 477 |
|
| 478 |
-
def
|
| 479 |
-
"""
|
| 480 |
-
|
| 481 |
-
start_time = time.time()
|
| 482 |
-
|
| 483 |
-
# 1๋จ๊ณ: ์์ ๋ฉ์์ง
|
| 484 |
-
yield f"""
|
| 485 |
-
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 486 |
-
<h3>Google Trends ๋ถ์ ์์!</h3>
|
| 487 |
-
<p><strong>์ค์ :</strong> {region} | {period} | {category}</p>
|
| 488 |
-
<p>์ค์๊ฐ์ผ๋ก ์งํ ์ํฉ์ ์
๋ฐ์ดํธํฉ๋๋ค...</p>
|
| 489 |
-
</div>
|
| 490 |
-
"""
|
| 491 |
-
|
| 492 |
-
progress_status = {"current": ""}
|
| 493 |
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
try:
|
| 498 |
-
#
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
<div style="color: red; padding: 20px; border: 1px solid red; border-radius: 5px;">
|
| 502 |
-
<h3> API ํค ๋๋ฝ</h3>
|
| 503 |
-
<p><strong>ANTHROPIC_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค!</strong></p>
|
| 504 |
-
<p><strong>ํด๊ฒฐ ๋ฐฉ๋ฒ:</strong> .env ํ์ผ์ API ํค๋ฅผ ์ค์ ํ๊ณ ์ฑ์ ์ฌ์์ํ์ธ์.</p>
|
| 505 |
-
</div>
|
| 506 |
-
"""
|
| 507 |
-
return
|
| 508 |
-
|
| 509 |
-
# 3๋จ๊ณ: ๋ธ๋ผ์ฐ์ ์คํ ์๋ฆผ
|
| 510 |
-
yield f"""
|
| 511 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 512 |
-
<h4> 1๋จ๊ณ: ์์ํฉ๋๋ค! ์
๋ ฅํ์ ์กฐ๊ฑด์ ํ์ธํฉ๋๋ค...</h4>
|
| 513 |
-
<p>Connect Start...</p>
|
| 514 |
-
</div>
|
| 515 |
-
|
| 516 |
-
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
| 517 |
-
<p> <strong>์งํ ์ค:</strong> ๋ธ๋ผ์ฐ์ ๋๋ผ์ด๋ฒ ์ค์ ๋ฐ ํ์ด์ง ๋ก๋ฉ...</p>
|
| 518 |
-
</div>
|
| 519 |
-
"""
|
| 520 |
-
|
| 521 |
-
# 4๋จ๊ณ: Google Trends ์๋ํ ์คํ
|
| 522 |
-
print(f" ๋ถ์ ์์: {region} | {period} | {category}")
|
| 523 |
-
automator = GoogleTrendsAutomator()
|
| 524 |
-
|
| 525 |
-
# 5๋จ๊ณ: ์ค์ ๋ณ๊ฒฝ ์๋ฆผ
|
| 526 |
-
if region != "๋ํ๋ฏผ๊ตญ" or period != "์ง๋ 24์๊ฐ" or category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ":
|
| 527 |
-
yield f"""
|
| 528 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 529 |
-
<h4>์๋ํ ์์</h4>
|
| 530 |
-
<p>In Running...</p>
|
| 531 |
-
</div>
|
| 532 |
-
|
| 533 |
-
<div style="padding: 15px; background-color: #e3f2fd; border-radius: 5px; margin-bottom: 15px;">
|
| 534 |
-
<p><strong>์ค์ ๋ณ๊ฒฝ ์ค:</strong></p>
|
| 535 |
-
<ul style="margin: 10px 0;">
|
| 536 |
-
{"<li>์ง์ญ: " + region + "</li>" if region != "๋ํ๋ฏผ๊ตญ" else ""}
|
| 537 |
-
{"<li>๊ธฐ๊ฐ: " + period + "</li>" if period != "์ง๋ 24์๊ฐ" else ""}
|
| 538 |
-
{"<li>์นดํ
๊ณ ๋ฆฌ: " + category + "</li>" if category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ" else ""}
|
| 539 |
-
</ul>
|
| 540 |
-
</div>
|
| 541 |
-
"""
|
| 542 |
-
|
| 543 |
-
# ๋ธ๋ผ์ฐ์ ์์
์ ๋ณ๋ ์ค๋ ๋์์ ์คํ
|
| 544 |
-
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 545 |
-
future = executor.submit(
|
| 546 |
-
automator.capture_trends,
|
| 547 |
-
region, period, category, progress_callback
|
| 548 |
-
)
|
| 549 |
-
|
| 550 |
-
# ์งํ ์ํฉ ์
๋ฐ์ดํธ
|
| 551 |
-
while not future.done():
|
| 552 |
-
current_progress = progress_status["current"]
|
| 553 |
-
if current_progress:
|
| 554 |
-
elapsed = time.time() - start_time
|
| 555 |
-
yield f"""
|
| 556 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 557 |
-
<h4>์๋ํ ์์</h4>
|
| 558 |
-
<p>In Running...</p>
|
| 559 |
-
</div>
|
| 560 |
-
|
| 561 |
-
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
| 562 |
-
<p><strong>์งํ ์ค:</strong> {current_progress}</p>
|
| 563 |
-
<p><strong>๊ฒฝ๊ณผ ์๊ฐ:</strong> {elapsed:.1f}์ด</p>
|
| 564 |
-
</div>
|
| 565 |
-
"""
|
| 566 |
-
time.sleep(0.5)
|
| 567 |
-
|
| 568 |
-
# ๊ฒฐ๊ณผ ๋ฐ๊ธฐ
|
| 569 |
-
screenshot_b64, success, status_msg = future.result()
|
| 570 |
-
|
| 571 |
-
browser_time = time.time() - start_time
|
| 572 |
-
|
| 573 |
-
if not success:
|
| 574 |
-
yield f"""
|
| 575 |
-
<div style="color: red; padding: 20px; border: 1px solid red; border-radius: 5px;">
|
| 576 |
-
<h3>Google Trends ์ ์ ์คํจ</h3>
|
| 577 |
-
<pre style="background-color: #fff5f5; padding: 15px; border-radius: 5px; margin: 15px 0;">
|
| 578 |
-
{status_msg}
|
| 579 |
-
</pre>
|
| 580 |
-
<p><strong>ํด๊ฒฐ ๋ฐฉ๋ฒ:</strong> Chrome ๋ธ๋ผ์ฐ์ ์ ChromeDriver๋ฅผ ์ค์นํ๊ณ ๋ค์ ์๋ํ์ธ์.</p>
|
| 581 |
-
</div>
|
| 582 |
-
"""
|
| 583 |
-
return
|
| 584 |
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 588 |
-
<h4>2๋จ๊ณ: ์
๋ ฅํ์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๊ณ , ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค...</h4>
|
| 589 |
-
<p>Google Trends ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์บก์ฒํ์ต๋๋ค.</p>
|
| 590 |
-
</div>
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
<p style="font-size: 14px; opacity: 0.9;">์ฝ 10-20์ด ์์ ์์</p>
|
| 596 |
-
</div>
|
| 597 |
-
"""
|
| 598 |
-
|
| 599 |
-
# 7๋จ๊ณ: Claude API ๋ถ์
|
| 600 |
-
ai_start_time = time.time()
|
| 601 |
-
print("์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์๊ณ , Claude ๋ถ์ ์์ํฉ๋๋ค...")
|
| 602 |
-
|
| 603 |
-
# AI ๋ถ์์ ๋ณ๋ ์ค๋ ๋์์ ์คํ
|
| 604 |
-
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 605 |
-
ai_future = executor.submit(
|
| 606 |
-
analyze_with_claude,
|
| 607 |
-
screenshot_b64, region, period, category
|
| 608 |
-
)
|
| 609 |
-
|
| 610 |
-
# AI ๋ถ์ ์งํ ์ํฉ ํ์
|
| 611 |
-
while not ai_future.done():
|
| 612 |
-
ai_elapsed = time.time() - ai_start_time
|
| 613 |
-
total_elapsed = time.time() - start_time
|
| 614 |
-
yield f"""
|
| 615 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 616 |
-
<h4>๐ธ 2๋จ๊ณ: ์
๋ ฅํ์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๊ณ , ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค...</h4>
|
| 617 |
-
<p>Google Trends ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์บก์ฒํ์ต๋๋ค.</p>
|
| 618 |
-
</div>
|
| 619 |
-
|
| 620 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #FF6B6B 0%, #FF8E8E 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 621 |
-
<h4>3๋จ๊ณ: AI ๋ถ์ ์งํ ์ค...</h4>
|
| 622 |
-
<p>Claude AI๊ฐ ํธ๋ ๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ๊ณ ์์ต๋๋ค. ({ai_elapsed:.1f}์ด)</p>
|
| 623 |
-
<p style="font-size: 14px; opacity: 0.9;">์ด ๊ฒฝ๊ณผ ์๊ฐ: {total_elapsed:.1f}์ด</p>
|
| 624 |
-
</div>
|
| 625 |
-
"""
|
| 626 |
-
time.sleep(0.5)
|
| 627 |
-
|
| 628 |
-
analysis_result = ai_future.result()
|
| 629 |
-
|
| 630 |
-
ai_time = time.time() - ai_start_time
|
| 631 |
-
total_time = time.time() - start_time
|
| 632 |
-
|
| 633 |
-
# 8๋จ๊ณ: ์ต์ข
๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 634 |
-
yield f"""
|
| 635 |
-
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 636 |
-
<h3> ๋ถ์ ์๋ฃ!</h3>
|
| 637 |
-
<p><strong>์ค์ :</strong> {region} | {period} | {category}</p>
|
| 638 |
-
<p>๋ชจ๋ ๋จ๊ณ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋์์ต๋๋ค.</p>
|
| 639 |
-
</div>
|
| 640 |
|
| 641 |
-
|
|
|
|
| 642 |
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
์์ ์ค์ ์ ๋ณ๊ฒฝํ๊ณ ๋ค์ ๋ฒํผ์ ํด๋ฆญํ์ธ์!
|
| 647 |
-
</p>
|
| 648 |
-
</div>
|
| 649 |
-
|
| 650 |
-
<div style="margin-top: 15px; padding: 10px; background-color: #f9f9f9; border-radius: 5px; font-size: 12px; color: #666; text-align: center;">
|
| 651 |
-
<p style="margin: 5px 0;"> <strong>๋ถ์ ์๋ฃ ์๊ฐ:</strong> ์ฝ 15-30์ด</p>
|
| 652 |
-
<p style="margin: 5px 0;"> <strong>AI ์์ง:</strong> Claude 3.5 Sonnet</p>
|
| 653 |
-
<p style="margin: 5px 0;"> <strong>๋ฐ์ดํฐ ์ถ์ฒ:</strong> Google Trends ์ค์๊ฐ ์คํฌ๋ฆฐ์ท</p>
|
| 654 |
-
</div>
|
| 655 |
-
"""
|
| 656 |
|
| 657 |
-
|
| 658 |
|
| 659 |
except Exception as e:
|
| 660 |
-
|
| 661 |
-
print(
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
|
| 677 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 678 |
def create_interface():
|
|
|
|
| 679 |
|
| 680 |
-
with gr.Blocks(
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
css="""
|
| 684 |
-
.gradio-container {
|
| 685 |
-
max-width: 1200px !important;
|
| 686 |
-
}
|
| 687 |
-
.output-html {
|
| 688 |
-
max-height: none !important;
|
| 689 |
-
}
|
| 690 |
-
"""
|
| 691 |
-
) as interface:
|
| 692 |
|
| 693 |
-
# Header
|
| 694 |
-
gr.HTML("""
|
| 695 |
-
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 696 |
-
<h1>Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ</h1>
|
| 697 |
-
<p>Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ AI๊ฐ ์๋์ผ๋ก ๋ถ์ํฉ๋๋ค</p>
|
| 698 |
-
<p style="font-size: 14px; opacity: 0.9;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก ๋จ๊ณ๋ณ ์งํ์ํฉ์ ํ์ธํ ์ ์์ต๋๋ค.</p>
|
| 699 |
-
</div>
|
| 700 |
-
""")
|
| 701 |
-
|
| 702 |
-
# ์
๋ ฅ ์น์
|
| 703 |
with gr.Row():
|
| 704 |
with gr.Column(scale=1):
|
| 705 |
-
|
| 706 |
-
choices=["
|
| 707 |
-
value="
|
| 708 |
-
label="์ง์ญ ์ ํ"
|
| 709 |
-
info="๋ถ์ํ ์ง์ญ์ ์ ํํ์ธ์"
|
| 710 |
)
|
| 711 |
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
label="๊ธฐ๊ฐ ์ ํ",
|
| 717 |
-
info="ํธ๋ ๋ ๋ถ์ ๊ธฐ๊ฐ์ ์ ํํ์ธ์"
|
| 718 |
)
|
| 719 |
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
choices=["๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ", "๊ฒ์", "๊ฑด๊ฐ", "๊ธฐ์ ", "์คํฌ์ธ ", "์ํฐํ
์ธ๋จผํธ", "๋ด์ค", "๋น์ฆ๋์ค"],
|
| 723 |
value="๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ",
|
| 724 |
-
label="์นดํ
๊ณ ๋ฆฌ ์ ํ"
|
| 725 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
)
|
|
|
|
| 727 |
|
| 728 |
-
#
|
| 729 |
-
analyze_btn
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
)
|
| 734 |
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
value="""
|
| 739 |
-
<div style="text-align: center; padding: 20px; border: 2px dashed #ccc; border-radius: 10px; color: #666;">
|
| 740 |
-
<h3>๋ถ์ ๋๊ธฐ ์ค</h3>
|
| 741 |
-
<p>์์ ์ค์ ์ ์ ํํ๊ณ '์ค์๊ฐ ํธ๋ ๋ ๋ถ์ ์์' ๋ฒํผ์ ํด๋ฆญํ์ธ์!</p>
|
| 742 |
-
<p style="font-size: 14px;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก <strong>15-30์ด</strong>๋ง์ ๋ถ์์ด ์๋ฃ๋ฉ๋๋ค.</p>
|
| 743 |
-
</div>
|
| 744 |
-
"""
|
| 745 |
)
|
| 746 |
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
inputs=[region_input, period_input, category_input],
|
| 751 |
-
outputs=output,
|
| 752 |
-
show_progress="hidden" # ๋ด์ฅ ์งํ๋ฅ ํ์ ์จ๊น (์์ฒด ์คํธ๋ฆฌ๋ฐ ์ฌ์ฉ)
|
| 753 |
)
|
| 754 |
-
|
| 755 |
-
# ์ฌ์ฉ๋ฒ ์๋ด
|
| 756 |
-
gr.HTML("""
|
| 757 |
-
<div style="margin-top: 30px; padding: 20px; background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%); border-radius: 10px;">
|
| 758 |
-
<h3 style="margin-top: 0; color: #333;"> ์๋ก์ด ๊ธฐ๋ฅ!</h3>
|
| 759 |
-
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; color: #555;">
|
| 760 |
-
<div>
|
| 761 |
-
<h4> ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ</h4>
|
| 762 |
-
<ul style="margin: 0; padding-left: 20px;">
|
| 763 |
-
<li>๋ธ๋ผ์ฐ์ ์คํ ์ํ ์ค์๊ฐ ํ์</li>
|
| 764 |
-
<li>์ค์ ๋ณ๊ฒฝ ๊ณผ์ ๋จ๊ณ๋ณ ํ์ธ</li>
|
| 765 |
-
<li>AI ๋ถ์ ์งํ์ํฉ ํ์</li>
|
| 766 |
-
</ul>
|
| 767 |
-
</div>
|
| 768 |
-
<div>
|
| 769 |
-
<h4> ์๋ ์ต์ ํ</h4>
|
| 770 |
-
<ul style="margin: 0; padding-left: 20px;">
|
| 771 |
-
<li>๋๊ธฐ ์๊ฐ ๋จ์ถ </li>
|
| 772 |
-
<li>๋ถํ์ํ ๋ก๋ฉ ์๊ฐ ์ ๊ฑฐ</li>
|
| 773 |
-
<li>ํจ์จ์ ์ธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ</li>
|
| 774 |
-
</ul>
|
| 775 |
-
</div>
|
| 776 |
-
</div>
|
| 777 |
-
</div>
|
| 778 |
-
""")
|
| 779 |
-
|
| 780 |
-
# footer
|
| 781 |
-
gr.HTML("""
|
| 782 |
-
<div style="text-align: center; margin-top: 20px; padding: 15px; color: #666; border-top: 1px solid #eee;">
|
| 783 |
-
<p> <strong>์ฌ์ฉ๋ฒ:</strong> ์ํ๋ ์ค์ ์ ์ ํํ๊ณ ๋ถ์ ๋ฒํผ์ ํด๋ฆญํ์ธ์</p>
|
| 784 |
-
<p> <strong>์๋:</strong> ์ ์ฐจ์ ์ผ๋ก ๊ฐ์ ์์ ์
๋๋ค.</p>
|
| 785 |
-
<p> <strong>Powered by:</strong> Google Trends + Claude AI + ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ</p>
|
| 786 |
-
</div>
|
| 787 |
-
""")
|
| 788 |
|
| 789 |
return interface
|
| 790 |
|
| 791 |
-
|
| 792 |
-
|
| 793 |
print("=" * 50)
|
| 794 |
print("Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ ์์!")
|
| 795 |
print("=" * 50)
|
| 796 |
|
| 797 |
# API ํค ํ์ธ
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
print("
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
|
| 805 |
-
|
| 806 |
-
print(" Gradio ์น ์ธํฐํ์ด์ค ์ค๋น ์ค...")
|
| 807 |
-
app = create_interface()
|
| 808 |
|
| 809 |
-
#
|
| 810 |
if os.getenv("SPACE_ID"):
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
quiet=False # ์์ธ ๋ก๊ทธ ํ์
|
| 835 |
-
)
|
| 836 |
-
except KeyboardInterrupt:
|
| 837 |
-
print("\n\nGoogle Trends ๋ถ์๊ธฐ๊ฐ ์ข
๋ฃ")
|
| 838 |
-
print("๊ฐ์ฌํฉ๋๋ค! ")
|
| 839 |
-
except Exception as e:
|
| 840 |
-
print(f"\n ์๋ฒ ์คํ ์ค ์ค๋ฅ: {e}")
|
| 841 |
-
print("ํฌํธ 7860์ด ์ด๋ฏธ ์ฌ์ฉ ์ค์ธ์ง ํ์ธ")
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import base64
|
| 4 |
+
import io
|
| 5 |
import traceback
|
| 6 |
+
import threading
|
| 7 |
+
import atexit
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from typing import Optional, Tuple
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
import anthropic
|
| 13 |
from selenium import webdriver
|
| 14 |
from selenium.webdriver.chrome.options import Options
|
| 15 |
+
from selenium.webdriver.chrome.service import Service
|
| 16 |
from selenium.webdriver.common.by import By
|
| 17 |
+
from selenium.webdriver.support.ui import WebDriverWait, Select
|
| 18 |
from selenium.webdriver.support import expected_conditions as EC
|
| 19 |
+
from selenium.common.exceptions import TimeoutException, WebDriverException
|
| 20 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 21 |
+
from PIL import Image
|
| 22 |
+
import requests
|
| 23 |
|
| 24 |
+
# ์ ์ญ ๋ณ์
|
| 25 |
+
driver = None
|
| 26 |
+
driver_lock = threading.Lock()
|
| 27 |
+
is_browser_ready = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Claude API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 30 |
+
def get_claude_client():
|
| 31 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 32 |
+
if not api_key:
|
| 33 |
+
raise ValueError("ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 34 |
+
return anthropic.Anthropic(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def init_browser():
|
| 37 |
+
"""๋ธ๋ผ์ฐ์ ๋ฅผ ์ด๊ธฐํํ๊ณ Google Trends ํ์ด์ง๋ก ์ด๋"""
|
| 38 |
+
global driver, is_browser_ready
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
print("Chrome ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค...")
|
| 42 |
+
|
| 43 |
+
# Chrome ์ต์
์ค์ (Hugging Face Space ์ต์ ํ)
|
| 44 |
+
chrome_options = Options()
|
| 45 |
+
chrome_options.add_argument("--headless")
|
| 46 |
+
chrome_options.add_argument("--no-sandbox")
|
| 47 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 48 |
+
chrome_options.add_argument("--disable-gpu")
|
| 49 |
+
chrome_options.add_argument("--disable-extensions")
|
| 50 |
+
chrome_options.add_argument("--disable-web-security")
|
| 51 |
+
chrome_options.add_argument("--allow-running-insecure-content")
|
| 52 |
+
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
|
| 53 |
+
chrome_options.add_argument("--disable-features=VizDisplayCompositor")
|
| 54 |
+
chrome_options.add_argument("--window-size=1920,1080")
|
| 55 |
+
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
| 56 |
+
|
| 57 |
+
# ChromeDriver ์๋น์ค ์ค์
|
| 58 |
+
service = Service(ChromeDriverManager().install())
|
| 59 |
+
|
| 60 |
+
# ๋ธ๋ผ์ฐ์ ์ธ์คํด์ค ์์ฑ
|
| 61 |
+
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 62 |
+
driver.set_page_load_timeout(30)
|
| 63 |
+
driver.implicitly_wait(10)
|
| 64 |
+
|
| 65 |
+
print("Google Trends ํ์ด์ง๋ก ์ด๋ ์ค...")
|
| 66 |
+
driver.get("https://trends.google.com/trending?geo=KR")
|
| 67 |
+
|
| 68 |
+
# ํ์ด์ง ๋ก๋ฉ ๋๊ธฐ
|
| 69 |
+
WebDriverWait(driver, 20).until(
|
| 70 |
+
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 71 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# ์ถ๊ฐ ๋ก๋ฉ ์๊ฐ
|
| 74 |
+
time.sleep(3)
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
is_browser_ready = True
|
| 77 |
+
print("๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์๋ฃ!")
|
|
|
|
| 78 |
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์คํจ: {str(e)}")
|
| 81 |
+
print(traceback.format_exc())
|
| 82 |
+
is_browser_ready = False
|
| 83 |
+
if driver:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
+
driver.quit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except:
|
| 87 |
+
pass
|
| 88 |
+
driver = None
|
| 89 |
+
|
| 90 |
+
def cleanup_browser():
|
| 91 |
+
"""๋ธ๋ผ์ฐ๏ฟฝ๏ฟฝ๏ฟฝ ์ ๋ฆฌ"""
|
| 92 |
+
global driver
|
| 93 |
+
if driver:
|
| 94 |
+
try:
|
| 95 |
+
driver.quit()
|
| 96 |
+
print("๋ธ๋ผ์ฐ์ ์ ๋ฆฌ ์๋ฃ")
|
| 97 |
+
except:
|
| 98 |
+
pass
|
| 99 |
+
driver = None
|
| 100 |
+
|
| 101 |
+
def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
| 102 |
+
"""Google Trends ํํฐ ์ ์ฉ"""
|
| 103 |
+
global driver
|
| 104 |
|
| 105 |
+
if not driver or not is_browser_ready:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
try:
|
| 109 |
+
with driver_lock:
|
| 110 |
+
# ํํฐ ๋ฒํผ๋ค์ ์ฐพ๊ณ ํด๋ฆญ
|
| 111 |
+
try:
|
| 112 |
+
# ์ง์ญ ์ค์
|
| 113 |
+
if geo != "KR": # ๊ธฐ๋ณธ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
|
| 114 |
+
geo_elements = driver.find_elements(By.XPATH, f"//div[contains(text(), '{geo}')]")
|
| 115 |
+
if geo_elements:
|
| 116 |
+
driver.execute_script("arguments[0].click();", geo_elements[0])
|
| 117 |
+
time.sleep(1)
|
| 118 |
|
| 119 |
+
# ์๊ฐ ๋ฒ์ ์ค์
|
| 120 |
+
if time_range != "24์๊ฐ":
|
| 121 |
+
time_elements = driver.find_elements(By.XPATH, f"//div[contains(text(), '{time_range}')]")
|
| 122 |
+
if time_elements:
|
| 123 |
+
driver.execute_script("arguments[0].click();", time_elements[0])
|
| 124 |
+
time.sleep(1)
|
|
|
|
| 125 |
|
| 126 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ค์
|
| 127 |
+
if category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ":
|
| 128 |
+
category_elements = driver.find_elements(By.XPATH, f"//div[contains(text(), '{category}')]")
|
| 129 |
+
if category_elements:
|
| 130 |
+
driver.execute_script("arguments[0].click();", category_elements[0])
|
| 131 |
+
time.sleep(1)
|
|
|
|
| 132 |
|
| 133 |
+
# ํํฐ ์ ์ฉ ํ ๋ก๋ฉ ๋๊ธฐ
|
| 134 |
+
time.sleep(3)
|
| 135 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
except Exception as filter_error:
|
| 138 |
+
print(f"ํํฐ ์ ์ฉ ์ค ์ค๋ฅ: {filter_error}")
|
| 139 |
+
return True # ํํฐ ์ ์ฉ ์คํจํด๋ ์คํฌ๋ฆฐ์ท์ ์ฐ์ ์ ์์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"ํํฐ ์ ์ฉ ์คํจ: {str(e)}")
|
| 143 |
+
return False
|
| 144 |
+
|
| 145 |
+
def capture_screenshot() -> Optional[Image.Image]:
|
| 146 |
+
"""ํ์ฌ ํ์ด์ง์ ์คํฌ๋ฆฐ์ท ์บก์ฒ"""
|
| 147 |
+
global driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
if not driver or not is_browser_ready:
|
| 150 |
+
return None
|
|
|
|
| 151 |
|
| 152 |
+
try:
|
| 153 |
+
with driver_lock:
|
| 154 |
+
# ํ์ด์ง๊ฐ ์์ ํ ๋ก๋๋ ๋๊น์ง ๋๊ธฐ
|
| 155 |
+
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
# ์คํฌ๋ฆฐ์ท ์บก์ฒ
|
| 158 |
+
screenshot_data = driver.get_screenshot_as_png()
|
| 159 |
+
screenshot = Image.open(io.BytesIO(screenshot_data))
|
| 160 |
|
| 161 |
+
return screenshot
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"์คํฌ๋ฆฐ์ท ์บก์ฒ ์คํจ: {str(e)}")
|
| 165 |
+
return None
|
| 166 |
|
| 167 |
+
def analyze_with_claude(image: Image.Image, user_prompt: str = "") -> str:
|
| 168 |
+
"""Claude API๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ๋ถ์"""
|
| 169 |
try:
|
| 170 |
+
client = get_claude_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
# ์ด๋ฏธ์ง๋ฅผ base64๋ก ์ธ์ฝ๋ฉ
|
| 173 |
+
buffer = io.BytesIO()
|
| 174 |
+
image.save(buffer, format="PNG")
|
| 175 |
+
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 176 |
|
| 177 |
+
# Claude API ํธ์ถ์ ์ํ ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 178 |
+
default_prompt = """
|
| 179 |
+
์ด Google Trends ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด ์คํฌ๋ฆฐ์ท์ ๋ถ์ํด์ฃผ์ธ์.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
๋ค์ ๋ด์ฉ์ ํฌํจํด์ ๋ถ์ํด์ฃผ์ธ์:
|
| 182 |
+
1. ํ์ฌ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๊ฒ์์ด๋ค๊ณผ ๊ทธ ์์
|
| 183 |
+
2. ๊ฐ ๊ฒ์์ด์ ์ฆ๊ฐ ์ถ์ธ๋ ํน์ด์ฌํญ
|
| 184 |
+
3. ๊ฒ์์ด๋ค์์ ๋ฐ๊ฒฌ๋๋ ์ฃผ์ ํธ๋ ๋๋ ํจํด
|
| 185 |
+
4. ์์ฌ์ /๋ฌธํ์ ๋งฅ๋ฝ์์์ ํด์
|
| 186 |
+
|
| 187 |
+
ํ๊ตญ์ด๋ก ์์ธํ๊ณ ๊ตฌ์ฒด์ ์ผ๋ก ๋ถ์ํด์ฃผ์ธ์.
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
prompt = user_prompt if user_prompt.strip() else default_prompt
|
| 191 |
+
|
| 192 |
+
response = client.messages.create(
|
| 193 |
+
model="claude-3-sonnet-20240229",
|
| 194 |
+
max_tokens=1500,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
messages=[
|
| 196 |
{
|
| 197 |
"role": "user",
|
|
|
|
| 205 |
"source": {
|
| 206 |
"type": "base64",
|
| 207 |
"media_type": "image/png",
|
| 208 |
+
"data": image_base64
|
| 209 |
}
|
| 210 |
}
|
| 211 |
]
|
|
|
|
| 213 |
]
|
| 214 |
)
|
| 215 |
|
| 216 |
+
return response.content[0].text
|
| 217 |
|
| 218 |
except Exception as e:
|
| 219 |
+
error_msg = f"Claude API ๋ถ์ ์คํจ: {str(e)}"
|
| 220 |
+
print(error_msg)
|
| 221 |
+
return error_msg
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
def refresh_trends_page():
|
| 224 |
+
"""Google Trends ํ์ด์ง ์๋ก๊ณ ์นจ"""
|
| 225 |
+
global driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
+
if not driver or not is_browser_ready:
|
| 228 |
+
return "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค."
|
| 229 |
+
|
| 230 |
+
try:
|
| 231 |
+
with driver_lock:
|
| 232 |
+
driver.refresh()
|
| 233 |
+
time.sleep(3)
|
| 234 |
+
return "ํ์ด์ง๊ฐ ์๋ก๊ณ ์นจ๋์์ต๋๋ค."
|
| 235 |
+
except Exception as e:
|
| 236 |
+
return f"ํ์ด์ง ์๋ก๊ณ ์นจ ์คํจ: {str(e)}"
|
| 237 |
+
|
| 238 |
+
def main_process(geo: str, time_range: str, category: str, user_prompt: str) -> Tuple[Optional[Image.Image], str]:
|
| 239 |
+
"""๋ฉ์ธ ์ฒ๋ฆฌ ํจ์"""
|
| 240 |
+
if not is_browser_ready:
|
| 241 |
+
return None, "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค. ํ์ด์ง๋ฅผ ์๋ก๊ณ ์นจํด์ฃผ์ธ์."
|
| 242 |
|
| 243 |
try:
|
| 244 |
+
# 1. ํํฐ ์ ์ฉ
|
| 245 |
+
print(f"ํํฐ ์ ์ฉ ์ค: ์ง์ญ={geo}, ์๊ฐ={time_range}, ์นดํ
๊ณ ๋ฆฌ={category}")
|
| 246 |
+
filter_success = apply_filters(geo, time_range, category)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
if not filter_success:
|
| 249 |
+
return None, "ํํฐ ์ ์ฉ์ ์คํจํ์ต๋๋ค."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
+
# 2. ์คํฌ๋ฆฐ์ท ์บก์ฒ
|
| 252 |
+
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค...")
|
| 253 |
+
screenshot = capture_screenshot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
+
if screenshot is None:
|
| 256 |
+
return None, "์คํฌ๋ฆฐ์ท ์บก์ฒ์ ์คํจํ์ต๋๋ค."
|
| 257 |
|
| 258 |
+
# 3. Claude API๋ก ๋ถ์
|
| 259 |
+
print("Claude API๋ก ๋ถ์ ์ค...")
|
| 260 |
+
analysis_result = analyze_with_claude(screenshot, user_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
return screenshot, analysis_result
|
| 263 |
|
| 264 |
except Exception as e:
|
| 265 |
+
error_msg = f"์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 266 |
+
print(error_msg)
|
| 267 |
+
print(traceback.format_exc())
|
| 268 |
+
return None, error_msg
|
| 269 |
+
|
| 270 |
+
def check_browser_status():
|
| 271 |
+
"""๋ธ๋ผ์ฐ์ ์ํ ํ์ธ"""
|
| 272 |
+
global is_browser_ready, driver
|
| 273 |
+
|
| 274 |
+
if not driver:
|
| 275 |
+
return "๋ธ๋ผ์ฐ์ ๊ฐ ์ด๊ธฐํ๋์ง ์์์ต๋๋ค."
|
| 276 |
+
|
| 277 |
+
if not is_browser_ready:
|
| 278 |
+
return "๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค์
๋๋ค..."
|
| 279 |
+
|
| 280 |
+
try:
|
| 281 |
+
with driver_lock:
|
| 282 |
+
current_url = driver.current_url
|
| 283 |
+
return f"๋ธ๋ผ์ฐ์ ์ค๋น ์๋ฃ - ํ์ฌ URL: {current_url}"
|
| 284 |
+
except:
|
| 285 |
+
return "๋ธ๋ผ์ฐ์ ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค."
|
| 286 |
|
| 287 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 288 |
def create_interface():
|
| 289 |
+
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
| 290 |
|
| 291 |
+
with gr.Blocks(title="Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ", theme=gr.themes.Soft()) as interface:
|
| 292 |
+
gr.Markdown("# Google Trends ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด ๋ถ์๊ธฐ")
|
| 293 |
+
gr.Markdown("Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ ์บก์ฒํ๊ณ Claude AI๋ก ๋ถ์ํฉ๋๋ค.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
with gr.Row():
|
| 296 |
with gr.Column(scale=1):
|
| 297 |
+
geo_dropdown = gr.Dropdown(
|
| 298 |
+
choices=["KR", "US", "JP", "GB", "DE", "FR"],
|
| 299 |
+
value="KR",
|
| 300 |
+
label="์ง์ญ ์ ํ"
|
|
|
|
| 301 |
)
|
| 302 |
|
| 303 |
+
time_dropdown = gr.Dropdown(
|
| 304 |
+
choices=["24์๊ฐ", "7์ผ", "30์ผ", "90์ผ", "12๊ฐ์"],
|
| 305 |
+
value="24์๊ฐ",
|
| 306 |
+
label="๊ธฐ๊ฐ ์ ํ"
|
|
|
|
|
|
|
| 307 |
)
|
| 308 |
|
| 309 |
+
category_dropdown = gr.Dropdown(
|
| 310 |
+
choices=["๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ", "์ํฐํ
์ธ๋จผํธ", "์คํฌ์ธ ", "๋น์ฆ๋์ค", "๊ณผํ๊ธฐ์ ", "๊ฑด๊ฐ"],
|
|
|
|
| 311 |
value="๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ",
|
| 312 |
+
label="์นดํ
๊ณ ๋ฆฌ ์ ํ"
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
user_prompt = gr.Textbox(
|
| 316 |
+
label="๋ถ์ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
| 317 |
+
placeholder="ํน๋ณํ ๋ถ์ํ๊ณ ์ถ์ ๋ด์ฉ์ด ์๋ค๋ฉด ์
๋ ฅํ์ธ์...",
|
| 318 |
+
lines=3
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
analyze_btn = gr.Button("๋ถ์ ์์", variant="primary")
|
| 322 |
+
refresh_btn = gr.Button("ํ์ด์ง ์๋ก๊ณ ์นจ", variant="secondary")
|
| 323 |
+
status_btn = gr.Button("๋ธ๋ผ์ฐ์ ์ํ ํ์ธ", variant="secondary")
|
| 324 |
+
|
| 325 |
+
with gr.Column(scale=2):
|
| 326 |
+
screenshot_output = gr.Image(label="Google Trends ์คํฌ๋ฆฐ์ท")
|
| 327 |
+
analysis_output = gr.Textbox(
|
| 328 |
+
label="Claude AI ๋ถ์ ๊ฒฐ๊ณผ",
|
| 329 |
+
lines=10,
|
| 330 |
+
max_lines=20
|
| 331 |
)
|
| 332 |
+
status_output = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
| 333 |
|
| 334 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 335 |
+
analyze_btn.click(
|
| 336 |
+
fn=main_process,
|
| 337 |
+
inputs=[geo_dropdown, time_dropdown, category_dropdown, user_prompt],
|
| 338 |
+
outputs=[screenshot_output, analysis_output]
|
| 339 |
)
|
| 340 |
|
| 341 |
+
refresh_btn.click(
|
| 342 |
+
fn=refresh_trends_page,
|
| 343 |
+
outputs=status_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
)
|
| 345 |
|
| 346 |
+
status_btn.click(
|
| 347 |
+
fn=check_browser_status,
|
| 348 |
+
outputs=status_output
|
|
|
|
|
|
|
|
|
|
| 349 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
return interface
|
| 352 |
|
| 353 |
+
def main():
|
| 354 |
+
"""๋ฉ์ธ ํจ์"""
|
| 355 |
print("=" * 50)
|
| 356 |
print("Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ ์์!")
|
| 357 |
print("=" * 50)
|
| 358 |
|
| 359 |
# API ํค ํ์ธ
|
| 360 |
+
try:
|
| 361 |
+
get_claude_client()
|
| 362 |
+
print("โ Claude API ํค ํ์ธ๋จ")
|
| 363 |
+
except ValueError as e:
|
| 364 |
+
print(f"โ {e}")
|
| 365 |
+
return
|
| 366 |
|
| 367 |
+
print("โ Gradio ์น ์ธํฐํ์ด์ค ์ค๋น ์ค...")
|
|
|
|
|
|
|
| 368 |
|
| 369 |
+
# ํ๊ฒฝ ํ์ธ
|
| 370 |
if os.getenv("SPACE_ID"):
|
| 371 |
+
print("โ Hugging Face Space ํ๊ฒฝ์์ ์คํ ์ค...")
|
| 372 |
+
|
| 373 |
+
# ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ (๋ฐฑ๊ทธ๋ผ์ด๋์์)
|
| 374 |
+
init_thread = threading.Thread(target=init_browser, daemon=True)
|
| 375 |
+
init_thread.start()
|
| 376 |
+
|
| 377 |
+
# ์ข
๋ฃ ์ ๋ธ๋ผ์ฐ์ ์ ๋ฆฌ
|
| 378 |
+
atexit.register(cleanup_browser)
|
| 379 |
+
|
| 380 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ
|
| 381 |
+
interface = create_interface()
|
| 382 |
+
|
| 383 |
+
# Hugging Face Space์์๋ share=False๋ก ์คํ
|
| 384 |
+
interface.launch(
|
| 385 |
+
server_name="0.0.0.0",
|
| 386 |
+
server_port=7860,
|
| 387 |
+
share=False,
|
| 388 |
+
show_error=True,
|
| 389 |
+
quiet=False
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
if __name__ == "__main__":
|
| 393 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|