File size: 4,170 Bytes
8062140
 
 
 
 
 
59fcb36
8062140
 
 
 
 
f86b580
38d6ec5
d47d510
9d7e90c
8062140
 
ac158aa
 
 
38d6ec5
59fcb36
 
 
ac158aa
 
 
38d6ec5
 
59fcb36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38d6ec5
59fcb36
 
38d6ec5
1f86947
59fcb36
 
4a8dd38
38d6ec5
59fcb36
 
1f86947
 
82d1a2a
1f86947
 
38d6ec5
59fcb36
1f86947
 
 
ac158aa
 
 
38d6ec5
ac158aa
8062140
 
9d7e90c
 
 
 
 
1f86947
 
 
 
 
 
 
 
ada5e88
9d7e90c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a612c2d
8062140
a612c2d
 
 
 
 
 
 
 
 
 
 
62cfbd8
8062140
 
d47d510
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
import gradio as gr
import hopsworks
import joblib
import pandas as pd
import numpy as np
import folium
import sklearn.preprocessing as proc
import json
import time
from datetime import timedelta, datetime
from branca.element import Figure

from functions import decode_features, get_weather_data, get_weather_df, get_weather_json_quick
##################

def greet(total_pred_days):    
    str1 = ""
    
    if(total_pred_days == ""):
        return "Empty input"
    
    count = int(total_pred_days) 
    if count > 14:
        str1 += "Warning: 14 days at most. " + '\n'
        count = 14
    if count <0:
        str1 = "Invalid input."
        return str1
    count = count + 1
    
    X = pd.DataFrame()
    
    for i in range(count+1):
        # Get, rename column and rescale
        next_day_date = datetime.today() + timedelta(days=i)
        next_day = next_day_date.strftime ('%Y-%m-%d')
        json = get_weather_json_quick(next_day)
        temp = get_weather_data(json)
        X = X.append(temp, ignore_index=True)
    
    
    # X reshape
    
    X.drop('preciptype', inplace = True, axis = 1)
    X.drop('severerisk', inplace = True, axis = 1)
    X.drop('stations', inplace = True, axis = 1)
    X.drop('sunrise', inplace = True, axis = 1)
    X.drop('sunset', inplace = True, axis = 1)
    X.drop('moonphase', inplace = True, axis = 1)
    X.drop('description', inplace = True, axis = 1)
    X.drop('icon', inplace = True, axis = 1)
    X = X.drop(columns=["sunriseEpoch", "sunsetEpoch", "source", "datetimeEpoch"]).fillna(0) 
    X = X.rename(columns={'pressure':'sealevelpressure'})
    
    # Merge X and query
    #Y = X.append(Q, ignore_index=True)
    
    # Data scaling
    X = X.drop(columns = ['conditions', "datetime", "temp", "tempmax", "tempmin"])
    category_cols = ['conditions']
    cat_std_cols = ['feelslikemax','feelslikemin','feelslike','dew','humidity','precip','precipprob','precipcover','snow','snowdepth','windgust','windspeed','winddir','sealevelpressure','cloudcover','visibility','solarradiation','solarenergy','uvindex']
    scaler_std = proc.StandardScaler()
    X.insert(19,"conditions",0)
    X.insert(0,"name",0)
    
    X[cat_std_cols] = scaler_std.fit_transform(X[cat_std_cols])
    X[category_cols] = scaler_std.fit_transform(X[category_cols])
    
    # Predict
    preds = model.predict(X[0:count])
    preds1= model1.predict(X[0:count])
    preds2= model2.predict(X[0:count])
    
    for x in range(count):
        if (x != 0):
            str1 += (datetime.now() + timedelta(days=x)).strftime('%Y-%m-%d') + " predicted temperature: " +str(float(preds[len(preds) - count + x]))+ "\npredicted max temperature: " +str(float(preds1[len(preds1) - count + x]))+ "\npredicted min temperature: " +str(float(preds2[len(preds2) - count + x]))+"\n"
    
    return str1

#######################################################
# Preparations
project = hopsworks.login()
mr=project.get_model_registry()

# fs = project.get_feature_store()
# weather_fg = fs.get_or_create_feature_group(
#     name = 'weather_fg',
#     version = 1
# )
# 
# query = weather_fg.select_all()
# Q = query.read()

model = mr.get_model("temp_model_new", version=1)
model_dir=model.download()

model1 = mr.get_model("tempmax_model_new", version=1) 
model_dir1=model1.download()

model2 = mr.get_model("tempmin_model_new", version=1)
model_dir2=model2.download()

model = joblib.load(model_dir + "/model_temp_new.pkl")
model1 = joblib.load(model_dir1 + "/model_tempmax_new.pkl")
model2 = joblib.load(model_dir2+ "/model_tempmin_new.pkl")


########################################################
# Gradio Interface
#demo = gr.Interface(fn=greet, inputs = "text", outputs="text")

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            days = gr.Slider(
                label="How many days do you want to predict the temperature of? ", value=1, minimum=1, maximum=15, step=1
            )
        with gr.Column():
            output = gr.Textbox(
                label="Predicted results: "
            )
        days.change(greet, days, output)
        
    
if __name__ == "__main__":
    demo.launch()