How to get access to gpt-4-vision-preview?

Your last version, fixed by AI, added imports, added output, formatted with Black.

import base64
from openai import OpenAI

client = OpenAI()


def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


encoded_string = encode_image("sample.png")

system_prompt = "You are an expert at analyzing images."
response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=[
        {
            "role": "system",
            "content": [
                {"type": "text", "text": system_prompt},
            ],
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{encoded_string}"},
                }
            ],
        },
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this art style."},
            ],
        },
    ],
    max_tokens=1000,
)

print(response.choices[0].message.content)
print(response.usage.model_dump())

text output:

The art style in the image you’ve provided appears to be digitally manipulated to resemble a painting, likely through a filter that simulates brush strokes and gives a softer, more textured appearance. The technique emphasizes color blending and has a somewhat impressionistic effect, with broad, smooth strokes that reduce detail and lend a dreamy, ethereal quality to the image. The outlines and finer details of the subjects are softened, which adds a sense of movement and fluidity, a fitting choice for depicting ballet dancers. This style may often be found in digital art applications and can be used to give photographs a more artistic and expressive feel.
response.usage.model_dump()
{‘completion_tokens’: 124, ‘prompt_tokens’: 1133, ‘total_tokens’: 1257}

for another demo image


1792 x 1024 Pixels (1.84 MPixels) (1.75)


round((response.usage.prompt_tokens*0.01 + response.usage.completion_tokens*0.03)/1000, 3)
0.015
1 Like