kivilaid commited on
Commit
1c65519
·
1 Parent(s): e4ae9b8

Upload 2 files

Browse files
Files changed (1) hide show
  1. gpt4v.py +15 -9
gpt4v.py CHANGED
@@ -1,30 +1,28 @@
1
  import base64
2
  from openai import OpenAI
3
 
4
-
5
  class GPT4Vision:
6
  def __init__(self):
7
  self.client = OpenAI()
8
 
9
- def encode_image(self, image_path):
10
  """
11
  Encode the image to base64 format.
12
 
13
- :param image_path: Path to the image file.
14
  :return: Base64 encoded string of the image.
15
  """
16
- with open(image_path, "rb") as image_file:
17
- return base64.b64encode(image_file.read()).decode('utf-8')
18
 
19
- def describe(self, image_path, user_message):
20
  """
21
  Get a description of the image using OpenAI's GPT-4 Vision API.
22
 
23
- :param image_path: Path to the image file.
24
  :param user_message: Custom text message to send as user input.
25
  :return: The API response.
26
  """
27
- base64_image = self.encode_image(image_path)
28
 
29
  response = self.client.chat.completions.create(
30
  model="gpt-4-vision-preview",
@@ -44,4 +42,12 @@ class GPT4Vision:
44
  ],
45
  max_tokens=1000,
46
  )
47
- return response.choices[0]
 
 
 
 
 
 
 
 
 
1
  import base64
2
  from openai import OpenAI
3
 
 
4
  class GPT4Vision:
5
  def __init__(self):
6
  self.client = OpenAI()
7
 
8
+ def encode_image(self, image_file):
9
  """
10
  Encode the image to base64 format.
11
 
12
+ :param image_file: File object of the image.
13
  :return: Base64 encoded string of the image.
14
  """
15
+ return base64.b64encode(image_file.read()).decode('utf-8')
 
16
 
17
+ def describe(self, image_file, user_message):
18
  """
19
  Get a description of the image using OpenAI's GPT-4 Vision API.
20
 
21
+ :param image_file: File object of the image.
22
  :param user_message: Custom text message to send as user input.
23
  :return: The API response.
24
  """
25
+ base64_image = self.encode_image(image_file)
26
 
27
  response = self.client.chat.completions.create(
28
  model="gpt-4-vision-preview",
 
42
  ],
43
  max_tokens=1000,
44
  )
45
+ return response.choices[0].message.content
46
+
47
+ # # Example usage
48
+ # gpt4v = GPT4Vision()
49
+ # image_path = "/path/to/image.png"
50
+ # with open(image_path, "rb") as image_file:
51
+ # user_message = "Describe this image for me."
52
+ # description = gpt4v.describe(image_file, user_message)
53
+ # print(description)