typical-cyber commited on
Commit
49fde22
·
verified ·
1 Parent(s): 5acc7d2

update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -1,33 +1,33 @@
1
  import gradio as gr
2
  import chess
3
  import numpy as np
4
- from pathlib import Path
5
  import os
6
 
7
- # Secure load with token
8
  TOKEN = os.environ.get("HF_TOKEN")
9
  REPO = "typical-cyber/chess-model"
10
 
11
- try:
12
- from huggingface_hub import hf_hub_download
13
- model_path = hf_hub_download(REPO, "chess_mcvs.py", token=TOKEN)
14
- exec(open(model_path).read())
15
- db_path = hf_hub_download(REPO, "user_models/user136/chess/data/chesszonedb.npz", token=TOKEN)
16
- zonedb_data = np.load(db_path, allow_pickle=True)
17
- except:
18
- # Local fallback (upload files)
19
- exec(open('chess_mcvs.py').read())
20
- zonedb_data = np.load('chesszonedb.npz', allow_pickle=True)
21
 
 
 
 
 
22
  zonedb = HilbertOrderedZoneDatabase()
23
  zonedb.winningmatrices = list(zonedb_data.get('winning', []))
 
 
24
 
25
  def get_move(fen, player):
26
  board = chess.Board(fen)
27
  game = Chess()
28
  game.board = board
29
- searcher = MCVSSearcher(None, None, zonedb, lambdazone=1.0)
30
  visits, _ = searcher.searchwithtimebudget(game, 1.0)
31
- return max(visits, key=visits.get).uci()
 
32
 
33
  gr.Interface(fn=get_move, inputs=gr.Textbox("FEN"), outputs="text").launch()
 
1
  import gradio as gr
2
  import chess
3
  import numpy as np
4
+ from huggingface_hub import hf_hub_download
5
  import os
6
 
 
7
  TOKEN = os.environ.get("HF_TOKEN")
8
  REPO = "typical-cyber/chess-model"
9
 
10
+ # BOM-PROOF download + exec
11
+ model_path = hf_hub_download(REPO, "chess_mcvs.py", token=TOKEN)
12
+ with open(model_path, 'r', encoding='utf-8-sig') as f: # utf-8-sig = BOM killer!
13
+ exec(f.read())
 
 
 
 
 
 
14
 
15
+ db_path = hf_hub_download(REPO, "user_models/user136/chess/data/chess_zone_db.npz", token=TOKEN)
16
+ zonedb_data = np.load(db_path, allow_pickle=True)
17
+
18
+ # Init YOUR zone DB
19
  zonedb = HilbertOrderedZoneDatabase()
20
  zonedb.winningmatrices = list(zonedb_data.get('winning', []))
21
+ zonedb.losingmatrices = list(zonedb_data.get('losing', []))
22
+ zonedb.drawmatrices = list(zonedb_data.get('draw', []))
23
 
24
  def get_move(fen, player):
25
  board = chess.Board(fen)
26
  game = Chess()
27
  game.board = board
28
+ searcher = MCVSSearcher(None, None, zonedb, lambdazone=1.0, kzone=5)
29
  visits, _ = searcher.searchwithtimebudget(game, 1.0)
30
+ best_move = max(visits, key=visits.get)
31
+ return best_move.uci()
32
 
33
  gr.Interface(fn=get_move, inputs=gr.Textbox("FEN"), outputs="text").launch()