Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,10 @@ 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
|
|
@@ -31,7 +35,19 @@ def get_claude_client():
|
|
| 31 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 32 |
if not api_key:
|
| 33 |
raise ValueError("ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def init_browser():
|
| 37 |
"""๋ธ๋ผ์ฐ์ ๋ฅผ ์ด๊ธฐํํ๊ณ Google Trends ํ์ด์ง๋ก ์ด๋"""
|
|
@@ -52,7 +68,12 @@ def init_browser():
|
|
| 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("--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# ChromeDriver ์๋น์ค ์ค์
|
| 58 |
service = Service(ChromeDriverManager().install())
|
|
@@ -71,7 +92,7 @@ def init_browser():
|
|
| 71 |
)
|
| 72 |
|
| 73 |
# ์ถ๊ฐ ๋ก๋ฉ ์๊ฐ
|
| 74 |
-
time.sleep(
|
| 75 |
|
| 76 |
is_browser_ready = True
|
| 77 |
print("๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์๋ฃ!")
|
|
@@ -182,6 +203,7 @@ def analyze_with_claude(image: Image.Image, user_prompt: str = "") -> str:
|
|
| 182 |
1. ํ์ฌ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๊ฒ์์ด๋ค๊ณผ ๊ทธ ์์
|
| 183 |
2. ๊ฐ ๊ฒ์์ด์ ์ฆ๊ฐ ์ถ์ธ๋ ํน์ด์ฌํญ
|
| 184 |
3. ๊ฒ์์ด๋ค์์ ๋ฐ๊ฒฌ๋๋ ์ฃผ์ ํธ๋ ๋๋ ํจํด
|
|
|
|
| 185 |
|
| 186 |
ํ๊ตญ์ด๋ก ์์ธํ๊ณ ๊ตฌ์ฒด์ ์ผ๋ก ๋ถ์ํด์ฃผ์ธ์.
|
| 187 |
"""
|
|
@@ -189,8 +211,8 @@ def analyze_with_claude(image: Image.Image, user_prompt: str = "") -> str:
|
|
| 189 |
prompt = user_prompt if user_prompt.strip() else default_prompt
|
| 190 |
|
| 191 |
response = client.messages.create(
|
| 192 |
-
model="claude-3-sonnet-
|
| 193 |
-
max_tokens=
|
| 194 |
messages=[
|
| 195 |
{
|
| 196 |
"role": "user",
|
|
@@ -219,6 +241,24 @@ def analyze_with_claude(image: Image.Image, user_prompt: str = "") -> str:
|
|
| 219 |
print(error_msg)
|
| 220 |
return error_msg
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
def refresh_trends_page():
|
| 223 |
"""Google Trends ํ์ด์ง ์๋ก๊ณ ์นจ"""
|
| 224 |
global driver
|
|
@@ -319,6 +359,7 @@ def create_interface():
|
|
| 319 |
|
| 320 |
analyze_btn = gr.Button("๋ถ์ ์์", variant="primary")
|
| 321 |
refresh_btn = gr.Button("ํ์ด์ง ์๋ก๊ณ ์นจ", variant="secondary")
|
|
|
|
| 322 |
status_btn = gr.Button("๋ธ๋ผ์ฐ์ ์ํ ํ์ธ", variant="secondary")
|
| 323 |
|
| 324 |
with gr.Column(scale=2):
|
|
@@ -342,6 +383,11 @@ def create_interface():
|
|
| 342 |
outputs=status_output
|
| 343 |
)
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
status_btn.click(
|
| 346 |
fn=check_browser_status,
|
| 347 |
outputs=status_output
|
|
@@ -355,13 +401,13 @@ def main():
|
|
| 355 |
print("Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ ์์!")
|
| 356 |
print("=" * 50)
|
| 357 |
|
| 358 |
-
# API ํค ํ์ธ
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
print("
|
| 362 |
-
except ValueError as e:
|
| 363 |
-
print(f"โ {e}")
|
| 364 |
return
|
|
|
|
|
|
|
| 365 |
|
| 366 |
print("โ Gradio ์น ์ธํฐํ์ด์ค ์ค๋น ์ค...")
|
| 367 |
|
|
|
|
| 20 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 21 |
from PIL import Image
|
| 22 |
import requests
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
|
| 25 |
+
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
| 26 |
+
load_dotenv()
|
| 27 |
|
| 28 |
# ์ ์ญ ๋ณ์
|
| 29 |
driver = None
|
|
|
|
| 35 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 36 |
if not api_key:
|
| 37 |
raise ValueError("ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# ๋ ์์ ํ ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 41 |
+
client = anthropic.Anthropic(
|
| 42 |
+
api_key=api_key,
|
| 43 |
+
max_retries=2,
|
| 44 |
+
timeout=60.0
|
| 45 |
+
)
|
| 46 |
+
return client
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Claude ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ค๋ฅ: {e}")
|
| 49 |
+
# ๊ธฐ๋ณธ ์ด๊ธฐํ ๋ฐฉ์์ผ๋ก ์ฌ์๋
|
| 50 |
+
return anthropic.Anthropic(api_key=api_key)
|
| 51 |
|
| 52 |
def init_browser():
|
| 53 |
"""๋ธ๋ผ์ฐ์ ๋ฅผ ์ด๊ธฐํํ๊ณ Google Trends ํ์ด์ง๋ก ์ด๋"""
|
|
|
|
| 68 |
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
|
| 69 |
chrome_options.add_argument("--disable-features=VizDisplayCompositor")
|
| 70 |
chrome_options.add_argument("--window-size=1920,1080")
|
| 71 |
+
chrome_options.add_argument("--disable-background-timer-throttling")
|
| 72 |
+
chrome_options.add_argument("--disable-backgrounding-occluded-windows")
|
| 73 |
+
chrome_options.add_argument("--disable-renderer-backgrounding")
|
| 74 |
+
chrome_options.add_argument("--disable-features=TranslateUI")
|
| 75 |
+
chrome_options.add_argument("--disable-ipc-flooding-protection")
|
| 76 |
+
chrome_options.add_argument("--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")
|
| 77 |
|
| 78 |
# ChromeDriver ์๋น์ค ์ค์
|
| 79 |
service = Service(ChromeDriverManager().install())
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
# ์ถ๊ฐ ๋ก๋ฉ ์๊ฐ
|
| 95 |
+
time.sleep(5)
|
| 96 |
|
| 97 |
is_browser_ready = True
|
| 98 |
print("๋ธ๋ผ์ฐ์ ์ด๊ธฐํ ์๋ฃ!")
|
|
|
|
| 203 |
1. ํ์ฌ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๊ฒ์์ด๋ค๊ณผ ๊ทธ ์์
|
| 204 |
2. ๊ฐ ๊ฒ์์ด์ ์ฆ๊ฐ ์ถ์ธ๋ ํน์ด์ฌํญ
|
| 205 |
3. ๊ฒ์์ด๋ค์์ ๋ฐ๊ฒฌ๋๋ ์ฃผ์ ํธ๋ ๋๋ ํจํด
|
| 206 |
+
4. ์์ฌ์ /๋ฌธํ์ ๋งฅ๋ฝ์์์ ํด์
|
| 207 |
|
| 208 |
ํ๊ตญ์ด๋ก ์์ธํ๊ณ ๊ตฌ์ฒด์ ์ผ๋ก ๋ถ์ํด์ฃผ์ธ์.
|
| 209 |
"""
|
|
|
|
| 211 |
prompt = user_prompt if user_prompt.strip() else default_prompt
|
| 212 |
|
| 213 |
response = client.messages.create(
|
| 214 |
+
model="claude-3-5-sonnet-20241022",
|
| 215 |
+
max_tokens=1500,
|
| 216 |
messages=[
|
| 217 |
{
|
| 218 |
"role": "user",
|
|
|
|
| 241 |
print(error_msg)
|
| 242 |
return error_msg
|
| 243 |
|
| 244 |
+
def reinit_browser():
|
| 245 |
+
"""๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ"""
|
| 246 |
+
global driver, is_browser_ready
|
| 247 |
+
|
| 248 |
+
print("๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์์...")
|
| 249 |
+
|
| 250 |
+
# ๊ธฐ์กด ๋ธ๋ผ์ฐ์ ์ ๋ฆฌ
|
| 251 |
+
cleanup_browser()
|
| 252 |
+
is_browser_ready = False
|
| 253 |
+
|
| 254 |
+
# ์๋ก ์ด๊ธฐํ
|
| 255 |
+
init_browser()
|
| 256 |
+
|
| 257 |
+
if is_browser_ready:
|
| 258 |
+
return "๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์๋ฃ!"
|
| 259 |
+
else:
|
| 260 |
+
return "๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ ์คํจ. ๋ค์ ์๋ํด์ฃผ์ธ์."
|
| 261 |
+
|
| 262 |
def refresh_trends_page():
|
| 263 |
"""Google Trends ํ์ด์ง ์๋ก๊ณ ์นจ"""
|
| 264 |
global driver
|
|
|
|
| 359 |
|
| 360 |
analyze_btn = gr.Button("๋ถ์ ์์", variant="primary")
|
| 361 |
refresh_btn = gr.Button("ํ์ด์ง ์๋ก๊ณ ์นจ", variant="secondary")
|
| 362 |
+
reinit_btn = gr.Button("๋ธ๋ผ์ฐ์ ์ฌ์ด๊ธฐํ", variant="secondary")
|
| 363 |
status_btn = gr.Button("๋ธ๋ผ์ฐ์ ์ํ ํ์ธ", variant="secondary")
|
| 364 |
|
| 365 |
with gr.Column(scale=2):
|
|
|
|
| 383 |
outputs=status_output
|
| 384 |
)
|
| 385 |
|
| 386 |
+
reinit_btn.click(
|
| 387 |
+
fn=reinit_browser,
|
| 388 |
+
outputs=status_output
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
status_btn.click(
|
| 392 |
fn=check_browser_status,
|
| 393 |
outputs=status_output
|
|
|
|
| 401 |
print("Google Trends ์ค์๊ฐ ๋ถ์๊ธฐ ์์!")
|
| 402 |
print("=" * 50)
|
| 403 |
|
| 404 |
+
# API ํค ์กด์ฌ ์ฌ๋ถ๋ง ํ์ธ (์ค์ ํด๋ผ๏ฟฝ๏ฟฝ์ธํธ ์ด๊ธฐํ๋ ๋์ค์)
|
| 405 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 406 |
+
if not api_key:
|
| 407 |
+
print("โ ANTHROPIC_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
|
|
|
|
|
|
| 408 |
return
|
| 409 |
+
else:
|
| 410 |
+
print("โ Claude API ํค ํ์ธ๋จ")
|
| 411 |
|
| 412 |
print("โ Gradio ์น ์ธํฐํ์ด์ค ์ค๋น ์ค...")
|
| 413 |
|