Arijit-hazra's picture
new version
4391e28
from data_processing import process_data, standardizeX
from sklearn.preprocessing import OneHotEncoder
from api_data import fetch_data
from tensorflow import keras
import pandas as pd
import gradio as gr
import numpy as np
import pickle
import io
#========================================================================================================================
# load the models
#========================================================================================================================
temp_model = keras.models.load_model('results/temp_model.h5')
hum_model = keras.models.load_model('results/hum_model.h5')
overview_model = keras.models.load_model('results/overview_model.h5')
#========================================================================================================================
# retrieve mean and std and encoder
#========================================================================================================================
encoder = pickle.load(open("results/encoder.pkl", "rb"))
scale = pickle.load(open("results/mean_std.pkl", "rb"))
mean = scale["mean"]
std = scale["std"]
temp_mean = mean[0]
temp_std = std[0]
hum_mean = mean[2]
hum_std = std[2]
# overview dictionary
overview = {
0:'Clear',
1: 'Clear',
2: 'Partially cloudy',
2: 'Rain',
3: 'Overcast',
}
#========================================================================================================================
# predict function
#========================================================================================================================
def forecast(time):
time = int(time)
if time>9 or time<0:
return "please enter valid time", "0 to 9", ""
response = fetch_data(9-time)
if response is not None:
# processing data to better fit the model
df = pd.read_csv(io.StringIO(response.content.decode('utf-8')))
df = process_data(df)
X = np.array(df[-24:]).reshape(-1, 24, 17)
X = standardizeX(X, mean, std)
# predictions
temp = temp_model.predict(X)
hum = hum_model.predict(X)
ovrvw = overview_model.predict(X)
# reverse scaling
temp = (temp[0][0]*temp_std) + temp_mean
hum = (hum[0][0]*hum_std) + hum_mean
ovrvw = encoder.inverse_transform(ovrvw)
# output formatting
temp = str(round(((temp-32)*5)/9, 2)) + " c"
hum = str(round(hum, 2)) + " %"
ovrvw = overview[ovrvw[0][0]]
return temp, hum, ovrvw
else:
return "API access denied", "", ""
gr.Interface(fn=forecast,
inputs = gr.Textbox(placeholder="Provide value between 0 to 9, 0 means present, 9 means 9 hrs in future "),
outputs = [gr.Textbox(label="Temperature"),gr.Textbox(label="Humidity"),gr.Textbox(label="Overview")]
).launch()