import os
import base64
from langchain_openai import ChatOpenAI
from langchain_google_vertexai import ChatVertexAI
from langchain_core.messages import HumanMessage

os.environ['OPENAI_API_KEY'] = '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'
os.environ["GOOGLE_API_KEY"] = 'AIzaSyD3VYEX7Ktb45JSRM7pfp0WdvGsFE1Fve0'


from PIL import Image
from io import BytesIO

def pil_to_b64(img: Image.Image) -> str:
    with BytesIO() as image_buffer:
        img.save(image_buffer, format="PNG")
        byte_data = image_buffer.getvalue()
        img_b64 = base64.b64encode(byte_data).decode("utf-8")
        img_b64 = "data:image/png;base64," + img_b64
    return img_b64

# Initialize the model

model = ChatOpenAI(model="gpt-4o-mini")

# Get list of avilable models

import base64


img_path="/home/mashalimay/webarena/WebGUIAgents/examples/bird.png"

image_data = pil_to_b64(Image.open(img_path))

# Load and encode the image
with open(img_path, "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode("utf-8")


message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ],
)
response = model.invoke([message])
print(response.content)