rafat0421 commited on
Commit
943ef8b
1 Parent(s): ea1cf0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -46
app.py CHANGED
@@ -2,72 +2,90 @@ import streamlit as st
2
  import hopsworks
3
  import joblib
4
  import pandas as pd
5
- import datetime
6
  from functions import *
7
- import pytz
8
 
9
- st.set_page_config(layout="wide")
10
-
11
- st.title('AQI prediction for Beijing in next week')
12
-
13
- project = hopsworks.login()
14
 
15
- #today=datetime.datetime.now(pytz.timezone('Asia/Shanghai')).date()
16
- #today=datetime.date.today()
 
17
 
18
- #weekly_data = get_weather_data_weekly(today)
19
-
20
- # get Hopsworks Model Registry
21
- #mr = project.get_model_registry()
22
- # get model object
23
- #model = mr.get_model("gradient_boost_model", version=1)
24
- #model_dir = model.download()
25
- #model = joblib.load(model_dir + "/model.pkl")
26
 
27
- #weekly_data = data_encoder(weekly_data)
28
 
29
- #start test
 
30
 
31
- fs = project.get_feature_store()
32
- feature_view = fs.get_feature_view(
33
- name = 'hel_air_fv1',
34
- version = 1
35
- )
36
 
37
- #start_date = datetime.now() - timedelta(days=1)
38
- #start_time = int(start_date.timestamp()) * 1000
39
 
40
- #end_date = datetime.now() - timedelta(days=1)
41
- #end_time = int(start_date.timestamp()) * 1000
42
 
43
- #print("start time:", start_time)
 
44
 
45
- X = feature_view.get_batch_data(start_time=1670194800000)
46
 
47
- print("batch data")
48
- print(X.tail(10))
49
 
50
- X = X.drop(columns=["date"]).fillna(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- data_to_display = decode_features(X, feature_view=feature_view)
53
 
54
- preds=model.predict(X)
55
 
56
- #preds=model.predict(weekly_data)
 
57
 
58
  next_week = [f"{(today + timedelta(days=d)).strftime('%Y-%m-%d')},{(today + timedelta(days=d)).strftime('%A')}" for d in range(7)]
59
 
60
- print("preds")
61
- print(preds)
62
-
63
- aqi_level = encoder_range(preds.T.reshape(-1, 1))
64
- #df = pd.DataFrame(data=[map(int,preds), aqi_level], index=["aqi","Air Pollution Level"], columns=next_week)
65
 
66
- print("aqi")
67
- print(aqi_level)
68
 
69
- df = pd.DataFrame(data=[map(int,preds), aqi_level], index=["aqi","Air Pollution Level"],columns=next_week)
70
 
71
- st.write(df)
72
 
73
- st.button("Re-run")
 
2
  import hopsworks
3
  import joblib
4
  import pandas as pd
5
+ from datetime import timedelta, datetime
6
  from functions import *
 
7
 
 
 
 
 
 
8
 
9
+ def fancy_header(text, font_size=24):
10
+ res = f'<p style="color:#ff5f72; font-size: {font_size}px; text-align:center;">{text}</p>'
11
+ st.markdown(res, unsafe_allow_html=True)
12
 
13
+ st.set_page_config(layout="wide")
 
 
 
 
 
 
 
14
 
15
+ st.title('Air Quality Prediction Project🌩')
16
 
17
+ st.write(9 * "-")
18
+ fancy_header('\n Connecting to Hopsworks Feature Store...')
19
 
20
+ project = hopsworks.login()
 
 
 
 
21
 
22
+ st.write("Successfully connected!✔️")
 
23
 
24
+ st.write(18 * "-")
25
+ fancy_header('\n Getting data from Feature Store...')
26
 
27
+ today = datetime.date.today()
28
+ weekly_data = get_weather_data_weekly(today)
29
 
 
30
 
31
+ st.write(27 * "-")
 
32
 
33
+ mr = project.get_model_registry()
34
+ # get Hopsworks Model Registry
35
+ mr = project.get_model_registry()
36
+ # get model object
37
+ model = mr.get_model("aqi_model_gb", version=1)
38
+ model_dir = model.download()
39
+ model = joblib.load(model_dir + "/gb_model.pkl")
40
+
41
+ weekly_data['aqi'] = 0
42
+ weekly_data['city'] = 0
43
+
44
+ weekly_data = data_encoder(weekly_data)
45
+
46
+ weekly_data.drop(['tempmax'], inplace = True, axis = 1)
47
+ weekly_data.drop('tempmin', inplace = True, axis = 1)
48
+ weekly_data.drop('feelslikemax', inplace = True, axis = 1)
49
+ weekly_data.drop('feelslikemin', inplace = True, axis = 1)
50
+ weekly_data.drop('feelslike', inplace = True, axis = 1)
51
+ weekly_data.drop('dew', inplace = True, axis = 1)
52
+ weekly_data.drop('precipprob', inplace = True, axis = 1)
53
+ weekly_data.drop('precipcover', inplace = True, axis = 1)
54
+ weekly_data.drop('snow', inplace = True, axis = 1)
55
+ weekly_data.drop('snowdepth', inplace = True, axis = 1)
56
+ weekly_data.drop('windgust', inplace = True, axis = 1)
57
+ weekly_data.drop('windspeed', inplace = True, axis = 1)
58
+ weekly_data.drop('winddir', inplace = True, axis = 1)
59
+ weekly_data.drop('solarradiation', inplace = True, axis = 1)
60
+ weekly_data.drop('solarenergy', inplace = True, axis = 1)
61
+ weekly_data.drop('pressure', inplace = True, axis = 1)
62
+
63
+ print(weekly_data)
64
+
65
+ preds=model.predict(weekly_data)
66
+ #model = get_model(project=project,
67
+ # model_name="gradient_boost_model",
68
+ # evaluation_metric="f1_score",
69
+ # sort_metrics_by="max")
70
+ #print("here")
71
+ #print(model)
72
+ #model_dir = model.download()
73
+ #model = joblib.load(model_dir + "/model.pkl")
74
 
75
+ st.write("-" * 36)
76
 
 
77
 
78
+ #preds = model.predict(data_encoder(weekly_data)).astype(int)
79
+ poll_level = get_aplevel(preds.T.reshape(-1, 1))
80
 
81
  next_week = [f"{(today + timedelta(days=d)).strftime('%Y-%m-%d')},{(today + timedelta(days=d)).strftime('%A')}" for d in range(7)]
82
 
83
+ print(next_week)
 
 
 
 
84
 
85
+ df = pd.DataFrame(data=[preds, poll_level], index=["AQI", "Air pollution level"], columns=next_week)
 
86
 
87
+ st.write(df)
88
 
89
+ print(st)
90
 
91
+ #st.button("Re-run")