Vasi001 commited on
Commit
83323ef
·
1 Parent(s): c7d45a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py CHANGED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hopsworks
3
+ import joblib
4
+ import pandas as pd
5
+ import numpy as np
6
+ import folium
7
+ from streamlit_folium import st_folium, folium_static
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_model
14
+
15
+
16
+ def fancy_header(text, font_size=24):
17
+ res = f'<span style="color:#ff5f27; font-size: {font_size}px;">{text}</span>'
18
+ st.markdown(res, unsafe_allow_html=True )
19
+
20
+ st.markdown(
21
+ f'''
22
+ <style>
23
+ .sidebar .sidebar-content {{
24
+ width: 1000px;
25
+ }}
26
+ </style>
27
+ ''',
28
+ unsafe_allow_html=True
29
+ )
30
+ st.title('⛅️Air Quality Prediction Project🌩')
31
+
32
+ progress_bar = st.sidebar.header('⚙️ Working Progress')
33
+ progress_bar = st.sidebar.progress(0)
34
+ st.write(36 * "-")
35
+ fancy_header('\n📡 Connecting to Hopsworks Feature Store...')
36
+
37
+ project = hopsworks.login()
38
+ fs = project.get_feature_store()
39
+ feature_view = fs.get_feature_view(
40
+ name = 'air_quality_fv',
41
+ version = 1
42
+ )
43
+
44
+ st.write("Successfully connected!✔️")
45
+ progress_bar.progress(20)
46
+
47
+ st.write(36 * "-")
48
+ fancy_header('\n☁️ Getting batch data from Feature Store...')
49
+ feature_view.init_batch_scoring(training_dataset_version=4)
50
+
51
+ start_date = datetime.now() - timedelta(days=1)
52
+ start_time = int(start_date.timestamp()) * 1000
53
+
54
+ X = feature_view.get_batch_data(start_time=start_time)
55
+ progress_bar.progress(50)
56
+
57
+ latest_date_unix = str(X.date.values[0])[:10]
58
+ latest_date = time.ctime(int(latest_date_unix))
59
+
60
+ st.write(f"⏱ Data for {latest_date}")
61
+ X=X.loc[X['date'] == max (X['date'])]
62
+ X = X.drop(columns=["date"]).fillna(0)
63
+
64
+ data_to_display = decode_features(X, feature_view=feature_view)
65
+
66
+ progress_bar.progress(60)
67
+
68
+ st.write(36 * "-")
69
+ fancy_header(f"🗺 Processing the map...")
70
+
71
+ fig = Figure(width=550,height=350)
72
+
73
+ my_map = folium.Map(location=[58, 20], zoom_start=3.71)
74
+ fig.add_child(my_map)
75
+ folium.TileLayer('Stamen Terrain').add_to(my_map)
76
+ folium.TileLayer('Stamen Toner').add_to(my_map)
77
+ folium.TileLayer('Stamen Water Color').add_to(my_map)
78
+ folium.TileLayer('cartodbpositron').add_to(my_map)
79
+ folium.TileLayer('cartodbdark_matter').add_to(my_map)
80
+ folium.LayerControl().add_to(my_map)
81
+
82
+ data_to_display = data_to_display[[ "temp", "humidity",
83
+ "conditions", "aqi"]]
84
+
85
+ cities_coords = {("Amsterdam", "Netherlands"): [52.377956, 4.897070]
86
+ }
87
+
88
+
89
+
90
+
91
+
92
+ cols_names_dict = {"temp": "Temperature",
93
+ "humidity": "Humidity",
94
+ "conditions": "Conditions",
95
+ "aqi": "AQI"}
96
+
97
+ data_to_display = data_to_display.rename(columns=cols_names_dict)
98
+
99
+ cols_ = ["Temperature", "Humidity", "AQI"]
100
+ data_to_display[cols_] = data_to_display[cols_].apply(lambda x: round(x, 1))
101
+
102
+ for city, country in cities_coords:
103
+ text = f"""
104
+ <h4 style="color:green;">{city}</h4>
105
+ <h5 style="color":"green">
106
+ <table style="text-align: right;">
107
+ <tr>
108
+ <th>Country:</th>
109
+ <td><b>{country}</b></td>
110
+ </tr>
111
+ """
112
+ for column in data_to_display.columns:
113
+ text += f"""
114
+ <tr>
115
+ <th>{column}:</th>
116
+ <td>{data_to_display.loc[0][column]}</td>
117
+ </tr>"""
118
+ text += """</table>
119
+ </h5>"""
120
+
121
+ city='Amsterdam'
122
+ country='Netherlands'
123
+ folium.Marker(
124
+ cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
125
+ ).add_to(my_map)
126
+
127
+
128
+ # call to render Folium map in Streamlit
129
+ folium_static(my_map)
130
+ progress_bar.progress(80)
131
+ st.sidebar.write("-" * 36)
132
+
133
+
134
+ model = get_model(project=project,
135
+ model_name="LSTM_model",
136
+ evaluation_metric="mse",
137
+ sort_metrics_by="min")
138
+
139
+ X=np.reshape(np.array(X),(len(X),1,len(X.columns)))
140
+
141
+ preds = model.predict(X)
142
+
143
+ cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
144
+
145
+ next_day_date = datetime.today() + timedelta(days=1)
146
+ last_day_date = datetime.today() + timedelta(days=7)
147
+
148
+ next_day = next_day_date.strftime ('%d/%m/%Y')
149
+ last_day=last_day_date.strftime ('%d/%m/%Y')
150
+ days=list()
151
+ temp_sec=int(latest_date_unix)
152
+ for i in range(0,7):
153
+ temp_sec=temp_sec+(3600*24)
154
+
155
+ days.append(time.ctime(temp_sec)[4:-14])
156
+ CQ=['Estimated AQI']
157
+
158
+ df = pd.DataFrame(data=preds,index=CQ, columns=days, dtype=int)
159
+
160
+ st.sidebar.write(df)
161
+ progress_bar.progress(100)
162
+ st.button("Re-run")
163
+ #, columns=[f"AQI Predictions from {next_day} to {last_day}"]