File size: 4,342 Bytes
cdd854f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d16e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdd854f
 
 
 
 
d5d16e5
 
cdd854f
 
 
 
d099906
cdd854f
d099906
 
 
 
 
 
 
 
 
 
cdd854f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys
import requests
import chess

API_URL = "https://dpv007-chess-web.hf.space/move"

def main():
    board = chess.Board()
    history = ["<bos>"]
    
    while True:
        try:
            line = sys.stdin.readline()
            if not line:
                break
            line = line.strip()
            if not line:
                continue
                
            parts = line.split()
            cmd = parts[0]
            
            if cmd == "uci":
                print("id name Neurex Cloud")
                print("id author DPV007")
                print("uciok")
                sys.stdout.flush()
                
            elif cmd == "isready":
                print("readyok")
                sys.stdout.flush()
                
            elif cmd == "position":
                board = chess.Board()
                history = ["<bos>"]
                
                if "startpos" in parts:
                    if "moves" in parts:
                        moves_idx = parts.index("moves")
                        for m_str in parts[moves_idx+1:]:
                            m = chess.Move.from_uci(m_str)
                            board.push(m)
                            history.append(m_str)
                elif "fen" in parts:
                    fen_idx = parts.index("fen")
                    fen_str = " ".join(parts[fen_idx+1:fen_idx+7])
                    board = chess.Board(fen_str)
                    if "moves" in parts:
                        moves_idx = parts.index("moves")
                        for m_str in parts[moves_idx+1:]:
                            m = chess.Move.from_uci(m_str)
                            board.push(m)
                            history.append(m_str)
                            
            elif cmd == "go":
                # Parse time allocation
                wtime = btime = winc = binc = 0
                if "wtime" in parts: wtime = int(parts[parts.index("wtime")+1])
                if "btime" in parts: btime = int(parts[parts.index("btime")+1])
                if "winc" in parts: winc = int(parts[parts.index("winc")+1])
                if "binc" in parts: binc = int(parts[parts.index("binc")+1])
                
                time_limit = 5.0
                if board.turn == chess.WHITE and wtime > 0:
                    time_limit = (wtime / 40.0 + winc * 0.8) / 1000.0
                elif board.turn == chess.BLACK and btime > 0:
                    time_limit = (btime / 40.0 + binc * 0.8) / 1000.0
                
                # Cap time_limit between 1.0s and 25.0s (HF API timeout is 30s)
                time_limit = max(1.0, min(25.0, time_limit))
                
                # Send the request to Hugging Face
                try:
                    payload = {
                        "fen": board.fen(),
                        "history": history,
                        "engine_choice": "neural",
                        "time_limit": time_limit
                    }
                    response = requests.post(API_URL, json=payload, timeout=30)
                    data = response.json()
                    
                    if "ai_move" in data and data["ai_move"]:
                        move = data["ai_move"]
                        
                        if "engine_info" in data and data["engine_info"]:
                            info = data["engine_info"]
                            info_str = "info"
                            if "depth" in info: info_str += f" depth {info['depth']}"
                            if "nodes" in info: info_str += f" nodes {info['nodes']}"
                            if "nps" in info: info_str += f" nps {info['nps']}"
                            if info_str != "info":
                                print(info_str)
                                
                        print(f"bestmove {move}")
                    else:
                        print("bestmove 0000")
                        
                except Exception as e:
                    # Fallback on network error
                    print("bestmove 0000")
                sys.stdout.flush()
                
            elif cmd == "quit":
                break
                
        except KeyboardInterrupt:
            break

if __name__ == "__main__":
    main()