Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -8,7 +8,7 @@ import chess
|
|
| 8 |
import chess.engine
|
| 9 |
import asyncio
|
| 10 |
|
| 11 |
-
app = FastAPI(title="Deepcastle
|
| 12 |
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
|
@@ -22,7 +22,7 @@ NNUE_PATH = os.environ.get("NNUE_PATH", "/app/engine/output.nnue")
|
|
| 22 |
|
| 23 |
class MoveRequest(BaseModel):
|
| 24 |
fen: str
|
| 25 |
-
time: float = 1.0
|
| 26 |
depth: Optional[int] = None
|
| 27 |
|
| 28 |
class MoveResponse(BaseModel):
|
|
@@ -34,15 +34,15 @@ class MoveResponse(BaseModel):
|
|
| 34 |
pv: str
|
| 35 |
|
| 36 |
class AnalyzeRequest(BaseModel):
|
| 37 |
-
moves: List[str]
|
| 38 |
-
time_per_move: float = 0.1
|
| 39 |
player_color: str = "white"
|
| 40 |
|
| 41 |
class MoveAnalysis(BaseModel):
|
| 42 |
move_num: int
|
| 43 |
san: str
|
| 44 |
fen: str
|
| 45 |
-
classification: str
|
| 46 |
cpl: float
|
| 47 |
score_before: float
|
| 48 |
score_after: float
|
|
@@ -53,6 +53,16 @@ class AnalyzeResponse(BaseModel):
|
|
| 53 |
moves: List[MoveAnalysis]
|
| 54 |
counts: Dict[str, int]
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
async def get_engine():
|
| 57 |
if not os.path.exists(ENGINE_PATH):
|
| 58 |
raise HTTPException(status_code=500, detail="Engine binary not found")
|
|
@@ -65,7 +75,7 @@ async def get_engine():
|
|
| 65 |
pass
|
| 66 |
return engine
|
| 67 |
|
| 68 |
-
def get_normalized_score(info):
|
| 69 |
if "score" not in info:
|
| 70 |
return 0.0
|
| 71 |
raw = info["score"].white()
|
|
@@ -73,13 +83,13 @@ def get_normalized_score(info):
|
|
| 73 |
return 10000.0 if (raw.mate() or 0) > 0 else -10000.0
|
| 74 |
return raw.score() or 0.0
|
| 75 |
|
| 76 |
-
@app.post("/move")
|
| 77 |
async def get_move(request: MoveRequest):
|
| 78 |
engine = None
|
| 79 |
try:
|
| 80 |
engine = await get_engine()
|
| 81 |
board = chess.Board(request.fen)
|
| 82 |
-
limit = chess.engine.Limit(time=request.time)
|
| 83 |
result = await engine.play(board, limit)
|
| 84 |
info = await engine.analyse(board, limit)
|
| 85 |
score_cp = get_normalized_score(info)
|
|
@@ -88,7 +98,7 @@ async def get_move(request: MoveRequest):
|
|
| 88 |
finally:
|
| 89 |
if engine: await engine.quit()
|
| 90 |
|
| 91 |
-
@app.post("/analyze-game")
|
| 92 |
async def analyze_game(request: AnalyzeRequest):
|
| 93 |
engine = None
|
| 94 |
try:
|
|
@@ -98,14 +108,17 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 98 |
analysis_results, total_cpl, player_moves_count = [], 0, 0
|
| 99 |
counts = {"Brilliant": 0, "Great": 0, "Best": 0, "Excellent": 0, "Good": 0, "Inaccuracy": 0, "Mistake": 0, "Blunder": 0}
|
| 100 |
|
| 101 |
-
|
| 102 |
-
current_score = get_normalized_score(
|
| 103 |
player_is_white = (request.player_color.lower() == "white")
|
| 104 |
|
| 105 |
for i, san_move in enumerate(request.moves):
|
| 106 |
-
is_player_turn = board.turn == chess.WHITE if player_is_white else
|
| 107 |
score_before = current_score
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
info_after = await engine.analyse(board, limit)
|
| 110 |
current_score = get_normalized_score(info_after)
|
| 111 |
|
|
|
|
| 8 |
import chess.engine
|
| 9 |
import asyncio
|
| 10 |
|
| 11 |
+
app = FastAPI(title="Deepcastle Engine API")
|
| 12 |
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
|
|
|
| 22 |
|
| 23 |
class MoveRequest(BaseModel):
|
| 24 |
fen: str
|
| 25 |
+
time: float = 1.0
|
| 26 |
depth: Optional[int] = None
|
| 27 |
|
| 28 |
class MoveResponse(BaseModel):
|
|
|
|
| 34 |
pv: str
|
| 35 |
|
| 36 |
class AnalyzeRequest(BaseModel):
|
| 37 |
+
moves: List[str]
|
| 38 |
+
time_per_move: float = 0.1
|
| 39 |
player_color: str = "white"
|
| 40 |
|
| 41 |
class MoveAnalysis(BaseModel):
|
| 42 |
move_num: int
|
| 43 |
san: str
|
| 44 |
fen: str
|
| 45 |
+
classification: str
|
| 46 |
cpl: float
|
| 47 |
score_before: float
|
| 48 |
score_after: float
|
|
|
|
| 53 |
moves: List[MoveAnalysis]
|
| 54 |
counts: Dict[str, int]
|
| 55 |
|
| 56 |
+
@app.get("/")
|
| 57 |
+
def home():
|
| 58 |
+
return {"status": "online", "engine": "Deepcastle Hybrid Neural", "platform": "Hugging Face Spaces"}
|
| 59 |
+
|
| 60 |
+
@app.get("/health")
|
| 61 |
+
def health():
|
| 62 |
+
if not os.path.exists(ENGINE_PATH):
|
| 63 |
+
return {"status": "error", "message": "Engine binary not found"}
|
| 64 |
+
return {"status": "ok", "engine": "Deepcastle"}
|
| 65 |
+
|
| 66 |
async def get_engine():
|
| 67 |
if not os.path.exists(ENGINE_PATH):
|
| 68 |
raise HTTPException(status_code=500, detail="Engine binary not found")
|
|
|
|
| 75 |
pass
|
| 76 |
return engine
|
| 77 |
|
| 78 |
+
def get_normalized_score(info, turn_color=chess.WHITE):
|
| 79 |
if "score" not in info:
|
| 80 |
return 0.0
|
| 81 |
raw = info["score"].white()
|
|
|
|
| 83 |
return 10000.0 if (raw.mate() or 0) > 0 else -10000.0
|
| 84 |
return raw.score() or 0.0
|
| 85 |
|
| 86 |
+
@app.post("/move", response_model=MoveResponse)
|
| 87 |
async def get_move(request: MoveRequest):
|
| 88 |
engine = None
|
| 89 |
try:
|
| 90 |
engine = await get_engine()
|
| 91 |
board = chess.Board(request.fen)
|
| 92 |
+
limit = chess.engine.Limit(time=request.time, depth=request.depth)
|
| 93 |
result = await engine.play(board, limit)
|
| 94 |
info = await engine.analyse(board, limit)
|
| 95 |
score_cp = get_normalized_score(info)
|
|
|
|
| 98 |
finally:
|
| 99 |
if engine: await engine.quit()
|
| 100 |
|
| 101 |
+
@app.post("/analyze-game", response_model=AnalyzeResponse)
|
| 102 |
async def analyze_game(request: AnalyzeRequest):
|
| 103 |
engine = None
|
| 104 |
try:
|
|
|
|
| 108 |
analysis_results, total_cpl, player_moves_count = [], 0, 0
|
| 109 |
counts = {"Brilliant": 0, "Great": 0, "Best": 0, "Excellent": 0, "Good": 0, "Inaccuracy": 0, "Mistake": 0, "Blunder": 0}
|
| 110 |
|
| 111 |
+
info_before = await engine.analyse(board, limit)
|
| 112 |
+
current_score = get_normalized_score(info_before)
|
| 113 |
player_is_white = (request.player_color.lower() == "white")
|
| 114 |
|
| 115 |
for i, san_move in enumerate(request.moves):
|
| 116 |
+
is_player_turn = board.turn == (chess.WHITE if player_is_white else chess.BLACK)
|
| 117 |
score_before = current_score
|
| 118 |
+
try:
|
| 119 |
+
board.push_san(san_move)
|
| 120 |
+
except Exception: break
|
| 121 |
+
|
| 122 |
info_after = await engine.analyse(board, limit)
|
| 123 |
current_score = get_normalized_score(info_after)
|
| 124 |
|