test1978 commited on
Commit
2d3953c
·
verified ·
1 Parent(s): 954cf39

update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -36
app.py CHANGED
@@ -3,37 +3,39 @@ import numpy as np
3
  from huggingface_hub import hf_hub_download
4
  import os
5
 
 
6
  TOKEN = os.environ.get("HF_TOKEN")
7
 
 
 
8
  model_repo = "test1978/breakthrough-model"
9
  model_path = hf_hub_download(
10
  model_repo,
11
  "breakthrough_mcvs.py",
12
  repo_type="model",
13
- token=TOKEN
14
  )
15
-
16
- ns = {}
17
  with open(model_path, "r", encoding="utf-8-sig") as f:
18
- exec(f.read(), ns)
19
 
 
 
20
  db_repo = "test1978/breakthrough-data"
21
  db_path = hf_hub_download(
22
  db_repo,
23
  "breakthrough_zone_db.npz",
24
  repo_type="dataset",
25
- token=TOKEN
26
  )
27
  zonedb_data = np.load(db_path, allow_pickle=True)
28
 
29
- HilbertOrderedZoneDatabase = ns["HilbertOrderedZoneDatabase"]
30
- Breakthrough = ns["Breakthrough"]
31
- MCVSSearcher = ns["MCVSSearcher"]
32
 
33
- zonedb = HilbertOrderedZoneDatabase("breakthroughzonedb.npz", max_size=10000)
34
- zonedb.winningmatrices = list(zonedb_data.get("winning", []))
35
- zonedb.losingmatrices = list(zonedb_data.get("losing", []))
36
- zonedb.drawmatrices = list(zonedb_data.get("draw", []))
 
 
37
 
38
  def parse_board(board_text):
39
  rows = [line.strip() for line in board_text.strip().splitlines() if line.strip()]
@@ -51,39 +53,39 @@ def parse_board(board_text):
51
  board[r, c] = mapping[cell]
52
  return board
53
 
 
 
 
 
 
 
 
54
  def get_move(board_text, player):
55
  game = Breakthrough()
56
  game.board = parse_board(board_text)
57
- game.move_count = 0
58
- game.cached_matrix = None
59
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
60
  visits, _ = searcher.search_with_time_budget(game, 1.0)
61
- if not visits:
62
- return "No legal move"
63
  best_move = max(visits, key=visits.get)
64
- return str(best_move)
65
 
66
  demo = gr.Interface(
67
  fn=get_move,
68
- inputs=[
69
- gr.Textbox(
70
- label="Board",
71
- lines=8,
72
- value=(
73
- "1 1 1 1 1 1 1 1\n"
74
- "1 1 1 1 1 1 1 1\n"
75
- ". . . . . . . .\n"
76
- ". . . . . . . .\n"
77
- ". . . . . . . .\n"
78
- ". . . . . . . .\n"
79
- "2 2 2 2 2 2 2 2\n"
80
- "2 2 2 2 2 2 2 2"
81
- )
82
  ),
83
- gr.Dropdown(choices=["1", "2"], value="1", label="Player")
84
- ],
85
- outputs=gr.Textbox(label="Best Move"),
86
- title="Breakthrough MCVS Secure"
87
  )
88
-
89
  demo.launch()
 
3
  from huggingface_hub import hf_hub_download
4
  import os
5
 
6
+
7
  TOKEN = os.environ.get("HF_TOKEN")
8
 
9
+
10
+ # MODEL repo (model type)
11
  model_repo = "test1978/breakthrough-model"
12
  model_path = hf_hub_download(
13
  model_repo,
14
  "breakthrough_mcvs.py",
15
  repo_type="model",
16
+ token=TOKEN,
17
  )
 
 
18
  with open(model_path, "r", encoding="utf-8-sig") as f:
19
+ exec(f.read())
20
 
21
+
22
+ # DB dataset (dataset type!)
23
  db_repo = "test1978/breakthrough-data"
24
  db_path = hf_hub_download(
25
  db_repo,
26
  "breakthrough_zone_db.npz",
27
  repo_type="dataset",
28
+ token=TOKEN,
29
  )
30
  zonedb_data = np.load(db_path, allow_pickle=True)
31
 
 
 
 
32
 
33
+ # Init YOUR zone DB
34
+ zonedb = HilbertOrderedZoneDatabase()
35
+ zonedb.winning_matrices = list(zonedb_data.get("winning", []))
36
+ zonedb.losing_matrices = list(zonedb_data.get("losing", []))
37
+ 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()]
 
53
  board[r, c] = mapping[cell]
54
  return board
55
 
56
+
57
+ def move_to_uci(move):
58
+ fr, fc, tr, tc = move
59
+ cols = "abcdefgh"
60
+ return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
61
+
62
+
63
  def get_move(board_text, player):
64
  game = Breakthrough()
65
  game.board = parse_board(board_text)
66
+ game.move_count = 0 if str(player) == "1" else 1
67
+ game._cached_matrix = None
68
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
69
  visits, _ = searcher.search_with_time_budget(game, 1.0)
 
 
70
  best_move = max(visits, key=visits.get)
71
+ return move_to_uci(best_move)
72
 
73
  demo = gr.Interface(
74
  fn=get_move,
75
+ inputs=gr.Textbox(
76
+ label="Breakthrough Board (1/2/.)",
77
+ value=(
78
+ "1 1 1 1 1 1 1 1\n"
79
+ "1 1 1 1 1 1 1 1\n"
80
+ ". . . . . . . .\n"
81
+ ". . . . . . . .\n"
82
+ ". . . . . . . .\n"
83
+ ". . . . . . . .\n"
84
+ "2 2 2 2 2 2 2 2\n"
85
+ "2 2 2 2 2 2 2 2"
 
 
 
86
  ),
87
+ ),
88
+ outputs=gr.Textbox(label="UCI Move"),
89
+ title="🎯 BreakthroughMCVS Secure (Model + Dataset)",
 
90
  )
 
91
  demo.launch()