ashish-001 commited on
Commit
aefc823
·
verified ·
1 Parent(s): db3dcee

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +98 -0
  2. requirements.txt +3 -0
  3. test.h5 +3 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chess
2
+ import chess.svg
3
+ import gradio as gr
4
+ import tempfile
5
+ import tensorflow as tf
6
+ import numpy as np
7
+
8
+ # Initializing the board
9
+ board = chess.Board()
10
+ model = tf.keras.models.load_model('test.h5')
11
+
12
+
13
+ def display_board(board_):
14
+ board_svg = chess.svg.board(board=board_)
15
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".svg") as f:
16
+ f.write(board_svg.encode('utf-8'))
17
+ return f.name
18
+
19
+
20
+ def one_hot_encode_pieces(piece):
21
+ pieces = list('rnbqkpRNBQKP.')
22
+ arr = np.zeros(len(pieces))
23
+ piece_to_index = {p: i for i, p in enumerate(pieces)}
24
+ index = piece_to_index[piece]
25
+ arr[index] = 1
26
+ return arr
27
+
28
+
29
+ def encode_board(board__):
30
+ board_str = str(board__)
31
+ board_str = board_str.replace(" ", "")
32
+ board_list = []
33
+ for row in board_str.split("\n"):
34
+ row_list = []
35
+ for i in row:
36
+ row_list.append(one_hot_encode_pieces(i))
37
+ board_list.append(row_list)
38
+ return np.array(board_list)
39
+
40
+
41
+ def play_nn(curr_fen, model):
42
+ board__ = chess.Board(curr_fen)
43
+ moves = []
44
+ input_vectors = []
45
+ for move in board__.legal_moves:
46
+ curr_board = board.copy()
47
+ moves.append(move)
48
+ curr_board.push(move)
49
+ input_vectors.append(encode_board(curr_board))
50
+
51
+ input_vectors = np.stack(input_vectors)
52
+ scores = model.predict(input_vectors, verbose=0)
53
+ if board.turn == chess.BLACK:
54
+ return moves[np.argmax(scores)]
55
+ else:
56
+ return moves[np.argmax(~scores)]
57
+
58
+
59
+ def make_move(move):
60
+ global board
61
+ if not board.outcome():
62
+ if len(move) > 3:
63
+ turn = "White"
64
+ new_turn = "Black"
65
+ if chess.Move.from_uci(move) in board.legal_moves:
66
+ board.push(chess.Move.from_uci(move))
67
+ ai_move = str(play_nn(board.fen(), model))
68
+ board.push(chess.Move.from_uci(ai_move))
69
+ message = f"{turn} move: {move},move accepted\nAI moved: {ai_move}\n\n{turn}'s move."
70
+ else:
71
+ message = f"{turn} move: Illegal move. Please try again."
72
+ return message, display_board(board)
73
+ else:
74
+ winner="White" if board.outcome()==chess.WHITE else "Black"
75
+ return f"{winner} won",display_board(board)
76
+
77
+ def reset_board():
78
+ global board
79
+ board = chess.Board()
80
+ return "Board reset.", display_board(board)
81
+
82
+
83
+ # Setting up gradio interface
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("## Chess board game")
86
+ with gr.Row():
87
+ with gr.Column():
88
+ move_input = gr.Textbox(placeholder="Enter your move in UCI format", lines=1)
89
+ move_button = gr.Button("Make move")
90
+ reset_button = gr.Button("Reset Board")
91
+ status_output = gr.Textbox(label='Status', interactive=False)
92
+ with gr.Column():
93
+ board_output = gr.Image(type='filepath', label='Chess Board',value=display_board(board))
94
+
95
+
96
+ move_button.click(make_move, inputs=move_input, outputs=[status_output, board_output])
97
+ reset_button.click(reset_board, inputs=None, outputs=[status_output, board_output])
98
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ chess==1.10.0
2
+ numpy==1.23.5
3
+ tensorflow==2.15.0
test.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ccb7e8338114c82bad7d1031c4e1458e4214bb9fee2ff83d74a03b8aa3fa345d
3
+ size 5152472