hanjunjung
commited on
Commit
ยท
b44da90
1
Parent(s):
a4bb82f
[fix]
Browse files- app.py +162 -312
- packages.txt +0 -5
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -3,17 +3,11 @@ import time
|
|
| 3 |
import base64
|
| 4 |
import json
|
| 5 |
import traceback
|
| 6 |
-
import
|
| 7 |
-
import sys
|
| 8 |
from typing import Tuple, Optional
|
| 9 |
import gradio as gr
|
| 10 |
import anthropic
|
| 11 |
-
from
|
| 12 |
-
from selenium.webdriver.chrome.options import Options
|
| 13 |
-
from selenium.webdriver.common.by import By
|
| 14 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
| 15 |
-
from selenium.webdriver.support import expected_conditions as EC
|
| 16 |
-
from selenium.common.exceptions import TimeoutException, NoSuchElementException
|
| 17 |
from concurrent.futures import ThreadPoolExecutor
|
| 18 |
import threading
|
| 19 |
|
|
@@ -21,50 +15,6 @@ import threading
|
|
| 21 |
IS_HUGGINGFACE = os.getenv("SPACE_ID") is not None
|
| 22 |
print(f'IS_HUGGINGFACE = {IS_HUGGINGFACE}')
|
| 23 |
|
| 24 |
-
# Hugging Face ํ๊ฒฝ์์ Chrome ์ค์น
|
| 25 |
-
def install_chrome():
|
| 26 |
-
if not IS_HUGGINGFACE:
|
| 27 |
-
return True
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
print("Chrome ์ค์น ์์...")
|
| 31 |
-
|
| 32 |
-
# Google Chrome ๋ค์ด๋ก๋ ๋ฐ ์ค์น
|
| 33 |
-
commands = [
|
| 34 |
-
"wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -",
|
| 35 |
-
"echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list",
|
| 36 |
-
"apt-get update",
|
| 37 |
-
"apt-get install -y google-chrome-stable"
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
for cmd in commands:
|
| 41 |
-
print(f"์คํ ์ค: {cmd}")
|
| 42 |
-
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 43 |
-
if result.returncode != 0:
|
| 44 |
-
print(f"๋ช
๋ น์ด ์คํจ: {cmd}")
|
| 45 |
-
print(f"์ค๋ฅ: {result.stderr}")
|
| 46 |
-
else:
|
| 47 |
-
print(f"์ฑ๊ณต: {cmd}")
|
| 48 |
-
|
| 49 |
-
# Chrome ๋ฒ์ ํ์ธ
|
| 50 |
-
result = subprocess.run("google-chrome --version", shell=True, capture_output=True, text=True)
|
| 51 |
-
if result.returncode == 0:
|
| 52 |
-
print(f"Chrome ์ค์น ์๋ฃ: {result.stdout.strip()}")
|
| 53 |
-
return True
|
| 54 |
-
else:
|
| 55 |
-
print("Chrome ์ค์น ์คํจ")
|
| 56 |
-
return False
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
print(f"Chrome ์ค์น ์ค ์ค๋ฅ: {e}")
|
| 60 |
-
return False
|
| 61 |
-
|
| 62 |
-
# ์์ ์ Chrome ์ค์น
|
| 63 |
-
if IS_HUGGINGFACE:
|
| 64 |
-
chrome_installed = install_chrome()
|
| 65 |
-
if not chrome_installed:
|
| 66 |
-
print("Chrome ์ค์น์ ์คํจํ์ง๋ง ๊ณ์ ์งํํฉ๋๋ค...")
|
| 67 |
-
|
| 68 |
# Claude API ์ค์ (Hugging Face Secrets์์ ๊ฐ์ ธ์ค๊ธฐ)
|
| 69 |
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 70 |
|
|
@@ -73,6 +23,33 @@ if not ANTHROPIC_API_KEY:
|
|
| 73 |
else:
|
| 74 |
print("Claude API ํค ํ์ธ ์๋ฃ")
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Claude ํด๋ผ์ด์ธํธ๋ฅผ ์์ฑ
|
| 77 |
def create_claude_client():
|
| 78 |
try:
|
|
@@ -83,229 +60,81 @@ def create_claude_client():
|
|
| 83 |
print(f"Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์คํจ: {e}")
|
| 84 |
return None
|
| 85 |
|
| 86 |
-
# Google Trends ์๋ํ ํด๋์ค
|
| 87 |
class GoogleTrendsAutomator:
|
| 88 |
def __init__(self):
|
| 89 |
-
self.
|
|
|
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
def
|
| 93 |
-
options = Options()
|
| 94 |
-
|
| 95 |
-
# Hugging Face Space ํ๊ฒฝ ์ค์
|
| 96 |
-
if IS_HUGGINGFACE:
|
| 97 |
-
print("Hugging Face Space ํ๊ฒฝ, Chrome ๋ธ๋ผ์ฐ์ ๋ก ์คํ")
|
| 98 |
-
# Chrome ๋ฐ์ด๋๋ฆฌ ๊ฒฝ๋ก ์ค์
|
| 99 |
-
chrome_paths = [
|
| 100 |
-
"/usr/bin/google-chrome",
|
| 101 |
-
"/usr/bin/google-chrome-stable",
|
| 102 |
-
"/usr/bin/chromium",
|
| 103 |
-
"/usr/bin/chromium-browser"
|
| 104 |
-
]
|
| 105 |
-
|
| 106 |
-
chrome_binary = None
|
| 107 |
-
for path in chrome_paths:
|
| 108 |
-
if os.path.exists(path):
|
| 109 |
-
chrome_binary = path
|
| 110 |
-
print(f"Chrome ๋ฐ์ด๋๋ฆฌ ๋ฐ๊ฒฌ: {path}")
|
| 111 |
-
break
|
| 112 |
-
|
| 113 |
-
if chrome_binary:
|
| 114 |
-
options.binary_location = chrome_binary
|
| 115 |
-
|
| 116 |
-
options.add_argument("--headless=new")
|
| 117 |
-
options.add_argument("--no-sandbox")
|
| 118 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 119 |
-
options.add_argument("--disable-gpu")
|
| 120 |
-
options.add_argument("--disable-software-rasterizer")
|
| 121 |
-
options.add_argument("--remote-debugging-port=9222")
|
| 122 |
-
options.add_argument("--single-process")
|
| 123 |
-
options.add_argument("--disable-background-timer-throttling")
|
| 124 |
-
options.add_argument("--disable-backgrounding-occluded-windows")
|
| 125 |
-
options.add_argument("--disable-renderer-backgrounding")
|
| 126 |
-
else:
|
| 127 |
-
options.add_argument("--headless")
|
| 128 |
-
|
| 129 |
-
# ์๋ ์ต์ ํ ์ค์
|
| 130 |
-
options.add_argument("--window-size=1280,720")
|
| 131 |
-
options.add_argument("--disable-blink-features=AutomationControlled")
|
| 132 |
-
options.add_argument("--disable-extensions")
|
| 133 |
-
options.add_argument("--disable-web-security")
|
| 134 |
-
options.add_argument("--allow-running-insecure-content")
|
| 135 |
-
options.add_argument("--disable-features=TranslateUI")
|
| 136 |
-
options.add_argument("--disable-ipc-flooding-protection")
|
| 137 |
-
|
| 138 |
-
# ๋คํธ์ํฌ ์ต์ ํ
|
| 139 |
-
options.add_argument("--aggressive-cache-discard")
|
| 140 |
-
options.add_argument("--disable-background-networking")
|
| 141 |
-
|
| 142 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 143 |
-
options.add_argument("--memory-pressure-off")
|
| 144 |
-
options.add_argument("--max_old_space_size=2048")
|
| 145 |
-
|
| 146 |
-
# ์ด๋ฏธ์ง/CSS ๋นํ์ฑํ๋ก ๋ก๋ฉ ์๋ ํฅ์
|
| 147 |
-
prefs = {
|
| 148 |
-
"profile.managed_default_content_settings.images": 2,
|
| 149 |
-
"profile.default_content_setting_values.notifications": 2,
|
| 150 |
-
}
|
| 151 |
-
options.add_experimental_option("prefs", prefs)
|
| 152 |
-
|
| 153 |
-
# ๋ด ๊ฐ์ง ํํผ
|
| 154 |
-
options.add_argument("--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
| 155 |
-
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
| 156 |
-
options.add_experimental_option('useAutomationExtension', False)
|
| 157 |
-
|
| 158 |
-
# ์ธ์ด ์ค์
|
| 159 |
-
options.add_argument("--lang=ko")
|
| 160 |
-
options.add_argument("--accept-lang=ko-KR,ko;q=0.9,en;q=0.8")
|
| 161 |
-
|
| 162 |
try:
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 188 |
-
from selenium.webdriver.chrome.service import Service
|
| 189 |
-
|
| 190 |
-
service = Service(ChromeDriverManager().install())
|
| 191 |
-
self.driver = webdriver.Chrome(service=service, options=options)
|
| 192 |
-
print("๋ก์ปฌ ChromeDriver ์ค์น ์ฑ๊ณต")
|
| 193 |
-
except:
|
| 194 |
-
self.driver = webdriver.Chrome(options=options)
|
| 195 |
-
print("๋ก์ปฌ ์์คํ
ChromeDriver ์ฌ์ฉ")
|
| 196 |
|
| 197 |
-
#
|
| 198 |
-
self.
|
| 199 |
-
self.driver.implicitly_wait(3)
|
| 200 |
|
| 201 |
-
#
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
except Exception as script_error:
|
| 209 |
-
print(f"๋ด ๊ฐ์ง ๋ฐฉ์ง ์คํฌ๋ฆฝํธ ์คํจ (๋ฌด์): {script_error}")
|
| 210 |
|
|
|
|
| 211 |
return True
|
| 212 |
|
| 213 |
except Exception as e:
|
| 214 |
-
print(f"
|
| 215 |
return False
|
| 216 |
|
| 217 |
-
#
|
| 218 |
-
def
|
| 219 |
-
for
|
| 220 |
try:
|
| 221 |
-
|
| 222 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 223 |
-
EC.presence_of_element_located((By.XPATH, selector))
|
| 224 |
-
)
|
| 225 |
-
elif selector_type == "css":
|
| 226 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 227 |
-
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
|
| 228 |
-
)
|
| 229 |
-
elif selector_type == "text":
|
| 230 |
-
element = WebDriverWait(self.driver, timeout).until(
|
| 231 |
-
EC.presence_of_element_located((By.XPATH, f"//*[contains(text(), '{selector}')]"))
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
-
if element:
|
| 235 |
-
return element
|
| 236 |
-
except:
|
| 237 |
-
continue
|
| 238 |
-
return None
|
| 239 |
-
|
| 240 |
-
# ์ฆ์ ํด๋ฆญ
|
| 241 |
-
def safe_click(self, selectors: list, timeout: int = 3) -> bool:
|
| 242 |
-
element = self.safe_find_element(selectors, timeout)
|
| 243 |
-
if element:
|
| 244 |
-
try:
|
| 245 |
-
# JavaScript ํด๋ฆญ ์ฐ์ ์๋ (๋ ๋น ๋ฆ)
|
| 246 |
-
self.driver.execute_script("arguments[0].click();", element)
|
| 247 |
return True
|
| 248 |
except:
|
| 249 |
-
|
| 250 |
-
element.click()
|
| 251 |
-
return True
|
| 252 |
-
except:
|
| 253 |
-
return False
|
| 254 |
return False
|
| 255 |
|
| 256 |
-
# ๋ณ๋ ฌ๋ก ์ค์ ๋ณ๊ฒฝ ์๋
|
| 257 |
-
def parallel_change_settings(self, region: str, period: str, category: str, progress_callback=None) -> dict:
|
| 258 |
-
results = {
|
| 259 |
-
'region': False,
|
| 260 |
-
'period': False,
|
| 261 |
-
'category': False,
|
| 262 |
-
'errors': []
|
| 263 |
-
}
|
| 264 |
-
|
| 265 |
-
# ์์ฐจ์ ์ผ๋ก ๋ณ๊ฒฝ (๋ณ๋ ฌ์ selenium์์ ์์ ํ์ง ์์)
|
| 266 |
-
try:
|
| 267 |
-
if region != "๋ํ๋ฏผ๊ตญ":
|
| 268 |
-
if progress_callback:
|
| 269 |
-
progress_callback("์ง์ญ ์ค์ ๋ณ๊ฒฝ ์ค...")
|
| 270 |
-
results['region'] = self.change_region(region)
|
| 271 |
-
time.sleep(0.5)
|
| 272 |
-
else:
|
| 273 |
-
results['region'] = True
|
| 274 |
-
|
| 275 |
-
if period != "์ง๋ 24์๊ฐ":
|
| 276 |
-
if progress_callback:
|
| 277 |
-
progress_callback("๊ธฐ๊ฐ ์ค์ ๋ณ๊ฒฝ ์ค...")
|
| 278 |
-
results['period'] = self.change_time_period(period)
|
| 279 |
-
time.sleep(0.5)
|
| 280 |
-
else:
|
| 281 |
-
results['period'] = True
|
| 282 |
-
|
| 283 |
-
if category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ":
|
| 284 |
-
if progress_callback:
|
| 285 |
-
progress_callback("์นดํ
๊ณ ๋ฆฌ ์ค์ ๋ณ๊ฒฝ ์ค...")
|
| 286 |
-
results['category'] = self.change_category(category)
|
| 287 |
-
time.sleep(0.5)
|
| 288 |
-
else:
|
| 289 |
-
results['category'] = True
|
| 290 |
-
|
| 291 |
-
except Exception as e:
|
| 292 |
-
results['errors'].append(str(e))
|
| 293 |
-
|
| 294 |
-
return results
|
| 295 |
-
|
| 296 |
# ์ง์ญ ๋ณ๊ฒฝ
|
| 297 |
-
def change_region(self, target_region: str) -> bool:
|
| 298 |
try:
|
| 299 |
region_selectors = [
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
]
|
| 304 |
|
| 305 |
-
if not self.safe_click(region_selectors):
|
| 306 |
return False
|
| 307 |
|
| 308 |
-
|
| 309 |
|
| 310 |
region_mapping = {
|
| 311 |
"์ ์ธ๊ณ": ["์ ์ธ๊ณ", "Worldwide"],
|
|
@@ -317,11 +146,12 @@ class GoogleTrendsAutomator:
|
|
| 317 |
if target_region in region_mapping:
|
| 318 |
for region_text in region_mapping[target_region]:
|
| 319 |
region_option_selectors = [
|
| 320 |
-
|
| 321 |
-
|
|
|
|
| 322 |
]
|
| 323 |
|
| 324 |
-
if self.safe_click(region_option_selectors, timeout=
|
| 325 |
return True
|
| 326 |
|
| 327 |
return False
|
|
@@ -331,17 +161,17 @@ class GoogleTrendsAutomator:
|
|
| 331 |
return False
|
| 332 |
|
| 333 |
# ๊ธฐ๊ฐ ๋ณ๊ฒฝ
|
| 334 |
-
def change_time_period(self, target_period: str) -> bool:
|
| 335 |
try:
|
| 336 |
period_selectors = [
|
| 337 |
-
|
| 338 |
-
|
| 339 |
]
|
| 340 |
|
| 341 |
-
if not self.safe_click(period_selectors):
|
| 342 |
return False
|
| 343 |
|
| 344 |
-
|
| 345 |
|
| 346 |
period_mapping = {
|
| 347 |
"์ง๋ 1์๊ฐ": ["์ง๋ 1์๊ฐ"],
|
|
@@ -353,11 +183,12 @@ class GoogleTrendsAutomator:
|
|
| 353 |
if target_period in period_mapping:
|
| 354 |
for period_text in period_mapping[target_period]:
|
| 355 |
period_option_selectors = [
|
| 356 |
-
|
| 357 |
-
|
|
|
|
| 358 |
]
|
| 359 |
|
| 360 |
-
if self.safe_click(period_option_selectors, timeout=
|
| 361 |
return True
|
| 362 |
|
| 363 |
return False
|
|
@@ -367,17 +198,17 @@ class GoogleTrendsAutomator:
|
|
| 367 |
return False
|
| 368 |
|
| 369 |
# ์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ
|
| 370 |
-
def change_category(self, target_category: str) -> bool:
|
| 371 |
try:
|
| 372 |
category_selectors = [
|
| 373 |
-
|
| 374 |
-
|
| 375 |
]
|
| 376 |
|
| 377 |
-
if not self.safe_click(category_selectors):
|
| 378 |
return False
|
| 379 |
|
| 380 |
-
|
| 381 |
|
| 382 |
category_mapping = {
|
| 383 |
"๊ฒ์": ["๊ฒ์"],
|
|
@@ -392,11 +223,12 @@ class GoogleTrendsAutomator:
|
|
| 392 |
if target_category in category_mapping:
|
| 393 |
for category_text in category_mapping[target_category]:
|
| 394 |
category_option_selectors = [
|
| 395 |
-
|
| 396 |
-
|
|
|
|
| 397 |
]
|
| 398 |
|
| 399 |
-
if self.safe_click(category_option_selectors, timeout=
|
| 400 |
return True
|
| 401 |
|
| 402 |
return False
|
|
@@ -406,61 +238,74 @@ class GoogleTrendsAutomator:
|
|
| 406 |
return False
|
| 407 |
|
| 408 |
# Google Trends ์บก์ฒ
|
| 409 |
-
def capture_trends(self, region: str, period: str, category: str, progress_callback=None) -> Tuple[Optional[str], bool, str]:
|
| 410 |
error_msg = ""
|
| 411 |
|
| 412 |
try:
|
| 413 |
if progress_callback:
|
| 414 |
-
progress_callback("๋ธ๋ผ์ฐ์
|
| 415 |
|
| 416 |
-
#
|
| 417 |
-
if not self.
|
| 418 |
-
return None, False, "๋ธ๋ผ์ฐ์
|
| 419 |
|
| 420 |
if progress_callback:
|
| 421 |
progress_callback("Google Trends ํ์ด์ง ์ ์ ์ค...")
|
| 422 |
|
| 423 |
-
# Google Trends ์ ์
|
| 424 |
-
self.
|
| 425 |
|
| 426 |
-
# ๋ก๋ฉ ๋๊ธฐ
|
| 427 |
-
wait_time =
|
| 428 |
-
|
| 429 |
|
| 430 |
if progress_callback:
|
| 431 |
progress_callback("ํ์ด์ง ๋ก๋ฉ ์๋ฃ, ์ค์ ๋ณ๊ฒฝ ์์...")
|
| 432 |
|
| 433 |
-
# ํ์ด์ง ์ค๋น ํ์ธ
|
| 434 |
try:
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
)
|
| 438 |
-
except TimeoutException:
|
| 439 |
pass
|
| 440 |
|
| 441 |
-
# ์ค์ ๋ณ๊ฒฝ
|
| 442 |
-
settings_result = self.parallel_change_settings(region, period, category, progress_callback)
|
| 443 |
-
|
| 444 |
-
# ๊ฒฐ๊ณผ ํ์ธ
|
| 445 |
changes_made = []
|
| 446 |
-
if settings_result['region']:
|
| 447 |
-
changes_made.append(f"์ง์ญ: {region}")
|
| 448 |
-
if settings_result['period']:
|
| 449 |
-
changes_made.append(f"๊ธฐ๊ฐ: {period}")
|
| 450 |
-
if settings_result['category']:
|
| 451 |
-
changes_made.append(f"์นดํ
๊ณ ๋ฆฌ: {category}")
|
| 452 |
|
| 453 |
-
if
|
| 454 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
|
| 456 |
if progress_callback:
|
| 457 |
-
progress_callback("์ค์ ๋ณ๊ฒฝ ์๋ฃ,
|
| 458 |
|
| 459 |
-
# ์ต์ข
|
| 460 |
-
|
| 461 |
|
| 462 |
# ์คํฌ๋ฆฐ์ท ์ดฌ์
|
| 463 |
-
screenshot = self.
|
| 464 |
screenshot_b64 = base64.b64encode(screenshot).decode()
|
| 465 |
|
| 466 |
success_msg = f"์ค์ ์๋ฃ: {', '.join(changes_made) if changes_made else '๊ธฐ๋ณธ ์ค์ ์ฌ์ฉ'}"
|
|
@@ -472,9 +317,10 @@ class GoogleTrendsAutomator:
|
|
| 472 |
return None, False, error_msg
|
| 473 |
|
| 474 |
finally:
|
| 475 |
-
if self.
|
| 476 |
try:
|
| 477 |
-
self.
|
|
|
|
| 478 |
except:
|
| 479 |
pass
|
| 480 |
|
|
@@ -551,6 +397,11 @@ HTML ํํ๋ก ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ณ , ์ด๋ชจ์ง๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์
|
|
| 551 |
</div>
|
| 552 |
"""
|
| 553 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
def main_analysis(region: str, period: str, category: str):
|
| 555 |
# ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ถ์ ํจ์ (Generator)
|
| 556 |
|
|
@@ -592,18 +443,17 @@ def main_analysis(region: str, period: str, category: str):
|
|
| 592 |
|
| 593 |
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
| 594 |
<p><strong>์งํ ์ค:</strong> ๋ธ๋ผ์ฐ์ ๋๋ผ์ด๋ฒ ์ค์ ๋ฐ ํ์ด์ง ๋ก๋ฉ...</p>
|
| 595 |
-
{'<p style="font-size: 12px; color: #666;">
|
| 596 |
</div>
|
| 597 |
"""
|
| 598 |
|
| 599 |
# 4๋จ๊ณ: Google Trends ์๋ํ ์คํ
|
| 600 |
print(f"๋ถ์ ์์: {region} | {period} | {category}")
|
| 601 |
-
automator = GoogleTrendsAutomator()
|
| 602 |
|
| 603 |
# ๋ธ๋ผ์ฐ์ ์์
์ ๋ณ๋ ์ค๋ ๋์์ ์คํ
|
| 604 |
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 605 |
future = executor.submit(
|
| 606 |
-
|
| 607 |
region, period, category, progress_callback
|
| 608 |
)
|
| 609 |
|
|
@@ -615,7 +465,7 @@ def main_analysis(region: str, period: str, category: str):
|
|
| 615 |
yield f"""
|
| 616 |
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 617 |
<h4>์๋ํ ์งํ ์ค</h4>
|
| 618 |
-
<p>
|
| 619 |
</div>
|
| 620 |
|
| 621 |
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
|
@@ -639,7 +489,7 @@ def main_analysis(region: str, period: str, category: str):
|
|
| 639 |
</pre>
|
| 640 |
<p><strong>ํด๊ฒฐ ๋ฐฉ๋ฒ:</strong></p>
|
| 641 |
<ul>
|
| 642 |
-
<li>
|
| 643 |
<li>Space๋ฅผ ์ฌ์์ํด๋ณด์ธ์</li>
|
| 644 |
<li>์ ์ ํ ๋ค์ ์๋ํด๋ณด์ธ์</li>
|
| 645 |
</ul>
|
|
@@ -685,7 +535,7 @@ def main_analysis(region: str, period: str, category: str):
|
|
| 685 |
</div>
|
| 686 |
|
| 687 |
<div style="margin-top: 15px; padding: 10px; background-color: #f9f9f9; border-radius: 5px; font-size: 12px; color: #666; text-align: center;">
|
| 688 |
-
<p style="margin: 5px 0;"><strong>์คํ ํ๊ฒฝ:</strong> {'Hugging Face Space (
|
| 689 |
<p style="margin: 5px 0;"><strong>AI ์์ง:</strong> Claude 3.5 Sonnet</p>
|
| 690 |
<p style="margin: 5px 0;"><strong>๋ฐ์ดํฐ ์ถ์ฒ:</strong> Google Trends ์ค์๊ฐ ์คํฌ๋ฆฐ์ท</p>
|
| 691 |
</div>
|
|
@@ -733,7 +583,7 @@ def create_interface():
|
|
| 733 |
<h1>Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ</h1>
|
| 734 |
<p>Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ AI๊ฐ ์๋์ผ๋ก ๋ถ์ํฉ๋๋ค</p>
|
| 735 |
<p style="font-size: 14px; opacity: 0.9;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก ๋จ๊ณ๋ณ ์งํ์ํฉ์ ํ์ธํ ์ ์์ต๋๋ค.</p>
|
| 736 |
-
{'<p style="font-size: 12px; opacity: 0.7;">Hugging Face Space (
|
| 737 |
</div>
|
| 738 |
""")
|
| 739 |
|
|
@@ -788,7 +638,7 @@ def create_interface():
|
|
| 788 |
<h3>๋ถ์ ๋๊ธฐ ์ค</h3>
|
| 789 |
<p>์์ ์ค์ ์ ์ ํํ๊ณ '์ค์๊ฐ ํธ๋ ๋ ๋ถ์ ์์' ๋ฒํผ์ ํด๋ฆญํ์ธ์!</p>
|
| 790 |
<p style="font-size: 14px;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก <strong>15-30์ด</strong>๋ง์ ๋ถ์์ด ์๋ฃ๋ฉ๋๋ค.</p>
|
| 791 |
-
{'<p style="font-size: 12px; color: #666;">
|
| 792 |
</div>
|
| 793 |
"""
|
| 794 |
)
|
|
@@ -832,7 +682,7 @@ def create_interface():
|
|
| 832 |
<p><strong>์ฌ์ฉ๋ฒ:</strong> ์ํ๋ ์ค์ ์ ์ ํํ๊ณ ๋ถ์ ๋ฒํผ์ ํด๋ฆญํ์ธ์</p>
|
| 833 |
<p><strong>์๋:</strong> ์ ์ฐจ์ ์ผ๋ก ๊ฐ์ ์์ ์
๋๋ค.</p>
|
| 834 |
<p><strong>Powered by:</strong> Google Trends + Claude AI + ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ</p>
|
| 835 |
-
{'<p style="font-size: 12px;">Running on Hugging Face Spaces with
|
| 836 |
</div>
|
| 837 |
""")
|
| 838 |
|
|
|
|
| 3 |
import base64
|
| 4 |
import json
|
| 5 |
import traceback
|
| 6 |
+
import asyncio
|
|
|
|
| 7 |
from typing import Tuple, Optional
|
| 8 |
import gradio as gr
|
| 9 |
import anthropic
|
| 10 |
+
from playwright.async_api import async_playwright
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from concurrent.futures import ThreadPoolExecutor
|
| 12 |
import threading
|
| 13 |
|
|
|
|
| 15 |
IS_HUGGINGFACE = os.getenv("SPACE_ID") is not None
|
| 16 |
print(f'IS_HUGGINGFACE = {IS_HUGGINGFACE}')
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Claude API ์ค์ (Hugging Face Secrets์์ ๊ฐ์ ธ์ค๊ธฐ)
|
| 19 |
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 20 |
|
|
|
|
| 23 |
else:
|
| 24 |
print("Claude API ํค ํ์ธ ์๋ฃ")
|
| 25 |
|
| 26 |
+
# Playwright ๋ธ๋ผ์ฐ์ ์ค์น (์ต์ด 1ํ)
|
| 27 |
+
async def install_playwright_browsers():
|
| 28 |
+
if IS_HUGGINGFACE:
|
| 29 |
+
try:
|
| 30 |
+
print("Playwright ๋ธ๋ผ์ฐ์ ์ค์น ์ค...")
|
| 31 |
+
import subprocess
|
| 32 |
+
result = subprocess.run(["playwright", "install", "chromium"], capture_output=True, text=True)
|
| 33 |
+
if result.returncode == 0:
|
| 34 |
+
print("Playwright Chromium ์ค์น ์๋ฃ")
|
| 35 |
+
return True
|
| 36 |
+
else:
|
| 37 |
+
print(f"Playwright ์ค์น ์คํจ: {result.stderr}")
|
| 38 |
+
return False
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Playwright ์ค์น ์ค ์ค๋ฅ: {e}")
|
| 41 |
+
return False
|
| 42 |
+
return True
|
| 43 |
+
|
| 44 |
+
# ์์ ์ Playwright ๋ธ๋ผ์ฐ์ ์ค์น
|
| 45 |
+
if IS_HUGGINGFACE:
|
| 46 |
+
try:
|
| 47 |
+
browser_installed = asyncio.run(install_playwright_browsers())
|
| 48 |
+
if not browser_installed:
|
| 49 |
+
print("Playwright ๋ธ๋ผ์ฐ์ ์ค์น์ ์คํจํ์ง๋ง ๊ณ์ ์งํํฉ๋๋ค...")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Playwright ์ด๊ธฐํ ์ค๋ฅ: {e}")
|
| 52 |
+
|
| 53 |
# Claude ํด๋ผ์ด์ธํธ๋ฅผ ์์ฑ
|
| 54 |
def create_claude_client():
|
| 55 |
try:
|
|
|
|
| 60 |
print(f"Claude API ํด๋ผ์ด์ธํธ ์์ฑ ์คํจ: {e}")
|
| 61 |
return None
|
| 62 |
|
| 63 |
+
# Google Trends ์๋ํ ํด๋์ค (Playwright ์ฌ์ฉ)
|
| 64 |
class GoogleTrendsAutomator:
|
| 65 |
def __init__(self):
|
| 66 |
+
self.browser = None
|
| 67 |
+
self.page = None
|
| 68 |
|
| 69 |
+
# Playwright ๋ธ๋ผ์ฐ์ ์ค์
|
| 70 |
+
async def setup_browser(self, progress_callback=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
+
if progress_callback:
|
| 73 |
+
progress_callback("Playwright ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค...")
|
| 74 |
+
|
| 75 |
+
self.playwright = await async_playwright().start()
|
| 76 |
+
|
| 77 |
+
# Chromium ๋ธ๋ผ์ฐ์ ์คํ
|
| 78 |
+
self.browser = await self.playwright.chromium.launch(
|
| 79 |
+
headless=True,
|
| 80 |
+
args=[
|
| 81 |
+
"--no-sandbox",
|
| 82 |
+
"--disable-dev-shm-usage",
|
| 83 |
+
"--disable-gpu",
|
| 84 |
+
"--disable-software-rasterizer",
|
| 85 |
+
"--memory-pressure-off",
|
| 86 |
+
"--disable-background-timer-throttling",
|
| 87 |
+
"--disable-backgrounding-occluded-windows",
|
| 88 |
+
"--disable-renderer-backgrounding",
|
| 89 |
+
"--disable-features=TranslateUI",
|
| 90 |
+
"--disable-ipc-flooding-protection",
|
| 91 |
+
"--disable-web-security",
|
| 92 |
+
"--lang=ko",
|
| 93 |
+
"--accept-lang=ko-KR,ko;q=0.9,en"
|
| 94 |
+
]
|
| 95 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# ์ ํ์ด์ง ์์ฑ
|
| 98 |
+
self.page = await self.browser.new_page()
|
|
|
|
| 99 |
|
| 100 |
+
# ์ด๋ฏธ์ง ์ฐจ๋จ์ผ๋ก ์๋ ํฅ์
|
| 101 |
+
await self.page.route("**/*.{png,jpg,jpeg,gif,webp,svg,ico}", lambda route: route.abort())
|
| 102 |
+
|
| 103 |
+
# User Agent ์ค์
|
| 104 |
+
await self.page.set_extra_http_headers({
|
| 105 |
+
"Accept-Language": "ko-KR,ko;q=0.9,en;q=0.8"
|
| 106 |
+
})
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
print("Playwright ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ฑ๊ณต")
|
| 109 |
return True
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
+
print(f"Playwright ๋ธ๋ผ์ฐ์ ์ค์ ์คํจ: {e}")
|
| 113 |
return False
|
| 114 |
|
| 115 |
+
# ์์ ํ ํด๋ฆญ
|
| 116 |
+
async def safe_click(self, selectors: list, timeout: int = 3000) -> bool:
|
| 117 |
+
for selector in selectors:
|
| 118 |
try:
|
| 119 |
+
await self.page.click(selector, timeout=timeout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return True
|
| 121 |
except:
|
| 122 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
return False
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# ์ง์ญ ๋ณ๊ฒฝ
|
| 126 |
+
async def change_region(self, target_region: str) -> bool:
|
| 127 |
try:
|
| 128 |
region_selectors = [
|
| 129 |
+
"button[aria-label*='์์น ์ ํ']",
|
| 130 |
+
"button[aria-label*='๋ํ๋ฏผ๊ตญ']",
|
| 131 |
+
"span:has-text('๋ํ๋ฏผ๊ตญ') >> xpath=.."
|
| 132 |
]
|
| 133 |
|
| 134 |
+
if not await self.safe_click(region_selectors):
|
| 135 |
return False
|
| 136 |
|
| 137 |
+
await self.page.wait_for_timeout(1000)
|
| 138 |
|
| 139 |
region_mapping = {
|
| 140 |
"์ ์ธ๊ณ": ["์ ์ธ๊ณ", "Worldwide"],
|
|
|
|
| 146 |
if target_region in region_mapping:
|
| 147 |
for region_text in region_mapping[target_region]:
|
| 148 |
region_option_selectors = [
|
| 149 |
+
f"span:has-text('{region_text}')",
|
| 150 |
+
f"text={region_text}",
|
| 151 |
+
f"*:has-text('{region_text}')"
|
| 152 |
]
|
| 153 |
|
| 154 |
+
if await self.safe_click(region_option_selectors, timeout=2000):
|
| 155 |
return True
|
| 156 |
|
| 157 |
return False
|
|
|
|
| 161 |
return False
|
| 162 |
|
| 163 |
# ๊ธฐ๊ฐ ๋ณ๊ฒฝ
|
| 164 |
+
async def change_time_period(self, target_period: str) -> bool:
|
| 165 |
try:
|
| 166 |
period_selectors = [
|
| 167 |
+
"button[aria-label*='๊ธฐ๊ฐ ์ ํ']",
|
| 168 |
+
"span:has-text('์ง๋ 24์๊ฐ') >> xpath=.."
|
| 169 |
]
|
| 170 |
|
| 171 |
+
if not await self.safe_click(period_selectors):
|
| 172 |
return False
|
| 173 |
|
| 174 |
+
await self.page.wait_for_timeout(1000)
|
| 175 |
|
| 176 |
period_mapping = {
|
| 177 |
"์ง๋ 1์๊ฐ": ["์ง๋ 1์๊ฐ"],
|
|
|
|
| 183 |
if target_period in period_mapping:
|
| 184 |
for period_text in period_mapping[target_period]:
|
| 185 |
period_option_selectors = [
|
| 186 |
+
f"span:has-text('{period_text}')",
|
| 187 |
+
f"text={period_text}",
|
| 188 |
+
f"*:has-text('{period_text}')"
|
| 189 |
]
|
| 190 |
|
| 191 |
+
if await self.safe_click(period_option_selectors, timeout=2000):
|
| 192 |
return True
|
| 193 |
|
| 194 |
return False
|
|
|
|
| 198 |
return False
|
| 199 |
|
| 200 |
# ์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ
|
| 201 |
+
async def change_category(self, target_category: str) -> bool:
|
| 202 |
try:
|
| 203 |
category_selectors = [
|
| 204 |
+
"button[aria-label*='์นดํ
๊ณ ๋ฆฌ ์ ํ']",
|
| 205 |
+
"span:has-text('๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ') >> xpath=.."
|
| 206 |
]
|
| 207 |
|
| 208 |
+
if not await self.safe_click(category_selectors):
|
| 209 |
return False
|
| 210 |
|
| 211 |
+
await self.page.wait_for_timeout(1000)
|
| 212 |
|
| 213 |
category_mapping = {
|
| 214 |
"๊ฒ์": ["๊ฒ์"],
|
|
|
|
| 223 |
if target_category in category_mapping:
|
| 224 |
for category_text in category_mapping[target_category]:
|
| 225 |
category_option_selectors = [
|
| 226 |
+
f"span:has-text('{category_text}')",
|
| 227 |
+
f"text={category_text}",
|
| 228 |
+
f"*:has-text('{category_text}')"
|
| 229 |
]
|
| 230 |
|
| 231 |
+
if await self.safe_click(category_option_selectors, timeout=2000):
|
| 232 |
return True
|
| 233 |
|
| 234 |
return False
|
|
|
|
| 238 |
return False
|
| 239 |
|
| 240 |
# Google Trends ์บก์ฒ
|
| 241 |
+
async def capture_trends(self, region: str, period: str, category: str, progress_callback=None) -> Tuple[Optional[str], bool, str]:
|
| 242 |
error_msg = ""
|
| 243 |
|
| 244 |
try:
|
| 245 |
if progress_callback:
|
| 246 |
+
progress_callback("๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค...")
|
| 247 |
|
| 248 |
+
# ๋ธ๋ผ์ฐ์ ์ค์
|
| 249 |
+
if not await self.setup_browser(progress_callback):
|
| 250 |
+
return None, False, "๋ธ๋ผ์ฐ์ ์ค์ ์คํจ"
|
| 251 |
|
| 252 |
if progress_callback:
|
| 253 |
progress_callback("Google Trends ํ์ด์ง ์ ์ ์ค...")
|
| 254 |
|
| 255 |
+
# Google Trends ์ ์
|
| 256 |
+
await self.page.goto("https://trends.google.com/trending?geo=KR&hl=ko", wait_until="domcontentloaded")
|
| 257 |
|
| 258 |
+
# ๋ก๋ฉ ๋๊ธฐ
|
| 259 |
+
wait_time = 4000 if IS_HUGGINGFACE else 2000
|
| 260 |
+
await self.page.wait_for_timeout(wait_time)
|
| 261 |
|
| 262 |
if progress_callback:
|
| 263 |
progress_callback("ํ์ด์ง ๋ก๋ฉ ์๋ฃ, ์ค์ ๋ณ๊ฒฝ ์์...")
|
| 264 |
|
| 265 |
+
# ํ์ด์ง ์ค๋น ํ์ธ
|
| 266 |
try:
|
| 267 |
+
await self.page.wait_for_selector("button", timeout=10000)
|
| 268 |
+
except:
|
|
|
|
|
|
|
| 269 |
pass
|
| 270 |
|
| 271 |
+
# ์ค์ ๋ณ๊ฒฝ
|
|
|
|
|
|
|
|
|
|
| 272 |
changes_made = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
if region != "๋ํ๋ฏผ๊ตญ":
|
| 275 |
+
if progress_callback:
|
| 276 |
+
progress_callback(f"์ง์ญ์ {region}์ผ๋ก ๋ณ๊ฒฝ ์ค...")
|
| 277 |
+
if await self.change_region(region):
|
| 278 |
+
changes_made.append(f"์ง์ญ: {region}")
|
| 279 |
+
await self.page.wait_for_timeout(500)
|
| 280 |
+
else:
|
| 281 |
+
error_msg += f"์ง์ญ ๋ณ๊ฒฝ ์คํจ ({region})\n"
|
| 282 |
+
|
| 283 |
+
if period != "์ง๋ 24์๊ฐ":
|
| 284 |
+
if progress_callback:
|
| 285 |
+
progress_callback(f"๊ธฐ๊ฐ์ {period}์ผ๋ก ๋ณ๊ฒฝ ์ค...")
|
| 286 |
+
if await self.change_time_period(period):
|
| 287 |
+
changes_made.append(f"๊ธฐ๊ฐ: {period}")
|
| 288 |
+
await self.page.wait_for_timeout(500)
|
| 289 |
+
else:
|
| 290 |
+
error_msg += f"๊ธฐ๊ฐ ๋ณ๊ฒฝ ์คํจ ({period})\n"
|
| 291 |
+
|
| 292 |
+
if category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ":
|
| 293 |
+
if progress_callback:
|
| 294 |
+
progress_callback(f"์นดํ
๊ณ ๋ฆฌ๋ฅผ {category}๋ก ๋ณ๊ฒฝ ์ค...")
|
| 295 |
+
if await self.change_category(category):
|
| 296 |
+
changes_made.append(f"์นดํ
๊ณ ๋ฆฌ: {category}")
|
| 297 |
+
await self.page.wait_for_timeout(500)
|
| 298 |
+
else:
|
| 299 |
+
error_msg += f"์นดํ
๊ณ ๋ฆฌ ๋ณ๊ฒฝ ์คํจ ({category})\n"
|
| 300 |
|
| 301 |
if progress_callback:
|
| 302 |
+
progress_callback("์ค์ ๋ณ๊ฒฝ ์๋ฃ, ์คํฌ๋ฆฐ์ท ์ดฌ์ ์ค...")
|
| 303 |
|
| 304 |
+
# ์ต์ข
๋ก๋ฉ ๋๊ธฐ
|
| 305 |
+
await self.page.wait_for_timeout(2000 if IS_HUGGINGFACE else 1000)
|
| 306 |
|
| 307 |
# ์คํฌ๋ฆฐ์ท ์ดฌ์
|
| 308 |
+
screenshot = await self.page.screenshot(full_page=False)
|
| 309 |
screenshot_b64 = base64.b64encode(screenshot).decode()
|
| 310 |
|
| 311 |
success_msg = f"์ค์ ์๋ฃ: {', '.join(changes_made) if changes_made else '๊ธฐ๋ณธ ์ค์ ์ฌ์ฉ'}"
|
|
|
|
| 317 |
return None, False, error_msg
|
| 318 |
|
| 319 |
finally:
|
| 320 |
+
if self.browser:
|
| 321 |
try:
|
| 322 |
+
await self.browser.close()
|
| 323 |
+
await self.playwright.stop()
|
| 324 |
except:
|
| 325 |
pass
|
| 326 |
|
|
|
|
| 397 |
</div>
|
| 398 |
"""
|
| 399 |
|
| 400 |
+
# ๋๊ธฐ ๋ํผ ํจ์
|
| 401 |
+
def run_async_capture(region: str, period: str, category: str, progress_callback=None):
|
| 402 |
+
automator = GoogleTrendsAutomator()
|
| 403 |
+
return asyncio.run(automator.capture_trends(region, period, category, progress_callback))
|
| 404 |
+
|
| 405 |
def main_analysis(region: str, period: str, category: str):
|
| 406 |
# ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ถ์ ํจ์ (Generator)
|
| 407 |
|
|
|
|
| 443 |
|
| 444 |
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
| 445 |
<p><strong>์งํ ์ค:</strong> ๋ธ๋ผ์ฐ์ ๋๋ผ์ด๋ฒ ์ค์ ๋ฐ ํ์ด์ง ๋ก๋ฉ...</p>
|
| 446 |
+
{'<p style="font-size: 12px; color: #666;">Playwright ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค...</p>' if IS_HUGGINGFACE else ''}
|
| 447 |
</div>
|
| 448 |
"""
|
| 449 |
|
| 450 |
# 4๋จ๊ณ: Google Trends ์๋ํ ์คํ
|
| 451 |
print(f"๋ถ์ ์์: {region} | {period} | {category}")
|
|
|
|
| 452 |
|
| 453 |
# ๋ธ๋ผ์ฐ์ ์์
์ ๋ณ๋ ์ค๋ ๋์์ ์คํ
|
| 454 |
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 455 |
future = executor.submit(
|
| 456 |
+
run_async_capture,
|
| 457 |
region, period, category, progress_callback
|
| 458 |
)
|
| 459 |
|
|
|
|
| 465 |
yield f"""
|
| 466 |
<div style="text-align: center; padding: 15px; background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%); color: white; border-radius: 10px; margin-bottom: 15px;">
|
| 467 |
<h4>์๋ํ ์งํ ์ค</h4>
|
| 468 |
+
<p>Playwright ๋ธ๋ผ์ฐ์ ์์ Google Trends ๋ฐ์ดํฐ ์์ง ์ค...</p>
|
| 469 |
</div>
|
| 470 |
|
| 471 |
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 15px;">
|
|
|
|
| 489 |
</pre>
|
| 490 |
<p><strong>ํด๊ฒฐ ๋ฐฉ๋ฒ:</strong></p>
|
| 491 |
<ul>
|
| 492 |
+
<li>Playwright ๋ธ๋ผ์ฐ์ ์ค์น ํ์ธ</li>
|
| 493 |
<li>Space๋ฅผ ์ฌ์์ํด๋ณด์ธ์</li>
|
| 494 |
<li>์ ์ ํ ๋ค์ ์๋ํด๋ณด์ธ์</li>
|
| 495 |
</ul>
|
|
|
|
| 535 |
</div>
|
| 536 |
|
| 537 |
<div style="margin-top: 15px; padding: 10px; background-color: #f9f9f9; border-radius: 5px; font-size: 12px; color: #666; text-align: center;">
|
| 538 |
+
<p style="margin: 5px 0;"><strong>์คํ ํ๊ฒฝ:</strong> {'Hugging Face Space (Playwright)' if IS_HUGGINGFACE else 'Local'}</p>
|
| 539 |
<p style="margin: 5px 0;"><strong>AI ์์ง:</strong> Claude 3.5 Sonnet</p>
|
| 540 |
<p style="margin: 5px 0;"><strong>๋ฐ์ดํฐ ์ถ์ฒ:</strong> Google Trends ์ค์๊ฐ ์คํฌ๋ฆฐ์ท</p>
|
| 541 |
</div>
|
|
|
|
| 583 |
<h1>Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ</h1>
|
| 584 |
<p>Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ AI๊ฐ ์๋์ผ๋ก ๋ถ์ํฉ๋๋ค</p>
|
| 585 |
<p style="font-size: 14px; opacity: 0.9;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก ๋จ๊ณ๋ณ ์งํ์ํฉ์ ํ์ธํ ์ ์์ต๋๋ค.</p>
|
| 586 |
+
{'<p style="font-size: 12px; opacity: 0.7;">Hugging Face Space (Playwright ๋ธ๋ผ์ฐ์ )</p>' if IS_HUGGINGFACE else ''}
|
| 587 |
</div>
|
| 588 |
""")
|
| 589 |
|
|
|
|
| 638 |
<h3>๋ถ์ ๋๊ธฐ ์ค</h3>
|
| 639 |
<p>์์ ์ค์ ์ ์ ํํ๊ณ '์ค์๊ฐ ํธ๋ ๋ ๋ถ์ ์์' ๋ฒํผ์ ํด๋ฆญํ์ธ์!</p>
|
| 640 |
<p style="font-size: 14px;">๊ฐ์ ๋ ๋ฐฉ์์ผ๋ก <strong>15-30์ด</strong>๋ง์ ๋ถ์์ด ์๋ฃ๋ฉ๋๋ค.</p>
|
| 641 |
+
{'<p style="font-size: 12px; color: #666;">Playwright ๋ธ๋ผ์ฐ์ ํ๊ฒฝ</p>' if IS_HUGGINGFACE else ''}
|
| 642 |
</div>
|
| 643 |
"""
|
| 644 |
)
|
|
|
|
| 682 |
<p><strong>์ฌ์ฉ๋ฒ:</strong> ์ํ๋ ์ค์ ์ ์ ํํ๊ณ ๋ถ์ ๋ฒํผ์ ํด๋ฆญํ์ธ์</p>
|
| 683 |
<p><strong>์๋:</strong> ์ ์ฐจ์ ์ผ๋ก ๊ฐ์ ์์ ์
๋๋ค.</p>
|
| 684 |
<p><strong>Powered by:</strong> Google Trends + Claude AI + ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ</p>
|
| 685 |
+
{'<p style="font-size: 12px;">Running on Hugging Face Spaces with Playwright Browser</p>' if IS_HUGGINGFACE else ''}
|
| 686 |
</div>
|
| 687 |
""")
|
| 688 |
|
packages.txt
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
wget
|
| 2 |
-
unzip
|
| 3 |
-
curl
|
| 4 |
-
gnupg
|
| 5 |
-
ca-certificates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
gradio==4.44.0
|
| 2 |
anthropic==0.34.0
|
| 3 |
-
|
| 4 |
-
webdriver-manager==4.0.2
|
| 5 |
python-dotenv==1.0.0
|
| 6 |
Pillow==10.4.0
|
|
|
|
| 1 |
gradio==4.44.0
|
| 2 |
anthropic==0.34.0
|
| 3 |
+
playwright==1.40.0
|
|
|
|
| 4 |
python-dotenv==1.0.0
|
| 5 |
Pillow==10.4.0
|