Spaces:
Build error
Build error
File size: 2,289 Bytes
343fa36 5e4365f 343fa36 5e4365f 343fa36 5e4365f 343fa36 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
"""
Gradio interface for plotting a board.
"""
import chess
import gradio as gr
from demo import constants
from lczerolens.board import LczeroBoard
def make_board_plot(board_fen, arrows, square):
try:
board = LczeroBoard(board_fen)
except ValueError:
board = LczeroBoard()
gr.Warning("Invalid FEN, using starting position.")
try:
if arrows:
arrows_list = arrows.split(" ")
chess_arrows = []
for arrow in arrows_list:
from_square, to_square = arrow[:2], arrow[2:]
chess_arrows.append(
(
chess.parse_square(from_square),
chess.parse_square(to_square),
)
)
else:
chess_arrows = []
except ValueError:
chess_arrows = []
gr.Warning("Invalid arrows, using none.")
color_dict = {chess.parse_square(square): "#FF0000"} if square else {}
svg_board = chess.svg.board(
board,
size=350,
arrows=chess_arrows,
fill=color_dict,
)
with open(f"{constants.FIGURE_DIRECTORY}/board.svg", "w") as f:
f.write(svg_board)
return f"{constants.FIGURE_DIRECTORY}/board.svg"
with gr.Blocks() as interface:
with gr.Row():
with gr.Column():
board_fen = gr.Textbox(
label="Board starting FEN",
lines=1,
max_lines=1,
value=chess.STARTING_FEN,
)
arrows = gr.Textbox(
label="Arrows",
lines=1,
max_lines=1,
value="",
placeholder="e2e4 e7e5",
)
square = gr.Textbox(
label="Square",
lines=1,
max_lines=1,
value="",
placeholder="e4",
)
with gr.Column():
image = gr.Image(label="Board", interactive=False)
inputs = [
board_fen,
arrows,
square,
]
board_fen.submit(make_board_plot, inputs=inputs, outputs=image)
arrows.submit(make_board_plot, inputs=inputs, outputs=image)
interface.load(make_board_plot, inputs=inputs, outputs=image)
|