-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
50 lines (40 loc) · 1.57 KB
/
backend.py
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
import threading
from flask import Flask, jsonify, render_template, request
import chess
import chess.engine
app = Flask(__name__)
STOCKFISH_PATH = "/usr/local/bin/stockfish"
engine_eval = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
engine_move = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
eval_lock = threading.Lock()
move_lock = threading.Lock()
@app.route('/')
def index():
return render_template('index.html')
@app.route("/evaluation", methods=["POST"])
def evaluation():
data = request.get_json()
fen = data['fen']
board = chess.Board(fen)
with eval_lock: # Lock only for the evaluation engine
try:
result = engine_eval.analyse(board, chess.engine.Limit(time=2.0)) # Analyze for 2 seconds
eval_score = result['score'].relative.score(mate_score=10000) # Get evaluation score
return jsonify({"evaluation": eval_score})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/best-move", methods=["POST"])
def best_move():
data = request.get_json()
fen = data['fen']
board = chess.Board(fen)
with move_lock: # Lock only for the best move engine
try:
result = engine_move.play(board, chess.engine.Limit(time=2.0)) # Analyze for 2 seconds
best_move = result.move.uci()
san_move = board.san(result.move)
return jsonify({"best_move": best_move, "san_move": san_move})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(port=3000)