File size: 3,012 Bytes
4391e28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()