Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,22 +9,11 @@ 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(
|
@@ -32,113 +21,41 @@ feature_view = fs.get_feature_view(
|
|
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 |
-
X = feature_view.get_batch_data(start_time=1673625600000)
|
47 |
-
progress_bar.progress(50)
|
48 |
-
|
49 |
-
print(X.date.values)
|
50 |
-
|
51 |
-
latest_date_unix = str(X.date.values[0])[:10]
|
52 |
-
latest_date = time.ctime(int(latest_date_unix))
|
53 |
-
|
54 |
-
st.write(f"⏱ Data for {latest_date}")
|
55 |
-
|
56 |
-
X = X.drop(columns=["date"]).fillna(0)
|
57 |
-
print("X is \n %s" % X)
|
58 |
-
|
59 |
-
data_to_display = decode_features(X, feature_view=feature_view)
|
60 |
-
data_to_display["city"] = "Oslo"
|
61 |
-
# print(data_to_display)
|
62 |
-
|
63 |
-
progress_bar.progress(60)
|
64 |
-
|
65 |
-
st.write(36 * "-")
|
66 |
-
fancy_header(f"🗺 Processing the map...")
|
67 |
-
|
68 |
-
fig = Figure(width=550,height=350)
|
69 |
-
|
70 |
-
my_map = folium.Map(location=[58, 20], zoom_start=3.71)
|
71 |
-
fig.add_child(my_map)
|
72 |
-
folium.TileLayer('Stamen Terrain').add_to(my_map)
|
73 |
-
folium.TileLayer('Stamen Toner').add_to(my_map)
|
74 |
-
folium.TileLayer('Stamen Water Color').add_to(my_map)
|
75 |
-
folium.TileLayer('cartodbpositron').add_to(my_map)
|
76 |
-
folium.TileLayer('cartodbdark_matter').add_to(my_map)
|
77 |
-
folium.LayerControl().add_to(my_map)
|
78 |
-
|
79 |
-
data_to_display = data_to_display[["city", "temp", "humidity",
|
80 |
-
"conditions", "aqi"]]
|
81 |
-
|
82 |
-
cities_coords = {("Oslo", "Norway"): [59.9139, 10.7522]}
|
83 |
-
|
84 |
-
data_to_display = data_to_display.set_index("city")
|
85 |
-
|
86 |
-
cols_names_dict = {"temp": "Temperature",
|
87 |
-
"humidity": "Humidity",
|
88 |
-
"conditions": "Conditions",
|
89 |
-
"aqi": "AQI"}
|
90 |
-
|
91 |
-
data_to_display = data_to_display.rename(columns=cols_names_dict)
|
92 |
-
|
93 |
-
cols_ = ["Temperature", "Humidity", "AQI"]
|
94 |
-
data_to_display[cols_] = data_to_display[cols_].apply(lambda x: round(x, 1))
|
95 |
-
|
96 |
-
for city, country in cities_coords:
|
97 |
-
text = f"""
|
98 |
-
<h4 style="color:green;">{city}</h4>
|
99 |
-
<h5 style="color":"green">
|
100 |
-
<table style="text-align: right;">
|
101 |
-
<tr>
|
102 |
-
<th>Country:</th>
|
103 |
-
<td><b>{country}</b></td>
|
104 |
-
</tr>
|
105 |
-
"""
|
106 |
-
for column in data_to_display.columns:
|
107 |
-
text += f"""
|
108 |
-
<tr>
|
109 |
-
<th>{column}:</th>
|
110 |
-
<td>{data_to_display.loc[city][column]}</td>
|
111 |
-
</tr>"""
|
112 |
-
text += """</table>
|
113 |
-
</h5>"""
|
114 |
-
|
115 |
-
folium.Marker(
|
116 |
-
cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
|
117 |
-
).add_to(my_map)
|
118 |
-
|
119 |
-
|
120 |
-
# call to render Folium map in Streamlit
|
121 |
-
folium_static(my_map)
|
122 |
-
progress_bar.progress(80)
|
123 |
-
st.sidebar.write("-" * 36)
|
124 |
-
|
125 |
-
|
126 |
model = get_model(project=project,
|
127 |
-
model_name="
|
128 |
evaluation_metric="f1_score",
|
129 |
-
sort_metrics_by="max"
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import time
|
10 |
from datetime import timedelta, datetime
|
11 |
from branca.element import Figure
|
12 |
+
import gradio as gr
|
13 |
|
14 |
+
from functions import decode_features, get_model, get_weather_df, get_weather_data
|
15 |
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
project = hopsworks.login()
|
18 |
fs = project.get_feature_store()
|
19 |
feature_view = fs.get_feature_view(
|
|
|
21 |
version = 1
|
22 |
)
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
model = get_model(project=project,
|
25 |
+
model_name="air_quality_model",
|
26 |
evaluation_metric="f1_score",
|
27 |
+
sort_metrics_by="max",
|
28 |
+
version_name=1)
|
29 |
+
|
30 |
+
|
31 |
+
def pred(name):
|
32 |
+
str1 = " "
|
33 |
+
for i in range(8):
|
34 |
+
target_date = datetime.today() + timedelta(days=i)
|
35 |
+
target_date = target_date.strftime('%Y-%m-%d')
|
36 |
+
|
37 |
+
data_weather = [get_weather_data('oslo',target_date)]
|
38 |
+
df_weather = get_weather_df(data_weather)
|
39 |
+
df_weather['conditions'] = df_weather['conditions'].replace(['Rain','Clear','Snow','Partially cloudy','Overcast','Snow, Partially cloudy',
|
40 |
+
'Rain, Partially cloudy','Rain, Overcast','Snow, Overcast',
|
41 |
+
'Snow, Freezing Drizzle/Freezing Rain, Overcast','Snow, Rain',
|
42 |
+
'Snow, Rain, Freezing Drizzle/Freezing Rain, Ice, Overcast',
|
43 |
+
'Snow, Rain, Freezing Drizzle/Freezing Rain, Overcast','Snow, Rain, Ice, Overcast',
|
44 |
+
'Snow, Rain, Overcast','Snow, Rain, Partially cloudy'],[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
|
45 |
+
df_weather = df_weather.drop(columns=["date"]).fillna(0)
|
46 |
+
df_weather["aqi"] = 0
|
47 |
+
preds = model.predict(df_weather)
|
48 |
+
print(preds)
|
49 |
+
if(i!=0):
|
50 |
+
str1 += target_date + " predicted aqi:" + str(int(preds))+"\n"
|
51 |
+
|
52 |
+
return str1
|
53 |
+
|
54 |
+
|
55 |
+
# next_day_date = datetime.today() + timedelta(days=i)
|
56 |
+
# next_day = next_day_date.strftime ('%d/%m/%Y')
|
57 |
+
|
58 |
+
demo = gr.Interface(fn=pred,title="Predict AQI of Oslo" ,inputs="text", outputs="text")
|
59 |
+
|
60 |
+
if __name__ == "__main__":
|
61 |
+
demo.launch()
|