File size: 5,688 Bytes
fb3c402 |
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# -*- coding: utf-8 -*-
"""flaskProto.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1VC7pgAb9uuRwD0Bm89x8sAvKi7uliBoF
"""
#cd drive/'My Drive'/datasetDevhack
import os
import flask
import pandas as pd
import tensorflow as tf
from keras.models import load_model
import requests
import datetime
from sklearn import preprocessing
import numpy as np
from sklearn.preprocessing import StandardScaler
import json
import pickle
from sklearn.pipeline import Pipeline
# instantiate flask
app = flask.Flask(__name__)
# load the model, and pass in the custom metric function
global graph
graph = tf.get_default_graph()
holidays_tt = ["2020-01-01",
"2020-01-15",
"2020-01-26",
"2020-02-21",
"2020-03-10",
"2020-03-25",
"2020-04-02",
"2020-04-06",
"2020-04-10",
"2020-05-01",
"2020-05-07",
"2020-05-25",
"2020-06-23",
"2020-08-01",
"2020-08-03",
"2020-08-12",
"2020-08-15",
"2020-08-22",
"2020-08-30",
"2020-08-31",
"2020-10-02",
"2020-10-25",
"2020-10-30",
"2020-11-14",
"2020-11-30",
"2020-12-25"
]
url = "https://api.openweathermap.org/data/2.5/weather?q=Bengaluru,in&APPID=b1a275b64af38a8f9823800a58345b93"
# homepage
@app.route("/", methods=["GET","POST"])
def homepage():
return flask.render_template("index.html")
# define a predict function as an endpoint
# @app.route("/feedback", methods=["POST"])
# def predict():
# formFinal = []
# formData1 = int(flask.request.form['formData1'])
# print(formData1)
# formData2 = int(flask.request.form['formData2'])
# print(formData2)
# formFinal.append(formData1)
# formFinal.append(formData2)
# print(formFinal)
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]
model = load_model('final_model.h5')
@app.route("/predict", methods=["POST"])
def predict():
dat = flask.request.form['date']
#print(dat)
time = flask.request.form['time']
# print(time)
#holiday
if str(dat) in holidays_tt:
holiday=1
else:
holiday=0
#print("Holiday =", holiday)
response = requests.get(url).json()
temp = float(response["main"]["temp"]) - 273.15
temp_min = float(response["main"]["temp_min"]) - 283.15 #made it 283.15 from 273.15
temp_max = float(response["main"]["temp_max"]) - 273.15
pressure = response["main"]["pressure"]
humidity = response["main"]["humidity"]
#print(temp, temp_min, temp_max, pressure, humidity)
#dat = "2023-11-01"
#time = "01:01"
#week
date_time_obj = datetime.datetime.strptime(dat, '%Y-%m-%d')
week = datetime.date(date_time_obj.year,date_time_obj.month,date_time_obj.day).isocalendar()[1]
if week<26:
week = week + 25
#hour
hour = int(time[:-3])
#population
dic = {
"HSR Division" : 105265,
"Koramangala Division" : 63987,
"Indiranagar" : 58830,
"Shivajinagar" : 57437,
"Hebbal" : 54301,
"Whitefield" : 84428,
"Malleshwaram" : 57107,
"Rajaji Nagara Division" : 55250,
"Jayanagar" : 56658,
"Jalahalli" : 63391,
"Kengeri Division" : 68087,
"R R NAGAR" : 82848,
"Vidhanasoudha" : 69057,
"Peenya Division" : 96549
}
lb = preprocessing.LabelBinarizer()
lb.fit(['HSR Division', 'Koramangala Division', 'Indiranagar',
'Shivajinagar', 'Hebbal', 'Whitefield', 'Malleshwaram',
'Rajaji Nagara Division', 'Jayanagar', 'Jalahalli',
'Kengeri Division', 'R R NAGAR', 'Vidhanasoudha',
'Peenya Division'])
lt = list(dic.keys())
df = pd.DataFrame(lt)
divs = lb.transform(df)
divs = pd.DataFrame(divs)
#divs['Week'] = week
#divs['Week'] = np.array(([week]*13).T
week = [week]*14
temp_max = [temp_max]*14
temp_min = [temp_min]*14
holiday = [holiday]*14
divs = pd.concat([pd.DataFrame(temp_max), divs], axis=1)
divs = pd.concat([pd.DataFrame(temp_min), divs], axis=1)
divs = pd.concat([pd.DataFrame(week), divs], axis=1)
divs = pd.concat([divs, pd.DataFrame(holiday)], axis=1)
pop = [dic[x] for x in lt]
#population
divs = pd.concat([divs, pd.DataFrame(pop)], axis=1)
hour = [hour]*14
divs = pd.concat([ divs, pd.DataFrame(hour)], axis=1)
smol = pd.read_excel('smol.xlsx')
smol = smol.iloc[:,1:]
#divs = pd.read_excel("pls.xlsx")
# divs.to_csv("pls.csv")
#print(smol.shape)
#print(divs.shape)
#fin = pd.concat([divs, smol])
df = pd.DataFrame( np.concatenate( (divs.values, smol.values), axis=0 ) )
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
df = sc_X.fit_transform(df)
with graph.as_default():
prd = model.predict(df)
#print("divs is: ", divs)
prd = abs(prd[0:14])#-mar)
print("fin",prd)
newprd = prd.tolist()
#print(newprd)
return flask.render_template("index.html", data = newprd)
# start the flask app, allow remote connections
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000) |