rafat0421 commited on
Commit
65c0215
1 Parent(s): 7e29e39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -38
app.py CHANGED
@@ -1,47 +1,68 @@
1
- import gradio as gr
2
  import hopsworks
3
  import joblib
4
  import pandas as pd
5
- import numpy as np
6
- import json
7
- import time
8
- from datetime import timedelta, datetime
9
 
10
 
11
- from functions import *
 
 
12
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  project = hopsworks.login()
15
- fs = project.get_feature_store()
16
-
17
-
18
- def air_quality(city):
19
- weather_df = pd.DataFrame()
20
- for i in range(8):
21
- weather_data = get_weather_df([get_weather_data((datetime.now() + timedelta(days=i)).strftime("%Y-%m-%d"))])
22
- weather_df = weather_df.append(weather_data)
 
 
 
 
23
 
24
- weather_df = weather_df.drop(columns=["precipprob", "uvindex", "date","city","conditions"]).fillna(0)
25
-
26
- mr = project.get_model_registry()
27
- model = mr.get_model("lond_gradient_boost_model", version=1)
28
- model_dir = model.download()
29
- model = joblib.load(model_dir + "/lond_model.pkl")
30
-
31
- preds = model.predict(weather_df)
32
-
33
- predictions = ''
34
- for k in range(7):
35
- predictions += "Predicted AQI on " + (datetime.now() + timedelta(days=k)).strftime('%Y-%m-%d') + ": " + str(int(preds[k]))+"\n"
36
-
37
- print(predictions)
38
- return predictions
39
-
40
-
41
- demo = gr.Interface(fn=air_quality, title="Air quality predictor",
42
- description="Input a value to get next weeks AQI prediction for Helsinki", inputs="text", outputs="text")
43
-
44
-
45
-
46
- if __name__ == "__main__":
47
- demo.launch()
 
 
 
 
 
 
 
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")