Commit
·
88cd6e7
1
Parent(s):
a8bc431
Deployment Fixes
Browse files- .gitignore +2 -0
- Dockerfile +11 -0
- Model_Handler.py +98 -0
- app.py +20 -0
- model.h5 +3 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
/__pycache__
|
2 |
+
/venv
|
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
Model_Handler.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import chess
|
3 |
+
import tensorflow as tf
|
4 |
+
from keras.models import load_model
|
5 |
+
|
6 |
+
class Model_handler :
|
7 |
+
def __init__(self) :
|
8 |
+
self.model = load_model("model.h5")
|
9 |
+
print("Model loaded and is ready to use !!!")
|
10 |
+
|
11 |
+
def predict(self, state, color) :
|
12 |
+
prediction = self.model(self.convert_state_to_input(state, color))
|
13 |
+
return self.convert_output_to_probs(state, color, prediction[1][0])
|
14 |
+
|
15 |
+
def convert_state_to_input(self, state, color) :
|
16 |
+
if type(state) == str :
|
17 |
+
temp = state.split("_")
|
18 |
+
temp_arr = np.zeros((8,8,12), dtype = np.float16)
|
19 |
+
arr2 = np.zeros((8,8,10), dtype = np.float16)
|
20 |
+
temp_arr, arr2 = self.convert_board_to_input(temp[-1], color)
|
21 |
+
for i in range(1,5) :
|
22 |
+
temp_arr = np.concatenate([self.convert_board_to_input(temp[-1-i], color, False), temp_arr], axis = 2)
|
23 |
+
return (np.expand_dims(temp_arr, axis = 0), np.expand_dims(arr2, axis = 0))
|
24 |
+
|
25 |
+
def convert_board_to_input(self, state, color, current = True) :
|
26 |
+
if current :
|
27 |
+
board = chess.Board(state)
|
28 |
+
board.turn = color
|
29 |
+
arr = np.zeros((8,8,12), dtype = np.float16)
|
30 |
+
arr2 = np.zeros((8,8,10), dtype = np.float16)
|
31 |
+
piece_to_value = self.get_piece_to_value(color)
|
32 |
+
piece_to_value2 = self.get_piece_to_value(color, False)
|
33 |
+
for i in range(64) :
|
34 |
+
if(board.piece_at(i) is not None) :
|
35 |
+
arr[i//8,i%8,piece_to_value[board.piece_at(i).symbol()]] = 1
|
36 |
+
for move in board.legal_moves :
|
37 |
+
square = move.to_square
|
38 |
+
arr[square//8, square%8, piece_to_value[board.piece_at(move.from_square).symbol()]] = 0.5
|
39 |
+
symbol = board.piece_at(move.from_square).symbol()
|
40 |
+
if move.promotion is not None :
|
41 |
+
arr2[move.promotion-2, move.from_square%8, 9] = 1
|
42 |
+
else :
|
43 |
+
arr2[square//8, square%8, piece_to_value2[symbol]] = 1
|
44 |
+
if(piece_to_value2[symbol] == 1 or piece_to_value2[symbol] == 3 or piece_to_value2[symbol] == 5) :
|
45 |
+
piece_to_value2[symbol] += 1
|
46 |
+
return (arr, arr2)
|
47 |
+
else :
|
48 |
+
arr = np.zeros((8,8,12), dtype = np.float16)
|
49 |
+
if len(state) == 0 :
|
50 |
+
return arr
|
51 |
+
board = chess.Board(state)
|
52 |
+
board.turn = color
|
53 |
+
piece_to_value = self.get_piece_to_value(color)
|
54 |
+
for i in range(64) :
|
55 |
+
if(board.piece_at(i) is not None) :
|
56 |
+
arr[i//8,i%8,piece_to_value[board.piece_at(i).symbol()]] = 1
|
57 |
+
return arr
|
58 |
+
|
59 |
+
def convert_output_to_probs(self, state, color, policy_output) :
|
60 |
+
policy = tf.reshape(policy_output, [8,8,10])
|
61 |
+
board = chess.Board(state.split("_")[-1])
|
62 |
+
board.turn = color
|
63 |
+
piece_to_value = self.get_piece_to_value(color,False)
|
64 |
+
move_dict = {}
|
65 |
+
for move in list(board.legal_moves) :
|
66 |
+
to_square = move.to_square
|
67 |
+
from_square = move.from_square
|
68 |
+
piece_type = piece_to_value[board.piece_at(from_square).symbol()]
|
69 |
+
if move.promotion is not None :
|
70 |
+
move_dict[move.uci()] = policy[move.promotion-2, from_square%8, 9]
|
71 |
+
else :
|
72 |
+
move_dict[move.uci()] = policy[to_square//8, to_square%8, piece_type]
|
73 |
+
if(piece_type == 1 or piece_type == 3 or piece_type == 5) :
|
74 |
+
piece_to_value[board.piece_at(from_square).symbol()] += 1
|
75 |
+
# print(list(board.legal_moves))
|
76 |
+
move = [item[0] for item in sorted(move_dict.items(), key = lambda x: x[1], reverse = True)][0]
|
77 |
+
print(move_dict[move])
|
78 |
+
return move
|
79 |
+
|
80 |
+
def get_piece_to_value(self, color, inp = True) :
|
81 |
+
if inp :
|
82 |
+
if(color == 1) :
|
83 |
+
return {
|
84 |
+
'P': 0, 'N': 1, 'B': 2, 'R': 3, 'Q': 4, 'K': 5,
|
85 |
+
'p': 6, 'n':7, 'b': 8, 'r': 9, 'q': 10, 'k': 11
|
86 |
+
}
|
87 |
+
return {
|
88 |
+
'p': 0, 'n': 1, 'b': 2, 'r': 3, 'q': 4, 'k': 5,
|
89 |
+
'P': 6, 'N':7, 'B': 8, 'R': 9, 'Q': 10, 'K': 11
|
90 |
+
}
|
91 |
+
else :
|
92 |
+
if(color == 1) :
|
93 |
+
return {
|
94 |
+
'P': 0, 'N': 1, 'B': 3, 'R': 5, 'Q': 7, 'K': 8
|
95 |
+
}
|
96 |
+
return {
|
97 |
+
'p': 0, 'n': 1, 'b': 3, 'r': 5, 'q': 7, 'k': 8
|
98 |
+
}
|
app.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Model_Handler import Model_handler
|
2 |
+
from flask import Flask, request
|
3 |
+
from flask_cors import CORS
|
4 |
+
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
CORS(app)
|
8 |
+
model = Model_handler()
|
9 |
+
|
10 |
+
@app.route('/')
|
11 |
+
def hello_world():
|
12 |
+
return 'Hello World'
|
13 |
+
|
14 |
+
@app.route('/generateMove', methods = ["POST"])
|
15 |
+
def generate_move() :
|
16 |
+
data = request.get_json()
|
17 |
+
return model.predict(data['state'], data['turn'])
|
18 |
+
|
19 |
+
if __name__ == '__main__':
|
20 |
+
app.run()
|
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d6b8f63730ca5d909416180bb22134db39575afc2a1b986b7da0c74fd43589d
|
3 |
+
size 20462528
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.13.0
|
2 |
+
numpy
|
3 |
+
python-chess
|
4 |
+
Flask
|
5 |
+
Flask-Cors
|
6 |
+
requests
|
7 |
+
gunicorn
|