Spaces:
Sleeping
Sleeping
File size: 6,646 Bytes
6ed1571 |
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
import json
import time
import pickle
import joblib
import hopsworks
import streamlit as st
from geopy import distance
import plotly.express as px
import folium
from streamlit_folium import st_folium
from functions import *
def print_fancy_header(text, font_size=22, color="#ff5f27"):
res = f'<span style="color:{color}; font-size: {font_size}px;">{text}</span>'
st.markdown(res, unsafe_allow_html=True)
# I want to cache this so streamlit would run much faster after restart (it restarts a lot)
@st.cache_data()
def get_feature_view():
st.write("Getting the Feature View...")
feature_view = fs.get_feature_view(
name = 'air_quality_fv',
version = 1
)
st.write("β
Success!")
return feature_view
@st.cache_data()
def get_batch_data_from_fs(td_version, date_threshold):
st.write(f"Retrieving the Batch data since {date_threshold}")
feature_view.init_batch_scoring(training_dataset_version=td_version)
batch_data = feature_view.get_batch_data(start_time=date_threshold)
return batch_data
@st.cache_data()
def download_model(name="air_quality_xgboost_model",
version=1):
mr = project.get_model_registry()
retrieved_model = mr.get_model(
name="air_quality_xgboost_model",
version=1
)
saved_model_dir = retrieved_model.download()
return saved_model_dir
def plot_pm2_5(df):
# create figure with plotly express
fig = px.line(df, x='date', y='pm2_5', color='city_name')
# customize line colors and styles
fig.update_traces(mode='lines+markers')
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'legend_title': 'City',
'legend_font': {'size': 12},
'legend_bgcolor': 'rgba(0, 0, 0, 0)',
'xaxis': {'title': 'Date'},
'yaxis': {'title': 'PM2.5'},
'shapes': [{
'type': 'line',
'x0': datetime.datetime.now().strftime('%Y-%m-%d'),
'y0': 0,
'x1': datetime.datetime.now().strftime('%Y-%m-%d'),
'y1': df['pm2_5'].max(),
'line': {'color': 'red', 'width': 2, 'dash': 'dashdot'}
}]
})
# show plot
st.plotly_chart(fig, use_container_width=True)
with open('target_cities.json') as json_file:
target_cities = json.load(json_file)
#########################
st.title('π« Air Quality Prediction π¦')
st.write(3 * "-")
print_fancy_header('\nπ‘ Connecting to Hopsworks Feature Store...')
st.write("Logging... ")
# (Attention! If the app has stopped at this step,
# please enter your Hopsworks API Key in the commmand prompt.)
project = hopsworks.login()
fs = project.get_feature_store()
st.write("β
Logged in successfully!")
feature_view = get_feature_view()
# I am going to load data for of last 60 days (for feature engineering)
today = datetime.date.today()
date_threshold = today
#- datetime.timedelta(days=60)
st.write(3 * "-")
print_fancy_header('\nβοΈ Retriving batch data from Feature Store...')
batch_data = get_batch_data_from_fs(td_version=1,
date_threshold=date_threshold)
st.write("Batch data:")
st.write(batch_data.sample(5))
# +
saved_model_dir = download_model(
name="air_quality_xgboost_model",
version=1
)
pipeline = joblib.load(saved_model_dir + "/xgboost_pipeline.pkl")
st.write("\n")
st.write("β
Model was downloaded and cached.")
# -
st.write(3 * '-')
st.write("\n")
print_fancy_header(text="π Select the cities using the form below. \
Click the 'Submit' button at the bottom of the form to continue.",
font_size=22)
dict_for_streamlit = {}
for continent in target_cities:
for city_name, coords in target_cities[continent].items():
dict_for_streamlit[city_name] = coords
selected_cities_full_list = []
with st.form(key="user_inputs"):
print_fancy_header(text='\nπΊ Here you can choose cities from the drop-down menu',
font_size=20, color="#00FFFF")
cities_multiselect = st.multiselect(label='',
options=dict_for_streamlit.keys())
selected_cities_full_list.extend(cities_multiselect)
st.write("_" * 3)
print_fancy_header(text="\nπ To add a city using the interactive map, click somewhere \
(for the coordinates to appear)",
font_size=20, color="#00FFFF")
my_map = folium.Map(location=[42.57, -44.092], zoom_start=2)
# Add markers for each city
for city_name, coords in dict_for_streamlit.items():
folium.CircleMarker(
location=coords
).add_to(my_map)
my_map.add_child(folium.LatLngPopup())
res_map = st_folium(my_map, width=640, height=480)
try:
new_lat, new_long = res_map["last_clicked"]["lat"], res_map["last_clicked"]["lng"]
# Calculate the distance between the clicked location and each city
distances = {city: distance.distance(coord, (new_lat, new_long)).km for city, coord in dict_for_streamlit.items()}
# Find the city with the minimum distance and print its name
nearest_city = min(distances, key=distances.get)
print_fancy_header(text=f"You have selected {nearest_city} using map", font_size=18, color="#52fa23")
selected_cities_full_list.append(nearest_city)
# st.write(label_encoder.transform([nearest_city])[0])
except Exception as err:
print(err)
pass
submit_button = st.form_submit_button(label='Submit')
# +
if submit_button:
st.write('Selected cities:', selected_cities_full_list)
st.write(3*'-')
dataset = batch_data
dataset = dataset.sort_values(by=["city_name", "date"])
st.write("\n")
print_fancy_header(text='\nπ§ Predicting PM2.5 for selected cities...',
font_size=18, color="#FDF4F5")
st.write("")
preds = pd.DataFrame(columns=dataset.columns)
for city_name in selected_cities_full_list:
st.write(f"\t * {city_name}...")
features = dataset.loc[dataset['city_name'] == city_name]
print(features.head())
features['pm2_5'] = pipeline.predict(features)
preds = pd.concat([preds, features])
st.write("")
print_fancy_header(text="πResults π",
font_size=22)
plot_pm2_5(preds[preds['city_name'].isin(selected_cities_full_list)])
st.write(3 * "-")
st.subheader('\nπ π π€ App Finished Successfully π€ π π')
st.button("Re-run")
|