WCarlW commited on
Commit
0dcc21f
·
1 Parent(s): 4ca656d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +53 -139
  2. requirements.txt +2 -3
app.py CHANGED
@@ -1,143 +1,57 @@
1
- import streamlit as st
 
 
 
2
  import hopsworks
3
  import joblib
4
- import pandas as pd
5
-
6
- import numpy as np
7
- import folium
8
- from streamlit_folium import st_folium, folium_static
9
- import json
10
- import time
11
- from datetime import timedelta, datetime
12
- from branca.element import Figure
13
-
14
- from functions import decode_features, get_model
15
-
16
-
17
- def fancy_header(text, font_size=24):
18
- res = f'<span style="color:#ff5f27; font-size: {font_size}px;">{text}</span>'
19
- st.markdown(res, unsafe_allow_html=True )
20
-
21
-
22
- st.title('⛅️Air Quality Prediction Project🌩')
23
-
24
- progress_bar = st.sidebar.header('⚙️ Working Progress')
25
- progress_bar = st.sidebar.progress(0)
26
- st.write(36 * "-")
27
- fancy_header('\n📡 Connecting to Hopsworks Feature Store...')
28
-
29
  project = hopsworks.login()
30
- fs = project.get_feature_store()
31
- feature_view = fs.get_feature_view(
32
- name = 'miami_air_quality_fv',
33
- version = 1
34
- )
35
-
36
-
37
- st.write("Successfully connected!✔️")
38
- progress_bar.progress(20)
39
-
40
- st.write(36 * "-")
41
- fancy_header('\n☁️ Getting batch data from Feature Store...')
42
-
43
- start_date = datetime.now() - timedelta(days=4) # date today minus 2023-1-10
44
- start_time = int(start_date.timestamp()) * 1000
45
-
46
- X = feature_view.get_batch_data(start_time=start_time)
47
- progress_bar.progress(50)
48
-
49
- latest_date_unix = str(X.date.values[0])[:10]
50
- latest_date = time.ctime(int(latest_date_unix))
51
-
52
- st.write(f"⏱ Data for {latest_date}")
53
-
54
- X = X.drop(columns=["date"]).fillna(0)
55
-
56
- data_to_display = decode_features(X, feature_view=feature_view)
57
-
58
- progress_bar.progress(60)
59
-
60
- st.write(36 * "-")
61
- fancy_header(f"🗺 Processing the map...")
62
-
63
- fig = Figure(width=550,height=350)
64
-
65
- my_map = folium.Map(location=[58, 20], zoom_start=3.71)
66
- fig.add_child(my_map)
67
- folium.TileLayer('Stamen Terrain').add_to(my_map)
68
- folium.TileLayer('Stamen Toner').add_to(my_map)
69
- folium.TileLayer('Stamen Water Color').add_to(my_map)
70
- folium.TileLayer('cartodbpositron').add_to(my_map)
71
- folium.TileLayer('cartodbdark_matter').add_to(my_map)
72
- folium.LayerControl().add_to(my_map)
73
-
74
- data_to_display = data_to_display[["city", "temp", "humidity",
75
- "conditions", "aqi"]]
76
-
77
- # cities_coords = {("Sundsvall", "Sweden"): [62.390811, 17.306927],
78
- # ("Stockholm", "Sweden"): [59.334591, 18.063240],
79
- # ("Malmo", "Sweden"): [55.604981, 13.003822]}
80
- cities_coords = {("Miami", "USA"): [25.761681, -80.191788]}
81
-
82
- # if "Kyiv" in data_to_display["city"]:
83
- # cities_coords[("Kyiv", "Ukraine")]: [50.450001, 30.523333]
84
-
85
- data_to_display = data_to_display.set_index("city")
86
-
87
- cols_names_dict = {"temp": "Temperature",
88
- "humidity": "Humidity",
89
- "conditions": "Conditions",
90
- "aqi": "AQI"}
91
-
92
- data_to_display = data_to_display.rename(columns=cols_names_dict)
93
-
94
- cols_ = ["Temperature", "Humidity", "AQI"]
95
- data_to_display[cols_] = data_to_display[cols_].apply(lambda x: round(x, 1))
96
-
97
- for city, country in cities_coords:
98
- text = f"""
99
- <h4 style="color:green;">{city}</h4>
100
- <h5 style="color":"green">
101
- <table style="text-align: right;">
102
- <tr>
103
- <th>Country:</th>
104
- <td><b>{country}</b></td>
105
- </tr>
106
- """
107
- for column in data_to_display.columns:
108
- text += f"""
109
- <tr>
110
- <th>{column}:</th>
111
- <td>{data_to_display.loc[city][column]}</td>
112
- </tr>"""
113
- text += """</table>
114
- </h5>"""
115
-
116
- folium.Marker(
117
- cities_coords[(city, country)], popup=text, tooltip=f"<strong>{city}</strong>"
118
- ).add_to(my_map)
119
-
120
-
121
- # call to render Folium map in Streamlit
122
- folium_static(my_map)
123
- progress_bar.progress(80)
124
- st.sidebar.write("-" * 36)
125
-
126
-
127
- model = get_model(project=project,
128
- model_name="xgboost_model",
129
- evaluation_metric="f1",
130
- sort_metrics_by="max")
131
-
132
- preds = model.predict(X)
133
-
134
- cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
135
-
136
- next_day_date = datetime.today() + timedelta(days=1)
137
- next_day = next_day_date.strftime ('%d/%m/%Y')
138
- print('-------', preds, '------', X, '--------')
139
- df = pd.DataFrame(data=preds, index=cities, columns=[f"AQI Predictions for {next_day}"], dtype=int)
140
 
141
- st.sidebar.write(df)
142
- progress_bar.progress(100)
143
- st.button("Re-run")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import requests
5
  import hopsworks
6
  import joblib
7
+ import os
8
+ from datetime import datetime, timedelta
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  project = hopsworks.login()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ mr = project.get_model_registry()
12
+ model = mr.get_model("xgboost_model", version=1)
13
+ model_dir = model.download()
14
+ model = joblib.load(model_dir + "/model.pkl")
15
+
16
+
17
+
18
+
19
+ def forecast():
20
+
21
+ fs = project.get_feature_store()
22
+ feature_view = fs.get_feature_view(
23
+ name = 'miami_air_quality_fv',
24
+ version = 1
25
+ )
26
+ train_data = feature_view.get_training_data(1)[0]
27
+ train_data = train_data.drop(labels = 'city_y',axis =1)
28
+ train_data = train_data.rename(columns = {'city_x':'city'})
29
+ train_data = train_data.sort_values(by="date", ascending=True).reset_index(drop=True)
30
+ train_data["aqi_next_day"] = train_data.groupby('city')['aqi'].shift(1)
31
+
32
+ X = train_data.drop(columns=["date"]).fillna(0)
33
+ y = X.pop("aqi_next_day")
34
+ X = X.drop(columns =['city','conditions']).fillna(0)
35
+
36
+ today_data = X[1:2]
37
+ y = model.predict(today_data)
38
+
39
+ res = int(y[0])
40
+ return res
41
+
42
+ date_today = datetime.now()
43
+ day = timedelta(days = 1)
44
+ date_today = date_today + day
45
+ date_today = date_today.strftime("%Y-%m-%d")
46
+ output_label = date_today + " 's air quality is "
47
+
48
+ demo = gr.Interface(
49
+ fn=forecast,
50
+ title="Air Quality Prediction",
51
+ description="Get aqi value",
52
+ allow_flagging="never",
53
+ inputs=[],
54
+ outputs=gr.Textbox(label=output_label))
55
+
56
+
57
+ demo.launch()
requirements.txt CHANGED
@@ -6,6 +6,5 @@ numpy==1.23.5
6
  pandas==1.5.2
7
  python-dotenv==0.21.0
8
  requests==2.28.1
9
- streamlit==1.17.0
10
- streamlit_folium==0.10.0
11
- xgboost==0.90
 
6
  pandas==1.5.2
7
  python-dotenv==0.21.0
8
  requests==2.28.1
9
+ xgboost==0.90
10
+ gradio