elisaklunder commited on
Commit
b11254e
1 Parent(s): f4930a4

data finally working

Browse files
Files changed (3) hide show
  1. app.py +1 -0
  2. predictions_history.csv +7 -0
  3. src/predict.py +30 -20
app.py CHANGED
@@ -15,6 +15,7 @@ st.set_page_config(
15
 
16
  alt.themes.enable("dark")
17
 
 
18
  week_data, predictions_O3, predictions_NO2 = get_data_and_predictions()
19
  today = week_data.iloc[-1]
20
  previous_day = week_data.iloc[-2]
 
15
 
16
  alt.themes.enable("dark")
17
 
18
+ model_NO2, model_O3 = load_models()
19
  week_data, predictions_O3, predictions_NO2 = get_data_and_predictions()
20
  today = week_data.iloc[-1]
21
  previous_day = week_data.iloc[-2]
predictions_history.csv ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ pollutant,date_predicted,date,prediction_value
2
+ O3,2024-10-24,2024-10-25,10.33808859423279
3
+ NO2,2024-10-24,2024-10-25,25.68519991558237
4
+ O3,2024-10-24,2024-10-26,16.000984317626852
5
+ NO2,2024-10-24,2024-10-26,25.760307451092384
6
+ O3,2024-10-24,2024-10-27,19.64377495640328
7
+ NO2,2024-10-24,2024-10-27,31.210576791105115
src/predict.py CHANGED
@@ -3,43 +3,53 @@ from datetime import date, timedelta
3
 
4
  import joblib
5
  import pandas as pd
 
6
  from dotenv import load_dotenv
7
  from huggingface_hub import hf_hub_download, login
 
8
  from src.data_api_calls import get_combined_data
9
  from src.features_pipeline import create_features
10
 
 
 
11
 
12
- def load_model(particle):
13
- load_dotenv()
14
  login(token=os.getenv("HUGGINGFACE_DOWNLOAD_TOKEN"))
15
 
16
- repo_id = f"elisaklunder/Utrecht-{particle}-Forecasting-Model"
17
- if particle == "O3":
18
- file_name = "O3_svr_model.pkl"
19
- elif particle == "NO2":
20
- file_name = "NO2_svr_model.pkl"
 
 
 
 
 
 
21
 
22
- model_path = hf_hub_download(repo_id=repo_id, filename=file_name)
23
- model = joblib.load(model_path)
24
- return model
25
 
 
 
 
 
 
26
 
27
- def run_model(particle, data):
28
- input_data = create_features(data=data, target_particle=particle)
29
- model = load_model(particle)
30
- prediction = model.predict(input_data)
31
- target_scaler = joblib.load(f"scalers/target_scaler_{particle}.joblib")
32
- prediction = target_scaler.inverse_transform(prediction)
33
- return prediction
34
 
 
35
 
36
- def get_data_and_predictions():
 
37
  PREDICTIONS_FILE = "predictions_history.csv"
38
 
39
  week_data = get_combined_data()
40
 
41
- o3_predictions = run_model("O3", data=week_data)
42
- no2_predictions = run_model("NO2", data=week_data)
43
 
44
  prediction_data = []
45
  for i in range(3):
 
3
 
4
  import joblib
5
  import pandas as pd
6
+ import streamlit as st
7
  from dotenv import load_dotenv
8
  from huggingface_hub import hf_hub_download, login
9
+
10
  from src.data_api_calls import get_combined_data
11
  from src.features_pipeline import create_features
12
 
13
+ load_dotenv()
14
+
15
 
16
+ st.cache_resource()
17
+ def load_models():
18
  login(token=os.getenv("HUGGINGFACE_DOWNLOAD_TOKEN"))
19
 
20
+ model_path = hf_hub_download(
21
+ repo_id="elisaklunder/Utrecht-O3-Forecasting-Model", filename="O3_svr_model.pkl"
22
+ )
23
+ model_NO2 = joblib.load(model_path)
24
+
25
+ model_path = hf_hub_download(
26
+ repo_id="elisaklunder/Utrecht-NO2-Forecasting-Model",
27
+ filename="NO2_svr_model.pkl",
28
+ )
29
+ model_O3 = joblib.load(model_path)
30
+ return model_NO2, model_O3
31
 
 
 
 
32
 
33
+ def run_models(model_NO2, model_O3, data):
34
+ input_data = create_features(data=data, target_particle="NO2")
35
+ prediction = model_NO2.predict(input_data)
36
+ target_scaler = joblib.load("scalers/target_scaler_NO2.joblib")
37
+ prediction_NO2 = target_scaler.inverse_transform(prediction)
38
 
39
+ input_data = create_features(data=data, target_particle="O3")
40
+ prediction = model_O3.predict(input_data)
41
+ target_scaler = joblib.load("scalers/target_scaler_O3.joblib")
42
+ prediction_O3 = target_scaler.inverse_transform(prediction)
 
 
 
43
 
44
+ return prediction_NO2, prediction_O3
45
 
46
+ st.cache_resource(ttl=300 * 24)
47
+ def get_data_and_predictions(model_NO2, model_O3):
48
  PREDICTIONS_FILE = "predictions_history.csv"
49
 
50
  week_data = get_combined_data()
51
 
52
+ o3_predictions, no2_predictions = run_models(model_NO2, model_O3, week_data)
 
53
 
54
  prediction_data = []
55
  for i in range(3):