tictactoe / app.py
Pawe艂 艁aba
zmiany w index
9616708
raw
history blame
3.65 kB
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
import numpy as np
from tensorflow import keras
import json
import os
app = FastAPI()
# Obs艂uga plik贸w statycznych
app.mount("/public", StaticFiles(directory="public"), name="public")
@app.get("/", response_class=HTMLResponse)
async def read_root():
"""Wy艣wietla plik index.html"""
try:
with open("public/index.html", "r") as file:
return HTMLResponse(content=file.read(), status_code=200)
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Plik index.html nie zosta艂 znaleziony")
class TicTacToeAI:
def __init__(self, model_path="model/model.keras"):
self.model_path = model_path
self.model = None
self.load_model()
def load_model(self):
"""Wczytuje model z pliku"""
if os.path.exists(self.model_path):
self.model = keras.models.load_model(self.model_path)
return True
return False
def get_move(self, board):
"""Zwraca najlepszy ruch dla danego stanu planszy"""
if self.model is None:
raise ValueError("Model nie zosta艂 wczytany")
board_array = np.array(board)
predictions = self.model.predict(board_array.reshape(1, -1), verbose=0)[0]
valid_moves = np.where(board_array == 0)[0]
if len(valid_moves) == 0:
raise ValueError("Brak dost臋pnych ruch贸w")
valid_predictions = [(i, pred) for i, pred in enumerate(predictions) if i in valid_moves]
return int(max(valid_predictions, key=lambda x: x[1])[0])
# Inicjalizacja AI
ai = TicTacToeAI()
@app.post("/move")
async def get_move(request: Request):
"""
Endpoint do wykonania ruchu AI
"""
try:
data = await request.json()
board = data.get("board")
if not board:
raise HTTPException(status_code=400, detail="Brak planszy w 偶膮daniu")
if len(board) != 9 or not all(x in [0, 1, -1] for x in board):
raise HTTPException(status_code=400, detail="Nieprawid艂owe dane planszy")
move = ai.get_move(board)
return JSONResponse(content={"status": "success", "move": move})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/game")
async def add_game(request: Request):
"""
Endpoint do dodania gry do pliku
"""
try:
data = await request.json()
board = data.get("board")
win = data.get("win")
if not board or win is None:
raise HTTPException(status_code=400, detail="Brak planszy lub informacji o wygranej w 偶膮daniu")
if len(board) != 9 or not all(x in [0, 1, -1] for x in board):
raise HTTPException(status_code=400, detail="Nieprawid艂owe dane planszy")
if os.path.exists("gry.json"):
with open("gry.json", "r") as file:
games_data = json.load(file)
else:
games_data = []
games_data.append({"board": board, "win": win})
with open("gry.json", "w") as file:
json.dump(games_data, file)
return JSONResponse(content={"status": "success"})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/status")
async def get_status():
"""Sprawdza status modelu"""
return JSONResponse(
content={
"status": "success",
"model_loaded": ai.model is not None,
"model_path": ai.model_path,
}
)