kk90ujhun commited on
Commit
2f6db43
·
1 Parent(s): 2a868f3

Create streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +149 -0
streamlit_app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
21
+ st.title('⛅️Air Quality Prediction Project🌩')
22
+
23
+ progress_bar = st.sidebar.header('⚙️ Working Progress')
24
+ progress_bar = st.sidebar.progress(0)
25
+ st.write(36 * "-")
26
+ fancy_header('\n📡 Connecting to Hopsworks Feature Store...')
27
+
28
+ project = hopsworks.login()
29
+ fs = project.get_feature_store()
30
+ feature_view = fs.get_feature_view(
31
+ name = 'air_quality_fv',
32
+ version = 1
33
+ )
34
+
35
+ st.write("Successfully connected!✔️")
36
+ progress_bar.progress(20)
37
+
38
+ st.write(36 * "-")
39
+ fancy_header('\n☁️ Getting batch data from Feature Store...')
40
+
41
+ start_date = datetime.now() - timedelta(days=1)
42
+ start_time = int(start_date.timestamp()) * 1000
43
+
44
+ # X = feature_view.get_batch_data(start_time=start_time)
45
+
46
+ # 1662652800000
47
+ X = feature_view.get_batch_data(start_time=1662652800000)
48
+ progress_bar.progress(50)
49
+
50
+ print(X.date.values)
51
+
52
+ latest_date_unix = str(X.date.values[0])[:10]
53
+ latest_date = time.ctime(int(latest_date_unix))
54
+
55
+ st.write(f"⏱ Data for {latest_date}")
56
+
57
+ X = X.drop(columns=["date"]).fillna(0)
58
+ print("X is \n %s" % X)
59
+
60
+ data_to_display = decode_features(X, feature_view=feature_view)
61
+
62
+ progress_bar.progress(60)
63
+
64
+ st.write(36 * "-")
65
+ fancy_header(f"🗺 Processing the map...")
66
+
67
+ fig = Figure(width=550,height=350)
68
+
69
+ my_map = folium.Map(location=[58, 20], zoom_start=3.71)
70
+ fig.add_child(my_map)
71
+ folium.TileLayer('Stamen Terrain').add_to(my_map)
72
+ folium.TileLayer('Stamen Toner').add_to(my_map)
73
+ folium.TileLayer('Stamen Water Color').add_to(my_map)
74
+ folium.TileLayer('cartodbpositron').add_to(my_map)
75
+ folium.TileLayer('cartodbdark_matter').add_to(my_map)
76
+ folium.LayerControl().add_to(my_map)
77
+
78
+ data_to_display = data_to_display[["city", "temp", "humidity",
79
+ "conditions", "aqi"]]
80
+
81
+ cities_coords = {("Sundsvall", "Sweden"): [62.390811, 17.306927],
82
+ ("Stockholm", "Sweden"): [59.334591, 18.063240],
83
+ ("Malmo", "Sweden"): [55.604981, 13.003822],
84
+ ("Kyiv", "Ukraine"): [50.450001, 30.523333]}
85
+
86
+ # if "Kyiv" in data_to_display["city"]:
87
+ # cities_coords[("Kyiv", "Ukraine")]: [50.450001, 30.523333]
88
+ # pass
89
+
90
+ data_to_display = data_to_display.set_index("city")
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[city][column]}</td>
117
+ </tr>"""
118
+ text += """</table>
119
+ </h5>"""
120
+
121
+ folium.Marker(
122
+ cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
123
+ ).add_to(my_map)
124
+
125
+
126
+ # call to render Folium map in Streamlit
127
+ folium_static(my_map)
128
+ progress_bar.progress(80)
129
+ st.sidebar.write("-" * 36)
130
+
131
+
132
+ model = get_model(project=project,
133
+ model_name="gradient_boost_model",
134
+ evaluation_metric="f1_score",
135
+ sort_metrics_by="max")
136
+
137
+ preds = model.predict(X)
138
+
139
+ cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
140
+ print("cities are %s" % cities)
141
+
142
+ next_day_date = datetime.today() + timedelta(days=1)
143
+ next_day = next_day_date.strftime ('%d/%m/%Y')
144
+ print("preds is %s" % preds)
145
+ df = pd.DataFrame(data=preds, index=cities, columns=[f"AQI Predictions for {next_day}"], dtype=int)
146
+
147
+ st.sidebar.write(df)
148
+ progress_bar.progress(100)
149
+ st.button("Re-run")