-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
97 lines (72 loc) · 2.58 KB
/
server.py
File metadata and controls
97 lines (72 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
server.py
FastAPI backend for Sudoku AI Solver.
Supports CSP + DLX solving.
Run:
uvicorn server:app --reload
Author: kennz_psix
"""
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
import os
# Import solver
from solver.csp import solve_sudoku
from solver.dlx import solve_sudoku_dlx
# ---------------------------------------------------------------------------
# Config (from .env)
# ---------------------------------------------------------------------------
PORT = int(os.getenv("PORT", 8000))
DEBUG = os.getenv("DEBUG", "true").lower() == "true"
DEFAULT_ALGO = os.getenv("DEFAULT_ALGORITHM", "csp")
# ---------------------------------------------------------------------------
# App Init
# ---------------------------------------------------------------------------
app = FastAPI(
title="Sudoku AI Solver API",
description="CSP + DLX Solver Backend",
version="1.0.0"
)
# ---------------------------------------------------------------------------
# Request Model
# ---------------------------------------------------------------------------
class SolveRequest(BaseModel):
board: List[List[int]]
algorithm: str = DEFAULT_ALGO
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.get("/")
def root():
return {"message": "Sudoku AI Solver API is running"}
@app.post("/solve")
def solve(req: SolveRequest):
board = req.board
algo = req.algorithm.lower()
if len(board) != 9 or any(len(row) != 9 for row in board):
raise HTTPException(status_code=400, detail="Invalid board size")
try:
if algo == "dlx":
solved_board = solve_sudoku_dlx(board)
return {
"success": True,
"board": solved_board,
"steps": 0
}
elif algo == "csp":
success, solved_board, steps = solve_sudoku(board)
return {
"success": success,
"board": solved_board,
"steps": steps
}
else:
raise HTTPException(status_code=400, detail="Unknown algorithm")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ---------------------------------------------------------------------------
# Run (optional)
# ---------------------------------------------------------------------------
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host="0.0.0.0", port=PORT, reload=DEBUG)