-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_description.py
55 lines (46 loc) · 1.63 KB
/
image_description.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
import base64
import requests
import json
# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"
def generate_image_description(image_path):
# Function to encode the image
def encode_image(path):
with open(path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Getting the base64 string
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "generate a brief description of the given image. do not use any symbols or special characters"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 2000
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
response_json = response.json()
# Extracting the content from the response
content = response_json['choices'][0]['message']['content']
return content
# # Example usage:
# image_path = "D:\\GPT\\GPT\\ielts_evaluation\\bar01.png"
# description = generate_image_description(image_path)
# print(description)