-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_image.py
More file actions
73 lines (66 loc) · 2.5 KB
/
openai_image.py
File metadata and controls
73 lines (66 loc) · 2.5 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
72
73
import argparse
import os
import base64
import openai
from dotenv import load_dotenv
load_dotenv()
client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))
# Encode local image to base64
def encode_image_to_base64(image_path):
with open(image_path, "rb") as f:
data = base64.b64encode(f.read()).decode("utf-8")
ext = os.path.splitext(image_path)[1].lower()
mime = "image/png" if ext == ".png" else "image/jpeg"
return f"data:{mime};base64,{data}"
# Ask a question about the image
def answer_from_image(image_data, question, is_url=False):
prompt = f"Answer the question based on the image: {question}. Ensure the answer is concise, relevant, and limited to one sentence."
message = {
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": image_data}},
],
}
response = client.chat.completions.create(
model="gpt-4o",
messages=[message]
)
return response.choices[0].message.content
# Generate a caption for the image
def caption_image(image_data, is_url=False):
prompt = "Describe the image in one concise sentence. Keep it crisp and sweet."
message = {
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": image_data}},
],
}
response = client.chat.completions.create(
model="gpt-4o",
messages=[message]
)
return response.choices[0].message.content
if __name__ == "__main__":
# Command-line interface
parser = argparse.ArgumentParser(description="Analyze images via URL or local path.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-image_path", type=str, help="Path to the local image file.")
group.add_argument("-image_url", type=str, help="URL of the image to analyze.")
parser.add_argument("-question", type=str, help="Question to ask about the image.")
args = parser.parse_args()
# Prepare image data
if args.image_path:
image_data = encode_image_to_base64(args.image_path)
is_url = False
else:
image_data = args.image_url
is_url = True
# Perform captioning or Q&A based on presence of question
if not args.question:
caption = caption_image(image_data, is_url=is_url)
print("Caption:", caption)
else:
answer = answer_from_image(image_data, args.question, is_url=is_url)
print("Answer:", answer)