flaskApp / main.py
Arafath10's picture
Update main.py
58941b3 verified
raw
history blame contribute delete
No virus
4.14 kB
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
from keras.optimizers import Adam
# instantiate flask
app = flask.Flask(__name__)
# Load the model without compilation
model = load_model('final_model.h5', compile=False)
# Recompile the model with the appropriate optimizer
optimizer = Adam(learning_rate=0.001) # Adjust learning_rate as needed
model.compile(optimizer=optimizer, loss='mean_squared_error') # Adjust the loss function as needed
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("/predict", methods=["POST"])
def predict():
dat = flask.request.form['date']
time = flask.request.form['time']
#holiday
holiday = 1 if str(dat) in holidays_tt else 0
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"]
# 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 += 25
# hour
hour = int(time[:-3])
# population dictionary
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(list(dic.keys()))
lt = list(dic.keys())
df = pd.DataFrame(lt)
divs = lb.transform(df)
divs = pd.DataFrame(divs)
# Preparing data
week_col = [week] * 14
temp_max_col = [temp_max] * 14
temp_min_col = [temp_min] * 14
holiday_col = [holiday] * 14
pop_col = [dic[x] for x in lt]
hour_col = [hour] * 14
divs = pd.concat([pd.DataFrame(temp_max_col), divs], axis=1)
divs = pd.concat([pd.DataFrame(temp_min_col), divs], axis=1)
divs = pd.concat([pd.DataFrame(week_col), divs], axis=1)
divs = pd.concat([divs, pd.DataFrame(holiday_col)], axis=1)
divs = pd.concat([divs, pd.DataFrame(pop_col)], axis=1)
divs = pd.concat([divs, pd.DataFrame(hour_col)], axis=1)
smol = pd.read_excel('smol.xlsx').iloc[:, 1:]
df = pd.DataFrame(np.concatenate((divs.values, smol.values), axis=0))
sc_X = StandardScaler()
df = sc_X.fit_transform(df)
prd = model.predict(df)
prd = abs(prd[0:14])
newprd = prd.tolist()
return flask.render_template("index.html", data=newprd)