Spaces:
Sleeping
Sleeping
File size: 2,889 Bytes
8d1f456 |
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 |
"""
Nexus-Core Inference API
Fast and efficient chess engine
"""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import time
import logging
from typing import Optional
from engine import NexusCoreEngine
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="Nexus-Core Inference API",
description="Fast chess engine (13M params)",
version="2.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(4, ge=1, le=6)
time_limit: Optional[int] = Field(3000, ge=1000, le=10000)
class MoveResponse(BaseModel):
best_move: str
evaluation: float
depth_searched: int
nodes_evaluated: int
time_taken: int
class HealthResponse(BaseModel):
status: str
model_loaded: bool
version: str
@app.on_event("startup")
async def startup_event():
global engine
logger.info("🚀 Starting Nexus-Core API...")
try:
engine = NexusCoreEngine(
model_path="/app/models/nexus_core.onnx",
num_threads=2
)
logger.info("✅ Nexus-Core loaded")
except Exception as e:
logger.error(f"❌ Failed: {e}")
raise
@app.get("/health", response_model=HealthResponse)
async def health_check():
return {
"status": "healthy" if engine else "unhealthy",
"model_loaded": engine is not None,
"version": "2.0.0"
}
@app.post("/get-move", response_model=MoveResponse)
async def get_move(request: MoveRequest):
if not engine:
raise HTTPException(503, "Engine not loaded")
if not engine.validate_fen(request.fen):
raise HTTPException(400, "Invalid FEN")
start = time.time()
try:
result = engine.get_best_move(
request.fen,
request.depth,
request.time_limit
)
logger.info(
f"Move: {result['best_move']} | "
f"Eval: {result['evaluation']:+.2f} | "
f"Depth: {result['depth_searched']} | "
f"Time: {result['time_taken']}ms"
)
return MoveResponse(**result)
except Exception as e:
logger.error(f"Error: {e}")
raise HTTPException(500, str(e))
@app.get("/")
async def root():
return {
"name": "Nexus-Core API",
"version": "2.0.0",
"model": "13M parameters",
"speed": "Ultra-fast",
"endpoints": {
"POST /get-move": "Get best move",
"GET /health": "Health check"
}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |