Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
Nexus-Core Inference API
|
| 3 |
-
Fast and efficient chess engine
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
|
@@ -8,11 +8,15 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
import time
|
| 10 |
import logging
|
|
|
|
| 11 |
from typing import Optional
|
| 12 |
|
| 13 |
from engine import NexusCoreEngine
|
| 14 |
|
| 15 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
app = FastAPI(
|
|
@@ -50,6 +54,7 @@ class HealthResponse(BaseModel):
|
|
| 50 |
status: str
|
| 51 |
model_loaded: bool
|
| 52 |
version: str
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
@app.on_event("startup")
|
|
@@ -57,14 +62,36 @@ async def startup_event():
|
|
| 57 |
global engine
|
| 58 |
logger.info("🚀 Starting Nexus-Core API...")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
engine = NexusCoreEngine(
|
| 62 |
-
model_path=
|
| 63 |
num_threads=2
|
| 64 |
)
|
| 65 |
-
logger.info("✅ Nexus-Core loaded")
|
| 66 |
except Exception as e:
|
| 67 |
-
logger.error(f"❌ Failed: {e}")
|
| 68 |
raise
|
| 69 |
|
| 70 |
|
|
@@ -73,7 +100,8 @@ async def health_check():
|
|
| 73 |
return {
|
| 74 |
"status": "healthy" if engine else "unhealthy",
|
| 75 |
"model_loaded": engine is not None,
|
| 76 |
-
"version": "2.0.0"
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
|
|
@@ -98,13 +126,14 @@ async def get_move(request: MoveRequest):
|
|
| 98 |
f"Move: {result['best_move']} | "
|
| 99 |
f"Eval: {result['evaluation']:+.2f} | "
|
| 100 |
f"Depth: {result['depth_searched']} | "
|
|
|
|
| 101 |
f"Time: {result['time_taken']}ms"
|
| 102 |
)
|
| 103 |
|
| 104 |
return MoveResponse(**result)
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
logger.error(f"Error: {e}")
|
| 108 |
raise HTTPException(500, str(e))
|
| 109 |
|
| 110 |
|
|
@@ -114,14 +143,16 @@ async def root():
|
|
| 114 |
"name": "Nexus-Core API",
|
| 115 |
"version": "2.0.0",
|
| 116 |
"model": "13M parameters",
|
| 117 |
-
"speed": "Ultra-fast",
|
|
|
|
| 118 |
"endpoints": {
|
| 119 |
"POST /get-move": "Get best move",
|
| 120 |
-
"GET /health": "Health check"
|
|
|
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
import uvicorn
|
| 127 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Nexus-Core Inference API (Fixed)
|
| 3 |
+
Fast and efficient chess engine with proper error handling
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
import time
|
| 10 |
import logging
|
| 11 |
+
import os
|
| 12 |
from typing import Optional
|
| 13 |
|
| 14 |
from engine import NexusCoreEngine
|
| 15 |
|
| 16 |
+
logging.basicConfig(
|
| 17 |
+
level=logging.INFO,
|
| 18 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 19 |
+
)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
app = FastAPI(
|
|
|
|
| 54 |
status: str
|
| 55 |
model_loaded: bool
|
| 56 |
version: str
|
| 57 |
+
model_path: Optional[str] = None
|
| 58 |
|
| 59 |
|
| 60 |
@app.on_event("startup")
|
|
|
|
| 62 |
global engine
|
| 63 |
logger.info("🚀 Starting Nexus-Core API...")
|
| 64 |
|
| 65 |
+
# Check model file
|
| 66 |
+
model_path = "/app/models/nexus_core.onnx"
|
| 67 |
+
|
| 68 |
+
# Debug: List files in models directory
|
| 69 |
+
if os.path.exists("/app/models"):
|
| 70 |
+
logger.info(f"📂 Files in /app/models/:")
|
| 71 |
+
for f in os.listdir("/app/models"):
|
| 72 |
+
full_path = os.path.join("/app/models", f)
|
| 73 |
+
size = os.path.getsize(full_path) / (1024*1024)
|
| 74 |
+
logger.info(f" - {f} ({size:.2f} MB)")
|
| 75 |
+
else:
|
| 76 |
+
logger.error("❌ /app/models/ directory does not exist!")
|
| 77 |
+
raise FileNotFoundError("/app/models/ not found")
|
| 78 |
+
|
| 79 |
+
# Check if model exists
|
| 80 |
+
if not os.path.exists(model_path):
|
| 81 |
+
logger.error(f"❌ Model not found at {model_path}")
|
| 82 |
+
logger.error("Available files:", os.listdir("/app/models") if os.path.exists("/app/models") else "No models dir")
|
| 83 |
+
raise FileNotFoundError(f"Model file not found: {model_path}")
|
| 84 |
+
|
| 85 |
+
logger.info(f"✅ Model found: {model_path} ({os.path.getsize(model_path)/(1024*1024):.2f} MB)")
|
| 86 |
+
|
| 87 |
try:
|
| 88 |
engine = NexusCoreEngine(
|
| 89 |
+
model_path=model_path,
|
| 90 |
num_threads=2
|
| 91 |
)
|
| 92 |
+
logger.info("✅ Nexus-Core engine loaded successfully")
|
| 93 |
except Exception as e:
|
| 94 |
+
logger.error(f"❌ Failed to load engine: {e}", exc_info=True)
|
| 95 |
raise
|
| 96 |
|
| 97 |
|
|
|
|
| 100 |
return {
|
| 101 |
"status": "healthy" if engine else "unhealthy",
|
| 102 |
"model_loaded": engine is not None,
|
| 103 |
+
"version": "2.0.0",
|
| 104 |
+
"model_path": "/app/models/nexus_core.onnx" if engine else None
|
| 105 |
}
|
| 106 |
|
| 107 |
|
|
|
|
| 126 |
f"Move: {result['best_move']} | "
|
| 127 |
f"Eval: {result['evaluation']:+.2f} | "
|
| 128 |
f"Depth: {result['depth_searched']} | "
|
| 129 |
+
f"Nodes: {result['nodes_evaluated']} | "
|
| 130 |
f"Time: {result['time_taken']}ms"
|
| 131 |
)
|
| 132 |
|
| 133 |
return MoveResponse(**result)
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
+
logger.error(f"Error during search: {e}", exc_info=True)
|
| 137 |
raise HTTPException(500, str(e))
|
| 138 |
|
| 139 |
|
|
|
|
| 143 |
"name": "Nexus-Core API",
|
| 144 |
"version": "2.0.0",
|
| 145 |
"model": "13M parameters",
|
| 146 |
+
"speed": "Ultra-fast (0.5-1s per move)",
|
| 147 |
+
"status": "healthy" if engine else "loading",
|
| 148 |
"endpoints": {
|
| 149 |
"POST /get-move": "Get best move",
|
| 150 |
+
"GET /health": "Health check",
|
| 151 |
+
"GET /docs": "API documentation"
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
import uvicorn
|
| 158 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|