Campfireman commited on
Commit
38d6ec5
1 Parent(s): f86b580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -60
app.py CHANGED
@@ -11,28 +11,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)
@@ -40,37 +22,37 @@ def greet(total_pred_days):
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")
@@ -88,11 +70,8 @@ def greet(total_pred_days):
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
 
@@ -106,43 +85,32 @@ def greet(total_pred_days):
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):
143
- 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"
144
 
145
- #print(str1)
146
  return str1
147
 
148
  demo = gr.Interface(fn=greet, inputs = "text", outputs="text")
 
11
  from branca.element import Figure
12
 
13
  from functions import decode_features, get_weather_data, get_weather_df, get_weather_json_quick
14
+ ##################
15
 
16
  def greet(total_pred_days):
 
 
 
17
  project = hopsworks.login()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  mr=project.get_model_registry()
20
  model = mr.get_model("temp_model_new", version=1)
 
22
 
23
  model1 = mr.get_model("tempmax_model_new", version=1)
24
  model_dir1=model1.download()
 
25
  model2 = mr.get_model("tempmin_model_new", version=1)
26
  model_dir2=model2.download()
27
 
28
  model = joblib.load(model_dir + "/model_temp_new.pkl")
29
  model1 = joblib.load(model_dir1 + "/model_tempmax_new.pkl")
30
+ model2 = joblib.load(model_dir2+ "/model_tempmin_new.pkl"
 
31
  print("temp_model is now right")
 
 
 
 
 
 
 
32
 
33
+ fs = project.get_feature_store()
34
+ weather_fg = fs.get_or_create_feature_group(
35
+ name = 'weather_fg',
36
+ version = 1
37
+ )
38
+
39
+ query = weather_fg.select_all()
40
+ Q = query.read()
41
 
42
  str1 = ""
43
 
44
  if(total_pred_days == ""):
45
  return "Empty input"
46
 
47
+ count = int(total_pred_days)
48
  if count > 14:
49
  str1 += "Warning: 14 days at most. " + '\n'
50
  count = 14
51
  if count <0:
52
  str1 = "Invalid input."
53
  return str1
54
+ count = count + 1
55
+
56
  # Get weather data
57
  fs = project.get_feature_store()
58
  print("get the store")
 
70
  # Get, rename column and rescale
71
  next_day_date = datetime.today() + timedelta(days=i)
72
  next_day = next_day_date.strftime ('%Y-%m-%d')
 
73
  json = get_weather_json_quick(next_day)
74
  temp = get_weather_data(json)
 
 
75
  X = X.append(temp, ignore_index=True)
76
 
77
 
 
85
  X.drop('moonphase', inplace = True, axis = 1)
86
  X.drop('description', inplace = True, axis = 1)
87
  X.drop('icon', inplace = True, axis = 1)
88
+ X = X.drop(columns=["sunriseEpoch", "sunsetEpoch", "source", "datetimeEpoch"]).fillna(0)
 
89
  X = X.rename(columns={'pressure':'sealevelpressure'})
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ # Merge X and query
92
+ Y = X.append(Q, ignore_index=True)
93
 
94
  # Data scaling
95
+ Y = Y.drop(columns = ['conditions', 'name', "datetime", "temp", "tempmax", "tempmin"])
96
+ category_cols = ['conditions']
97
  cat_std_cols = ['feelslikemax','feelslikemin','feelslike','dew','humidity','precip','precipprob','precipcover','snow','snowdepth','windgust','windspeed','winddir','sealevelpressure','cloudcover','visibility','solarradiation','solarenergy','uvindex']
98
  scaler_std = proc.StandardScaler()
99
+ Y.insert(19,"conditions",0)
100
+ Y.insert(0,"name",0)
101
+ print(Y)
102
+ Y[cat_std_cols] = scaler_std.fit_transform(Y[cat_std_cols])
103
+ Y[category_cols] = scaler_std.fit_transform(Y[category_cols])
104
+
105
  # Predict
106
+ preds = model.predict(Y[0:count])
107
+ preds1= model1.predict(Y[0:count])
108
+ preds2= model2.predict(Y[0:count])
109
 
110
  for x in range(count):
111
  if (x != 0):
112
+ 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"
113
 
 
114
  return str1
115
 
116
  demo = gr.Interface(fn=greet, inputs = "text", outputs="text")