LarissaE commited on
Commit
65a06c2
1 Parent(s): 0a0e9a6
Files changed (3) hide show
  1. .hw_api_key +1 -0
  2. app.py +139 -0
  3. requirements.txt +3 -0
.hw_api_key ADDED
@@ -0,0 +1 @@
 
 
1
+ 43HCgRJba9W7KLQK.XUYxmHC4KiQ3GQ0xeG5R9aZM5dyvWRrcCgxFGvjtT8G7VBnPNiQ3HnYwMYcEGMz6
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('⛅️Rafat and Larissa do an 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
+ progress_bar.progress(50)
46
+
47
+ latest_date_unix = str(X.date.values[0])[:10]
48
+ latest_date = time.ctime(int(latest_date_unix))
49
+
50
+ st.write(f"⏱ Data for {latest_date}")
51
+
52
+ X = X.drop(columns=["date"]).fillna(0)
53
+
54
+ data_to_display = decode_features(X, feature_view=feature_view)
55
+
56
+ progress_bar.progress(60)
57
+
58
+ st.write(36 * "-")
59
+ fancy_header(f"🗺 Processing the map...")
60
+
61
+ fig = Figure(width=550,height=350)
62
+
63
+ my_map = folium.Map(location=[58, 20], zoom_start=3.71)
64
+ fig.add_child(my_map)
65
+ folium.TileLayer('Stamen Terrain').add_to(my_map)
66
+ folium.TileLayer('Stamen Toner').add_to(my_map)
67
+ folium.TileLayer('Stamen Water Color').add_to(my_map)
68
+ folium.TileLayer('cartodbpositron').add_to(my_map)
69
+ folium.TileLayer('cartodbdark_matter').add_to(my_map)
70
+ folium.LayerControl().add_to(my_map)
71
+
72
+ data_to_display = data_to_display[["city", "temp", "humidity",
73
+ "conditions", "aqi"]]
74
+
75
+ cities_coords = {("Sundsvall", "Sweden"): [62.390811, 17.306927],
76
+ ("Stockholm", "Sweden"): [59.334591, 18.063240],
77
+ ("Malmo", "Sweden"): [55.604981, 13.003822]}
78
+
79
+ if "Kyiv" in data_to_display["city"]:
80
+ cities_coords[("Kyiv", "Ukraine")]: [50.450001, 30.523333]
81
+
82
+ data_to_display = data_to_display.set_index("city")
83
+
84
+ cols_names_dict = {"temp": "Temperature",
85
+ "humidity": "Humidity",
86
+ "conditions": "Conditions",
87
+ "aqi": "AQI"}
88
+
89
+ data_to_display = data_to_display.rename(columns=cols_names_dict)
90
+
91
+ cols_ = ["Temperature", "Humidity", "AQI"]
92
+ data_to_display[cols_] = data_to_display[cols_].apply(lambda x: round(x, 1))
93
+
94
+ for city, country in cities_coords:
95
+ text = f"""
96
+ <h4 style="color:green;">{city}</h4>
97
+ <h5 style="color":"green">
98
+ <table style="text-align: right;">
99
+ <tr>
100
+ <th>Country:</th>
101
+ <td><b>{country}</b></td>
102
+ </tr>
103
+ """
104
+ for column in data_to_display.columns:
105
+ text += f"""
106
+ <tr>
107
+ <th>{column}:</th>
108
+ <td>{data_to_display.loc[city][column]}</td>
109
+ </tr>"""
110
+ text += """</table>
111
+ </h5>"""
112
+
113
+ folium.Marker(
114
+ cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
115
+ ).add_to(my_map)
116
+
117
+
118
+ # call to render Folium map in Streamlit
119
+ folium_static(my_map)
120
+ progress_bar.progress(80)
121
+ st.sidebar.write("-" * 36)
122
+
123
+
124
+ model = get_model(project=project,
125
+ model_name="gradient_boost_model",
126
+ evaluation_metric="f1_score",
127
+ sort_metrics_by="max")
128
+
129
+ preds = model.predict(X)
130
+
131
+ cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
132
+
133
+ next_day_date = datetime.today() + timedelta(days=1)
134
+ next_day = next_day_date.strftime ('%d/%m/%Y')
135
+ df = pd.DataFrame(data=preds, index=cities, columns=[f"AQI Predictions for {next_day}"], dtype=int)
136
+
137
+ st.sidebar.write(df)
138
+ progress_bar.progress(100)
139
+ st.button("Re-run")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ hopsworks
2
+ joblib
3
+ scikit-learn