Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import hopsworks
|
| 3 |
+
import joblib
|
| 4 |
+
from datetime import date
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datetime import timedelta, datetime
|
| 7 |
+
from functions import *
|
| 8 |
+
import numpy as np
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
+
|
| 11 |
+
import folium
|
| 12 |
+
from streamlit_folium import st_folium, folium_static
|
| 13 |
+
import json
|
| 14 |
+
import time
|
| 15 |
+
from branca.element import Figure
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def fancy_header(text, font_size=24):
|
| 19 |
+
res = f'<p style="color:#ff5f72; font-size: {font_size}px; text-align:center;">{text}</p>'
|
| 20 |
+
st.markdown(res, unsafe_allow_html=True)
|
| 21 |
+
|
| 22 |
+
st.set_page_config(layout="wide")
|
| 23 |
+
|
| 24 |
+
st.title('Air Quality Prediction Project🌩')
|
| 25 |
+
|
| 26 |
+
st.write(36 * "-")
|
| 27 |
+
fancy_header('\n Connecting to Hopsworks Feature Store...')
|
| 28 |
+
|
| 29 |
+
project = hopsworks.login()
|
| 30 |
+
|
| 31 |
+
st.write("Successfully connected!✔️")
|
| 32 |
+
|
| 33 |
+
st.write(36 * "-")
|
| 34 |
+
fancy_header('\n Getting data from Feature Store...')
|
| 35 |
+
|
| 36 |
+
today = date.today()
|
| 37 |
+
city = "Beijing"
|
| 38 |
+
df_weather = get_weather_data_weekly(city, today)
|
| 39 |
+
df_weather.date = df_weather.date.apply(timestamp_2_time)
|
| 40 |
+
df_weather_x = df_weather.drop(columns=["date"]).fillna(0)
|
| 41 |
+
df_weather_nn=np.array(df_weather_x)
|
| 42 |
+
scaler = StandardScaler()
|
| 43 |
+
scaler.fit(df_weather_x)
|
| 44 |
+
|
| 45 |
+
df_weather_use=scaler.transform(df_weather_x)
|
| 46 |
+
|
| 47 |
+
df_weather_use_1= pd.DataFrame(df_weather_use)
|
| 48 |
+
|
| 49 |
+
#preds_zzz = model.predict(df_weather_use_1).astype(int)
|
| 50 |
+
|
| 51 |
+
st.write(36 * "-")
|
| 52 |
+
|
| 53 |
+
mr = project.get_model_registry()
|
| 54 |
+
model = mr.get_model("air_quality_modal_choosed", version=1)
|
| 55 |
+
model_dir = model.download()
|
| 56 |
+
model = joblib.load(model_dir + "/air_quality_model_choosed.pkl")
|
| 57 |
+
|
| 58 |
+
st.write("-" * 36)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
preds = model.predict(df_weather_use_1).astype(int)
|
| 62 |
+
pollution_level = get_aplevel(preds.T.reshape(-1, 1))
|
| 63 |
+
|
| 64 |
+
next_week = [f"{(today + timedelta(days=d)).strftime('%Y-%m-%d')},{(today + timedelta(days=d)).strftime('%A')}" for d in range(8)]
|
| 65 |
+
|
| 66 |
+
df = pd.DataFrame(data=[preds, pollution_level], index=["AQI", "Air pollution level"], columns=next_week)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
st.write(df)
|
| 70 |
+
|
| 71 |
+
st.button("Re-run")
|