-
Notifications
You must be signed in to change notification settings - Fork 0
/
openaivision.py
62 lines (46 loc) · 1.52 KB
/
openaivision.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
from openai import OpenAI
import base64
import copykitten
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def openai_image_reponse(API_KEY,image_path):
# Path to your image
#image_path = "./cropped_screenshot.png"
# Getting the base64 string
base64_image = encode_image(image_path)
client = OpenAI(api_key=API_KEY)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please answer question in this image. response shouldn't be like an ai assistant, just answer is enough."},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}",
},
},
],
}
],
max_tokens=300,
)
result = response.choices[0].message.content
print(result)
if result is None:
result = "oops"
copykitten.copy(result)
if __name__ == "__main__":
from settings import load_settings
import platformdirs
import os
app_auther = "vaisaskh"
app_name = "aihelper"
config_dir = platformdirs.user_config_dir(app_name,app_auther)
config_file = os.path.join(config_dir,"settings.ini")
image_file_path = os.path.join(config_dir,"cropped_screenshot.png")
settings = load_settings(config_file,app_name)
openai_image_reponse(settings['openai_key'],image_file_path)