Update main.py
Browse files
main.py
CHANGED
@@ -1,13 +1,3 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""flaskProto.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1VC7pgAb9uuRwD0Bm89x8sAvKi7uliBoF
|
8 |
-
"""
|
9 |
-
|
10 |
-
#cd drive/'My Drive'/datasetDevhack
|
11 |
import os
|
12 |
import flask
|
13 |
import pandas as pd
|
@@ -25,75 +15,51 @@ from sklearn.pipeline import Pipeline
|
|
25 |
# instantiate flask
|
26 |
app = flask.Flask(__name__)
|
27 |
|
28 |
-
# load the model
|
29 |
-
|
30 |
-
graph = tf.get_default_graph()
|
31 |
|
32 |
holidays_tt = ["2020-01-01",
|
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 |
url = "https://api.openweathermap.org/data/2.5/weather?q=Bengaluru,in&APPID=b1a275b64af38a8f9823800a58345b93"
|
|
|
60 |
# homepage
|
61 |
@app.route("/", methods=["GET","POST"])
|
62 |
def homepage():
|
63 |
return flask.render_template("index.html")
|
64 |
|
65 |
# define a predict function as an endpoint
|
66 |
-
# @app.route("/feedback", methods=["POST"])
|
67 |
-
# def predict():
|
68 |
-
# formFinal = []
|
69 |
-
# formData1 = int(flask.request.form['formData1'])
|
70 |
-
# print(formData1)
|
71 |
-
# formData2 = int(flask.request.form['formData2'])
|
72 |
-
# print(formData2)
|
73 |
-
# formFinal.append(formData1)
|
74 |
-
# formFinal.append(formData2)
|
75 |
-
# print(formFinal)
|
76 |
-
|
77 |
-
|
78 |
-
mar = [0.14,0.53,0.24,0.13,0.67,0.87,0.22,0.23,0.12,0.56,0.23,0.25,0.78,0.12]
|
79 |
-
model = load_model('final_model.h5')
|
80 |
-
|
81 |
-
|
82 |
@app.route("/predict", methods=["POST"])
|
83 |
def predict():
|
84 |
dat = flask.request.form['date']
|
85 |
-
#print(dat)
|
86 |
time = flask.request.form['time']
|
87 |
-
# print(time)
|
88 |
|
89 |
#holiday
|
90 |
-
if str(dat) in holidays_tt
|
91 |
-
holiday=1
|
92 |
-
else:
|
93 |
-
holiday=0
|
94 |
-
|
95 |
-
#print("Holiday =", holiday)
|
96 |
-
|
97 |
|
98 |
response = requests.get(url).json()
|
99 |
temp = float(response["main"]["temp"]) - 273.15
|
@@ -101,120 +67,64 @@ def predict():
|
|
101 |
temp_max = float(response["main"]["temp_max"]) - 273.15
|
102 |
pressure = response["main"]["pressure"]
|
103 |
humidity = response["main"]["humidity"]
|
104 |
-
#print(temp, temp_min, temp_max, pressure, humidity)
|
105 |
-
|
106 |
-
#dat = "2023-11-01"
|
107 |
-
#time = "01:01"
|
108 |
|
109 |
-
#week
|
110 |
date_time_obj = datetime.datetime.strptime(dat, '%Y-%m-%d')
|
111 |
-
week = datetime.date(date_time_obj.year,date_time_obj.month,date_time_obj.day).isocalendar()[1]
|
112 |
-
|
113 |
-
|
114 |
-
week = week + 25
|
115 |
-
|
116 |
|
117 |
-
#hour
|
118 |
hour = int(time[:-3])
|
119 |
|
120 |
-
#population
|
121 |
dic = {
|
122 |
-
"HSR Division"
|
123 |
-
"Koramangala Division"
|
124 |
-
"Indiranagar"
|
125 |
-
"Shivajinagar"
|
126 |
-
"Hebbal"
|
127 |
-
"Whitefield"
|
128 |
-
"Malleshwaram"
|
129 |
-
"Rajaji Nagara Division"
|
130 |
-
"Jayanagar"
|
131 |
-
"Jalahalli"
|
132 |
-
"Kengeri Division"
|
133 |
-
"R R NAGAR"
|
134 |
-
"Vidhanasoudha"
|
135 |
-
"Peenya Division"
|
136 |
-
|
137 |
-
|
138 |
|
139 |
lb = preprocessing.LabelBinarizer()
|
140 |
-
lb.fit(
|
141 |
-
'Shivajinagar', 'Hebbal', 'Whitefield', 'Malleshwaram',
|
142 |
-
'Rajaji Nagara Division', 'Jayanagar', 'Jalahalli',
|
143 |
-
'Kengeri Division', 'R R NAGAR', 'Vidhanasoudha',
|
144 |
-
'Peenya Division'])
|
145 |
-
|
146 |
-
|
147 |
lt = list(dic.keys())
|
148 |
-
|
149 |
df = pd.DataFrame(lt)
|
150 |
-
|
151 |
divs = lb.transform(df)
|
152 |
-
|
153 |
divs = pd.DataFrame(divs)
|
154 |
|
155 |
-
#
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
holiday = [holiday]*14
|
165 |
|
166 |
-
divs = pd.concat([pd.DataFrame(temp_max), divs], axis=1)
|
167 |
-
divs = pd.concat([pd.DataFrame(temp_min), divs], axis=1)
|
168 |
-
divs = pd.concat([pd.DataFrame(week), divs], axis=1)
|
169 |
-
divs = pd.concat([divs, pd.DataFrame(holiday)], axis=1)
|
170 |
-
|
171 |
-
|
172 |
-
pop = [dic[x] for x in lt]
|
173 |
-
|
174 |
-
#population
|
175 |
-
divs = pd.concat([divs, pd.DataFrame(pop)], axis=1)
|
176 |
-
|
177 |
-
|
178 |
-
hour = [hour]*14
|
179 |
-
|
180 |
-
divs = pd.concat([ divs, pd.DataFrame(hour)], axis=1)
|
181 |
-
|
182 |
-
|
183 |
-
smol = pd.read_excel('smol.xlsx')
|
184 |
-
smol = smol.iloc[:,1:]
|
185 |
-
#divs = pd.read_excel("pls.xlsx")
|
186 |
-
|
187 |
-
# divs.to_csv("pls.csv")
|
188 |
-
|
189 |
-
#print(smol.shape)
|
190 |
-
#print(divs.shape)
|
191 |
-
|
192 |
-
#fin = pd.concat([divs, smol])
|
193 |
-
|
194 |
-
df = pd.DataFrame( np.concatenate( (divs.values, smol.values), axis=0 ) )
|
195 |
-
|
196 |
-
from sklearn.preprocessing import StandardScaler
|
197 |
sc_X = StandardScaler()
|
198 |
df = sc_X.fit_transform(df)
|
199 |
-
|
200 |
-
|
201 |
-
with graph.as_default():
|
202 |
-
prd = model.predict(df)
|
203 |
-
|
204 |
-
|
205 |
-
#print("divs is: ", divs)
|
206 |
-
prd = abs(prd[0:14])#-mar)
|
207 |
|
208 |
-
|
|
|
209 |
newprd = prd.tolist()
|
210 |
-
#print(newprd)
|
211 |
-
|
212 |
-
|
213 |
-
return flask.render_template("index.html", data = newprd)
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
|
218 |
-
|
219 |
-
if __name__ == "__main__":
|
220 |
-
app.run(host='0.0.0.0', port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import flask
|
3 |
import pandas as pd
|
|
|
15 |
# instantiate flask
|
16 |
app = flask.Flask(__name__)
|
17 |
|
18 |
+
# load the model
|
19 |
+
model = load_model('final_model.h5')
|
|
|
20 |
|
21 |
holidays_tt = ["2020-01-01",
|
22 |
+
"2020-01-15",
|
23 |
+
"2020-01-26",
|
24 |
+
"2020-02-21",
|
25 |
+
"2020-03-10",
|
26 |
+
"2020-03-25",
|
27 |
+
"2020-04-02",
|
28 |
+
"2020-04-06",
|
29 |
+
"2020-04-10",
|
30 |
+
"2020-05-01",
|
31 |
+
"2020-05-07",
|
32 |
+
"2020-05-25",
|
33 |
+
"2020-06-23",
|
34 |
+
"2020-08-01",
|
35 |
+
"2020-08-03",
|
36 |
+
"2020-08-12",
|
37 |
+
"2020-08-15",
|
38 |
+
"2020-08-22",
|
39 |
+
"2020-08-30",
|
40 |
+
"2020-08-31",
|
41 |
+
"2020-10-02",
|
42 |
+
"2020-10-25",
|
43 |
+
"2020-10-30",
|
44 |
+
"2020-11-14",
|
45 |
+
"2020-11-30",
|
46 |
+
"2020-12-25"
|
47 |
+
]
|
48 |
url = "https://api.openweathermap.org/data/2.5/weather?q=Bengaluru,in&APPID=b1a275b64af38a8f9823800a58345b93"
|
49 |
+
|
50 |
# homepage
|
51 |
@app.route("/", methods=["GET","POST"])
|
52 |
def homepage():
|
53 |
return flask.render_template("index.html")
|
54 |
|
55 |
# define a predict function as an endpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
@app.route("/predict", methods=["POST"])
|
57 |
def predict():
|
58 |
dat = flask.request.form['date']
|
|
|
59 |
time = flask.request.form['time']
|
|
|
60 |
|
61 |
#holiday
|
62 |
+
holiday = 1 if str(dat) in holidays_tt else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
response = requests.get(url).json()
|
65 |
temp = float(response["main"]["temp"]) - 273.15
|
|
|
67 |
temp_max = float(response["main"]["temp_max"]) - 273.15
|
68 |
pressure = response["main"]["pressure"]
|
69 |
humidity = response["main"]["humidity"]
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
# week
|
72 |
date_time_obj = datetime.datetime.strptime(dat, '%Y-%m-%d')
|
73 |
+
week = datetime.date(date_time_obj.year, date_time_obj.month, date_time_obj.day).isocalendar()[1]
|
74 |
+
if week < 26:
|
75 |
+
week += 25
|
|
|
|
|
76 |
|
77 |
+
# hour
|
78 |
hour = int(time[:-3])
|
79 |
|
80 |
+
# population dictionary
|
81 |
dic = {
|
82 |
+
"HSR Division": 105265,
|
83 |
+
"Koramangala Division": 63987,
|
84 |
+
"Indiranagar": 58830,
|
85 |
+
"Shivajinagar": 57437,
|
86 |
+
"Hebbal": 54301,
|
87 |
+
"Whitefield": 84428,
|
88 |
+
"Malleshwaram": 57107,
|
89 |
+
"Rajaji Nagara Division": 55250,
|
90 |
+
"Jayanagar": 56658,
|
91 |
+
"Jalahalli": 63391,
|
92 |
+
"Kengeri Division": 68087,
|
93 |
+
"R R NAGAR": 82848,
|
94 |
+
"Vidhanasoudha": 69057,
|
95 |
+
"Peenya Division": 96549
|
96 |
+
}
|
|
|
97 |
|
98 |
lb = preprocessing.LabelBinarizer()
|
99 |
+
lb.fit(list(dic.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
lt = list(dic.keys())
|
|
|
101 |
df = pd.DataFrame(lt)
|
|
|
102 |
divs = lb.transform(df)
|
|
|
103 |
divs = pd.DataFrame(divs)
|
104 |
|
105 |
+
# Preparing data
|
106 |
+
week_col = [week] * 14
|
107 |
+
temp_max_col = [temp_max] * 14
|
108 |
+
temp_min_col = [temp_min] * 14
|
109 |
+
holiday_col = [holiday] * 14
|
110 |
+
pop_col = [dic[x] for x in lt]
|
111 |
+
hour_col = [hour] * 14
|
112 |
|
113 |
+
divs = pd.concat([pd.DataFrame(temp_max_col), divs], axis=1)
|
114 |
+
divs = pd.concat([pd.DataFrame(temp_min_col), divs], axis=1)
|
115 |
+
divs = pd.concat([pd.DataFrame(week_col), divs], axis=1)
|
116 |
+
divs = pd.concat([divs, pd.DataFrame(holiday_col)], axis=1)
|
117 |
+
divs = pd.concat([divs, pd.DataFrame(pop_col)], axis=1)
|
118 |
+
divs = pd.concat([divs, pd.DataFrame(hour_col)], axis=1)
|
119 |
|
120 |
+
smol = pd.read_excel('smol.xlsx').iloc[:, 1:]
|
121 |
+
df = pd.DataFrame(np.concatenate((divs.values, smol.values), axis=0))
|
|
|
|
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
sc_X = StandardScaler()
|
124 |
df = sc_X.fit_transform(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
+
prd = model.predict(df)
|
127 |
+
prd = abs(prd[0:14])
|
128 |
newprd = prd.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
return flask.render_template("index.html", data=newprd)
|
|
|
|