File size: 1,467 Bytes
a5f1a61 |
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 |
import base64
from openai import OpenAI
class GPT4Vision:
def __init__(self):
self.client = OpenAI()
def encode_image(self, image_path):
"""
Encode the image to base64 format.
:param image_path: Path to the image file.
:return: Base64 encoded string of the image.
"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def describe(self, image_path, user_message):
"""
Get a description of the image using OpenAI's GPT-4 Vision API.
:param image_path: Path to the image file.
:param user_message: Custom text message to send as user input.
:return: The API response.
"""
base64_image = self.encode_image(image_path)
response = self.client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": user_message},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
},
},
],
}
],
max_tokens=1000,
)
return response.choices[0]
|