Spaces:
Sleeping
Sleeping
File size: 9,140 Bytes
5bf0029 d03718f 5bf0029 cbee027 5bf0029 cbee027 5bf0029 d03718f 5bf0029 cbee027 5bf0029 cbee027 d03718f cbee027 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 cbee027 d03718f cbee027 d03718f cbee027 d03718f cbee027 d03718f cbee027 5bf0029 cbee027 d03718f 5bf0029 d03718f 5bf0029 cbee027 d03718f cbee027 5bf0029 d03718f 5bf0029 d03718f 5bf0029 cbee027 d03718f cbee027 d03718f cbee027 d03718f cbee027 d03718f 5bf0029 d03718f 5bf0029 d03718f 5bf0029 d03718f | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | """
Nexus-Nano Inference API - Path Fixed
Model: /app/models/nexus-nano.onnx
Ultra-lightweight single-file engine
"""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import onnxruntime as ort
import numpy as np
import chess
import time
import logging
import os
from typing import Optional, Tuple
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# ==================== NANO ENGINE ====================
class NexusNanoEngine:
"""Ultra-lightweight chess engine"""
PIECE_VALUES = {
chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3,
chess.ROOK: 5, chess.QUEEN: 9, chess.KING: 0
}
def __init__(self, model_path: str):
if not os.path.exists(model_path):
raise FileNotFoundError(f"Model not found: {model_path}")
logger.info(f"π¦ Loading model: {model_path}")
logger.info(f"πΎ Size: {os.path.getsize(model_path)/(1024*1024):.2f} MB")
sess_options = ort.SessionOptions()
sess_options.intra_op_num_threads = 2
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
self.session = ort.InferenceSession(
model_path,
sess_options=sess_options,
providers=['CPUExecutionProvider']
)
self.input_name = self.session.get_inputs()[0].name
self.output_name = self.session.get_outputs()[0].name
self.nodes = 0
logger.info("β
Engine ready!")
def fen_to_tensor(self, fen: str) -> np.ndarray:
board = chess.Board(fen)
tensor = np.zeros((1, 12, 8, 8), dtype=np.float32)
piece_map = {
chess.PAWN: 0, chess.KNIGHT: 1, chess.BISHOP: 2,
chess.ROOK: 3, chess.QUEEN: 4, chess.KING: 5
}
for sq, piece in board.piece_map().items():
r, f = divmod(sq, 8)
ch = piece_map[piece.piece_type] + (6 if piece.color == chess.BLACK else 0)
tensor[0, ch, r, f] = 1.0
return tensor
def evaluate(self, board: chess.Board) -> float:
self.nodes += 1
tensor = self.fen_to_tensor(board.fen())
output = self.session.run([self.output_name], {self.input_name: tensor})
score = float(output[0][0][0]) * 400.0
return -score if board.turn == chess.BLACK else score
def order_moves(self, board: chess.Board, moves):
scored = []
for m in moves:
s = 0
if board.is_capture(m):
v = board.piece_at(m.to_square)
a = board.piece_at(m.from_square)
if v and a:
s = self.PIECE_VALUES.get(v.piece_type, 0) * 10
s -= self.PIECE_VALUES.get(a.piece_type, 0)
if m.promotion == chess.QUEEN:
s += 90
scored.append((s, m))
scored.sort(key=lambda x: x[0], reverse=True)
return [m for _, m in scored]
def alpha_beta(
self,
board: chess.Board,
depth: int,
alpha: float,
beta: float
) -> Tuple[float, Optional[chess.Move]]:
if board.is_game_over():
return (-10000 if board.is_checkmate() else 0), None
if depth == 0:
return self.evaluate(board), None
moves = list(board.legal_moves)
if not moves:
return 0, None
moves = self.order_moves(board, moves)
best_move = moves[0]
best_score = float('-inf')
for move in moves:
board.push(move)
score, _ = self.alpha_beta(board, depth - 1, -beta, -alpha)
score = -score
board.pop()
if score > best_score:
best_score = score
best_move = move
alpha = max(alpha, score)
if alpha >= beta:
break
return best_score, best_move
def search(self, fen: str, depth: int = 3):
board = chess.Board(fen)
self.nodes = 0
moves = list(board.legal_moves)
if len(moves) == 0:
return {'best_move': '0000', 'evaluation': 0.0, 'nodes': 0, 'depth': 0}
if len(moves) == 1:
return {
'best_move': moves[0].uci(),
'evaluation': round(self.evaluate(board) / 100.0, 2),
'nodes': 1,
'depth': 0
}
best_move = moves[0]
best_score = float('-inf')
current_depth = 1
for d in range(1, depth + 1):
try:
score, move = self.alpha_beta(board, d, float('-inf'), float('inf'))
if move:
best_move = move
best_score = score
current_depth = d
except:
break
return {
'best_move': best_move.uci(),
'evaluation': round(best_score / 100.0, 2),
'depth': current_depth,
'nodes': self.nodes
}
# ==================== FASTAPI APP ====================
app = FastAPI(
title="Nexus-Nano Inference API",
description="Ultra-lightweight chess engine",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
engine = None
class MoveRequest(BaseModel):
fen: str
depth: Optional[int] = Field(3, ge=1, le=5)
class MoveResponse(BaseModel):
best_move: str
evaluation: float
depth_searched: int
nodes_evaluated: int
time_taken: int
@app.on_event("startup")
async def startup():
global engine
logger.info("π Starting Nexus-Nano API...")
# FIXED: Correct path with hyphen
model_path = "/app/models/nexus-nano.onnx"
logger.info(f"π Looking for: {model_path}")
if os.path.exists("/app/models"):
logger.info("π Files in /app/models/:")
for f in os.listdir("/app/models"):
full_path = os.path.join("/app/models", f)
if os.path.isfile(full_path):
size = os.path.getsize(full_path) / (1024*1024)
logger.info(f" β {f} ({size:.2f} MB)")
else:
logger.error("β /app/models/ not found!")
raise FileNotFoundError("/app/models/ directory missing")
if not os.path.exists(model_path):
logger.error(f"β Model not found: {model_path}")
logger.error("π‘ Available:", os.listdir("/app/models"))
raise FileNotFoundError(f"Missing: {model_path}")
try:
engine = NexusNanoEngine(model_path)
logger.info("π Nexus-Nano ready!")
except Exception as e:
logger.error(f"β Load failed: {e}", exc_info=True)
raise
@app.get("/health")
async def health():
return {
"status": "healthy" if engine else "unhealthy",
"model": "nexus-nano",
"version": "1.0.0",
"model_loaded": engine is not None,
"model_path": "/app/models/nexus-nano.onnx"
}
@app.post("/get-move", response_model=MoveResponse)
async def get_move(req: MoveRequest):
if not engine:
raise HTTPException(status_code=503, detail="Engine not loaded")
try:
chess.Board(req.fen)
except:
raise HTTPException(status_code=400, detail="Invalid FEN")
start = time.time()
try:
result = engine.search(req.fen, req.depth)
elapsed = int((time.time() - start) * 1000)
logger.info(
f"β Move: {result['best_move']} | "
f"Eval: {result['evaluation']:+.2f} | "
f"Depth: {result['depth']} | "
f"Nodes: {result['nodes']} | "
f"Time: {elapsed}ms"
)
return MoveResponse(
best_move=result['best_move'],
evaluation=result['evaluation'],
depth_searched=result['depth'],
nodes_evaluated=result['nodes'],
time_taken=elapsed
)
except Exception as e:
logger.error(f"β Search error: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {
"name": "Nexus-Nano Inference API",
"version": "1.0.0",
"model": "2.8M parameters",
"architecture": "Compact ResNet",
"speed": "0.2-0.5s per move @ depth 3",
"status": "online" if engine else "starting",
"endpoints": {
"POST /get-move": "Get best move",
"GET /health": "Health check",
"GET /docs": "API docs"
}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host="0.0.0.0",
port=7860,
log_level="info",
access_log=True
) |