Spaces:
Runtime error
Runtime error
File size: 4,401 Bytes
65a06c2 7be10f4 53aa295 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 57f8e39 65a06c2 57f8e39 65a06c2 57f8e39 65a06c2 39284d8 65a06c2 39284d8 57f8e39 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 39284d8 65a06c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import streamlit as st
import hopsworks
import joblib
import pandas as pd
import numpy as np
#import folium
#from streamlit_folium import st_folium, folium_static
import json
import time
from datetime import timedelta, datetime
from branca.element import Figure
from functions import decode_features, get_model
def fancy_header(text, font_size=24):
res = f'<span style="color:#ff5f27; font-size: {font_size}px;">{text}</span>'
st.markdown(res, unsafe_allow_html=True )
st.title('⛅️Rafat and Larissa do an Air Quality Prediction Project🌩')
progress_bar = st.sidebar.header('⚙️ Working Progress')
progress_bar = st.sidebar.progress(0)
st.write(36 * "-")
fancy_header('\n📡 Connecting to Hopsworks Feature Store...')
project = hopsworks.login()
mr = project.get_model_registry()
model = mr.get_model("gradient_boost_model", version=1)
model_dir = model.download()
# model = joblib.load(model_dir + "/model.pkl")
fs = project.get_feature_store()
feature_view = fs.get_feature_view(
name = 'helsinki_aqi_fv',
version = 1
)
st.write("Successfully connected!✔️")
progress_bar.progress(20)
st.write(36 * "-")
fancy_header('\n☁️ Getting batch data from Feature Store...')
start_date = datetime.now() - timedelta(days=1)
start_time = int(start_date.timestamp()) * 1000
X = feature_view.get_batch_data(start_time=start_time)
progress_bar.progress(50)
latest_date_unix = str(X.date.values[0])[:10]
latest_date = time.ctime(int(latest_date_unix))
st.write(f"⏱ Data for {latest_date}")
X = X.drop(columns=["date"]).fillna(0)
data_to_display = decode_features(X, feature_view=feature_view)
progress_bar.progress(60)
st.write(36 * "-")
fancy_header(f"🗺 Processing the map...")
fig = Figure(width=550,height=350)
"""
my_map = folium.Map(location=[58, 20], zoom_start=3.71)
fig.add_child(my_map)
folium.TileLayer('Stamen Terrain').add_to(my_map)
folium.TileLayer('Stamen Toner').add_to(my_map)
folium.TileLayer('Stamen Water Color').add_to(my_map)
folium.TileLayer('cartodbpositron').add_to(my_map)
folium.TileLayer('cartodbdark_matter').add_to(my_map)
folium.LayerControl().add_to(my_map)
"""
data_to_display = data_to_display[["city", "temp", "humidity",
"conditions", "aqi"]]
cities_coords = {("Helsinki", "Finland"): [62.390811, 17.306927]}
data_to_display = data_to_display.set_index("city")
cols_names_dict = {"temp": "Temperature",
"humidity": "Humidity",
"visibility": "Visibility",
"precip": "Precipitation",
"cloudcover": "Cloud cover",
"uvindex": "UV index",
"conditions": "Conditions",
"aqi": "AQI"}
data_to_display = data_to_display.rename(columns=cols_names_dict)
cols_ = ["Temperature", "Humidity", "AQI", "Visibility", "Precipitation", "Cloud cover", "UV index"]
data_to_display[cols_] = data_to_display[cols_].apply(lambda x: round(x, 1))
"""
for city, country in cities_coords:
text = f
<h4 style="color:green;">{city}</h4>
<h5 style="color":"green">
<table style="text-align: right;">
<tr>
<th>Country:</th>
<td><b>{country}</b></td>
</tr>
for column in data_to_display.columns:
text += f
<tr>
<th>{column}:</th>
<td>{data_to_display.loc[city][column]}</td>
</tr>
text += </table>
</h5>
folium.Marker(
cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
).add_to(my_map)
# call to render Folium map in Streamlit
folium_static(my_map)
"""
progress_bar.progress(80)
st.sidebar.write("-" * 36)
model = get_model(project=project,
model_name="gradient_boost_model",
evaluation_metric="f1_score",
sort_metrics_by="max")
preds = model.predict(X)
cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
next_day_date = datetime.today() + timedelta(days=1)
next_day = next_day_date.strftime ('%d/%m/%Y')
df = pd.DataFrame(data=preds, index=cities, columns=[f"AQI Predictions for {next_day}"], dtype=int)
st.sidebar.write(df)
progress_bar.progress(100)
st.button("Re-run")
|