Spaces:
Runtime error
Runtime error
Campfireman
commited on
Commit
•
59fcb36
1
Parent(s):
82326ea
Update app.py
Browse files
app.py
CHANGED
@@ -4,162 +4,139 @@ import joblib
|
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
import folium
|
|
|
7 |
import json
|
8 |
import time
|
9 |
from datetime import timedelta, datetime
|
10 |
from branca.element import Figure
|
11 |
|
12 |
-
from functions import decode_features
|
13 |
#import functions
|
14 |
|
15 |
def greet(total_pred_days):
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
project = hopsworks.login()
|
20 |
-
|
21 |
-
#
|
22 |
-
#
|
23 |
-
#
|
24 |
-
# # The latest available data timestamp
|
25 |
-
start_time = 20221201
|
26 |
-
#
|
27 |
-
# # end_time = 1670972400000
|
28 |
-
#
|
29 |
-
# #start_date = datetime.now() - timedelta(days=1)
|
30 |
-
# #start_time = int(start_date.timestamp()) * 1000
|
31 |
-
#
|
32 |
-
# print("Time Stamp Set. ")
|
33 |
-
#
|
34 |
-
#
|
35 |
-
#
|
36 |
-
# print("latest_date")
|
37 |
#
|
38 |
-
# mr=project.get_model_registry()
|
39 |
-
# model = mr.get_model("temp_model", version=2)
|
40 |
-
# model_dir=model.download()
|
41 |
-
#
|
42 |
-
# model1 = mr.get_model("tempmax_model", version=2)
|
43 |
-
# model_dir1=model1.download()
|
44 |
-
#
|
45 |
-
# model2 = mr.get_model("tempmin_model", version=2)
|
46 |
-
# model_dir2=model2.download()
|
47 |
-
#
|
48 |
-
# model = joblib.load(model_dir + "/model_temp.pkl")
|
49 |
-
# model1 = joblib.load(model_dir1 + "/model_tempmax.pkl")
|
50 |
-
# model2 = joblib.load(model_dir2+ "/model_tempmin.pkl")
|
51 |
-
#
|
52 |
-
# print("temp_model is now right")
|
53 |
-
#
|
54 |
-
fs = project.get_feature_store()
|
55 |
-
print("get the store")
|
56 |
-
feature_view = fs.get_feature_view(
|
57 |
-
name = 'weather_fv',
|
58 |
-
version = 1
|
59 |
-
)
|
60 |
-
print("get the fv")
|
61 |
-
X = feature_view.get_batch_data(start_time=start_time)
|
62 |
-
print(X)
|
63 |
-
# print("Data batched")
|
64 |
-
# latest_date_unix = str(X.datetime.values[0])[:10]
|
65 |
-
# latest_date = time.ctime(int(latest_date_unix))
|
66 |
-
# X = X.drop(columns=["datetime"]).fillna(0)
|
67 |
-
# preds = model.predict(X)
|
68 |
-
# preds1= model1.predict(X)
|
69 |
-
# preds2= model2.predict(X)
|
70 |
-
#
|
71 |
-
#
|
72 |
-
#
|
73 |
-
# # cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
|
74 |
-
#
|
75 |
-
# next_day_date = datetime.today() + timedelta(days=1)
|
76 |
-
# next_day = next_day_date.strftime ('%d/%m/%Y')
|
77 |
-
# str1 = ""
|
78 |
-
#
|
79 |
-
# if(total_pred_days == ""):
|
80 |
-
# return "Empty input"
|
81 |
-
#
|
82 |
-
# count = int(total_pred_days)
|
83 |
-
# if count > 20:
|
84 |
-
# str1 += "Warning: 20 days at most. " + '\n'
|
85 |
-
# count = 20
|
86 |
-
# if count <0:
|
87 |
-
# str1 = "Invalid input."
|
88 |
-
# return str1
|
89 |
-
#
|
90 |
-
# for x in range(count):
|
91 |
-
# if (x != 0):
|
92 |
-
# str1 += (datetime.now() + timedelta(days=x)).strftime('%Y-%m-%d') + " predicted temperature: " +str(int(preds[len(preds) - count + x]))+ " predicted max temperature: " +str(int(preds1[len(preds1) - count + x]))+ " predicted min temperature: " +str(int(preds2[len(preds2) - count + x]))+"\n"
|
93 |
-
#
|
94 |
-
# #print(str1)
|
95 |
-
return str1
|
96 |
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
if __name__ == "__main__":
|
103 |
-
demo.launch()
|
104 |
-
|
105 |
-
'''
|
106 |
-
def greet(total_pred_days):
|
107 |
-
|
108 |
-
project = hopsworks.login()
|
109 |
-
#api = project.get_dataset_api()
|
110 |
-
fs = project.get_feature_store()
|
111 |
-
feature_view = fs.get_feature_view(
|
112 |
-
name = 'weather_fv',
|
113 |
-
version = 1
|
114 |
-
)
|
115 |
-
|
116 |
-
# The latest available data timestamp
|
117 |
-
start_time = 1635112800000
|
118 |
|
119 |
#start_date = datetime.now() - timedelta(days=1)
|
120 |
#start_time = int(start_date.timestamp()) * 1000
|
121 |
|
|
|
122 |
|
123 |
-
|
124 |
-
latest_date_unix = str(X.datetime.values[0])[:10]
|
125 |
-
latest_date = time.ctime(int(latest_date_unix))
|
126 |
-
model = get_model(project=project,
|
127 |
-
model_name="temp_model",
|
128 |
-
evaluation_metric="f1_score",
|
129 |
-
sort_metrics_by="max")
|
130 |
-
model1 = get_model1(project=project,
|
131 |
-
model_name="tempmax_model",
|
132 |
-
evaluation_metric="f1_score",
|
133 |
-
sort_metrics_by="max")
|
134 |
-
model2 = get_model2(project=project,
|
135 |
-
model_name="tempmin_model",
|
136 |
-
evaluation_metric="f1_score",
|
137 |
-
sort_metrics_by="max")
|
138 |
-
X = X.drop(columns=["datetime"]).fillna(0)
|
139 |
-
|
140 |
-
preds = model.predict(X)
|
141 |
|
142 |
-
|
|
|
|
|
143 |
|
144 |
-
|
|
|
|
|
|
|
|
|
145 |
|
|
|
|
|
|
|
146 |
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
next_day = next_day_date.strftime ('%d/%m/%Y')
|
151 |
str1 = ""
|
152 |
|
153 |
if(total_pred_days == ""):
|
154 |
return "Empty input"
|
155 |
|
156 |
count = int(total_pred_days)
|
157 |
-
if count >
|
158 |
-
str1 += "Warning:
|
159 |
-
count =
|
160 |
if count <0:
|
161 |
str1 = "Invalid input."
|
162 |
return str1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
for x in range(count):
|
165 |
if (x != 0):
|
@@ -168,16 +145,9 @@ def greet(total_pred_days):
|
|
168 |
#print(str1)
|
169 |
return str1
|
170 |
|
171 |
-
|
172 |
-
demo = gr.Interface(
|
173 |
-
fn=greet,
|
174 |
-
inputs=gr.Slider(label="Days of prediction (start from tomorrow)", value=1, minimum=1, maximum=20, step=1),
|
175 |
-
outputs=gr.outputs.Textbox(label="Prediction results"),
|
176 |
-
)
|
177 |
|
178 |
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
demo.launch()
|
182 |
-
|
183 |
-
'''
|
|
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
import folium
|
7 |
+
import sklearn.preprocessing as proc
|
8 |
import json
|
9 |
import time
|
10 |
from datetime import timedelta, datetime
|
11 |
from branca.element import Figure
|
12 |
|
13 |
+
#from functions import decode_features, get_weather_data, get_weather_df, get_weather_json_quick
|
14 |
#import functions
|
15 |
|
16 |
def greet(total_pred_days):
|
17 |
+
|
18 |
|
19 |
+
# print("hi")
|
20 |
project = hopsworks.login()
|
21 |
+
# print("connected")
|
22 |
+
# #api = project.get_dataset_api()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# # The latest available data timestamp
|
26 |
+
# start_time = 1649196000000
|
27 |
|
28 |
+
# # end_time = 1670972400000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
#start_date = datetime.now() - timedelta(days=1)
|
31 |
#start_time = int(start_date.timestamp()) * 1000
|
32 |
|
33 |
+
#print("Time Stamp Set. ")
|
34 |
|
35 |
+
#print("latest_date")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
mr=project.get_model_registry()
|
38 |
+
model = mr.get_model("temp_model_new", version=1)
|
39 |
+
model_dir=model.download()
|
40 |
|
41 |
+
model1 = mr.get_model("tempmax_model_new", version=1)
|
42 |
+
model_dir1=model1.download()
|
43 |
+
|
44 |
+
model2 = mr.get_model("tempmin_model_new", version=1)
|
45 |
+
model_dir2=model2.download()
|
46 |
|
47 |
+
model = joblib.load(model_dir + "/model_temp_new.pkl")
|
48 |
+
model1 = joblib.load(model_dir1 + "/model_tempmax_new.pkl")
|
49 |
+
model2 = joblib.load(model_dir2+ "/model_tempmin_new.pkl")
|
50 |
|
51 |
+
print("temp_model is now right")
|
52 |
+
|
53 |
+
|
54 |
+
#X = feature_view.get_batch_data(start_time=start_time)
|
55 |
+
|
56 |
+
#latest_date_unix = str(X.datetime.values[0])[:10]
|
57 |
+
#latest_date = time.ctime(int(latest_date_unix))
|
58 |
+
# cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
|
59 |
|
60 |
+
|
|
|
61 |
str1 = ""
|
62 |
|
63 |
if(total_pred_days == ""):
|
64 |
return "Empty input"
|
65 |
|
66 |
count = int(total_pred_days)
|
67 |
+
if count > 14:
|
68 |
+
str1 += "Warning: 14 days at most. " + '\n'
|
69 |
+
count = 14
|
70 |
if count <0:
|
71 |
str1 = "Invalid input."
|
72 |
return str1
|
73 |
+
|
74 |
+
# Get weather data
|
75 |
+
fs = project.get_feature_store()
|
76 |
+
print("get the store")
|
77 |
+
feature_view = fs.get_feature_view(
|
78 |
+
name = 'weathernew_fv',
|
79 |
+
version = 1
|
80 |
+
)
|
81 |
+
print("get the fv")
|
82 |
+
|
83 |
+
|
84 |
+
global X
|
85 |
+
X = pd.DataFrame()
|
86 |
+
|
87 |
+
for i in range(count+1):
|
88 |
+
# Get, rename column and rescale
|
89 |
+
next_day_date = datetime.today() + timedelta(days=i)
|
90 |
+
next_day = next_day_date.strftime ('%Y-%m-%d')
|
91 |
+
print(next_day)
|
92 |
+
json = get_weather_json_quick(next_day)
|
93 |
+
temp = get_weather_data(json)
|
94 |
+
print("Raw data")
|
95 |
+
print(temp)
|
96 |
+
X = X.append(temp, ignore_index=True)
|
97 |
+
|
98 |
+
|
99 |
+
# X reshape
|
100 |
+
|
101 |
+
X.drop('preciptype', inplace = True, axis = 1)
|
102 |
+
X.drop('severerisk', inplace = True, axis = 1)
|
103 |
+
X.drop('stations', inplace = True, axis = 1)
|
104 |
+
X.drop('sunrise', inplace = True, axis = 1)
|
105 |
+
X.drop('sunset', inplace = True, axis = 1)
|
106 |
+
X.drop('moonphase', inplace = True, axis = 1)
|
107 |
+
X.drop('description', inplace = True, axis = 1)
|
108 |
+
X.drop('icon', inplace = True, axis = 1)
|
109 |
+
|
110 |
+
X = X.drop(columns=["datetime", "temp", "tempmax", "tempmin", "sunriseEpoch", "sunsetEpoch", "source", "datetimeEpoch", ]).fillna(0)
|
111 |
+
X = X.rename(columns={'pressure':'sealevelpressure'})
|
112 |
+
X = X.drop(columns = ['conditions'])
|
113 |
+
print("Check dataframe")
|
114 |
+
print(X)
|
115 |
+
print("Data batched.")
|
116 |
+
# Rescale
|
117 |
+
#X = decode_features(X, feature_view=feature_view)
|
118 |
+
# Data scaling
|
119 |
+
#category_cols = ['name','datetime','conditions', 'tempmin', 'tempmax', 'temp']
|
120 |
+
|
121 |
+
#mapping_transformers = {col_name:fs.get_transformation_function(name='standard_scaler') for col_name in col_names if col_name not in category_cols}
|
122 |
+
#category_cols = {col_name:fs.get_transformation_function(name='label_encoder') for col_name in category_cols if col_name not in ['datetime', 'tempmin', 'tempmax', 'temp']}
|
123 |
+
|
124 |
+
#mapping_transformers.update(category_cols)
|
125 |
+
|
126 |
+
|
127 |
+
# Data scaling
|
128 |
+
#category_cols = ['conditions']
|
129 |
+
cat_std_cols = ['feelslikemax','feelslikemin','feelslike','dew','humidity','precip','precipprob','precipcover','snow','snowdepth','windgust','windspeed','winddir','sealevelpressure','cloudcover','visibility','solarradiation','solarenergy','uvindex']
|
130 |
+
scaler_std = proc.StandardScaler()
|
131 |
+
#scaler_lb = proc.LabelEncoder()
|
132 |
+
X.insert(19,"conditions",0)
|
133 |
+
X[cat_std_cols] = scaler_std.fit_transform(X[cat_std_cols])
|
134 |
+
#X[category_cols] = scaler_std.transform(X[category_cols])
|
135 |
+
X.insert(0,"name",0)
|
136 |
+
# Predict
|
137 |
+
preds = model.predict(X)
|
138 |
+
preds1= model1.predict(X)
|
139 |
+
preds2= model2.predict(X)
|
140 |
|
141 |
for x in range(count):
|
142 |
if (x != 0):
|
|
|
145 |
#print(str1)
|
146 |
return str1
|
147 |
|
148 |
+
demo = gr.Interface(fn=greet, inputs = "text", outputs="text")
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
demo.launch()
|
|
|
|