Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,54 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import chess
|
| 3 |
-
import chess.svg
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
-
import cairosvg
|
| 7 |
-
|
| 8 |
-
from main import get_moves # your engine wrapper
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def predict_from_image(img):
|
| 12 |
-
# Save uploaded image
|
| 13 |
-
img = img.convert("RGB")
|
| 14 |
-
img.save("board.png", format="PNG")
|
| 15 |
-
|
| 16 |
-
# Your function should return (fen, moves)
|
| 17 |
-
fen, moves = get_moves("board.png")
|
| 18 |
-
|
| 19 |
-
# Convert FEN -> SVG -> PNG for Gradio display
|
| 20 |
-
board = chess.Board(fen)
|
| 21 |
-
svg_data = chess.svg.board(board=board, size=350)
|
| 22 |
-
|
| 23 |
-
# convert svg to png (since Gradio Image widget expects raster)
|
| 24 |
-
png_bytes = cairosvg.svg2png(bytestring=svg_data)
|
| 25 |
-
board_img = Image.open(io.BytesIO(png_bytes))
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import chess
|
| 3 |
+
import chess.svg
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import cairosvg
|
| 7 |
+
|
| 8 |
+
from main import get_moves # your engine wrapper
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def predict_from_image(img):
|
| 12 |
+
# Save uploaded image
|
| 13 |
+
img = img.convert("RGB")
|
| 14 |
+
img.save("board.png", format="PNG")
|
| 15 |
+
|
| 16 |
+
# Your function should return (fen, moves)
|
| 17 |
+
fen, moves = get_moves("board.png")
|
| 18 |
+
|
| 19 |
+
# Convert FEN -> SVG -> PNG for Gradio display
|
| 20 |
+
board = chess.Board(fen)
|
| 21 |
+
svg_data = chess.svg.board(board=board, size=350)
|
| 22 |
+
|
| 23 |
+
# convert svg to png (since Gradio Image widget expects raster)
|
| 24 |
+
png_bytes = cairosvg.svg2png(bytestring=svg_data)
|
| 25 |
+
board_img = Image.open(io.BytesIO(png_bytes))
|
| 26 |
+
white_moves_clean = [m[1] for m in moves_white]
|
| 27 |
+
black_moves_clean = [m[1] for m in moves_black]
|
| 28 |
+
|
| 29 |
+
return fen, "\n".join(white_moves_clean), "\n".join(black_moves_clean), board_img
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# ♟️ Chess AI from Image")
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
image = gr.Image(type="pil", label="Upload Chessboard Image")
|
| 38 |
+
board_display = gr.Image(type="pil", label="Detected Board")
|
| 39 |
+
|
| 40 |
+
fen_out = gr.Textbox(label="Detected FEN")
|
| 41 |
+
moves_out = gr.Textbox(label="Predicted Best Moves")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
moves_white = gr.Textbox(label="White's Best Moves", lines=6)
|
| 44 |
+
moves_black = gr.Textbox(label="Black's Best Moves", lines=6)
|
| 45 |
+
|
| 46 |
+
btn = gr.Button("Analyze Board")
|
| 47 |
+
|
| 48 |
+
btn.click(
|
| 49 |
+
fn=predict_from_image,
|
| 50 |
+
inputs=image,
|
| 51 |
+
outputs=[fen_out, moves_white, moves_black, board_display]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|