Paweł Łaba
commited on
Commit
·
e1e7a0d
1
Parent(s):
e9d5caf
dodanie api
Browse files- app.py +136 -5
- model/model.keras +0 -0
- requirements.txt +4 -1
app.py
CHANGED
@@ -1,7 +1,138 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
app =
|
|
|
4 |
|
5 |
-
|
6 |
-
def
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
from flask_cors import CORS
|
4 |
+
import numpy as np
|
5 |
+
from tensorflow import keras
|
6 |
+
import json
|
7 |
+
import os
|
8 |
|
9 |
+
app = Flask(__name__)
|
10 |
+
CORS(app)
|
11 |
|
12 |
+
class TicTacToeAI:
|
13 |
+
def __init__(self, model_path='model/model.keras'):
|
14 |
+
self.model_path = model_path
|
15 |
+
self.model = None
|
16 |
+
self.load_model()
|
17 |
+
|
18 |
+
def load_model(self):
|
19 |
+
"""Wczytuje model z pliku"""
|
20 |
+
if os.path.exists(self.model_path):
|
21 |
+
self.model = keras.models.load_model(self.model_path)
|
22 |
+
return True
|
23 |
+
return False
|
24 |
+
|
25 |
+
def get_move(self, board):
|
26 |
+
"""Zwraca najlepszy ruch dla danego stanu planszy"""
|
27 |
+
if self.model is None:
|
28 |
+
raise ValueError("Model nie został wczytany")
|
29 |
+
|
30 |
+
board_array = np.array(board)
|
31 |
+
predictions = self.model.predict(board_array.reshape(1, -1), verbose=0)[0]
|
32 |
+
valid_moves = np.where(board_array == 0)[0]
|
33 |
+
|
34 |
+
if len(valid_moves) == 0:
|
35 |
+
raise ValueError("Brak dostępnych ruchów")
|
36 |
+
|
37 |
+
valid_predictions = [(i, pred) for i, pred in enumerate(predictions) if i in valid_moves]
|
38 |
+
return int(max(valid_predictions, key=lambda x: x[1])[0])
|
39 |
+
|
40 |
+
# Inicjalizacja AI
|
41 |
+
ai = TicTacToeAI()
|
42 |
+
|
43 |
+
@app.route('/api/move', methods=['POST'])
|
44 |
+
def get_move():
|
45 |
+
"""
|
46 |
+
Endpoint do wykonania ruchu AI
|
47 |
+
|
48 |
+
Przykładowe żądanie:
|
49 |
+
{
|
50 |
+
"board": [0, 0, 0, 0, 1, 0, 0, 0, -1]
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
try:
|
54 |
+
data = request.get_json()
|
55 |
+
|
56 |
+
if 'board' not in data:
|
57 |
+
return jsonify({'error': 'Brak planszy w żądaniu'}), 400
|
58 |
+
|
59 |
+
board = data['board']
|
60 |
+
|
61 |
+
if len(board) != 9:
|
62 |
+
return jsonify({'error': 'Nieprawidłowy rozmiar planszy'}), 400
|
63 |
+
|
64 |
+
if not all(x in [0, 1, -1] for x in board):
|
65 |
+
return jsonify({'error': 'Nieprawidłowe wartości na planszy'}), 400
|
66 |
+
|
67 |
+
move = ai.get_move(board)
|
68 |
+
|
69 |
+
return jsonify({
|
70 |
+
'status': 'success',
|
71 |
+
'move': move
|
72 |
+
})
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
return jsonify({'error': str(e)}), 500
|
76 |
+
|
77 |
+
|
78 |
+
# dodaj posta dodającego do gry.json kolejne wygrane i przegrane, jako parametry dostaniesz board i win budowa pliku [{"board":[-1,1,0,-1,1,0,0,1,-1],"win":true}]
|
79 |
+
@app.route('/api/game', methods=['POST'])
|
80 |
+
def add_game():
|
81 |
+
"""
|
82 |
+
Endpoint do dodania gry do pliku
|
83 |
+
|
84 |
+
Przykładowe żądanie:
|
85 |
+
{
|
86 |
+
"board": [0, 0, 0, 0, 1, 0, 0, 0, -1],
|
87 |
+
"win": true
|
88 |
+
}
|
89 |
+
"""
|
90 |
+
try:
|
91 |
+
data = request.get_json()
|
92 |
+
|
93 |
+
if 'board' not in data:
|
94 |
+
return jsonify({'error': 'Brak planszy w żądaniu'}), 400
|
95 |
+
|
96 |
+
if 'win' not in data:
|
97 |
+
return jsonify({'error': 'Brak informacji o wygranej w żądaniu'}), 400
|
98 |
+
|
99 |
+
board = data['board']
|
100 |
+
win = data['win']
|
101 |
+
|
102 |
+
if len(board) != 9:
|
103 |
+
return jsonify({'error': 'Nieprawidłowy rozmiar planszy'}), 400
|
104 |
+
|
105 |
+
if not all(x in [0, 1, -1] for x in board):
|
106 |
+
return jsonify({'error': 'Nieprawidłowe wartości na planszy'}), 400
|
107 |
+
|
108 |
+
with open('gry.json', 'r') as file:
|
109 |
+
games_data = json.load(file)
|
110 |
+
|
111 |
+
games_data.append({
|
112 |
+
'board': board,
|
113 |
+
'win': win
|
114 |
+
})
|
115 |
+
|
116 |
+
with open('gry.json', 'w') as file:
|
117 |
+
json.dump(games_data, file)
|
118 |
+
|
119 |
+
return jsonify({
|
120 |
+
'status': 'success'
|
121 |
+
})
|
122 |
+
|
123 |
+
except Exception as e:
|
124 |
+
return jsonify({'error': str(e)}), 500
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
@app.route('/api/status', methods=['GET'])
|
129 |
+
def get_status():
|
130 |
+
"""Sprawdza status modelu"""
|
131 |
+
return jsonify({
|
132 |
+
'status': 'success',
|
133 |
+
'model_loaded': ai.model is not None,
|
134 |
+
'model_path': ai.model_path
|
135 |
+
})
|
136 |
+
|
137 |
+
if __name__ == '__main__':
|
138 |
+
app.run(host='0.0.0.0', port=5001)
|
model/model.keras
ADDED
Binary file (151 kB). View file
|
|
requirements.txt
CHANGED
@@ -1,2 +1,5 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
uvicorn[standard]
|
|
|
1 |
+
flask
|
2 |
+
flask_cors
|
3 |
+
tensorflow
|
4 |
+
json
|
5 |
uvicorn[standard]
|