Spaces:
Sleeping
Sleeping
Update main.py
Browse files13 unmodified lines14151617181953 unmodified lines7374757677787940 unmodified lines1201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501516 unmodified lines15815916016116216316416516616716816917017117217317417517617717817918018118218318418515 unmodified lines20120220320420520620720820921021113 unmodified lines# βββ Multiplaying / Challenge Manager ββββββββββββββββββββββββββββββββββββββββββclass ConnectionManager:def __init__(self):self.active_connections: Dict[str, List[WebSocket]] = {}async def connect(self, websocket: WebSocket, match_id: str):53 unmodified lines"DEEPCASTLE_ENGINE_PATH",os.environ.get("ENGINE_PATH", "/app/engine_bin/deepcastle"),)STOCKFISH_ENGINE_PATH = os.environ.get("STOCKFISH_ENGINE_PATH", "/usr/games/stockfish")NNUE_PATH = os.environ.get("NNUE_PATH", "/app/engine_bin/output.nnue")NNUE_SMALL_PATH = os.environ.get("NNUE_SMALL_PATH", "/app/engine_bin/small_output.nnue")40 unmodified lines@app.get("/health")def health():missing = []if not os.path.exists(DEEPCASTLE_ENGINE_PATH):missing.append("deepcastle")if not os.path.exists(STOCKFISH_ENGINE_PATH):missing.append("stockfish")if missing:return {"status": "error", "message": f"Missing engine binary: {', '.join(missing)}"}return {"status": "ok", "engines": ["deepcastle", "stockfish"]}# Global engine instances to save memory and improve performance_GLOBAL_DEEPCASTLE_ENGINE = None_GLOBAL_STOCKFISH_ENGINE = Noneasync def _get_or_start_engine(engine_path: str, *, role: str, options: Optional[dict] = None):global _GLOBAL_DEEPCASTLE_ENGINE, _GLOBAL_STOCKFISH_ENGINEcurrent_engine = _GLOBAL_DEEPCASTLE_ENGINE if role == "deepcastle" else _GLOBAL_STOCKFISH_ENGINEif current_engine is not None:try:if not current_engine.is_terminated():return current_engineexcept Exception:if role == "deepcastle":_GLOBAL_DEEPCASTLE_ENGINE = Noneelse:_GLOBAL_STOCKFISH_ENGINE = Noneif not os.path.exists(engine_path):raise HTTPException(status_code=500, detail=f"{role} binary NOT FOUND at {engine_path}")6 unmodified linesif options:await engine.configure(options)if role == "deepcastle":if os.path.exists(NNUE_PATH):try:await engine.configure({"EvalFile": NNUE_PATH})print("[DEBUG] DeepCastle big net loaded successfully.")except Exception as ne:print(f"[ERROR] DeepCastle big net load failed: {str(ne)}")else:print(f"[WARNING] DeepCastle big net not found at {NNUE_PATH}")if os.path.exists(NNUE_SMALL_PATH):try:await engine.configure({"EvalFileSmall": NNUE_SMALL_PATH})print("[DEBUG] DeepCastle small net loaded successfully.")except Exception as ne:print(f"[ERROR] DeepCastle small net load failed: {str(ne)}")else:print(f"[WARNING] DeepCastle small net not found at {NNUE_SMALL_PATH}")_GLOBAL_DEEPCASTLE_ENGINE = engineelse:_GLOBAL_STOCKFISH_ENGINE = enginereturn engineexcept Exception as e:15 unmodified lines)async def get_stockfish_engine():return await _get_or_start_engine(STOCKFISH_ENGINE_PATH,role="stockfish",options={"Hash": 128, "Threads": 1},)def get_normalized_score(info) -> tuple[float, Optional[int]]:"""Returns the score from White's perspective in centipawns."""13 unmodified lines1415161718192053 unmodified lines74757677787940 unmodified lines1201211221231241251261271281291301311321331341351361371381391401411426 unmodified lines14915015115215315415515615715815916016116216316416516616716816917017117217315 unmodified lines18919019119219319419519613 unmodified lines# βββ Multiplaying / Challenge Manager ββββββββββββββββββββββββββββββββββββββββββclass ConnectionManager:def __init__(self):# match_id -> list of websocketsself.active_connections: Dict[str, List[WebSocket]] = {}async def connect(self, websocket: WebSocket, match_id: str):53 unmodified lines"DEEPCASTLE_ENGINE_PATH",os.environ.get("ENGINE_PATH", "/app/engine_bin/deepcastle"),)NNUE_PATH = os.environ.get("NNUE_PATH", "/app/engine_bin/output.nnue")NNUE_SMALL_PATH = os.environ.get("NNUE_SMALL_PATH", "/app/engine_bin/small_output.nnue")40 unmodified lines@app.get("/health")def health():if not os.path.exists(DEEPCASTLE_ENGINE_PATH):return {"status": "error", "message": "Missing engine binary: deepcastle"}return {"status": "ok", "engine": "deepcastle"}# Global engine instances to save memory and improve performance_GLOBAL_DEEPCASTLE_ENGINE = Noneasync def _get_or_start_engine(engine_path: str, *, role: str, options: Optional[dict] = None):global _GLOBAL_DEEPCASTLE_ENGINEcurrent_engine = _GLOBAL_DEEPCASTLE_ENGINEif current_engine is not None:try:if not current_engine.is_terminated():return current_engineexcept Exception:_GLOBAL_DEEPCASTLE_ENGINE = Noneif not os.path.exists(engine_path):raise HTTPException(status_code=500, detail=f"{role} binary NOT FOUND at {engine_path}")6 unmodified linesif options:await engine.configure(options)if os.path.exists(NNUE_PATH):try:await engine.configure({"EvalFile": NNUE_PATH})print("[DEBUG] DeepCastle big net loaded successfully.")except Exception as ne:print(f"[ERROR] DeepCastle big net load failed: {str(ne)}")else:print(f"[WARNING] DeepCastle big net not found at {NNUE_PATH}")if os.path.exists(NNUE_SMALL_PATH):try:await engine.configure({"EvalFileSmall": NNUE_SMALL_PATH})print("[DEBUG] DeepCastle small net loaded successfully.")except Exception as ne:print(f"[ERROR] DeepCastle small net load failed: {str(ne)}")else:print(f"[WARNING] DeepCastle small net not found at {NNUE_SMALL_PATH}")_GLOBAL_DEEPCASTLE_ENGINE = enginereturn engineexcept Exception as e:15 unmodified lines)async def get_stockfish_engine():# Compatibility alias: analysis now also uses DeepCastle.return await get_deepcastle_engine()def get_normalized_score(info) -> tuple[float, Optional[int]]:"""Returns the score from White's perspective in centipawns."""
main.py+25-40