test1978 commited on
Commit
1bca279
·
verified ·
1 Parent(s): 5f35e08

update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import numpy as np
3
  from huggingface_hub import hf_hub_download
4
  import os
5
- import traceback
6
 
7
  TOKEN = os.environ.get("HF_TOKEN")
8
 
@@ -38,18 +37,21 @@ zonedb.draw_matrices = list(zonedb_data.get("draw", []))
38
 
39
 
40
  def parse_board(board_text):
41
- rows = [line.strip() for line in board_text.strip().splitlines() if line.strip()]
 
 
42
  if len(rows) != 8:
43
- raise ValueError("Board must have exactly 8 rows.")
 
44
  board = np.zeros((8, 8), dtype=np.int32)
45
  mapping = {".": 0, "1": 1, "2": 2}
46
  for r, line in enumerate(rows):
47
  parts = line.split()
48
  if len(parts) != 8:
49
- raise ValueError("Each row must have 8 space-separated cells.")
50
  for c, cell in enumerate(parts):
51
  if cell not in mapping:
52
- raise ValueError("Use '.', '1', or '2' only.")
53
  board[r, c] = mapping[cell]
54
  return board
55
 
@@ -60,27 +62,30 @@ def move_to_uci(move):
60
  return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
61
 
62
 
63
- def get_move(board_text, player):
64
- try:
65
- game = Breakthrough()
66
- game.board = parse_board(board_text)
67
- game.move_count = 0 if str(player) == "1" else 1
68
- game._cached_matrix = None
69
-
70
- searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
71
- visits, _ = searcher.search_with_time_budget(game, 1.0)
72
-
73
- best_move = max(visits, key=visits.get)
74
- result = move_to_uci(best_move)
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- print(f"[DEBUG] get_move OK: board={board_text} player={player} -> {result}")
77
- return result
78
-
79
- except Exception as exc:
80
- print(f"[ERROR] get_move failed: {exc}")
81
- traceback.print_exc()
82
- # Return a dummy move so Gradio at least sends something
83
- return "a1a2"
84
 
85
  demo = gr.Interface(
86
  fn=get_move,
@@ -100,4 +105,5 @@ demo = gr.Interface(
100
  outputs=gr.Textbox(label="UCI Move"),
101
  title="🎯 BreakthroughMCVS Secure (Model + Dataset)",
102
  )
 
103
  demo.launch()
 
2
  import numpy as np
3
  from huggingface_hub import hf_hub_download
4
  import os
 
5
 
6
  TOKEN = os.environ.get("HF_TOKEN")
7
 
 
37
 
38
 
39
  def parse_board(board_text):
40
+ print(f"[DEBUG] parse_board raw input:\n{repr(board_text)}")
41
+ lines = board_text.strip().splitlines()
42
+ rows = [line.strip() for line in lines if line.strip()]
43
  if len(rows) != 8:
44
+ raise ValueError(f"Board must have exactly 8 rows (got {len(rows)}), raw lines:\n{lines}")
45
+
46
  board = np.zeros((8, 8), dtype=np.int32)
47
  mapping = {".": 0, "1": 1, "2": 2}
48
  for r, line in enumerate(rows):
49
  parts = line.split()
50
  if len(parts) != 8:
51
+ raise ValueError(f"Each row must have 8 space-separated cells (row {r+1}: {line})")
52
  for c, cell in enumerate(parts):
53
  if cell not in mapping:
54
+ raise ValueError(f"Use '.', '1', or '2' only (cell {r},{c}: {cell})")
55
  board[r, c] = mapping[cell]
56
  return board
57
 
 
62
  return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
63
 
64
 
65
+ def get_move(board_text, player=None):
66
+ print(f"[DEBUG] get_move called: board_text={repr(board_text)}, player={player}")
67
+ lines = [line.strip() for line in board_text.strip().splitlines() if line.strip()]
68
+ if len(lines) != 8:
69
+ raise ValueError(f"get_move: board must have exactly 8 rows (got {len(lines)}):\n{board_text}")
70
+ board_text = "\n".join(lines)
71
+
72
+ game = Breakthrough()
73
+ game.board = parse_board(board_text)
74
+ if player == "1":
75
+ game.move_count = 0
76
+ elif player == "2":
77
+ game.move_count = 1
78
+ else:
79
+ # Default: white to move
80
+ game.move_count = 0
81
+ game._cached_matrix = None
82
+ searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
83
+ visits, _ = searcher.search_with_time_budget(game, 1.0)
84
+ best_move = max(visits, key=visits.get)
85
+ move_str = move_to_uci(best_move)
86
+ print(f"[DEBUG] get_move OK: board={board_text}\nplayer={player} -> {move_str}")
87
+ return move_str
88
 
 
 
 
 
 
 
 
 
89
 
90
  demo = gr.Interface(
91
  fn=get_move,
 
105
  outputs=gr.Textbox(label="UCI Move"),
106
  title="🎯 BreakthroughMCVS Secure (Model + Dataset)",
107
  )
108
+
109
  demo.launch()