Using "gpt-4-vision-preview" for Image Interpretation from an Uploaded Folder

Hello everyone,

I am currently working on a project where I need to use GPT-4 to interpret images that are loaded from a specific folder. or when an user upload an image. My goal is to make the model analyze an uploaded image and provide insights or descriptions based on its contents.

Here’s the code snippet I am using:

if uploaded_image is not None:
    image = Image.open(uploaded_image)
    st.image(image, caption='Uploaded Image', use_column_width=True)
   
    response = client.chat.completions.create(
        model="gpt-4-vision-preview",
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "What’s in this image?"},
                ],
            }
        ],
        max_tokens=300,
    )

    print(response.choices[0])

I am facing challenges in getting this to work as intended. Has anyone here worked on a similar task or can offer guidance on how to correctly implement this feature using GPT-4? Any insights or suggestions would be greatly appreciated.

Thank you in advance for your help!

1 Like

OK, so “Images can are made available to the model in two main ways: by passing a link to the image or by passing the base64 encoded image directly in the request.” Thank you @wclayf

2 Likes