import sys import requests import chess API_URL = "https://dpv007-chess-web.hf.space/move" def main(): board = chess.Board() history = [""] 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 = [""] 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()