-
Notifications
You must be signed in to change notification settings - Fork 2
/
website_analyzer.py
88 lines (69 loc) · 2.68 KB
/
website_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
78
79
80
81
82
83
84
85
86
87
88
import requests
from bs4 import BeautifulSoup
from playwright.sync_api import sync_playwright
import base64
import os
from dotenv import load_dotenv
from datetime import datetime
from PIL import Image
import io
from groq import Groq
load_dotenv()
def analyze_website(url):
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
text = soup.get_text(separator=' ', strip=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
screenshot_path = f"screenshots/screenshot_{timestamp}.png"
thumbnail_path = f"thumbnails/thumbnail_{timestamp}.png"
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url)
page.set_viewport_size({"width": 1920, "height": 1080})
page.screenshot(path=screenshot_path, full_page=False)
browser.close()
with Image.open(screenshot_path) as img:
img.thumbnail((400, 300))
img.save(thumbnail_path, "PNG")
if img.width > 1920 or img.height > 1080:
img.thumbnail((1920, 1080), Image.Resampling.LANCZOS)
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG', optimize=True, quality=85)
img_byte_arr = img_byte_arr.getvalue()
encoded_image = base64.b64encode(img_byte_arr).decode('utf-8')
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
llava_model = 'llava-v1.5-7b-4096-preview'
prompt = f"""Please analyze this website and provide a detailed description.
Consider the visual layout, color scheme, and main elements visible in the image.
Also analyze the following text content from the website:
{text[:15000]}
Provide a comprehensive analysis of both the visual and textual elements."""
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{encoded_image}",
},
},
],
}
],
model=llava_model,
max_tokens=1000
)
analysis = chat_completion.choices[0].message.content
return analysis, thumbnail_path
# Example usage
if __name__ == "__main__":
website_url = "https://stackoverflow.com/"
analysis, thumbnail_path = analyze_website(website_url)
print(analysis)
print(f"Thumbnail saved at: {thumbnail_path}")