Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
import hopsworks
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
from datetime import timedelta, datetime
|
| 9 |
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
project = hopsworks.login()
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import hopsworks
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
+
import datetime
|
| 6 |
+
from functions import get_weather_data_weekly, data_encoder, get_aplevel, get_color
|
| 7 |
+
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
+
def fancy_header(text, font_size=24):
|
| 11 |
+
res = f'<p style="color:#ff5f27; font-size: {font_size}px;text-align:center">{text}</p>'
|
| 12 |
+
st.markdown(res, unsafe_allow_html=True)
|
| 13 |
|
| 14 |
+
st.set_page_config(layout="wide")
|
| 15 |
+
st.title('Air Quality Prediction Project for Vienna! 🌩')
|
| 16 |
+
vienna_image = Image.open('vienna.jpg')
|
| 17 |
+
st.image(vienna_image, use_column_width='auto')
|
| 18 |
+
st.write(36 * "-")
|
| 19 |
+
|
| 20 |
+
st.markdown("# This is a final project in the course ID2223 Scalable Machine Learning and Deep Learning :computer:")
|
| 21 |
+
st.markdown("My task was to predict the Air Quality Index (AQI) for one city (I choose Vienna) based on different weather data (pressure, snow-and cloud-coverage, temperature, etc.).")
|
| 22 |
+
st.markdown("For the full list of weather data, please click [here](https://visualcrossing.com/resources/documentation/weather-api/timeline-weather-api)")
|
| 23 |
+
fancy_header('\n Connecting to Hopsworks Feature Store...')
|
| 24 |
|
| 25 |
project = hopsworks.login()
|
| 26 |
+
|
| 27 |
+
st.write("Successfully connected!✔️")
|
| 28 |
+
|
| 29 |
+
st.write(36 * "-")
|
| 30 |
+
fancy_header('\n Collecting the weather data from Vienna...')
|
| 31 |
+
|
| 32 |
+
today = datetime.date.today()
|
| 33 |
+
city = "vienna"
|
| 34 |
+
weather_df = pd.DataFrame()
|
| 35 |
+
for i in range(8):
|
| 36 |
+
weather_data = get_weather_df([get_weather_data((datetime.now() + timedelta(days=i)).strftime("%Y-%m-%d"))])
|
| 37 |
+
weather_df = weather_df.append(weather_data)
|
| 38 |
|
| 39 |
+
weather_df = weather_df.drop(columns=["precipprob", "uvindex", "date","city","conditions"]).fillna(0)
|
| 40 |
+
st.write("Successfully collected!✔️")
|
| 41 |
+
|
| 42 |
+
st.write(36 * "-")
|
| 43 |
+
|
| 44 |
+
fancy_header("Loading the fitted XGBoost model...")
|
| 45 |
+
mr = project.get_model_registry()
|
| 46 |
+
model = mr.get_model("lond_gradient_boost_model", version=1)
|
| 47 |
+
model_dir = model.download()
|
| 48 |
+
model = joblib.load(model_dir + "/lond_model.pkl")
|
| 49 |
+
|
| 50 |
+
st.write("Succesfully loaded!✔️")
|
| 51 |
+
st.sidebar.write("-" * 36)
|
| 52 |
+
|
| 53 |
+
fancy_header("Making AQI predictions for the next 7 days")
|
| 54 |
+
|
| 55 |
+
preds = model.predict(weather_df)
|
| 56 |
+
|
| 57 |
+
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
| 58 |
+
poll_level = get_aplevel(preds.T.reshape(-1, 1), air_pollution_level)
|
| 59 |
+
|
| 60 |
+
next_week_datetime = [today + datetime.timedelta(days=d) for d in range(7)]
|
| 61 |
+
next_week_str = [f"{days.strftime('%A')}, {days.strftime('%Y-%m-%d')}" for days in next_week_datetime]
|
| 62 |
+
|
| 63 |
+
df = pd.DataFrame(data=[preds, poll_level], index=["AQI", "Air pollution level"], columns=next_week_str)
|
| 64 |
+
|
| 65 |
+
st.write("Here they are!")
|
| 66 |
+
st.dataframe(df.style.apply(get_color, subset=(["Air pollution level"], slice(None))))
|
| 67 |
+
|
| 68 |
+
st.button("Re-run")
|