-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgptHelper.py
More file actions
71 lines (59 loc) · 2.19 KB
/
gptHelper.py
File metadata and controls
71 lines (59 loc) · 2.19 KB
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 requests
from datetime import datetime
import json
import os
# OpenAI API Key
api_key = "your-api-key-make-sure-has-gpt4-enabled" # Ensure you use your actual API key
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Function to save the question and response to a file
def save_result(question, response, image_path, filename="result.json"):
# Load existing data from the file
if os.path.exists(filename):
with open(filename, "r") as file:
data = json.load(file)
if not isinstance(data, list):
data = []
else:
data = []
# Extract the image name from the image path
image_name = os.path.basename(image_path)
# Append the new result
data.append({
"timestamp": datetime.now().isoformat(),
"question": question,
"response": response,
"referenced_image": image_name
})
# Save the updated data back to the file
with open(filename, "w") as file:
json.dump(data, file, indent=4)
# Function to analyze the image using GPT-4
def analyze_image(image_path, question):
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4",
"messages": [
{"role": "user", "content": question},
{"role": "user", "content": {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
response_data = response.json()
response_message = response_data['choices'][0]['message']['content'].strip()
return response_message
if __name__ == "__main__":
# Path to your image
image_path = "current_view.png"
question = "What objects can you identify in this image?"
response_message = analyze_image(image_path, question)
print(response_message)
save_result(question, response_message, image_path)