Rafs-an09002 commited on
Commit
8d1f456
·
verified ·
1 Parent(s): da6ae37

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Nexus-Core Inference API
3
+ Fast and efficient chess engine
4
+ """
5
+
6
+ from fastapi import FastAPI, HTTPException
7
+ 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(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
+ app = FastAPI(
19
+ title="Nexus-Core Inference API",
20
+ description="Fast chess engine (13M params)",
21
+ version="2.0.0"
22
+ )
23
+
24
+ app.add_middleware(
25
+ CORSMiddleware,
26
+ allow_origins=["*"],
27
+ allow_credentials=True,
28
+ allow_methods=["*"],
29
+ allow_headers=["*"],
30
+ )
31
+
32
+ engine = None
33
+
34
+
35
+ class MoveRequest(BaseModel):
36
+ fen: str
37
+ depth: Optional[int] = Field(4, ge=1, le=6)
38
+ time_limit: Optional[int] = Field(3000, ge=1000, le=10000)
39
+
40
+
41
+ class MoveResponse(BaseModel):
42
+ best_move: str
43
+ evaluation: float
44
+ depth_searched: int
45
+ nodes_evaluated: int
46
+ time_taken: int
47
+
48
+
49
+ class HealthResponse(BaseModel):
50
+ status: str
51
+ model_loaded: bool
52
+ version: str
53
+
54
+
55
+ @app.on_event("startup")
56
+ async def startup_event():
57
+ global engine
58
+ logger.info("🚀 Starting Nexus-Core API...")
59
+
60
+ try:
61
+ engine = NexusCoreEngine(
62
+ model_path="/app/models/nexus_core.onnx",
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
+
71
+ @app.get("/health", response_model=HealthResponse)
72
+ 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
+
80
+ @app.post("/get-move", response_model=MoveResponse)
81
+ async def get_move(request: MoveRequest):
82
+ if not engine:
83
+ raise HTTPException(503, "Engine not loaded")
84
+
85
+ if not engine.validate_fen(request.fen):
86
+ raise HTTPException(400, "Invalid FEN")
87
+
88
+ start = time.time()
89
+
90
+ try:
91
+ result = engine.get_best_move(
92
+ request.fen,
93
+ request.depth,
94
+ request.time_limit
95
+ )
96
+
97
+ logger.info(
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
+
111
+ @app.get("/")
112
+ async def root():
113
+ return {
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)