Spaces:
Sleeping
Sleeping
Update tools/chess_tools.py
Browse files- tools/chess_tools.py +22 -77
tools/chess_tools.py
CHANGED
|
@@ -1,88 +1,33 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
-
class
|
| 6 |
-
name = "
|
| 7 |
-
description = "
|
| 8 |
-
inputs = {}
|
| 9 |
-
output_type = "string"
|
| 10 |
-
|
| 11 |
-
def forward(self) -> str:
|
| 12 |
-
engine_path = shutil.which("stockfish")
|
| 13 |
-
return engine_path or "Stockfish engine not found."
|
| 14 |
-
|
| 15 |
|
| 16 |
-
class ChessboardImageToFENTool(Tool):
|
| 17 |
-
name = "image_to_fen"
|
| 18 |
-
description = "Analyzes a chessboard image and returns its FEN representation."
|
| 19 |
inputs = {
|
| 20 |
"image_url": {
|
| 21 |
"type": "string",
|
| 22 |
-
"description": "
|
| 23 |
}
|
| 24 |
}
|
| 25 |
output_type = "string"
|
| 26 |
|
| 27 |
-
def __init__(self, endpoint_url: str, hf_token: str, **kwargs):
|
| 28 |
-
super().__init__(**kwargs)
|
| 29 |
-
self.endpoint_url = endpoint_url
|
| 30 |
-
self.hf_token = hf_token
|
| 31 |
-
|
| 32 |
def forward(self, image_url: str) -> str:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"content": [
|
| 49 |
-
{
|
| 50 |
-
"type": "input_text",
|
| 51 |
-
"text": (
|
| 52 |
-
"Using the extracted pieces, list each one with its position in format <piece><square> (e.g., Kd4).\n"
|
| 53 |
-
"Use uppercase for white, lowercase for black. Output only the lines with positions."
|
| 54 |
-
)
|
| 55 |
-
}
|
| 56 |
-
]
|
| 57 |
-
}
|
| 58 |
-
]
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
response = requests.post(self.endpoint_url, headers=headers, json=payload)
|
| 62 |
-
response.raise_for_status()
|
| 63 |
-
raw_text = response.json().get("generated_text", "")
|
| 64 |
-
|
| 65 |
-
squares = {}
|
| 66 |
-
for line in raw_text.splitlines():
|
| 67 |
-
line = line.strip()
|
| 68 |
-
if len(line) == 3:
|
| 69 |
-
squares[line[1:3]] = line[0]
|
| 70 |
-
|
| 71 |
-
fen_rows = []
|
| 72 |
-
for rank in range(8, 0, -1):
|
| 73 |
-
row = ""
|
| 74 |
-
empty = 0
|
| 75 |
-
for file in "abcdefgh":
|
| 76 |
-
pos = f"{file}{rank}"
|
| 77 |
-
if pos in squares:
|
| 78 |
-
if empty > 0:
|
| 79 |
-
row += str(empty)
|
| 80 |
-
empty = 0
|
| 81 |
-
row += squares[pos]
|
| 82 |
-
else:
|
| 83 |
-
empty += 1
|
| 84 |
-
if empty:
|
| 85 |
-
row += str(empty)
|
| 86 |
-
fen_rows.append(row)
|
| 87 |
-
|
| 88 |
-
return "/".join(fen_rows)
|
|
|
|
| 1 |
+
from tools.tool_base import BaseTool
|
| 2 |
+
from PIL import Image
|
| 3 |
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
|
| 6 |
+
class ImageToChessBoardFENTool(BaseTool):
|
| 7 |
+
name = "image_to_chess_fen"
|
| 8 |
+
description = "Extracts the FEN representation of a chess board from an image."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
inputs = {
|
| 11 |
"image_url": {
|
| 12 |
"type": "string",
|
| 13 |
+
"description": "The URL of the chessboard image.",
|
| 14 |
}
|
| 15 |
}
|
| 16 |
output_type = "string"
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def forward(self, image_url: str) -> str:
|
| 19 |
+
try:
|
| 20 |
+
response = requests.get(image_url)
|
| 21 |
+
response.raise_for_status()
|
| 22 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Failed to load image: {e}"
|
| 25 |
+
|
| 26 |
+
# This is a placeholder implementation.
|
| 27 |
+
# You should replace it with an actual model or logic that processes the image and returns the FEN.
|
| 28 |
+
fen = self._mock_fen_extraction(image)
|
| 29 |
+
return fen
|
| 30 |
+
|
| 31 |
+
def _mock_fen_extraction(self, image):
|
| 32 |
+
# Replace this logic with your actual FEN model prediction if available.
|
| 33 |
+
return "r1bqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|