Update app.py
Browse files
app.py
CHANGED
|
@@ -48,14 +48,21 @@ 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 |
os.system("playwright install chromium")
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return True
|
| 57 |
except Exception as e:
|
| 58 |
-
print(f"๋ธ๋ผ์ฐ์ ์ค์น ์คํจ: {e}")
|
| 59 |
return False
|
| 60 |
|
| 61 |
def init_browser():
|
|
@@ -92,7 +99,10 @@ def init_browser():
|
|
| 92 |
'--disable-features=TranslateUI',
|
| 93 |
'--disable-ipc-flooding-protection',
|
| 94 |
'--disable-plugins',
|
| 95 |
-
'--disable-images'
|
|
|
|
|
|
|
|
|
|
| 96 |
]
|
| 97 |
)
|
| 98 |
|
|
@@ -104,9 +114,17 @@ def init_browser():
|
|
| 104 |
|
| 105 |
# User-Agent ์ค์
|
| 106 |
page.set_extra_http_headers({
|
| 107 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
|
|
|
|
| 108 |
})
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
print("Google Trends ํ์ด์ง๋ก ์ด๋ ์ค...")
|
| 111 |
page.goto("https://trends.google.com/trending?geo=KR", wait_until="networkidle", timeout=30000)
|
| 112 |
|
|
@@ -170,25 +188,61 @@ def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
|
| 170 |
try:
|
| 171 |
print(f"ํํฐ ์ ์ฉ: ์ง์ญ={geo}")
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
page.reload(wait_until="networkidle", timeout=30000)
|
| 175 |
-
time.sleep(3)
|
| 176 |
-
|
| 177 |
-
# ํํฐ ์ ์ฉ์ URL ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ
|
| 178 |
geo_map = {
|
| 179 |
"KR": "KR", "US": "US", "JP": "JP",
|
| 180 |
"GB": "GB", "DE": "DE", "FR": "FR"
|
| 181 |
}
|
| 182 |
|
| 183 |
-
# URL ๊ตฌ์ฑ
|
| 184 |
base_url = "https://trends.google.com/trending"
|
| 185 |
geo_param = geo_map.get(geo, "KR")
|
| 186 |
url = f"{base_url}?geo={geo_param}"
|
| 187 |
|
| 188 |
print(f"ํํฐ ์ ์ฉ๋ URL๋ก ์ด๋: {url}")
|
| 189 |
page.goto(url, wait_until="networkidle", timeout=30000)
|
|
|
|
|
|
|
| 190 |
time.sleep(3)
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
print("ํํฐ ์ ์ฉ ์๋ฃ")
|
| 193 |
return True
|
| 194 |
|
|
@@ -198,7 +252,7 @@ def apply_filters(geo: str, time_range: str, category: str) -> bool:
|
|
| 198 |
return False
|
| 199 |
|
| 200 |
def capture_screenshot() -> Optional[Image.Image]:
|
| 201 |
-
"""ํ์ฌ ํ์ด์ง์ ์คํฌ๋ฆฐ์ท ์บก์ฒ"""
|
| 202 |
global page
|
| 203 |
|
| 204 |
# ๋ธ๋ผ์ฐ์ ์ค๋น ์ํ ํ์ธ
|
|
@@ -212,10 +266,41 @@ def capture_screenshot() -> Optional[Image.Image]:
|
|
| 212 |
# ํ์ด์ง๊ฐ ์์ ํ ๋ก๋๋ ๋๊น์ง ๋๊ธฐ
|
| 213 |
time.sleep(2)
|
| 214 |
|
| 215 |
-
#
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
|
|
|
| 219 |
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ")
|
| 220 |
return screenshot
|
| 221 |
|
|
@@ -310,8 +395,52 @@ def refresh_trends_page():
|
|
| 310 |
except Exception as e:
|
| 311 |
return f"ํ์ด์ง ์๋ก๊ณ ์นจ ์คํจ: {str(e)}"
|
| 312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
def main_process(geo: str, time_range: str, category: str) -> Tuple[Optional[Image.Image], str]:
|
| 314 |
-
"""
|
| 315 |
try:
|
| 316 |
# 1. ๋ธ๋ผ์ฐ์ ์ค๋น ํ์ธ
|
| 317 |
if not ensure_browser_ready():
|
|
@@ -369,6 +498,9 @@ def create_interface():
|
|
| 369 |
gr.Markdown("# Google Trends ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด ๋ถ์๊ธฐ")
|
| 370 |
gr.Markdown("Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ ์บก์ฒํ๊ณ Claude AI๋ก ๋ถ์ํฉ๋๋ค.")
|
| 371 |
|
|
|
|
|
|
|
|
|
|
| 372 |
with gr.Row():
|
| 373 |
with gr.Column(scale=1):
|
| 374 |
geo_dropdown = gr.Dropdown(
|
|
@@ -389,27 +521,57 @@ def create_interface():
|
|
| 389 |
label="์นดํ
๊ณ ๋ฆฌ ์ ํ"
|
| 390 |
)
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
with gr.Column(scale=2):
|
| 398 |
screenshot_output = gr.Image(label="Google Trends ์คํฌ๋ฆฐ์ท")
|
| 399 |
analysis_output = gr.Textbox(
|
| 400 |
label="Claude AI ๋ถ์ ๊ฒฐ๊ณผ",
|
| 401 |
lines=10,
|
| 402 |
-
max_lines=20
|
|
|
|
| 403 |
)
|
| 404 |
status_output = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
| 405 |
|
| 406 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
inputs=[geo_dropdown, time_dropdown, category_dropdown],
|
| 410 |
-
outputs=[screenshot_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
)
|
| 412 |
|
|
|
|
| 413 |
refresh_btn.click(
|
| 414 |
fn=refresh_trends_page,
|
| 415 |
outputs=status_output
|
|
|
|
| 48 |
return anthropic.Anthropic(api_key=api_key)
|
| 49 |
|
| 50 |
def install_browsers():
|
| 51 |
+
"""Playwright ๋ธ๋ผ์ฐ์ ๋ฐ ํ๊ธ ํฐํธ ์ค์น"""
|
| 52 |
try:
|
| 53 |
print("Playwright ๋ธ๋ผ์ฐ์ ์ค์น ์ค...")
|
| 54 |
os.system("playwright install chromium")
|
| 55 |
+
|
| 56 |
+
print("ํ๊ธ ํฐํธ ์ค์น ์ค...")
|
| 57 |
+
# ํ๊ธ ํฐํธ ์ค์น
|
| 58 |
+
os.system("apt-get update")
|
| 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"๋ธ๋ผ์ฐ์ /ํฐํธ ์ค์น ์คํจ: {e}")
|
| 66 |
return False
|
| 67 |
|
| 68 |
def init_browser():
|
|
|
|
| 99 |
'--disable-features=TranslateUI',
|
| 100 |
'--disable-ipc-flooding-protection',
|
| 101 |
'--disable-plugins',
|
| 102 |
+
'--disable-images',
|
| 103 |
+
'--force-device-scale-factor=1',
|
| 104 |
+
'--font-render-hinting=medium',
|
| 105 |
+
'--disable-font-subpixel-positioning'
|
| 106 |
]
|
| 107 |
)
|
| 108 |
|
|
|
|
| 114 |
|
| 115 |
# User-Agent ์ค์
|
| 116 |
page.set_extra_http_headers({
|
| 117 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
|
| 118 |
+
"Accept-Language": "ko-KR,ko;q=0.9,en;q=0.8"
|
| 119 |
})
|
| 120 |
|
| 121 |
+
# ํ๊ธ ํฐํธ ์ค์ ์ ์ํ CSS ์ถ๊ฐ
|
| 122 |
+
page.add_style_tag(content="""
|
| 123 |
+
* {
|
| 124 |
+
font-family: 'Nanum Gothic', 'Malgun Gothic', '๋ง์ ๊ณ ๋', 'Noto Sans CJK KR', 'Apple SD Gothic Neo', sans-serif !important;
|
| 125 |
+
}
|
| 126 |
+
""")
|
| 127 |
+
|
| 128 |
print("Google Trends ํ์ด์ง๋ก ์ด๋ ์ค...")
|
| 129 |
page.goto("https://trends.google.com/trending?geo=KR", wait_until="networkidle", timeout=30000)
|
| 130 |
|
|
|
|
| 188 |
try:
|
| 189 |
print(f"ํํฐ ์ ์ฉ: ์ง์ญ={geo}")
|
| 190 |
|
| 191 |
+
# ์ง์ญ ์ค์ ์ ์ํ URL ๊ตฌ์ฑ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
geo_map = {
|
| 193 |
"KR": "KR", "US": "US", "JP": "JP",
|
| 194 |
"GB": "GB", "DE": "DE", "FR": "FR"
|
| 195 |
}
|
| 196 |
|
|
|
|
| 197 |
base_url = "https://trends.google.com/trending"
|
| 198 |
geo_param = geo_map.get(geo, "KR")
|
| 199 |
url = f"{base_url}?geo={geo_param}"
|
| 200 |
|
| 201 |
print(f"ํํฐ ์ ์ฉ๋ URL๋ก ์ด๋: {url}")
|
| 202 |
page.goto(url, wait_until="networkidle", timeout=30000)
|
| 203 |
+
|
| 204 |
+
# ํ์ด์ง ๋ก๋ฉ ์๋ฃ ๋๊ธฐ
|
| 205 |
time.sleep(3)
|
| 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 |
|
|
|
|
| 252 |
return False
|
| 253 |
|
| 254 |
def capture_screenshot() -> Optional[Image.Image]:
|
| 255 |
+
"""ํ์ฌ ํ์ด์ง์ ์คํฌ๋ฆฐ์ท ์บก์ฒ (ํธ๋ ๋ ํ
์ด๋ธ ์์ญ๋ง)"""
|
| 256 |
global page
|
| 257 |
|
| 258 |
# ๋ธ๋ผ์ฐ์ ์ค๋น ์ํ ํ์ธ
|
|
|
|
| 266 |
# ํ์ด์ง๊ฐ ์์ ํ ๋ก๋๋ ๋๊น์ง ๋๊ธฐ
|
| 267 |
time.sleep(2)
|
| 268 |
|
| 269 |
+
# ํธ๋ ๋ ํ
์ด๋ธ ์์ญ ์ฐพ๊ธฐ
|
| 270 |
+
try:
|
| 271 |
+
# ์ฌ๋ฌ ๊ฐ๋ฅํ ์
๋ ํฐ ์๋
|
| 272 |
+
selectors = [
|
| 273 |
+
'[data-module="TrendingSearches"]', # ๋ฉ์ธ ํธ๋ ๋ ๋ชจ๋
|
| 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 |
+
print("ํน์ ์์ญ์ ์ฐพ์ง ๋ชปํด ํ์ด์ง ์๋จ ์์ญ ์บก์ฒ")
|
| 297 |
+
screenshot_bytes = page.screenshot(clip={"x": 0, "y": 100, "width": 1920, "height": 800})
|
| 298 |
+
|
| 299 |
+
except Exception as selector_error:
|
| 300 |
+
print(f"์์ญ ์ ํ ์คํจ, ์ ์ฒด ํ์ด์ง ์บก์ฒ: {selector_error}")
|
| 301 |
+
screenshot_bytes = page.screenshot(full_page=True)
|
| 302 |
|
| 303 |
+
screenshot = Image.open(io.BytesIO(screenshot_bytes))
|
| 304 |
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ")
|
| 305 |
return screenshot
|
| 306 |
|
|
|
|
| 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"ํํฐ ์ ์ฉ ์ค: ์ง์ญ={geo}, ์๊ฐ={time_range}, ์นดํ
๊ณ ๋ฆฌ={category}")
|
| 407 |
+
filter_success = apply_filters(geo, time_range, category)
|
| 408 |
+
|
| 409 |
+
if not filter_success:
|
| 410 |
+
return None, "ํํฐ ์ ์ฉ์ ์คํจํ์ต๋๋ค."
|
| 411 |
+
|
| 412 |
+
# 3. ์คํฌ๋ฆฐ์ท ์บก์ฒ
|
| 413 |
+
print("์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค...")
|
| 414 |
+
screenshot = capture_screenshot()
|
| 415 |
+
|
| 416 |
+
if screenshot is None:
|
| 417 |
+
return None, "์คํฌ๋ฆฐ์ท ์บก์ฒ์ ์คํจํ์ต๋๋ค."
|
| 418 |
+
|
| 419 |
+
return screenshot, "์คํฌ๋ฆฐ์ท ์บก์ฒ ์๋ฃ! Claude ๋ถ์์ ์งํํฉ๋๋ค..."
|
| 420 |
+
|
| 421 |
+
except Exception as e:
|
| 422 |
+
error_msg = f"์คํฌ๋ฆฐ์ท ์บก์ฒ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 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 API๋ก ๋ถ์ ์ค...")
|
| 434 |
+
analysis_result = analyze_with_claude(screenshot)
|
| 435 |
+
return analysis_result
|
| 436 |
+
|
| 437 |
+
except Exception as e:
|
| 438 |
+
error_msg = f"Claude ๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 439 |
+
print(error_msg)
|
| 440 |
+
return error_msg
|
| 441 |
+
|
| 442 |
def main_process(geo: str, time_range: str, category: str) -> Tuple[Optional[Image.Image], str]:
|
| 443 |
+
"""์ ์ฒด ํ๋ก์ธ์ค (ํธํ์ฑ์ ์ํด ์ ์ง)"""
|
| 444 |
try:
|
| 445 |
# 1. ๋ธ๋ผ์ฐ์ ์ค๋น ํ์ธ
|
| 446 |
if not ensure_browser_ready():
|
|
|
|
| 498 |
gr.Markdown("# Google Trends ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด ๋ถ์๊ธฐ")
|
| 499 |
gr.Markdown("Google Trends์ ์ค์๊ฐ ์ธ๊ธฐ ๊ฒ์์ด๋ฅผ ์บก์ฒํ๊ณ Claude AI๋ก ๋ถ์ํฉ๋๋ค.")
|
| 500 |
|
| 501 |
+
# ์ํ ์ ์ฅ์ ์ํ State ์ปดํฌ๋ํธ
|
| 502 |
+
screenshot_state = gr.State(None)
|
| 503 |
+
|
| 504 |
with gr.Row():
|
| 505 |
with gr.Column(scale=1):
|
| 506 |
geo_dropdown = gr.Dropdown(
|
|
|
|
| 521 |
label="์นดํ
๊ณ ๋ฆฌ ์ ํ"
|
| 522 |
)
|
| 523 |
|
| 524 |
+
with gr.Row():
|
| 525 |
+
capture_btn = gr.Button("๐ฏ ์คํฌ๋ฆฐ์ท ์บก์ฒ", variant="primary")
|
| 526 |
+
analyze_btn = gr.Button("๐ค Claude ๋ถ์", variant="secondary", interactive=False)
|
| 527 |
+
|
| 528 |
+
with gr.Row():
|
| 529 |
+
refresh_btn = gr.Button("๐ ํ์ด์ง ์๋ก๊ณ ์นจ", variant="secondary")
|
| 530 |
+
reinit_btn = gr.Button("โ๏ธ ๋ธ๋ผ์ฐ์ ์ด๊ธฐํ", variant="secondary")
|
| 531 |
+
status_btn = gr.Button("๐ ๋ธ๋ผ์ฐ์ ์ํ ํ์ธ", variant="secondary")
|
| 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=10,
|
| 538 |
+
max_lines=20,
|
| 539 |
+
placeholder="์คํฌ๋ฆฐ์ท์ ์บก์ฒํ ํ 'Claude ๋ถ์' ๋ฒํผ์ ํด๋ฆญํ์ธ์."
|
| 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:
|
| 549 |
+
# ๋ถ์ ๋ฒํผ ํ์ฑํ
|
| 550 |
+
return screenshot, screenshot, message, gr.update(interactive=True)
|
| 551 |
+
else:
|
| 552 |
+
return None, None, message, gr.update(interactive=False)
|
| 553 |
+
|
| 554 |
+
capture_btn.click(
|
| 555 |
+
fn=on_capture_click,
|
| 556 |
inputs=[geo_dropdown, time_dropdown, category_dropdown],
|
| 557 |
+
outputs=[screenshot_output, screenshot_state, status_output, analyze_btn]
|
| 558 |
+
)
|
| 559 |
+
|
| 560 |
+
# Claude ๋ถ์ ๋ฒํผ
|
| 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
|
| 567 |
+
|
| 568 |
+
analyze_btn.click(
|
| 569 |
+
fn=on_analyze_click,
|
| 570 |
+
inputs=[screenshot_state],
|
| 571 |
+
outputs=[analysis_output]
|
| 572 |
)
|
| 573 |
|
| 574 |
+
# ๊ธฐํ ๋ฒํผ๋ค
|
| 575 |
refresh_btn.click(
|
| 576 |
fn=refresh_trends_page,
|
| 577 |
outputs=status_output
|