Spaces:
Sleeping
Sleeping
File size: 2,213 Bytes
1c14abd 5f63a4c 1c14abd 4082859 992c62f a7ef237 c623f30 1c14abd abd0ef6 1c14abd a7ef237 1c14abd 56c430a 1c14abd 35318a3 abffb61 992c62f 1b0b6ed 1c14abd 13f31e3 1c14abd 1361e95 1c14abd 1cf59de |
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 |
from langchain_core.tools import tool
from image_to_text_tool import image_to_text
from utils import get_base64
from typing import Literal, Dict
from utils import get_base64
import requests
import json
@tool
def chess_image_to_fen(image_path_in_base64:str, current_player: Literal["black", "white"]) -> Dict[str,str]:
"""
Convert chess image to FEN (Forsyth-Edwards Notation) notation.
Args:
image_path_in_base64: Path to the image file in base64 format.
current_player: Whose turn it is to play. Must be either 'black' or 'white'.
Returns:
JSON with FEN (Forsyth-Edwards Notation) string representing the current board position. Use this to find the best move.
"""
print(f"Image to Fen invocada com os seguintes parametros:")
print(f"image_path: {image_path_in_base64}")
print(f"current_player: {current_player}")
CHESSVISION_TO_FEN_URL = "http://app.chessvision.ai/predict"
if current_player not in ["black", "white"]:
raise ValueError("current_player must be 'black' or 'white'")
print('Reading chess image in base 64: ' + image_path_in_base64)
base64_image = get_base64(image_path_in_base64)
print("content in base64:\n\n" + base64_image)
if not base64_image:
raise ValueError("Failed to encode image to base64.")
base64_image_encoded = f"data:image/png;base64,{base64_image}"
url = CHESSVISION_TO_FEN_URL
payload = {
"board_orientation": "predict",
"cropped": False,
"current_player": current_player,
"image": base64_image_encoded,
"predict_turn": False
}
response = requests.post(url, json=payload)
if response.status_code == 200:
dados = response.json()
if dados.get("success"):
print(f"Retorno Chessvision {dados}")
fen = dados.get("result")
fen = fen.replace("_", " ") #retorna _ no lugar de espaço em branco
return json.dumps({"fen": fen})
else:
raise Exception("Requisição feita, mas falhou na predição.")
else:
raise Exception("Deu erro na chamada a API para obtencao do FEN: " + response.text) |