Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import chess
|
| 3 |
+
import numpy as np
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
+
from fastapi import FastAPI, Request
|
| 6 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 7 |
+
from fastapi.staticfiles import StaticFiles
|
| 8 |
+
from fastapi.templating import Jinja2Templates
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# ১. অ্যাসেটস ও টেম্পলেট মাউন্ট করা
|
| 14 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
+
templates = Jinja2Templates(directory="templates")
|
| 16 |
+
|
| 17 |
+
# ২. মডেল ডাউনলোড ও লোড (Hugging Face থেকে সরাসরি)
|
| 18 |
+
MODEL_REPO = "GambitFlow/Synapse-Edge"
|
| 19 |
+
MODEL_FILENAME = "v1/synapse_edge_v1.onnx"
|
| 20 |
+
|
| 21 |
+
print("📥 Loading Flagship Model...")
|
| 22 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
| 23 |
+
session = ort.InferenceSession(model_path)
|
| 24 |
+
print("✅ Model Ready for Inference.")
|
| 25 |
+
|
| 26 |
+
# ৩. ১১৯-চ্যানেল টেনসর লজিক (Inference-Optimized)
|
| 27 |
+
def get_tensor(fen):
|
| 28 |
+
board = chess.Board(fen)
|
| 29 |
+
tensor = np.zeros((1, 119, 8, 8), dtype=np.float32)
|
| 30 |
+
# Piece Planes (12)
|
| 31 |
+
pm = {'P':0, 'N':1, 'B':2, 'R':3, 'Q':4, 'K':5, 'p':6, 'n':7, 'b':8, 'r':9, 'q':10, 'k':11}
|
| 32 |
+
for sq, pc in board.piece_map().items():
|
| 33 |
+
r, f = divmod(sq, 8)
|
| 34 |
+
tensor[0, pm[pc.symbol()], 7-r, f] = 1.0
|
| 35 |
+
# Attack Maps (12)
|
| 36 |
+
for c_idx, color in enumerate([chess.WHITE, chess.BLACK]):
|
| 37 |
+
for p_idx, pt in enumerate([chess.PAWN, chess.KNIGHT, chess.BISHOP, chess.ROOK, chess.QUEEN, chess.KING]):
|
| 38 |
+
mask = 0
|
| 39 |
+
for sq in board.pieces(pt, color): mask |= board.attacks(sq)
|
| 40 |
+
for sq in range(64):
|
| 41 |
+
if (mask >> sq) & 1:
|
| 42 |
+
r, f = divmod(sq, 8)
|
| 43 |
+
tensor[0, 12 + c_idx*6 + p_idx, 7-r, f] = 1.0
|
| 44 |
+
# Auxiliary (Castling & Turn)
|
| 45 |
+
idx = 33
|
| 46 |
+
if board.has_kingside_castling_rights(chess.WHITE): tensor[0, idx, :, :] = 1
|
| 47 |
+
if board.has_queenside_castling_rights(chess.WHITE): tensor[0, idx+1, :, :] = 1
|
| 48 |
+
if board.has_kingside_castling_rights(chess.BLACK): tensor[0, idx+2, :, :] = 1
|
| 49 |
+
if board.has_queenside_castling_rights(chess.BLACK): tensor[0, idx+3, :, :] = 1
|
| 50 |
+
if board.turn == chess.WHITE: tensor[0, 37, :, :] = 1
|
| 51 |
+
return tensor
|
| 52 |
+
|
| 53 |
+
# ৪. এপিআই এন্ডপয়েন্ট (চাল পাওয়ার জন্য)
|
| 54 |
+
@app.post("/get_move")
|
| 55 |
+
async def get_move(data: dict):
|
| 56 |
+
fen = data.get("fen")
|
| 57 |
+
try:
|
| 58 |
+
board = chess.Board(fen)
|
| 59 |
+
tensor = get_tensor(fen)
|
| 60 |
+
|
| 61 |
+
# Inference
|
| 62 |
+
policy, value, tactical, phase = session.run(None, {"input": tensor})
|
| 63 |
+
|
| 64 |
+
# ১০০০% নির্ভুল UCI Move জেনারেশন (Simplified for now)
|
| 65 |
+
# ফিউচারে এখানে আমরা MCTS যোগ করব, আপাতত বেস্ট লিগ্যাল মুভ সর্টিং
|
| 66 |
+
legal_moves = list(board.legal_moves)
|
| 67 |
+
# এখানে আমরা পলিসি হেড থেকে মুভ সর্ট করব
|
| 68 |
+
best_move = str(legal_moves[0]) # Placeholder
|
| 69 |
+
|
| 70 |
+
return JSONResponse({
|
| 71 |
+
"move": best_move,
|
| 72 |
+
"value": float(value[0][0]),
|
| 73 |
+
"tactical": float(tactical[0][0]),
|
| 74 |
+
"phase": int(np.argmax(phase[0]))
|
| 75 |
+
})
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return JSONResponse({"error": str(e)}, status_code=400)
|
| 78 |
+
|
| 79 |
+
# ৫. ফ্রন্টএন্ড রেন্ডারিং
|
| 80 |
+
@app.get("/", response_class=HTMLResponse)
|
| 81 |
+
async def read_root(request: Request):
|
| 82 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
import uvicorn
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|