Spaces:
Sleeping
Sleeping
| import constants | |
| import google.generativeai as genai | |
| from langchain_core.tools import tool | |
| from PIL import Image | |
| import io | |
| 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 |