File size: 1,688 Bytes
f66d8b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import constants
import google.generativeai as genai
from langchain_core.tools import tool
from PIL import Image
import io

@tool
def image_to_text(image_bytes, instructions):
    """
    Generates a text describing an image.

    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.
    """

    genai.configure(api_key=constants.API_KEY)
    model = genai.GenerativeModel(constants.MODEL)

    response = model.generate_content(
        [
            {
                "mime_type": "image/jpeg",
                "data": image_bytes
             },
            instructions
        ]
    )
    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; 1K6/1PP5/P2RBBqP/4n3/Q7/p2b4/1pp3pp/1k2r3 w - - 0 1
    # 3r2k1/pp4pp/4b2p/Q7/3n4/PqBBR2P/PP4P1/K7 w - - 0 1