Spaces:
Sleeping
Sleeping
File size: 1,820 Bytes
f66d8b7 30003b4 f66d8b7 30003b4 f66d8b7 30003b4 f66d8b7 30003b4 f66d8b7 7953a0e f66d8b7 a419591 f66d8b7 30003b4 |
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 57 58 59 60 |
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` |