File size: 1,926 Bytes
009dd66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23ac248
 
009dd66
 
 
 
 
 
 
 
5c8b0c5
 
 
 
 
 
 
009dd66
 
 
 
 
 
 
 
5c8b0c5
 
009dd66
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import chess
import chess.svg
from PIL import Image
import io
import cairosvg

from main import get_moves   # your engine wrapper


def predict_from_image(img):
    # Save uploaded image
    img = img.convert("RGB")
    img.save("board.png", format="PNG")

    # Your function should return (fen, moves)
    fen, moves = get_moves("board.png")

    # Convert FEN -> SVG -> PNG for Gradio display
    board = chess.Board(fen)
    svg_data = chess.svg.board(board=board, size=350)

    # convert svg to png (since Gradio Image widget expects raster)
    png_bytes = cairosvg.svg2png(bytestring=svg_data)
    board_img = Image.open(io.BytesIO(png_bytes))
    moves_white = moves['white_moves']
    moves_black=moves['black_moves']
    white_moves_clean = [m[1] for m in moves_white]
    black_moves_clean = [m[1] for m in moves_black]

    return fen, "\n".join(white_moves_clean), "\n".join(black_moves_clean), board_img



with gr.Blocks() as demo:
    gr.Markdown("""
        #  chess move predictor using alpha beta pruning
        _Upload an image of an online chessboard and get the FEN & best moves._  

        ⚠️ **Warning:** Please upload screenshots from online chess games (e.g., Lichess, Chess.com).  
        Physical/real-world chessboard photos are not supported yet.
        """)

    with gr.Row():
        image = gr.Image(type="pil", label="Upload Chessboard Image")
        board_display = gr.Image(type="pil", label="Detected Board")

    fen_out = gr.Textbox(label="Detected FEN")
    moves_out = gr.Textbox(label="Predicted Best Moves")
    with gr.Row():
        moves_white = gr.Textbox(label="White's Moves", lines=6)
        moves_black = gr.Textbox(label="Black's Moves", lines=6)

    btn = gr.Button("Analyze Board")

    btn.click(
        fn=predict_from_image,
        inputs=image,
        outputs=[fen_out, moves_white, moves_black, board_display]
    )

demo.launch()