File size: 2,520 Bytes
a3d05c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from data_processing import process_data, standardizeX
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')


#========================================================================================================================
#                                          retrieve mean and std
#========================================================================================================================
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]


#========================================================================================================================
#                                          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, 16)
        X = standardizeX(X, mean, std)

        # predictions
        temp = temp_model.predict(X)
        hum = hum_model.predict(X)

        # reverse scaling
        temp = (temp[0][0]*temp_std) + temp_mean
        hum = (hum[0][0]*hum_std) + hum_mean

        # output formatting
        temp = str(round(((temp-32)*5)/9, 2)) + " c"
        hum = str(round(hum, 2)) + " %"

        return temp, hum
    
    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")]
            ).launch()