-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
141 lines (109 loc) · 3.56 KB
/
app.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Import dependencies
from flask import Flask, render_template, request, jsonify
import joblib
import pandas as pd
import pickle as pkl
# Initialize Flask app
app = Flask(__name__)
# Load ML model and data scaler
model = joblib.load('static/data/Resources/model.joblib')
scaler = joblib.load('static/data/Resources/scaler.joblib')
# Home Page
@ app.route('/')
def home():
return render_template('home.html')
# Predictor Page
@ app.route('/predictor')
def predictor():
return render_template('predictor.html')
# Prediction API fetch
@ app.route('/predict', methods=['POST'])
def predict():
dataInput = (request.json)
data = {}
try:
for x in range(52):
if int(dataInput['week']) == x+1:
data[f"week_of_year_{x+1}"] = [1]
else:
data[f"week_of_year_{x+1}"] = [0]
for x in range(10):
if int(dataInput['store']) == x+1 and x+1 != 9:
data[f"Store_{x+1}"] = [1]
elif x+1 != 9:
data[f"Store_{x+1}"] = [0]
else:
continue
for x in range(3):
if int(dataInput['product']) == x+1:
data[f"Product_{x+1}"] = [1]
else:
data[f"Product_{x+1}"] = [0]
if float(dataInput["price"]) < float(dataInput["basePrice"]):
data['promotion'] = [1]
else:
data['promotion'] = [0]
data["Base Price"] = [float(dataInput["basePrice"])]
data["Price"] = [float(dataInput["price"])]
data["Is_Holiday"] = [float(dataInput["holiday"])]
except:
return jsonify({'quantity': -1})
input_df = pd.DataFrame(data)
input_scaled = scaler.transform(input_df)
output = model.predict(input_scaled)
return jsonify({"quantity": output[0]})
@ app.route('/projections')
def projections():
return render_template('projections.html')
#
@ app.route('/predict_projections', methods=['POST'])
def predict_projections():
dataInput = (request.json)
data = {}
try:
for x in range(52):
if int(dataInput['week']) == x+1:
data[f"week_of_year_{x+1}"] = [1]
else:
data[f"week_of_year_{x+1}"] = [0]
for x in range(10):
if int(dataInput['store']) == x+1 and x+1 != 9:
data[f"Store_{x+1}"] = [1]
elif x+1 != 9:
data[f"Store_{x+1}"] = [0]
else:
continue
for x in range(3):
if int(dataInput['product']) == x+1:
data[f"Product_{x+1}"] = [1]
else:
data[f"Product_{x+1}"] = [0]
if int(dataInput["price"]) < int(dataInput["basePrice"]):
data['promotion'] = [1]
else:
data['promotion'] = [0]
data["Base Price"] = [int(dataInput["basePrice"])]
data["Price"] = [int(dataInput["price"])]
data["Is_Holiday"] = [int(dataInput["holiday"])]
except:
return jsonify({'quantity': -1})
input_df = pd.DataFrame(data)
output = model.predict(input_df)
return jsonify({"quantity": output[0]})
# Data Page
@app.route('/data')
def data():
return render_template('data.html')
# About Page
@app.route('/about')
def about():
return render_template('about.html')
# Error Handler Page
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
if __name__ == '__main__':
# Run this when running on LOCAL server...
app.run(debug=True)
# ...OR run this when PRODUCTION server.
# app.run(debug=False)