This project provides a Flask-based server to predict chess moves using a pre-trained TensorFlow model or simply display the live board state without predictions. The server can be interacted with via a simple JavaScript client.
- Update and display the current chess board state.
- Predict the next best move using a pre-trained TensorFlow model (optional).
- Easy integration with a web client using JavaScript.
- Python =< 3.11
- Flask
- TensorFlow
- NumPy
- python-chess
-
Clone the repository:
git clone https://github.com/yourusername/chess-prediction-server.git cd chess-prediction-server
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages:
pip install flask flask-cors python-chess numpy tensorflow
-
Place the model and move encoder files:
- Ensure that
hikaru_chess_model_v2.h5
andmove_encoder_classes_v2.npy
are in the 'model/' directory of the project.
- Ensure that
You can run the server in two modes:
- Default mode: Only updates the live board state without predictions, for your own projects.
- Prediction mode: Uses a pre-trained TensorFlow model to predict the next best move.
python app.py default
python app.py prediction
The server will start running at http://localhost:5000
.
To interact with the Flask server, you can use the provided JavaScript code. We recommend using Google Chrome and the F12 web console for testing and debugging.
Place the following JavaScript code in your web page:
// Define the getBoardState function
function getBoardState() {
const pieces = document.querySelectorAll('.piece');
let boardState = [];
pieces.forEach(piece => {
let pieceClasses = Array.from(piece.classList);
let pieceClass = pieceClasses.find(cls => cls.length === 2); // 'wr', 'bp', etc.
let positionClass = pieceClasses.find(cls => cls.startsWith('square-')); // 'square-11'
if (!pieceClass || !positionClass) return;
let position = positionClass.split('-')[1];
let pieceType = pieceClass[1]; // 'r' for rook, 'p' for pawn, etc.
let color = pieceClass[0]; // 'w' for white, 'b' for black
pieceType = (color === 'w' ? pieceType.toUpperCase() : pieceType.toLowerCase());
boardState.push({ position: position, piece: pieceType });
});
return boardState;
}
// Define the sendBoardStateToPython function
function sendBoardStateToPython() {
let boardState = getBoardState();
fetch('http://localhost:5000/update_board', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(boardState),
}).then(response => response.json())
.then(data => console.log(data));
}
// Set an interval to regularly update the board state and store the interval ID
let intervalID = setInterval(sendBoardStateToPython, 1000);
// Function to stop the interval loop (does not currently work, just refresh the page)
function stopInterval() {
clearInterval(intervalID);
console.log('Interval stopped.');
}
- Open your web page in Google Chrome.
- Press
F12
to open the web console. - Ensure that the chess board on your web page has the correct class names for pieces and squares.
- The script will send the board state to the Flask server every second and log the server's response.
This project is licensed under the MIT License - see the LICENSE file for details.