Amogh1221 commited on
Commit
7111f1d
Β·
verified Β·
1 Parent(s): 60b735a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -24
main.py CHANGED
@@ -86,19 +86,20 @@ class MoveResponse(BaseModel):
86
  nps: int
87
  pv: str
88
  mate_in: Optional[int] = None
 
89
 
90
- # ─── New Analysis Types ────────────────────────────────────────────────────────
91
  class AnalyzeRequest(BaseModel):
92
- moves: List[str] # e.g., ["e4", "e5", "Nf3", "Nc6", ...]
93
- time_per_move: float = 0.1 # quick eval per move
94
  player_color: str = "white"
95
  start_fen: Optional[str] = None
96
 
97
  class MoveAnalysis(BaseModel):
98
  move_num: int
99
  san: str
100
- fen: str
101
- classification: str # Best, Excellent, Good, Inaccuracy, Mistake, Blunder, Brilliant
 
102
  cpl: float
103
  score_before: float
104
  score_after: float
@@ -176,6 +177,10 @@ async def get_move(request: MoveRequest):
176
  # Map mate score to pawns representation to not break old UI
177
  score_pawns = score_cp / 100.0 if abs(score_cp) < 9900 else (100.0 if score_cp > 0 else -100.0)
178
 
 
 
 
 
179
  return MoveResponse(
180
  bestmove=result.move.uci(),
181
  score=score_pawns,
@@ -183,7 +188,8 @@ async def get_move(request: MoveRequest):
183
  nodes=nodes,
184
  nps=nps,
185
  pv=pv,
186
- mate_in=mate_in
 
187
  )
188
  except Exception as e:
189
  print(f"Error: {e}")
@@ -306,9 +312,9 @@ def get_move_classification(
306
  is_white_move: bool,
307
  played_move: chess.Move,
308
  best_move_before: chess.Move,
309
- alt_win_pct: float,
310
- fen_two_moves_ago: str,
311
- uci_next_two_moves: tuple,
312
  board_before_move: chess.Board,
313
  best_pv_after: list
314
  ) -> str:
@@ -375,13 +381,15 @@ async def analyze_game(request: AnalyzeRequest):
375
  except Exception:
376
  break # Invalid move
377
 
378
- info_before = infos_before[0]
379
- win_pct_before = get_win_percentage(info_before)
380
- best_pv_before = info_before.get("pv", [])
381
- best_move_before = best_pv_before[0] if best_pv_before else None
382
 
383
- alt_win_pct_before = None
 
 
384
  if len(infos_before) > 1:
 
385
  for line in infos_before:
386
  if line.get("pv") and line.get("pv")[0] != move:
387
  alt_win_pct_before = get_win_percentage(line)
@@ -393,15 +401,16 @@ async def analyze_game(request: AnalyzeRequest):
393
  move_history.append(move)
394
  fen_history.append(board.fen())
395
 
396
- infos_after = await engine.analyse(board, limit, multipv=2)
397
- infos_after = infos_after if isinstance(infos_after, list) else [infos_after]
398
- info_after = infos_after[0]
 
399
 
400
- win_pct_after = get_win_percentage(info_after)
401
- score_after, _ = get_normalized_score(info_after)
402
  current_score = score_after
403
 
404
- best_pv_after = info_after.get("pv", [])
405
 
406
  fen_two_moves_ago = None
407
  uci_next_two_moves = None
@@ -410,10 +419,11 @@ async def analyze_game(request: AnalyzeRequest):
410
  uci_next_two_moves = (move_history[-2], move_history[-1])
411
 
412
  cls = "Book"
 
413
  board_fen_only = board.fen().split(" ")[0]
414
  if board_fen_only in openings_db:
415
- # Intercept with exact chesskit Book classification logic
416
  cls = "Book"
 
417
  else:
418
  cls = get_move_classification(
419
  last_win_pct=win_pct_before,
@@ -442,9 +452,11 @@ async def analyze_game(request: AnalyzeRequest):
442
  san=san_move,
443
  fen=board.fen(),
444
  classification=cls,
445
- cpl=cpl,
446
- score_before=score_before / 100.0,
447
- score_after=score_after / 100.0
 
 
448
  ))
449
 
450
  infos_before = infos_after
 
86
  nps: int
87
  pv: str
88
  mate_in: Optional[int] = None
89
+ opening: Optional[str] = None
90
 
 
91
  class AnalyzeRequest(BaseModel):
92
+ moves: List[str]
93
+ time_per_move: float = 0.1
94
  player_color: str = "white"
95
  start_fen: Optional[str] = None
96
 
97
  class MoveAnalysis(BaseModel):
98
  move_num: int
99
  san: str
100
+ best_move: str
101
+ classification: str
102
+ opening: Optional[str] = None
103
  cpl: float
104
  score_before: float
105
  score_after: float
 
177
  # Map mate score to pawns representation to not break old UI
178
  score_pawns = score_cp / 100.0 if abs(score_cp) < 9900 else (100.0 if score_cp > 0 else -100.0)
179
 
180
+ # Check for opening name
181
+ board_fen_only = board.fen().split(" ")[0]
182
+ opening_name = openings_db.get(board_fen_only)
183
+
184
  return MoveResponse(
185
  bestmove=result.move.uci(),
186
  score=score_pawns,
 
188
  nodes=nodes,
189
  nps=nps,
190
  pv=pv,
191
+ mate_in=mate_in,
192
+ opening=opening_name
193
  )
194
  except Exception as e:
195
  print(f"Error: {e}")
 
312
  is_white_move: bool,
313
  played_move: chess.Move,
314
  best_move_before: chess.Move,
315
+ alt_win_pct: Optional[float],
316
+ fen_two_moves_ago: Optional[str],
317
+ uci_next_two_moves: Optional[Tuple[chess.Move, chess.Move]],
318
  board_before_move: chess.Board,
319
  best_pv_after: list
320
  ) -> str:
 
381
  except Exception:
382
  break # Invalid move
383
 
384
+ info_dict = infos_before[0]
385
+ pv_list = info_dict.get("pv", [])
386
+ best_move_before = pv_list[0] if pv_list else None
 
387
 
388
+ score_before, _ = get_normalized_score(info_dict)
389
+ win_pct_before = get_win_percentage(info_dict)
390
+ alt_win_pct_before: Optional[float] = None
391
  if len(infos_before) > 1:
392
+ # Find the first alternative move that is not the played move
393
  for line in infos_before:
394
  if line.get("pv") and line.get("pv")[0] != move:
395
  alt_win_pct_before = get_win_percentage(line)
 
401
  move_history.append(move)
402
  fen_history.append(board.fen())
403
 
404
+ infos_after_raw = await engine.analyse(board, limit, multipv=2)
405
+ infos_after: List[dict] = infos_after_raw if isinstance(infos_after_raw, list) else [infos_after_raw]
406
+
407
+ info_after_dict: dict = infos_after[0]
408
 
409
+ win_pct_after = get_win_percentage(info_after_dict)
410
+ score_after, _ = get_normalized_score(info_after_dict)
411
  current_score = score_after
412
 
413
+ best_pv_after = info_after_dict.get("pv", [])
414
 
415
  fen_two_moves_ago = None
416
  uci_next_two_moves = None
 
419
  uci_next_two_moves = (move_history[-2], move_history[-1])
420
 
421
  cls = "Book"
422
+ opening_name = None
423
  board_fen_only = board.fen().split(" ")[0]
424
  if board_fen_only in openings_db:
 
425
  cls = "Book"
426
+ opening_name = openings_db[board_fen_only]
427
  else:
428
  cls = get_move_classification(
429
  last_win_pct=win_pct_before,
 
452
  san=san_move,
453
  fen=board.fen(),
454
  classification=cls,
455
+ cpl=float(cpl),
456
+ score_before=float(score_before / 100.0),
457
+ score_after=float(score_after / 100.0),
458
+ best_move=best_move_before.uci() if best_move_before else "",
459
+ opening=opening_name
460
  ))
461
 
462
  infos_before = infos_after