forked from ishan0102/vimGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.py
71 lines (61 loc) · 3.14 KB
/
vision.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
import base64
import json
import os
from io import BytesIO
import openai
from dotenv import load_dotenv
from PIL import Image
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
IMG_RES = 1080
# Function to encode the image
def encode_and_resize(image):
W, H = image.size
image = image.resize((IMG_RES, int(IMG_RES * H / W)))
buffer = BytesIO()
image.save(buffer, format="PNG")
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
return encoded_image
def get_actions(screenshot, objective):
encoded_screenshot = encode_and_resize(screenshot)
response = openai.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"You need to choose which action to take to help a user do this task: {objective}. Your options are navigate, type, click, and done. Navigate should take you to the specified URL. Type and click take strings where if you want to click on an object, return the string with the yellow character sequence you want to click on, and to type just a string with the message you want to type. For clicks, please only respond with the 1-2 letter sequence in the yellow box, and if there are multiple valid options choose the one you think a user would select. For typing, please return a click to click on the box along with a type with the message to write. When the page seems satisfactory, return done as a key with no value. You must respond in JSON only with no other fluff or bad things will happen. The JSON keys must ONLY be one of navigate, type, or click. Do not return the JSON inside a code block.",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{encoded_screenshot}",
},
},
],
}
],
max_tokens=100,
)
try:
json_response = json.loads(response.choices[0].message.content)
except json.JSONDecodeError:
print("Error: Invalid JSON response")
cleaned_response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant to fix an invalid JSON response. You need to fix the invalid JSON response to be valid JSON. You must respond in JSON only with no other fluff or bad things will happen. Do not return the JSON inside a code block."},
{"role": "user", "content": f"The invalid JSON response is: {response.choices[0].message.content}"}
]
)
try: cleaned_json_response = json.loads(cleaned_response.choices[0].message.content)
except json.JSONDecodeError:
print("Error: Invalid JSON response")
return {}
return cleaned_json_response
return json_response
if __name__ == "__main__":
image = Image.open("image.png")
actions = get_actions(image, "upvote the pinterest post")