File size: 1,333 Bytes
24e5d63 |
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 |
import os
import base64
import requests
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Function to get image description
def get_image_description(image_path, title):
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": 'YOUR_GPT4o_API_KEY'
}
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": title
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
"detail": "low"
}
}
]
}
],
"max_tokens": 1800
}
response = requests.post("https://api.kwwai.top/v1/chat/completions", headers=headers, json=payload)
if response.status_code != 200:
print(response.text)
return None
# Extract the description from the response
if 'content' in response.json()['choices'][0]['message']:
description = response.json()['choices'][0]['message']['content']
else:
description = None
return description
|