import constants import google.generativeai as genai from langchain_core.tools import tool from PIL import Image import io import base64 @tool def image_to_text(image_base64_str, instructions): """ Generates a text describing an image provided as a base64 string. Args: image_bytes (bytes): the bytes of the image to de described. instructions (str): instructions to describe the image. Returns: str: A string describing the image according to the instructions given. """ image_bytes = base64.b64decode(image_base64_str) genai.configure(api_key=constants.API_KEY) model = genai.GenerativeModel('gemini-2.5-pro-preview-05-06') response = model.generate_content( [ { "mime_type": "image/jpeg", "data": image_bytes }, instructions ] ) print('generated text from image: ' + response.text) return response.text if __name__ == '__main__': # Example usage: # 1. Load an image from a file (replace with your image path) image_path = r"C:\Users\agazo\Downloads\cca530fc-4052-43b2-b130-b30968d8aa44_file.png" # Replace with a valid image path try: with open(image_path, "rb") as image_file: image_bytes = image_file.read() except FileNotFoundError: print(f"Error: File not found at {image_path}. Please make sure the path is correct and the file exists.") exit() # 2. Call the function text = image_to_text.invoke( { "image_bytes": image_bytes, "instructions": 'Describe the chessboard in this image and provide the FEN notation.' } ) print(f"FEN: {text}") #resultado esperado do FEN; `3r2k1/pp3pp1/4b2p/7Q/3n4/PqBBR2P/5PP1/6K1 w - - 0 1`