Spaces:
Configuration error
Configuration error
File size: 2,090 Bytes
28dbbfb | 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 | from langchain_google_genai import ChatGoogleGenerativeAI
import os
import base64
from langchain_core.messages import HumanMessage
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
load_dotenv()
class ImageQuestionAnswer:
def __init__(self):
'''self.vision_llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0.1,
api_key=os.getenv("GEMINI_API_KEY")
)'''
self.vision_llm = ChatOpenAI(model="gpt-4o", openai_api_key=os.getenv("OPENAI_API_KEY"))
def answer(self, image_path: str, question: str) -> str:
print(f"Sending image to OpenAI: {image_path}")
print(f"Question: {question}")
with open(image_path, "rb") as image_file:
image_bytes = image_file.read()
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
# Prepare the prompt including the base64 image data
message = [
HumanMessage(
content=[
{
"type": "text",
"text": (
"Answer the question based on the image. "
f"The question is: {question}"
),
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{image_base64}"
},
},
]
)
]
# Call the vision-capable model
response = self.vision_llm.invoke(message)
print(f"Image question answer: {response.content}")
return response.content
'''if __name__ == "__main__":
image_question_answer = ImageQuestionAnswer()
image_question_answer.answer("cca530fc-4052-43b2-b130-b30968d8aa44.png", "Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.")''' |