Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import numpy as np
|
3 |
+
import gym
|
4 |
+
from flask import Flask, render_template, request
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Load the Q-learning model
|
9 |
+
with open("q-learning.pkl", "rb") as f:
|
10 |
+
model = pickle.load(f)
|
11 |
+
|
12 |
+
# Initialize the Taxi-v3 environment
|
13 |
+
env = gym.make("Taxi-v3")
|
14 |
+
|
15 |
+
@app.route('/')
|
16 |
+
def home():
|
17 |
+
return render_template('index.html')
|
18 |
+
|
19 |
+
@app.route('/predict', methods=['POST'])
|
20 |
+
def predict():
|
21 |
+
try:
|
22 |
+
# Get the state from the user input
|
23 |
+
state = int(request.form['state'])
|
24 |
+
|
25 |
+
# Get the action from the model
|
26 |
+
action = model.predict(np.array([state])) # This assumes model.predict can work with the state as input
|
27 |
+
|
28 |
+
# Return the result
|
29 |
+
return render_template('index.html', state=state, action=action)
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error: {str(e)}"
|
32 |
+
|
33 |
+
if __name__ == '__main__':
|
34 |
+
app.run(debug=True)
|