-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvision_researcher.py
220 lines (176 loc) · 7.15 KB
/
vision_researcher.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import sys
from urllib.parse import urljoin, urlparse
from dotenv import load_dotenv
import requests
from bs4 import BeautifulSoup
from openai import OpenAI
import time
from PIL import Image
from io import BytesIO
import base64
# Load environment variables
load_dotenv()
SCREENSHOTONE_API_KEY = os.getenv("SCREENSHOTONE_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not SCREENSHOTONE_API_KEY or not OPENAI_API_KEY:
print("Please set SCREENSHOTONE_API_KEY and OPENAI_API_KEY in .env file")
sys.exit(1)
# Initialize OpenAI client, it will automatically use OPENAI_API_KEY from environment
client = OpenAI()
def get_screenshot_and_html(url):
"""Get screenshot and HTML content using ScreenshotOne API"""
print(f"📸 Taking screenshot of {url}...")
api_url = "https://api.screenshotone.com/take"
params = {
"access_key": SCREENSHOTONE_API_KEY,
"url": url,
"full_page": "true",
"format": "jpg",
"cache": "true",
"response_type": "json",
"metadata_content": "true",
}
start_time = time.time()
response = requests.get(api_url, params=params)
if response.status_code != 200:
print(f"❌ Error getting screenshot: {response.text}")
return None, None
data = response.json()
screenshot_url = data.get("cache_url") # URL of the screenshot
html_url = data.get("content", {}).get("url")
# Get HTML content directly from the URL
try:
html_response = requests.get(html_url)
html_content = html_response.text if html_response.status_code == 200 else None
except Exception as e:
print(f"❌ Error fetching HTML content: {e}")
html_content = None
duration = time.time() - start_time
print(f"✅ Screenshot taken successfully ({duration:.2f}s)")
if not screenshot_url:
print("❌ No screenshot URL in response")
if not html_content:
print("❌ No HTML content fetched")
return screenshot_url, html_content
def analyze_image(image_url, prompt):
"""Analyze image using OpenAI Vision API by splitting into chunks"""
print("🔍 Analyzing screenshot with OpenAI Vision...")
print(f" Prompt: {prompt}")
try:
# Download image
print(" Downloading image...")
response = requests.get(image_url)
if response.status_code != 200:
print("❌ Failed to download image")
return None
# Open image and get dimensions
image = Image.open(BytesIO(response.content))
width, height = image.size
chunk_height = 1000 # Height of each chunk in pixels
results = []
start_time = time.time()
# Split and analyze each chunk
for y in range(0, height, chunk_height):
# Calculate chunk boundaries
chunk_bottom = min(y + chunk_height, height)
chunk = image.crop((0, y, width, chunk_bottom))
# Save chunk to bytes
chunk_bytes = BytesIO()
chunk.save(chunk_bytes, format='JPEG')
chunk_bytes.seek(0)
print(f" Analyzing chunk {y//chunk_height + 1} of {(height + chunk_height - 1)//chunk_height}...")
# Analyze chunk with OpenAI Vision
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64.b64encode(chunk_bytes.getvalue()).decode()}"
}
},
],
}
],
max_tokens=300,
)
chunk_result = response.choices[0].message.content
if chunk_result:
results.append(chunk_result)
duration = time.time() - start_time
print(f"✅ Analysis completed successfully ({duration:.2f}s)")
return "\n".join(results)
except Exception as e:
print(f"❌ Error analyzing image: {e}")
return None
def get_internal_links(html_content, base_url):
"""Extract internal links from HTML content"""
print("🔗 Extracting internal links...")
try:
soup = BeautifulSoup(html_content, "html.parser")
base_domain = urlparse(base_url).netloc
internal_links = set()
for link in soup.find_all("a", href=True):
href = link["href"]
absolute_url = urljoin(base_url, href)
if urlparse(absolute_url).netloc == base_domain:
internal_links.add(absolute_url)
print(f"✅ Found {len(internal_links)} internal links")
return internal_links
except Exception as e:
print(f"❌ Error extracting internal links: {e}")
return set()
def main():
if len(sys.argv) != 4:
print("Usage: python vision_researcher.py <url> <prompt> <max_pages>")
sys.exit(1)
url = sys.argv[1]
prompt = sys.argv[2]
max_pages = int(sys.argv[3])
print("\n🚀 Starting Vision Researcher")
print(f" Initial URL: {url}")
print(f" Max Pages: {max_pages}")
print(f" Prompt: {prompt}\n")
visited_urls = set()
pages_processed = 0
start_time = time.time()
while pages_processed < max_pages and url and url not in visited_urls:
print(f"\n📄 Page {pages_processed + 1}/{max_pages}")
print(f"🌐 Processing: {url}")
visited_urls.add(url)
# Get screenshot and HTML
screenshot_url, html_content = get_screenshot_and_html(url)
if not screenshot_url or not html_content:
print("⏭️ Skipping page due to errors")
continue
print(f"🖼️ Screenshot available at: {screenshot_url}")
# Analyze screenshot with OpenAI Vision
vision_result = analyze_image(screenshot_url, prompt)
if vision_result:
print("\n📝 Analysis Result:")
print(" " + vision_result.replace("\n", "\n "))
# Get internal links for next iteration
internal_links = get_internal_links(html_content, url)
pages_processed += 1
# Get next unvisited URL
next_urls = [link for link in internal_links if link not in visited_urls]
url = next_urls[0] if next_urls else None
if not url:
print("\n🏁 No more unvisited internal links")
elif pages_processed >= max_pages:
print("\n🏁 Reached maximum number of pages")
total_duration = time.time() - start_time
print(f"\n✨ Vision Research completed")
print(f" Pages processed: {pages_processed}")
print(f" Total time: {total_duration:.2f}s")
if pages_processed > 0:
print(f" Average time per page: {total_duration/pages_processed:.2f}s")
else:
print(" No pages were processed")
if __name__ == "__main__":
main()