GPT 4o Vision 활용하기
이미지를 호출하여 encode 하고, openapi 호출하기
# 이미지를 저장할 폴더 이름
folder_name = 'image'
# 저장할 파일의 경로를 설정. 여기서는 'image/파일명.jpg' 형식을 사용합니다.
image_path = os.path.join(folder_name, stockCode + "_" + current_date_str + ".jpg")
# Getting the base64 string
base64_image = self.encode_image(image_path=image_path)
description = self.describe_image(api_key=self.openai_secret_key, base64_image=base64_image)
# print(f"이미지 설명: {description}")
if description is None:
# text = "gpt-4o-mini 검증 실패"
# self.ui.pteLog.appendPlainText("%s(%s) %s" % (stockName, stockCode, text))
market_trand = 0
elif 'No' in description: # Case-insensitive check for 'No'
text = "%s(%s) gpt-4o vision says %s" % (stockName, stockCode, description)
self.ui.pteLog.appendPlainText(text)
market_trand = 0
elif 'Yes' in description: # Case-insensitive check for 'Yes'
text = "%s(%s) gpt-4o vision says %s" % (stockName, stockCode, description)
self.ui.pteLog.appendPlainText(text)
market_trand = 5
else:
# text = "%s(%s) gpt-4o vision says %s" % (stockName, stockCode, description)
# self.ui.pteLog.appendPlainText(text)
market_trand = 0
사용된 함수들
# Function to encode the image
def encode_image(self, image_path=''):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def describe_image(self, api_key='', base64_image=''): # GPT-4 Vision 모델용
"""
GPT-4 Vision 모델을 사용하여 이미지를 설명하는 함수
"""
# print("GPT-4 Vision 모델을 사용하여 이미지를 설명하는 함수")
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4o", # "model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "If the price is holding support and showing signs of upward momentum, it might be a good time to buy, right? Look at the left-side day chart and right-side 5-minute chart. After closing inspection of the 5-minute chart, Please say 'Yes' if you sure it is fully and clearly transitioned into an upward trend momentum. if not, say No."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
except Exception as e:
print("GPT-4 Vision 모델을 사용하여 이미지를 설명: %s" % e)
return None
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
# print(response.json())
# Parse the JSON response
response_data = response.json()
# Print only the 'choices' part
choices = response_data.get('choices', [])
if choices:
content = choices[0]['message']['content']
# print(content)
return content
else:
print("No content found.")
return None
Telegram Bot을 통해 결과 확인.