Update app.py
Browse files
app.py
CHANGED
|
@@ -48,41 +48,34 @@ def get_claude_client():
|
|
| 48 |
return anthropic.Anthropic(api_key=api_key)
|
| 49 |
|
| 50 |
def install_browsers():
|
| 51 |
-
"""Playwright ๋ธ๋ผ์ฐ์ ๋ฐ ํ๊ธ ํฐํธ ์ค์น"""
|
| 52 |
try:
|
| 53 |
-
print("Playwright ๋ธ๋ผ์ฐ์ ์ค์น ์ค...")
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print("ํ๊ธ ํฐํธ ์ค์น ์ค...")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
os.system("apt-get install -y fonts-nanum fonts-nanum-coding fonts-nanum-extra")
|
| 60 |
-
os.system("fc-cache -fv")
|
| 61 |
-
|
| 62 |
-
print("Playwright ๋ธ๋ผ์ฐ์ ๋ฐ ํฐํธ ์ค์น ์๋ฃ!")
|
| 63 |
return True
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"
|
| 66 |
return False
|
| 67 |
|
| 68 |
def init_browser():
|
| 69 |
-
"""Playwright ๋ธ๋ผ์ฐ์ ๋ฅผ ์ด๊ธฐํํ๊ณ Google Trends ํ์ด์ง๋ก ์ด๋"""
|
| 70 |
global browser, page, playwright_instance, is_browser_ready, browser_thread_id
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
# ํ์ฌ ์ค๋ ๋ ID ์ ์ฅ
|
| 74 |
browser_thread_id = threading.current_thread().ident
|
| 75 |
-
print(f"๋ธ๋ผ์ฐ์
|
| 76 |
-
|
| 77 |
-
print("Playwright ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค...")
|
| 78 |
|
| 79 |
-
# ๋ธ๋ผ์ฐ์ ์ค์น
|
| 80 |
install_browsers()
|
| 81 |
|
| 82 |
# Playwright ์ธ์คํด์ค ์์ฑ
|
| 83 |
playwright_instance = sync_playwright().start()
|
| 84 |
|
| 85 |
-
# ๋ธ๋ผ์ฐ์ ์คํ
|
| 86 |
browser = playwright_instance.chromium.launch(
|
| 87 |
headless=True,
|
| 88 |
args=[
|
|
@@ -91,52 +84,44 @@ def init_browser():
|
|
| 91 |
'--disable-gpu',
|
| 92 |
'--disable-extensions',
|
| 93 |
'--disable-web-security',
|
| 94 |
-
'--allow-running-insecure-content',
|
| 95 |
'--disable-features=VizDisplayCompositor',
|
| 96 |
'--disable-background-timer-throttling',
|
| 97 |
-
'--disable-backgrounding-occluded-windows',
|
| 98 |
'--disable-renderer-backgrounding',
|
| 99 |
-
'--disable-features=TranslateUI',
|
| 100 |
-
'--disable-ipc-flooding-protection',
|
| 101 |
'--disable-plugins',
|
| 102 |
-
'--disable-images',
|
|
|
|
|
|
|
| 103 |
'--force-device-scale-factor=1',
|
| 104 |
-
'--
|
| 105 |
-
'--
|
| 106 |
]
|
| 107 |
)
|
| 108 |
|
| 109 |
# ์ ํ์ด์ง ์์ฑ
|
| 110 |
page = browser.new_page()
|
| 111 |
|
| 112 |
-
# ๋ทฐํฌํธ ์ค์
|
| 113 |
-
page.set_viewport_size({"width":
|
| 114 |
|
| 115 |
-
#
|
| 116 |
page.set_extra_http_headers({
|
| 117 |
-
"
|
| 118 |
-
"Accept-Language": "ko-KR,ko;q=0.9,en;q=0.8"
|
| 119 |
})
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
""")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# ์ถ๊ฐ ๋ก๋ฉ ์๊ฐ
|
| 132 |
-
time.sleep(5)
|
| 133 |
|
| 134 |
is_browser_ready = True
|
| 135 |
-
print("๋ธ๋ผ์ฐ์
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
print(f"๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์คํจ: {str(e)}")
|
| 139 |
-
print(traceback.format_exc())
|
| 140 |
is_browser_ready = False
|
| 141 |
cleanup_browser()
|
| 142 |
|
|
@@ -177,16 +162,15 @@ def cleanup_browser():
|
|
| 177 |
print(f"๋ธ๋ผ์ฐ์ ์ ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|
| 178 |
|
| 179 |
def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
| 180 |
-
"""Google Trends ํํฐ ์ ์ฉ"""
|
| 181 |
global page
|
| 182 |
|
| 183 |
-
# ๋ธ๋ผ์ฐ์ ์ค๋น ์ํ ํ์ธ
|
| 184 |
if not ensure_browser_ready():
|
| 185 |
print("๋ธ๋ผ์ฐ์ ์ค๋น ์คํจ")
|
| 186 |
return False
|
| 187 |
|
| 188 |
try:
|
| 189 |
-
print(f"ํํฐ ์ ์ฉ: ์ง์ญ={geo}")
|
| 190 |
|
| 191 |
# ์ง์ญ ์ค์ ์ ์ํ URL ๊ตฌ์ฑ
|
| 192 |
geo_map = {
|
|
@@ -198,119 +182,61 @@ def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
|
| 198 |
geo_param = geo_map.get(geo, "KR")
|
| 199 |
url = f"{base_url}?geo={geo_param}"
|
| 200 |
|
| 201 |
-
print(f"
|
| 202 |
-
|
|
|
|
| 203 |
|
| 204 |
-
#
|
| 205 |
-
time.sleep(
|
| 206 |
|
| 207 |
-
|
| 208 |
-
try:
|
| 209 |
-
# ์๊ฐ ๋ฒ์ ํํฐ ์๋
|
| 210 |
-
if time_range != "24์๊ฐ":
|
| 211 |
-
time_selector = page.locator('[data-bucket="0"]').first
|
| 212 |
-
if time_selector.is_visible():
|
| 213 |
-
time_selector.click()
|
| 214 |
-
time.sleep(1)
|
| 215 |
-
|
| 216 |
-
# ์๊ฐ ์ต์
๋ค ์ค ์ ํ
|
| 217 |
-
if time_range == "7์ผ":
|
| 218 |
-
page.locator('text="Past 7 days"').first.click()
|
| 219 |
-
elif time_range == "30์ผ":
|
| 220 |
-
page.locator('text="Past 30 days"').first.click()
|
| 221 |
-
time.sleep(2)
|
| 222 |
-
|
| 223 |
-
# ์นดํ
๊ณ ๋ฆฌ ํํฐ ์๋
|
| 224 |
-
if category != "๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ":
|
| 225 |
-
category_selector = page.locator('[data-bucket="2"]').first
|
| 226 |
-
if category_selector.is_visible():
|
| 227 |
-
category_selector.click()
|
| 228 |
-
time.sleep(1)
|
| 229 |
-
|
| 230 |
-
# ์นดํ
๊ณ ๋ฆฌ ๋งคํ
|
| 231 |
-
category_map = {
|
| 232 |
-
"์ํฐํ
์ธ๋จผํธ": "Entertainment",
|
| 233 |
-
"์คํฌ์ธ ": "Sports",
|
| 234 |
-
"๋น์ฆ๋์ค": "Business",
|
| 235 |
-
"๊ณผํ๊ธฐ์ ": "Science & Tech",
|
| 236 |
-
"๊ฑด๊ฐ": "Health"
|
| 237 |
-
}
|
| 238 |
-
|
| 239 |
-
if category in category_map:
|
| 240 |
-
page.locator(f'text="{category_map[category]}"').first.click()
|
| 241 |
-
time.sleep(2)
|
| 242 |
-
|
| 243 |
-
except Exception as filter_error:
|
| 244 |
-
print(f"ํํฐ ์์ ํด๋ฆญ ์คํจ (๊ณ์ ์งํ): {filter_error}")
|
| 245 |
-
|
| 246 |
-
print("ํํฐ ์ ์ฉ ์๋ฃ")
|
| 247 |
return True
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
print(f"ํํฐ ์ ์ฉ ์คํจ: {str(e)}")
|
| 251 |
-
|
| 252 |
-
return
|
| 253 |
|
| 254 |
def capture_screenshot() -> Optional[Image.Image]:
|
| 255 |
-
"""ํ์ฌ ํ์ด์ง์ ์คํฌ๋ฆฐ์ท ์บก์ฒ (
|
| 256 |
global page
|
| 257 |
|
| 258 |
-
# ๋ธ๋ผ์ฐ์ ์ค๋น ์ํ ํ์ธ
|
| 259 |
if not ensure_browser_ready():
|
| 260 |
print("๋ธ๋ผ์ฐ์ ์ค๋น ์คํจ")
|
| 261 |
return None
|
| 262 |
|
| 263 |
try:
|
| 264 |
-
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค...")
|
| 265 |
|
| 266 |
-
#
|
| 267 |
-
time.sleep(
|
| 268 |
|
| 269 |
-
#
|
| 270 |
try:
|
| 271 |
-
#
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
'.trends-table', # ํธ๋ ๋ ํ
์ด๋ธ
|
| 275 |
-
'.trending-searches', # ํธ๋ ๋ ๊ฒ์์ด ์์ญ
|
| 276 |
-
'[role="main"]', # ๋ฉ์ธ ์ฝํ
์ธ ์์ญ
|
| 277 |
-
'main' # ๊ธฐ๋ณธ ๋ฉ์ธ ํ๊ทธ
|
| 278 |
-
]
|
| 279 |
-
|
| 280 |
-
element = None
|
| 281 |
-
for selector in selectors:
|
| 282 |
-
try:
|
| 283 |
-
element = page.locator(selector).first
|
| 284 |
-
if element.is_visible():
|
| 285 |
-
print(f"์บก์ฒ ์์ญ ๋ฐ๊ฒฌ: {selector}")
|
| 286 |
-
break
|
| 287 |
-
except:
|
| 288 |
-
continue
|
| 289 |
-
|
| 290 |
-
if element and element.is_visible():
|
| 291 |
-
# ํน์ ์์ญ๋ง ์คํฌ๋ฆฐ์ท
|
| 292 |
screenshot_bytes = element.screenshot()
|
| 293 |
-
print("
|
| 294 |
else:
|
| 295 |
-
# ๋์:
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
-
except Exception
|
| 300 |
-
|
| 301 |
-
screenshot_bytes = page.screenshot(
|
|
|
|
| 302 |
|
| 303 |
screenshot = Image.open(io.BytesIO(screenshot_bytes))
|
| 304 |
-
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ")
|
| 305 |
return screenshot
|
| 306 |
|
| 307 |
except Exception as e:
|
| 308 |
print(f"์คํฌ๋ฆฐ์ท ์บก์ฒ ์คํจ: {str(e)}")
|
| 309 |
-
print(traceback.format_exc())
|
| 310 |
return None
|
| 311 |
|
| 312 |
def analyze_with_claude(image: Image.Image) -> str:
|
| 313 |
-
"""Claude API๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ๋ถ์"""
|
| 314 |
try:
|
| 315 |
client = get_claude_client()
|
| 316 |
|
|
@@ -319,22 +245,16 @@ def analyze_with_claude(image: Image.Image) -> str:
|
|
| 319 |
image.save(buffer, format="PNG")
|
| 320 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 321 |
|
| 322 |
-
#
|
| 323 |
-
prompt = """
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
2. ๊ฐ ๊ฒ์์ด์ ์ฆ๊ฐ ์ถ์ธ๋ ํน์ด์ฌํญ
|
| 329 |
-
3. ๊ฒ์์ด๋ค์์ ๋ฐ๊ฒฌ๋๋ ์ฃผ์ ํธ๋ ๋๋ ํจํด
|
| 330 |
-
4. ์์ฌ์ /๋ฌธํ์ ๋งฅ๋ฝ์์์ ํด์
|
| 331 |
-
|
| 332 |
-
ํ๊ตญ์ด๋ก ์์ธํ๊ณ ๊ตฌ์ฒด์ ์ผ๋ก ๋ถ์ํด์ฃผ์ธ์.
|
| 333 |
-
"""
|
| 334 |
|
| 335 |
response = client.messages.create(
|
| 336 |
model="claude-3-5-sonnet-20241022",
|
| 337 |
-
max_tokens=
|
| 338 |
messages=[
|
| 339 |
{
|
| 340 |
"role": "user",
|
|
@@ -364,78 +284,88 @@ def analyze_with_claude(image: Image.Image) -> str:
|
|
| 364 |
return error_msg
|
| 365 |
|
| 366 |
def reinit_browser():
|
| 367 |
-
"""๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ"""
|
| 368 |
-
global
|
| 369 |
-
|
| 370 |
-
print("๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์์...")
|
| 371 |
-
|
| 372 |
-
# ๊ธฐ์กด ๋ธ๋ผ์ฐ์ ์ ๋ฆฌ
|
| 373 |
-
cleanup_browser()
|
| 374 |
-
is_browser_ready = False
|
| 375 |
-
|
| 376 |
-
# ์๋ก ์ด๊ธฐํ
|
| 377 |
-
init_browser()
|
| 378 |
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
def refresh_trends_page():
|
| 385 |
-
"""Google Trends ํ์ด์ง ์๋ก๊ณ ์นจ"""
|
| 386 |
global page
|
| 387 |
|
| 388 |
if not ensure_browser_ready():
|
| 389 |
return "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค."
|
| 390 |
|
| 391 |
try:
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
| 395 |
except Exception as e:
|
| 396 |
return f"ํ์ด์ง ์๋ก๊ณ ์นจ ์คํจ: {str(e)}"
|
| 397 |
|
| 398 |
def main_process_screenshot(geo: str, time_range: str, category: str) -> Tuple[Optional[Image.Image], str]:
|
| 399 |
-
"""์คํฌ๋ฆฐ์ท ์บก์ฒ๋ง ์ํํ๋ ํจ์"""
|
| 400 |
try:
|
| 401 |
-
# 1. ๋ธ๋ผ์ฐ์ ์ค๋น ํ์ธ
|
| 402 |
if not ensure_browser_ready():
|
| 403 |
-
return None, "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค.
|
| 404 |
|
| 405 |
-
# 2. ํํฐ ์ ์ฉ
|
| 406 |
-
print(f"
|
| 407 |
filter_success = apply_filters(geo, time_range, category)
|
| 408 |
|
|
|
|
| 409 |
if not filter_success:
|
| 410 |
-
|
| 411 |
|
| 412 |
-
# 3. ์คํฌ๋ฆฐ์ท ์บก์ฒ
|
| 413 |
-
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค...")
|
| 414 |
screenshot = capture_screenshot()
|
| 415 |
|
| 416 |
if screenshot is None:
|
| 417 |
return None, "์คํฌ๋ฆฐ์ท ์บก์ฒ์ ์คํจํ์ต๋๋ค."
|
| 418 |
|
| 419 |
-
return screenshot, "์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ!
|
| 420 |
|
| 421 |
except Exception as e:
|
| 422 |
-
error_msg = f"
|
| 423 |
print(error_msg)
|
| 424 |
-
print(traceback.format_exc())
|
| 425 |
return None, error_msg
|
| 426 |
|
| 427 |
def analyze_screenshot_with_claude(screenshot: Image.Image) -> str:
|
| 428 |
-
"""Claude API๋ก ์คํฌ๋ฆฐ์ท ๋ถ์"""
|
| 429 |
if screenshot is None:
|
| 430 |
return "๋ถ์ํ ์คํฌ๋ฆฐ์ท์ด ์์ต๋๋ค."
|
| 431 |
|
| 432 |
try:
|
| 433 |
-
print("Claude
|
| 434 |
analysis_result = analyze_with_claude(screenshot)
|
| 435 |
return analysis_result
|
| 436 |
|
| 437 |
except Exception as e:
|
| 438 |
-
error_msg = f"Claude ๋ถ์
|
| 439 |
print(error_msg)
|
| 440 |
return error_msg
|
| 441 |
|
|
@@ -473,30 +403,30 @@ def main_process(geo: str, time_range: str, category: str) -> Tuple[Optional[Ima
|
|
| 473 |
return None, error_msg
|
| 474 |
|
| 475 |
def check_browser_status():
|
| 476 |
-
"""๋ธ๋ผ์ฐ์ ์ํ ํ์ธ"""
|
| 477 |
global is_browser_ready, page, browser_thread_id
|
| 478 |
|
| 479 |
current_thread_id = threading.current_thread().ident
|
| 480 |
|
| 481 |
if not page:
|
| 482 |
-
return "๋ธ๋ผ์ฐ์
|
| 483 |
|
| 484 |
if not is_browser_ready:
|
| 485 |
-
return "๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค
|
| 486 |
|
| 487 |
try:
|
| 488 |
current_url = page.url
|
| 489 |
-
return f"๋ธ๋ผ์ฐ์ ์ค๋น
|
| 490 |
except Exception as e:
|
| 491 |
-
return f"๋ธ๋ผ์ฐ์ ์ฐ๊ฒฐ
|
| 492 |
|
| 493 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 494 |
def create_interface():
|
| 495 |
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
| 496 |
|
| 497 |
-
with gr.Blocks(title="Google Trends
|
| 498 |
-
gr.Markdown("# Google Trends
|
| 499 |
-
gr.Markdown("Google Trends
|
| 500 |
|
| 501 |
# ์ํ ์ ์ฅ์ ์ํ State ์ปดํฌ๋ํธ
|
| 502 |
screenshot_state = gr.State(None)
|
|
@@ -522,27 +452,27 @@ def create_interface():
|
|
| 522 |
)
|
| 523 |
|
| 524 |
with gr.Row():
|
| 525 |
-
capture_btn = gr.Button("
|
| 526 |
-
analyze_btn = gr.Button("๐ค
|
| 527 |
|
| 528 |
with gr.Row():
|
| 529 |
-
refresh_btn = gr.Button("๐
|
| 530 |
-
reinit_btn = gr.Button("โ๏ธ
|
| 531 |
-
status_btn = gr.Button("๐
|
| 532 |
|
| 533 |
with gr.Column(scale=2):
|
| 534 |
screenshot_output = gr.Image(label="Google Trends ์คํฌ๋ฆฐ์ท")
|
| 535 |
analysis_output = gr.Textbox(
|
| 536 |
-
label="Claude AI ๋ถ์ ๊ฒฐ๊ณผ",
|
| 537 |
-
lines=
|
| 538 |
-
max_lines=
|
| 539 |
-
placeholder="
|
| 540 |
)
|
| 541 |
status_output = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
| 542 |
|
| 543 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 544 |
|
| 545 |
-
#
|
| 546 |
def on_capture_click(geo, time_range, category):
|
| 547 |
screenshot, message = main_process_screenshot(geo, time_range, category)
|
| 548 |
if screenshot is not None:
|
|
@@ -557,10 +487,10 @@ def create_interface():
|
|
| 557 |
outputs=[screenshot_output, screenshot_state, status_output, analyze_btn]
|
| 558 |
)
|
| 559 |
|
| 560 |
-
#
|
| 561 |
def on_analyze_click(screenshot):
|
| 562 |
if screenshot is None:
|
| 563 |
-
return "๋ถ์ํ ์คํฌ๋ฆฐ์ท์ด ์์ต๋๋ค. ๋จผ์ '
|
| 564 |
|
| 565 |
analysis_result = analyze_screenshot_with_claude(screenshot)
|
| 566 |
return analysis_result
|
|
@@ -590,12 +520,12 @@ def create_interface():
|
|
| 590 |
return interface
|
| 591 |
|
| 592 |
def main():
|
| 593 |
-
"""๋ฉ์ธ ํจ์"""
|
| 594 |
print("=" * 50)
|
| 595 |
-
print("Google Trends
|
| 596 |
print("=" * 50)
|
| 597 |
|
| 598 |
-
# API ํค
|
| 599 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 600 |
if not api_key:
|
| 601 |
print("โ ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
|
@@ -603,13 +533,17 @@ def main():
|
|
| 603 |
else:
|
| 604 |
print("โ Claude API ํค ํ์ธ๋จ")
|
| 605 |
|
| 606 |
-
print("โ Gradio
|
| 607 |
|
| 608 |
# ํ๊ฒฝ ํ์ธ
|
| 609 |
if os.getenv("SPACE_ID"):
|
| 610 |
print("โ Hugging Face Space ํ๊ฒฝ์์ ์คํ ์ค...")
|
| 611 |
|
| 612 |
-
print("โ ๋ธ๋ผ์ฐ์
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
# ์ข
๋ฃ ์ ๋ธ๋ผ์ฐ์ ์ ๋ฆฌ
|
| 615 |
atexit.register(cleanup_browser)
|
|
@@ -617,13 +551,15 @@ def main():
|
|
| 617 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ
|
| 618 |
interface = create_interface()
|
| 619 |
|
| 620 |
-
|
|
|
|
|
|
|
| 621 |
interface.launch(
|
| 622 |
server_name="0.0.0.0",
|
| 623 |
server_port=7860,
|
| 624 |
share=False,
|
| 625 |
show_error=True,
|
| 626 |
-
quiet=
|
| 627 |
)
|
| 628 |
|
| 629 |
if __name__ == "__main__":
|
|
|
|
| 48 |
return anthropic.Anthropic(api_key=api_key)
|
| 49 |
|
| 50 |
def install_browsers():
|
| 51 |
+
"""Playwright ๋ธ๋ผ์ฐ์ ๋ฐ ํ๊ธ ํฐํธ ์ค์น (ํ๋ฒ๋ง ์คํ)"""
|
| 52 |
try:
|
| 53 |
+
print("Playwright ๋ธ๋ผ์ฐ์ ๋น ๋ฅธ ์ค์น ์ค...")
|
| 54 |
+
# ๋ณ๋ ฌ ์ค์น๋ก ์๋ ํฅ์
|
| 55 |
+
os.system("playwright install chromium --force")
|
| 56 |
+
print("ํ๊ธ ํฐํธ ๋น ๋ฅธ ์ค์น ์ค...")
|
| 57 |
+
os.system("apt-get install -y fonts-nanum fonts-noto-cjk &")
|
| 58 |
+
print("๋ธ๋ผ์ฐ์ ๋ฐ ํฐํธ ์ค์น ์๋ฃ!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return True
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"์ค์น ์คํจ: {e}")
|
| 62 |
return False
|
| 63 |
|
| 64 |
def init_browser():
|
| 65 |
+
"""Playwright ๋ธ๋ผ์ฐ์ ๋ฅผ ์ด๊ธฐํํ๊ณ Google Trends ํ์ด์ง๋ก ์ด๋ (WarmUp ์ ๋ต)"""
|
| 66 |
global browser, page, playwright_instance, is_browser_ready, browser_thread_id
|
| 67 |
|
| 68 |
try:
|
|
|
|
| 69 |
browser_thread_id = threading.current_thread().ident
|
| 70 |
+
print(f"๋ธ๋ผ์ฐ์ WarmUp ์์ (์ค๋ ๋: {browser_thread_id})")
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# ๋น ๋ฅธ ๋ธ๋ผ์ฐ์ ์ค์น
|
| 73 |
install_browsers()
|
| 74 |
|
| 75 |
# Playwright ์ธ์คํด์ค ์์ฑ
|
| 76 |
playwright_instance = sync_playwright().start()
|
| 77 |
|
| 78 |
+
# ์๋ ์ต์ ํ๋ ๋ธ๋ผ์ฐ์ ์คํ
|
| 79 |
browser = playwright_instance.chromium.launch(
|
| 80 |
headless=True,
|
| 81 |
args=[
|
|
|
|
| 84 |
'--disable-gpu',
|
| 85 |
'--disable-extensions',
|
| 86 |
'--disable-web-security',
|
|
|
|
| 87 |
'--disable-features=VizDisplayCompositor',
|
| 88 |
'--disable-background-timer-throttling',
|
|
|
|
| 89 |
'--disable-renderer-backgrounding',
|
|
|
|
|
|
|
| 90 |
'--disable-plugins',
|
| 91 |
+
'--disable-images', # ์ด๋ฏธ์ง ๋ก๋ฉ ๋นํ์ฑํ๋ก ์๋ ํฅ์
|
| 92 |
+
'--disable-javascript', # JS ๋นํ์ฑํ๋ก ์๋ ํฅ์
|
| 93 |
+
'--disable-css', # CSS ๋นํ์ฑํ๋ก ์๋ ํฅ์
|
| 94 |
'--force-device-scale-factor=1',
|
| 95 |
+
'--aggressive-cache-discard',
|
| 96 |
+
'--memory-pressure-off'
|
| 97 |
]
|
| 98 |
)
|
| 99 |
|
| 100 |
# ์ ํ์ด์ง ์์ฑ
|
| 101 |
page = browser.new_page()
|
| 102 |
|
| 103 |
+
# ๋ทฐํฌํธ ์ค์ (์๊ฒ ์ค์ ํ์ฌ ์๋ ํฅ์)
|
| 104 |
+
page.set_viewport_size({"width": 1200, "height": 800})
|
| 105 |
|
| 106 |
+
# ๋น ๋ฅธ ํค๋ ์ค์
|
| 107 |
page.set_extra_http_headers({
|
| 108 |
+
"Accept-Language": "ko-KR,ko;q=0.9"
|
|
|
|
| 109 |
})
|
| 110 |
|
| 111 |
+
print("Google Trends ํ์ด์ง๋ก ๋น ๋ฅธ ์ด๋ ์ค...")
|
| 112 |
+
# ๋น ๋ฅธ ๋ก๋ฉ์ ์ํด domcontentloaded ์ฌ์ฉ, ํ์์์ ๋จ์ถ
|
| 113 |
+
page.goto("https://trends.google.com/trending?geo=KR",
|
| 114 |
+
wait_until="domcontentloaded",
|
| 115 |
+
timeout=15000)
|
|
|
|
| 116 |
|
| 117 |
+
# ์ต์ ๋๊ธฐ ์๊ฐ์ผ๋ก ๋จ์ถ
|
| 118 |
+
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
is_browser_ready = True
|
| 121 |
+
print("๋ธ๋ผ์ฐ์ WarmUp ์๏ฟฝ๏ฟฝ๏ฟฝ! (๊ณ ์ ๋ชจ๋)")
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
print(f"๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์คํจ: {str(e)}")
|
|
|
|
| 125 |
is_browser_ready = False
|
| 126 |
cleanup_browser()
|
| 127 |
|
|
|
|
| 162 |
print(f"๋ธ๋ผ์ฐ์ ์ ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|
| 163 |
|
| 164 |
def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
| 165 |
+
"""Google Trends ํํฐ ์ ์ฉ (๊ณ ์ ๋ชจ๋)"""
|
| 166 |
global page
|
| 167 |
|
|
|
|
| 168 |
if not ensure_browser_ready():
|
| 169 |
print("๋ธ๋ผ์ฐ์ ์ค๋น ์คํจ")
|
| 170 |
return False
|
| 171 |
|
| 172 |
try:
|
| 173 |
+
print(f"๊ณ ์ ํํฐ ์ ์ฉ: ์ง์ญ={geo}")
|
| 174 |
|
| 175 |
# ์ง์ญ ์ค์ ์ ์ํ URL ๊ตฌ์ฑ
|
| 176 |
geo_map = {
|
|
|
|
| 182 |
geo_param = geo_map.get(geo, "KR")
|
| 183 |
url = f"{base_url}?geo={geo_param}"
|
| 184 |
|
| 185 |
+
print(f"๋น ๋ฅธ ์ด๋: {url}")
|
| 186 |
+
# ๋น ๋ฅธ ๋ก๋ฉ์ ์ํด domcontentloaded ์ฌ์ฉ, ํ์์์ ๋ํญ ๋จ์ถ
|
| 187 |
+
page.goto(url, wait_until="domcontentloaded", timeout=10000)
|
| 188 |
|
| 189 |
+
# ์ต์ ๋๊ธฐ ์๊ฐ
|
| 190 |
+
time.sleep(1)
|
| 191 |
|
| 192 |
+
print("๊ณ ์ ํํฐ ์ ์ฉ ์๋ฃ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
return True
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
print(f"ํํฐ ์ ์ฉ ์คํจ: {str(e)}")
|
| 197 |
+
# ์คํจํด๋ ํ์ฌ ํ์ด์ง์์ ์คํฌ๋ฆฐ์ท ์๋
|
| 198 |
+
return True # ์คํจํด๋ ๊ณ์ ์งํ
|
| 199 |
|
| 200 |
def capture_screenshot() -> Optional[Image.Image]:
|
| 201 |
+
"""ํ์ฌ ํ์ด์ง์ ์คํฌ๋ฆฐ์ท ์บก์ฒ (๊ณ ์ ๋ชจ๋)"""
|
| 202 |
global page
|
| 203 |
|
|
|
|
| 204 |
if not ensure_browser_ready():
|
| 205 |
print("๋ธ๋ผ์ฐ์ ์ค๋น ์คํจ")
|
| 206 |
return None
|
| 207 |
|
| 208 |
try:
|
| 209 |
+
print("๊ณ ์ ์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค...")
|
| 210 |
|
| 211 |
+
# ๋๊ธฐ ์๊ฐ ์ต์ํ
|
| 212 |
+
time.sleep(0.5)
|
| 213 |
|
| 214 |
+
# ๋น ๋ฅธ ์คํฌ๋ฆฐ์ท์ ์ํด ํน์ ์์ญ๋ง ์บก์ฒ
|
| 215 |
try:
|
| 216 |
+
# ํธ๋ ๋ ํ
์ด๋ธ ์์ญ ์ฐพ๊ธฐ (๋น ๋ฅธ ์
๋ ํฐ๋ง ์ฌ์ฉ)
|
| 217 |
+
element = page.locator('main').first
|
| 218 |
+
if element.is_visible(timeout=2000):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
screenshot_bytes = element.screenshot()
|
| 220 |
+
print("๋ฉ์ธ ์์ญ ๊ณ ์ ์บก์ฒ ์๋ฃ")
|
| 221 |
else:
|
| 222 |
+
# ๋์: ํ์ด์ง ์ค์ ์์ญ๋ง ๋น ๋ฅด๊ฒ ์บก์ฒ
|
| 223 |
+
screenshot_bytes = page.screenshot(clip={"x": 0, "y": 150, "width": 1200, "height": 600})
|
| 224 |
+
print("์ค์ ์์ญ ๊ณ ์ ์บก์ฒ ์๋ฃ")
|
| 225 |
|
| 226 |
+
except Exception:
|
| 227 |
+
# ์ต์ข
๋์: ์ ์ฒด ํ์ด์ง ์บก์ฒ
|
| 228 |
+
screenshot_bytes = page.screenshot()
|
| 229 |
+
print("์ ์ฒด ํ์ด์ง ๊ณ ์ ์บก์ฒ ์๋ฃ")
|
| 230 |
|
| 231 |
screenshot = Image.open(io.BytesIO(screenshot_bytes))
|
|
|
|
| 232 |
return screenshot
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
print(f"์คํฌ๋ฆฐ์ท ์บก์ฒ ์คํจ: {str(e)}")
|
|
|
|
| 236 |
return None
|
| 237 |
|
| 238 |
def analyze_with_claude(image: Image.Image) -> str:
|
| 239 |
+
"""Claude API๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ๋ถ์ (๊ณ ์ ๋ชจ๋)"""
|
| 240 |
try:
|
| 241 |
client = get_claude_client()
|
| 242 |
|
|
|
|
| 245 |
image.save(buffer, format="PNG")
|
| 246 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 247 |
|
| 248 |
+
# ์ถ์ฝ๋ ํ๋กฌํํธ (์๋ ํฅ์)
|
| 249 |
+
prompt = """Google Trends ์คํฌ๋ฆฐ์ท์ ๊ฐ๋จํ ๋ถ์ํด์ฃผ์ธ์:
|
| 250 |
+
1. ์์ 5๊ฐ ๊ฒ์์ด์ ์์
|
| 251 |
+
2. ์ฃผ์ ํธ๋ ๋ ํจํด
|
| 252 |
+
3. ํน์ด์ฌํญ
|
| 253 |
+
ํ๊ตญ์ด๋ก ๊ฐ๋จ๋ช
๋ฃํ๊ฒ ๋ต๋ณํ์ธ์."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
response = client.messages.create(
|
| 256 |
model="claude-3-5-sonnet-20241022",
|
| 257 |
+
max_tokens=800, # ํ ํฐ ์ ๋ํญ ์ถ์
|
| 258 |
messages=[
|
| 259 |
{
|
| 260 |
"role": "user",
|
|
|
|
| 284 |
return error_msg
|
| 285 |
|
| 286 |
def reinit_browser():
|
| 287 |
+
"""๋ธ๋ผ์ฐ์ ๊ฒฝ๋ ์ฌ์ด๊ธฐํ (๊ธฐ์กด ๋ธ๋ผ์ฐ์ ์ ์ง)"""
|
| 288 |
+
global page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
+
try:
|
| 291 |
+
print("๊ฒฝ๋ ๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์์...")
|
| 292 |
+
|
| 293 |
+
if page:
|
| 294 |
+
# ๋ธ๋ผ์ฐ์ ๋ฅผ ์์ ํ ์ฌ์์ํ์ง ์๊ณ ํ์ด์ง๋ง ์๋ก๊ณ ์นจ
|
| 295 |
+
page.goto("https://trends.google.com/trending?geo=KR",
|
| 296 |
+
wait_until="domcontentloaded",
|
| 297 |
+
timeout=10000)
|
| 298 |
+
time.sleep(1)
|
| 299 |
+
return "๋ธ๋ผ์ฐ์ ๊ฒฝ๋ ์ฌ์ด๊ธฐํ ์๋ฃ! (๊ธฐ์กด ๋ธ๋ผ์ฐ์ ์ ์ง)"
|
| 300 |
+
else:
|
| 301 |
+
# ํ์ด์ง๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง ์์ ์ฌ์ด๊ธฐํ
|
| 302 |
+
init_browser()
|
| 303 |
+
if is_browser_ready:
|
| 304 |
+
return "๋ธ๋ผ์ฐ์ ์์ ์ฌ์ด๊ธฐํ ์๋ฃ!"
|
| 305 |
+
else:
|
| 306 |
+
return "๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์คํจ"
|
| 307 |
+
|
| 308 |
+
except Exception as e:
|
| 309 |
+
print(f"์ฌ์ด๊ธฐํ ์คํจ: {e}")
|
| 310 |
+
return f"์ฌ์ด๊ธฐํ ์คํจ: {str(e)}"
|
| 311 |
|
| 312 |
def refresh_trends_page():
|
| 313 |
+
"""Google Trends ํ์ด์ง ๋น ๋ฅธ ์๋ก๊ณ ์นจ"""
|
| 314 |
global page
|
| 315 |
|
| 316 |
if not ensure_browser_ready():
|
| 317 |
return "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค."
|
| 318 |
|
| 319 |
try:
|
| 320 |
+
print("๋น ๋ฅธ ํ์ด์ง ์๋ก๊ณ ์นจ...")
|
| 321 |
+
# reload() ๋์ ํ์ฌ URL๋ก ๋น ๋ฅด๊ฒ ์ด๋
|
| 322 |
+
current_url = page.url
|
| 323 |
+
page.goto(current_url, wait_until="domcontentloaded", timeout=8000)
|
| 324 |
+
time.sleep(0.5)
|
| 325 |
+
return "ํ์ด์ง ๋น ๋ฅธ ์๋ก๊ณ ์นจ ์๋ฃ!"
|
| 326 |
except Exception as e:
|
| 327 |
return f"ํ์ด์ง ์๋ก๊ณ ์นจ ์คํจ: {str(e)}"
|
| 328 |
|
| 329 |
def main_process_screenshot(geo: str, time_range: str, category: str) -> Tuple[Optional[Image.Image], str]:
|
| 330 |
+
"""์คํฌ๋ฆฐ์ท ์บก์ฒ๋ง ์ํํ๋ ํจ์ (๊ณ ์ ๋ชจ๋)"""
|
| 331 |
try:
|
| 332 |
+
# 1. ๋ธ๋ผ์ฐ์ ์ค๋น ํ์ธ (๋น ๋ฅธ ํ์ธ)
|
| 333 |
if not ensure_browser_ready():
|
| 334 |
+
return None, "๋ธ๋ผ์ฐ์ ๊ฐ ์ค๋น๋์ง ์์์ต๋๋ค."
|
| 335 |
|
| 336 |
+
# 2. ๊ณ ์ ํํฐ ์ ์ฉ
|
| 337 |
+
print(f"๊ณ ์ ์ฒ๋ฆฌ: ์ง์ญ={geo}")
|
| 338 |
filter_success = apply_filters(geo, time_range, category)
|
| 339 |
|
| 340 |
+
# ํํฐ ์คํจํด๋ ํ์ฌ ํ์ด์ง์์ ์คํฌ๋ฆฐ์ท ์๋
|
| 341 |
if not filter_success:
|
| 342 |
+
print("ํํฐ ์ ์ฉ ์คํจํ์ง๋ง ํ์ฌ ํ์ด์ง์์ ์คํฌ๋ฆฐ์ท ์๋")
|
| 343 |
|
| 344 |
+
# 3. ๊ณ ์ ์คํฌ๋ฆฐ์ท ์บก์ฒ
|
|
|
|
| 345 |
screenshot = capture_screenshot()
|
| 346 |
|
| 347 |
if screenshot is None:
|
| 348 |
return None, "์คํฌ๋ฆฐ์ท ์บก์ฒ์ ์คํจํ์ต๋๋ค."
|
| 349 |
|
| 350 |
+
return screenshot, "๊ณ ์ ์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ!"
|
| 351 |
|
| 352 |
except Exception as e:
|
| 353 |
+
error_msg = f"๊ณ ์ ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {str(e)}"
|
| 354 |
print(error_msg)
|
|
|
|
| 355 |
return None, error_msg
|
| 356 |
|
| 357 |
def analyze_screenshot_with_claude(screenshot: Image.Image) -> str:
|
| 358 |
+
"""Claude API๋ก ์คํฌ๋ฆฐ์ท ๋ถ์ (๊ณ ์ ๋ชจ๋)"""
|
| 359 |
if screenshot is None:
|
| 360 |
return "๋ถ์ํ ์คํฌ๋ฆฐ์ท์ด ์์ต๋๋ค."
|
| 361 |
|
| 362 |
try:
|
| 363 |
+
print("Claude ๊ณ ์ ๋ถ์ ์ค...")
|
| 364 |
analysis_result = analyze_with_claude(screenshot)
|
| 365 |
return analysis_result
|
| 366 |
|
| 367 |
except Exception as e:
|
| 368 |
+
error_msg = f"Claude ๋ถ์ ์คํจ: {str(e)}"
|
| 369 |
print(error_msg)
|
| 370 |
return error_msg
|
| 371 |
|
|
|
|
| 403 |
return None, error_msg
|
| 404 |
|
| 405 |
def check_browser_status():
|
| 406 |
+
"""๋ธ๋ผ์ฐ์ ์ํ ํ์ธ (๊ณ ์)"""
|
| 407 |
global is_browser_ready, page, browser_thread_id
|
| 408 |
|
| 409 |
current_thread_id = threading.current_thread().ident
|
| 410 |
|
| 411 |
if not page:
|
| 412 |
+
return "๋ธ๋ผ์ฐ์ ๋ฏธ์ด๊ธฐํ"
|
| 413 |
|
| 414 |
if not is_browser_ready:
|
| 415 |
+
return "๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์ค..."
|
| 416 |
|
| 417 |
try:
|
| 418 |
current_url = page.url
|
| 419 |
+
return f"๋ธ๋ผ์ฐ์ ์ค๋น์๋ฃ - URL: {current_url[:50]}..."
|
| 420 |
except Exception as e:
|
| 421 |
+
return f"๋ธ๋ผ์ฐ์ ์ฐ๊ฒฐ ๋ฌธ์ : {str(e)[:30]}..."
|
| 422 |
|
| 423 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 424 |
def create_interface():
|
| 425 |
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
| 426 |
|
| 427 |
+
with gr.Blocks(title="Google Trends ๊ณ ์ ๋ถ์๊ธฐ", theme=gr.themes.Soft()) as interface:
|
| 428 |
+
gr.Markdown("# โก Google Trends ๊ณ ์ ๋ถ์๊ธฐ")
|
| 429 |
+
gr.Markdown("**์ด๊ณ ์ ๋ชจ๋**: Google Trends ์ค์๊ฐ ๊ฒ์์ด๋ฅผ ๋น ๋ฅด๊ฒ ์บก์ฒํ๊ณ AI ๋ถ์ํฉ๋๋ค.")
|
| 430 |
|
| 431 |
# ์ํ ์ ์ฅ์ ์ํ State ์ปดํฌ๋ํธ
|
| 432 |
screenshot_state = gr.State(None)
|
|
|
|
| 452 |
)
|
| 453 |
|
| 454 |
with gr.Row():
|
| 455 |
+
capture_btn = gr.Button("โก ๊ณ ์ ์บก์ฒ", variant="primary", size="lg")
|
| 456 |
+
analyze_btn = gr.Button("๐ค AI ๋ถ์", variant="secondary", interactive=False)
|
| 457 |
|
| 458 |
with gr.Row():
|
| 459 |
+
refresh_btn = gr.Button("๐ ๋น ๋ฅธ ์๋ก๊ณ ์นจ", variant="secondary", size="sm")
|
| 460 |
+
reinit_btn = gr.Button("โ๏ธ ๊ฒฝ๋ ์ฌ์์", variant="secondary", size="sm")
|
| 461 |
+
status_btn = gr.Button("๐ ์ํ ํ์ธ", variant="secondary", size="sm")
|
| 462 |
|
| 463 |
with gr.Column(scale=2):
|
| 464 |
screenshot_output = gr.Image(label="Google Trends ์คํฌ๋ฆฐ์ท")
|
| 465 |
analysis_output = gr.Textbox(
|
| 466 |
+
label="Claude AI ๊ณ ์ ๋ถ์ ๊ฒฐ๊ณผ",
|
| 467 |
+
lines=8,
|
| 468 |
+
max_lines=15,
|
| 469 |
+
placeholder="๊ณ ์ ์บก์ฒ ํ 'AI ๋ถ์' ๋ฒํผ์ ํด๋ฆญํ์ธ์."
|
| 470 |
)
|
| 471 |
status_output = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
| 472 |
|
| 473 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 474 |
|
| 475 |
+
# ๊ณ ์ ์บก์ฒ ๋ฒํผ
|
| 476 |
def on_capture_click(geo, time_range, category):
|
| 477 |
screenshot, message = main_process_screenshot(geo, time_range, category)
|
| 478 |
if screenshot is not None:
|
|
|
|
| 487 |
outputs=[screenshot_output, screenshot_state, status_output, analyze_btn]
|
| 488 |
)
|
| 489 |
|
| 490 |
+
# AI ๋ถ์ ๋ฒํผ
|
| 491 |
def on_analyze_click(screenshot):
|
| 492 |
if screenshot is None:
|
| 493 |
+
return "๋ถ์ํ ์คํฌ๋ฆฐ์ท์ด ์์ต๋๋ค. ๋จผ์ '๊ณ ์ ์บก์ฒ' ๋ฒํผ์ ํด๋ฆญํ์ธ์."
|
| 494 |
|
| 495 |
analysis_result = analyze_screenshot_with_claude(screenshot)
|
| 496 |
return analysis_result
|
|
|
|
| 520 |
return interface
|
| 521 |
|
| 522 |
def main():
|
| 523 |
+
"""๋ฉ์ธ ํจ์ (WarmUp ์ ๋ต ์ ์ฉ)"""
|
| 524 |
print("=" * 50)
|
| 525 |
+
print("Google Trends ๊ณ ์ ๋ถ์๊ธฐ ์์!")
|
| 526 |
print("=" * 50)
|
| 527 |
|
| 528 |
+
# API ํค ํ์ธ
|
| 529 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 530 |
if not api_key:
|
| 531 |
print("โ ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
|
|
|
| 533 |
else:
|
| 534 |
print("โ Claude API ํค ํ์ธ๋จ")
|
| 535 |
|
| 536 |
+
print("โ Gradio ๊ณ ์ ์ธํฐํ์ด์ค ์ค๋น ์ค...")
|
| 537 |
|
| 538 |
# ํ๊ฒฝ ํ์ธ
|
| 539 |
if os.getenv("SPACE_ID"):
|
| 540 |
print("โ Hugging Face Space ํ๊ฒฝ์์ ์คํ ์ค...")
|
| 541 |
|
| 542 |
+
print("โ ๋ธ๋ผ์ฐ์ WarmUp ์์ (๋ฐฑ๊ทธ๋ผ์ด๋)")
|
| 543 |
+
|
| 544 |
+
# WarmUp ์ ๋ต: ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ธ๋ผ์ฐ์ ๋ฏธ๋ฆฌ ์ค๋น
|
| 545 |
+
warmup_thread = threading.Thread(target=init_browser, daemon=True)
|
| 546 |
+
warmup_thread.start()
|
| 547 |
|
| 548 |
# ์ข
๋ฃ ์ ๋ธ๋ผ์ฐ์ ์ ๋ฆฌ
|
| 549 |
atexit.register(cleanup_browser)
|
|
|
|
| 551 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ
|
| 552 |
interface = create_interface()
|
| 553 |
|
| 554 |
+
print("โ ๊ณ ์ ๋ถ์๊ธฐ ์ค๋น ์๋ฃ!")
|
| 555 |
+
|
| 556 |
+
# ๋น ๋ฅธ ์คํ์ ์ํ ์ค์
|
| 557 |
interface.launch(
|
| 558 |
server_name="0.0.0.0",
|
| 559 |
server_port=7860,
|
| 560 |
share=False,
|
| 561 |
show_error=True,
|
| 562 |
+
quiet=True # ๋ก๊ทธ ์ถ๋ ฅ ์ต์ํ
|
| 563 |
)
|
| 564 |
|
| 565 |
if __name__ == "__main__":
|