Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -129,7 +129,7 @@ def _engine_hash_mb() -> int:
|
|
| 129 |
v = int(os.environ.get("ENGINE_HASH_MB", "512"))
|
| 130 |
except ValueError:
|
| 131 |
v = 128
|
| 132 |
-
return max(8, min(
|
| 133 |
|
| 134 |
|
| 135 |
async def _get_or_start_engine(engine_path: str, *, role: str, options: Optional[dict] = None):
|
|
@@ -187,15 +187,16 @@ async def _get_or_start_engine(engine_path: str, *, role: str, options: Optional
|
|
| 187 |
raise HTTPException(status_code=500, detail=f"{role} crash: {str(e)}")
|
| 188 |
|
| 189 |
|
| 190 |
-
async def get_deepcastle_engine():
|
|
|
|
| 191 |
return await _get_or_start_engine(
|
| 192 |
DEEPCASTLE_ENGINE_PATH,
|
| 193 |
role="deepcastle",
|
| 194 |
-
options={"Hash":
|
| 195 |
)
|
| 196 |
|
| 197 |
-
async def get_stockfish_engine():
|
| 198 |
-
return await get_deepcastle_engine()
|
| 199 |
|
| 200 |
|
| 201 |
async def _clear_engine_hash(engine) -> None:
|
|
@@ -549,7 +550,7 @@ async def get_move(request: MoveRequest):
|
|
| 549 |
@app.post("/analysis-move", response_model=MoveResponse)
|
| 550 |
async def get_analysis_move(request: MoveRequest):
|
| 551 |
try:
|
| 552 |
-
engine = await get_stockfish_engine()
|
| 553 |
board = chess.Board(request.fen)
|
| 554 |
limit = chess.engine.Limit(time=request.time, depth=request.depth)
|
| 555 |
tsec = _search_timeout_sec(request.time, request.depth)
|
|
@@ -772,7 +773,7 @@ def get_move_classification(
|
|
| 772 |
@app.post("/analyze-game", response_model=AnalyzeResponse)
|
| 773 |
async def analyze_game(request: AnalyzeRequest):
|
| 774 |
try:
|
| 775 |
-
engine = await get_stockfish_engine()
|
| 776 |
board = chess.Board(request.start_fen) if request.start_fen else chess.Board()
|
| 777 |
limit = chess.engine.Limit(time=request.time_per_move)
|
| 778 |
|
|
@@ -785,6 +786,10 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 785 |
engine.analyse(board, limit, multipv=2),
|
| 786 |
ply_timeout,
|
| 787 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
infos_before = infos_before if isinstance(infos_before, list) else [infos_before]
|
| 789 |
|
| 790 |
counts = {
|
|
@@ -839,11 +844,15 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 839 |
fen_window.pop(0)
|
| 840 |
|
| 841 |
async with _ENGINE_IO_LOCK:
|
|
|
|
| 842 |
infos_after_raw = await _engine_call(
|
| 843 |
engine,
|
| 844 |
engine.analyse(board, limit, multipv=2),
|
| 845 |
ply_timeout,
|
| 846 |
)
|
|
|
|
|
|
|
|
|
|
| 847 |
infos_after: List[dict] = infos_after_raw if isinstance(infos_after_raw, list) else [infos_after_raw]
|
| 848 |
|
| 849 |
info_after_dict: dict = infos_after[0]
|
|
|
|
| 129 |
v = int(os.environ.get("ENGINE_HASH_MB", "512"))
|
| 130 |
except ValueError:
|
| 131 |
v = 128
|
| 132 |
+
return max(8, min(2048, v))
|
| 133 |
|
| 134 |
|
| 135 |
async def _get_or_start_engine(engine_path: str, *, role: str, options: Optional[dict] = None):
|
|
|
|
| 187 |
raise HTTPException(status_code=500, detail=f"{role} crash: {str(e)}")
|
| 188 |
|
| 189 |
|
| 190 |
+
async def get_deepcastle_engine(hash_mb: Optional[int] = None):
|
| 191 |
+
h = hash_mb if hash_mb is not None else _engine_hash_mb()
|
| 192 |
return await _get_or_start_engine(
|
| 193 |
DEEPCASTLE_ENGINE_PATH,
|
| 194 |
role="deepcastle",
|
| 195 |
+
options={"Hash": h, "Threads": 1},
|
| 196 |
)
|
| 197 |
|
| 198 |
+
async def get_stockfish_engine(hash_mb: Optional[int] = None):
|
| 199 |
+
return await get_deepcastle_engine(hash_mb=hash_mb)
|
| 200 |
|
| 201 |
|
| 202 |
async def _clear_engine_hash(engine) -> None:
|
|
|
|
| 550 |
@app.post("/analysis-move", response_model=MoveResponse)
|
| 551 |
async def get_analysis_move(request: MoveRequest):
|
| 552 |
try:
|
| 553 |
+
engine = await get_stockfish_engine(hash_mb=2048)
|
| 554 |
board = chess.Board(request.fen)
|
| 555 |
limit = chess.engine.Limit(time=request.time, depth=request.depth)
|
| 556 |
tsec = _search_timeout_sec(request.time, request.depth)
|
|
|
|
| 773 |
@app.post("/analyze-game", response_model=AnalyzeResponse)
|
| 774 |
async def analyze_game(request: AnalyzeRequest):
|
| 775 |
try:
|
| 776 |
+
engine = await get_stockfish_engine(hash_mb=2048)
|
| 777 |
board = chess.Board(request.start_fen) if request.start_fen else chess.Board()
|
| 778 |
limit = chess.engine.Limit(time=request.time_per_move)
|
| 779 |
|
|
|
|
| 786 |
engine.analyse(board, limit, multipv=2),
|
| 787 |
ply_timeout,
|
| 788 |
)
|
| 789 |
+
# Restart after initial evaluation to clear memory
|
| 790 |
+
await _detach_and_quit_engine(engine)
|
| 791 |
+
force_memory_release()
|
| 792 |
+
|
| 793 |
infos_before = infos_before if isinstance(infos_before, list) else [infos_before]
|
| 794 |
|
| 795 |
counts = {
|
|
|
|
| 844 |
fen_window.pop(0)
|
| 845 |
|
| 846 |
async with _ENGINE_IO_LOCK:
|
| 847 |
+
engine = await get_stockfish_engine(hash_mb=2048)
|
| 848 |
infos_after_raw = await _engine_call(
|
| 849 |
engine,
|
| 850 |
engine.analyse(board, limit, multipv=2),
|
| 851 |
ply_timeout,
|
| 852 |
)
|
| 853 |
+
# Restart engine after each move in full game review
|
| 854 |
+
await _detach_and_quit_engine(engine)
|
| 855 |
+
force_memory_release()
|
| 856 |
infos_after: List[dict] = infos_after_raw if isinstance(infos_after_raw, list) else [infos_after_raw]
|
| 857 |
|
| 858 |
info_after_dict: dict = infos_after[0]
|