-
Notifications
You must be signed in to change notification settings - Fork 25
/
gpt4v_scraper.py
86 lines (72 loc) Β· 2.6 KB
/
gpt4v_scraper.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
import os
import base64
import subprocess
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize OpenAI model
ai_model = OpenAI()
ai_model.timeout = 40
# Function to encode image to base64
def encode_image_to_base64(file_path):
with open(file_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode()
# Function to capture website snapshot
def capture_website(url):
print(f"Accessing {url}")
snapshot_path = "snapshot.jpg"
if os.path.exists(snapshot_path):
os.remove(snapshot_path)
subprocess_result = subprocess.run(
["node", "snapshot.js", url],
capture_output=True,
text=True
)
if not os.path.exists(snapshot_path):
print("β Snapshot failed")
return "β oopsie poopsie this snapshot failed"
return encode_image_to_base64(snapshot_path)
# Function to extract information using AI model
def extract_info_from_image(encoded_image, user_prompt):
ai_response = ai_model.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "system",
"content": "You are now a Web Scraper. Your task is extracting maximum info possible based on a snapshot the user provides and the user's instructions.",
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{encoded_image}",
},
{
"type": "text",
"text": user_prompt,
}
]
}
],
max_tokens=1024,
)
response_content = ai_response.choices[0].message.content
if "ANSWER_NOT_FOUND" in response_content:
print("π No answer found. Whoops.")
return "π Could not find answer on this website"
else:
print(f"π€ AI Model Response: {response_content}")
return response_content
# Function to perform website capture and information extraction
def web_capture_and_extract(url, prompt):
base64_snapshot = capture_website(url)
print("Snapshot successful πΈβ
")
if base64_snapshot == "β Snapshot failed":
return "β Snapshot error"
else:
return extract_info_from_image(base64_snapshot, prompt)
# Running the function w/ a example website + prompt
result = web_capture_and_extract("https://vd7.io/experience", "Extract this dude's Experience info")
print(result)