from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles import numpy as np from tensorflow import keras import json import os # Ustawienie prefiksu aplikacji app = FastAPI(root_path="/spaces/labapawel/tictactoe") # Montowanie katalogu publicznego app.mount("/static", StaticFiles(directory="public"), name="static") @app.get("/", response_class=HTMLResponse) async def read_root(): try: with open("public/index.html", "r") as file: return HTMLResponse(content=file.read(), status_code=200) except FileNotFoundError: return HTMLResponse(content="Plik index.html nie został znaleziony", status_code=404) 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(data: dict): """ Endpoint do wykonania ruchu AI """ try: 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 {"status": "success", "move": move} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/game") async def add_game(data: dict): """ Endpoint do dodania gry do pliku """ try: 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 {"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 { "status": "success", "model_loaded": ai.model is not None, "model_path": ai.model_path, }