senyakk commited on
Commit
0a8e45b
·
1 Parent(s): 3fb84f3

predictions

Browse files
Files changed (1) hide show
  1. app.py +34 -30
app.py CHANGED
@@ -15,11 +15,14 @@ from pr_featureEng import FeatureEngineeringPredict
15
  from forecast_dataset import preprocess_function
16
  from forecast_api import download_data
17
 
 
 
 
 
 
 
 
18
 
19
- # Streamlit App Interface
20
- st.title("O₃ and NO₂ forecast")
21
-
22
- metric = st.selectbox("Select the metric for prediction", ["O₃", "NO₂"])
23
 
24
  fitted_pcaO3 = pk.load(open("pcaO3.pkl",'rb'))
25
  fitted_pcaNO2 = pk.load(open("pcaNO2.pkl",'rb'))
@@ -30,18 +33,9 @@ fitted_scalerNO2_features = pk.load(open("scalerNO2_features.pkl",'rb'))
30
  fitted_scalerO3_targets = pk.load(open("scalerO3_targets.pkl",'rb'))
31
  fitted_scalerNO2_targets = pk.load(open("scalerNO2_targets.pkl",'rb'))
32
 
33
- hf_map = {
34
- "O₃": "mlpO3.keras",
35
- "NO₂": "mlpNO2.keras"
36
- }
37
 
38
- # Model is cached to prevent reloading
39
- @st.cache_resource()
40
- def load_model(model_name):
41
-
42
- model_dir = hf_hub_download(repo_id="senyakk/pollution_MLP", filename=model_name)
43
-
44
- return tf.keras.models.load_model(model_dir)
45
 
46
  def input_processing(data):
47
 
@@ -61,6 +55,7 @@ def input_processing(data):
61
 
62
  return X_input
63
 
 
64
  # Function to automatically refresh weather data and predictions every 6 hours
65
  @st.cache_data(ttl=6*3600) # Cache data for 6 hours
66
  def auto_update_data_and_prediction():
@@ -68,28 +63,31 @@ def auto_update_data_and_prediction():
68
  data = download_data()
69
  data = preprocess_function(data)
70
 
71
-
72
  input_data = input_processing(data)
73
 
74
-
75
- model = load_model(hf_map[metric])
76
-
77
  # Make prediction
78
- prediction = model.predict(input_data)
79
- prediction_flat = prediction.flatten() # Flatten to 1D array
 
 
 
 
 
 
80
 
81
- # Round predictions
82
- rounded_predictions = [round(pred, 2) for pred in prediction_flat]
83
 
84
  # Create DataFrame for predictions
85
- hours = np.arange(1, len(prediction_flat) + 1)
86
- prediction_df = pd.DataFrame({f"{metric} Prediction": rounded_predictions}, index=hours)
87
 
88
  # Save data and predictions
89
- prediction_df.to_csv(f"data/prediction_{metric}_{datetime.now().strftime('%Y-%m-%d_%H-%M')}.csv", index=False)
90
 
91
  return data, prediction_df
92
 
 
 
 
93
  st.markdown("Below is the preprocessed data:")
94
  data_placeholder = st.empty()
95
 
@@ -105,12 +103,18 @@ data_placeholder.write(hourly_dataframe)
105
  # Plot predictions as a graph
106
  st.write("Predicted values over time:")
107
  fig, ax = plt.subplots()
108
- ax.plot(prediction_df.index, prediction_df[f"{metric} Prediction"], marker='o', linestyle='-', color='b')
109
  ax.set_xlabel('Hours')
110
- ax.set_ylabel(f'{metric} Prediction')
111
- ax.set_title(f'{metric} Prediction over Time')
 
112
 
113
- # Display prediction graph
 
 
 
 
 
114
  prediction_graph_placeholder.pyplot(fig)
115
 
116
  # Option to display the table
 
15
  from forecast_dataset import preprocess_function
16
  from forecast_api import download_data
17
 
18
+ # Model is cached to prevent reloading
19
+ @st.cache_resource()
20
+ def load_model(model_name):
21
+
22
+ model_dir = hf_hub_download(repo_id="senyakk/pollution_MLP", filename=model_name)
23
+
24
+ return tf.keras.models.load_model(model_dir)
25
 
 
 
 
 
26
 
27
  fitted_pcaO3 = pk.load(open("pcaO3.pkl",'rb'))
28
  fitted_pcaNO2 = pk.load(open("pcaNO2.pkl",'rb'))
 
33
  fitted_scalerO3_targets = pk.load(open("scalerO3_targets.pkl",'rb'))
34
  fitted_scalerNO2_targets = pk.load(open("scalerNO2_targets.pkl",'rb'))
35
 
36
+ modelNO2 = load_model("mlpNO2.keras")
37
+ modelO3 = load_model("mlpO3.keras")
 
 
38
 
 
 
 
 
 
 
 
39
 
40
  def input_processing(data):
41
 
 
55
 
56
  return X_input
57
 
58
+
59
  # Function to automatically refresh weather data and predictions every 6 hours
60
  @st.cache_data(ttl=6*3600) # Cache data for 6 hours
61
  def auto_update_data_and_prediction():
 
63
  data = download_data()
64
  data = preprocess_function(data)
65
 
 
66
  input_data = input_processing(data)
67
 
 
 
 
68
  # Make prediction
69
+ predictionO3 = modelO3.predict(input_data)
70
+ predictionO3_flat = predictionO3.flatten() # Flatten to 1D array
71
+ rounded_predictionsO3 = [round(pred, 2) for pred in predictionO3_flat]
72
+
73
+
74
+ predictionNO2 = modelNO2.predict(input_data)
75
+ predictionNO2_flat = predictionNO2.flatten() # Flatten to 1D array
76
+ rounded_predictionsNO2 = [round(pred, 2) for pred in predictionNO2_flat]
77
 
 
 
78
 
79
  # Create DataFrame for predictions
80
+ hours = np.arange(1, len(predictionO3_flat) + 1)
81
+ prediction_df = pd.DataFrame({f"O3 Prediction": rounded_predictionsO3, "NO2 Prediction": rounded_predictionsNO2}, index=hours)
82
 
83
  # Save data and predictions
84
+ prediction_df.to_csv(f"data/prediction_{datetime.now().strftime('%Y-%m-%d_%H-%M')}.csv", index=False)
85
 
86
  return data, prediction_df
87
 
88
+ # Streamlit App Interface
89
+ st.title("O₃ and NO₂ forecast")
90
+
91
  st.markdown("Below is the preprocessed data:")
92
  data_placeholder = st.empty()
93
 
 
103
  # Plot predictions as a graph
104
  st.write("Predicted values over time:")
105
  fig, ax = plt.subplots()
106
+ ax.plot(prediction_df.index, prediction_df[f"O3 Prediction"], marker='o', linestyle='-', color='b')
107
  ax.set_xlabel('Hours')
108
+ ax.set_ylabel(f'O3 Prediction')
109
+ ax.set_title(f'O3 Prediction over Time')
110
+ prediction_graph_placeholder.pyplot(fig)
111
 
112
+
113
+ fig, ax = plt.subplots()
114
+ ax.plot(prediction_df.index, prediction_df[f"NO2 Prediction"], marker='o', linestyle='-', color='b')
115
+ ax.set_xlabel('Hours')
116
+ ax.set_ylabel(f'NO2 Prediction')
117
+ ax.set_title(f'NO2 Prediction over Time')
118
  prediction_graph_placeholder.pyplot(fig)
119
 
120
  # Option to display the table