test1978 commited on
Commit
f1a347e
·
verified ·
1 Parent(s): 45abad0

update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -24
app.py CHANGED
@@ -5,39 +5,86 @@ import os
5
 
6
  TOKEN = os.environ.get("HF_TOKEN")
7
 
8
- # MODEL repo (model type)
9
- model_repo = "test1978/breakthrough-model"
10
- model_path = hf_hub_download(model_repo, "breakthrough_mcvs.py", repo_type="model", token=TOKEN)
11
- with open(model_path, 'r', encoding='utf-8-sig') as f:
12
- exec(f.read())
13
-
14
- # DB dataset (dataset type!)
15
- db_repo = "test1978/breakthrough-data"
16
- db_path = hf_hub_download(db_repo, "breakthrough_zone_db.npz", repo_type="dataset", token=TOKEN)
 
 
 
 
 
 
 
 
 
 
17
  zonedb_data = np.load(db_path, allow_pickle=True)
18
 
19
- # Init YOUR zone DB
20
- zonedb = HilbertOrderedZoneDatabase()
21
- zonedb.winningmatrices = list(zonedb_data.get('winning', []))
22
- zonedb.losingmatrices = list(zonedb_data.get('losing', []))
23
- zonedb.drawmatrices = list(zonedb_data.get('draw', []))
24
 
25
- def get_move(fen, player):
26
- board = Breakthrough.Board(fen)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  game = Breakthrough()
28
- game.board = board
 
 
 
29
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
30
  visits, _ = searcher.search_with_time_budget(game, 1.0)
 
 
31
  best_move = max(visits, key=visits.get)
32
- return best_move.uci()
33
 
34
  demo = gr.Interface(
35
  fn=get_move,
36
- inputs=gr.Textbox(
37
- label="FEN",
38
- value="rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
39
- ),
40
- outputs=gr.Textbox(label="UCI Move"),
41
- title="🎯 ChessMCVS Secure (Model + Dataset)"
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  )
 
43
  demo.launch()
 
5
 
6
  TOKEN = os.environ.get("HF_TOKEN")
7
 
8
+ model_repo = "typical-cyber/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 = "typical-cyber/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", maxsize=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()]
40
+ if len(rows) != 8:
41
+ raise ValueError("Board must have exactly 8 rows.")
42
+ board = np.zeros((8, 8), dtype=np.int32)
43
+ mapping = {".": 0, "1": 1, "2": 2}
44
+ for r, line in enumerate(rows):
45
+ parts = line.split()
46
+ if len(parts) != 8:
47
+ raise ValueError("Each row must have 8 space-separated cells.")
48
+ for c, cell in enumerate(parts):
49
+ if cell not in mapping:
50
+ raise ValueError("Use '.', '1', or '2' only.")
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.movecount = 0
58
+ game.cachedmatrix = None
59
+
60
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
61
  visits, _ = searcher.search_with_time_budget(game, 1.0)
62
+ if not visits:
63
+ return "No legal move"
64
  best_move = max(visits, key=visits.get)
65
+ return str(best_move)
66
 
67
  demo = gr.Interface(
68
  fn=get_move,
69
+ inputs=[
70
+ gr.Textbox(
71
+ label="Board",
72
+ lines=8,
73
+ value=(
74
+ "1 1 1 1 1 1 1 1\n"
75
+ "1 1 1 1 1 1 1 1\n"
76
+ ". . . . . . . .\n"
77
+ ". . . . . . . .\n"
78
+ ". . . . . . . .\n"
79
+ ". . . . . . . .\n"
80
+ "2 2 2 2 2 2 2 2\n"
81
+ "2 2 2 2 2 2 2 2"
82
+ )
83
+ ),
84
+ gr.Dropdown(choices=["1", "2"], value="1", label="Player")
85
+ ],
86
+ outputs=gr.Textbox(label="Best Move"),
87
+ title="Breakthrough MCVS Secure"
88
  )
89
+
90
  demo.launch()