Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +57 -0
- functions.py +206 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
functions.py
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is from the hopsworks-tutorials
|
| 2 |
+
# https://github.com/logicalclocks/hopsworks-tutorials/tree/master/advanced_tutorials/air_quality
|
| 3 |
+
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
import joblib
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
def decode_features(df, feature_view):
|
| 14 |
+
"""Decodes features in the input DataFrame using corresponding Hopsworks Feature Store transformation functions"""
|
| 15 |
+
df_res = df.copy()
|
| 16 |
+
|
| 17 |
+
import inspect
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
td_transformation_functions = feature_view._batch_scoring_server._transformation_functions
|
| 21 |
+
|
| 22 |
+
res = {}
|
| 23 |
+
for feature_name in td_transformation_functions:
|
| 24 |
+
if feature_name in df_res.columns:
|
| 25 |
+
td_transformation_function = td_transformation_functions[feature_name]
|
| 26 |
+
sig, foobar_locals = inspect.signature(td_transformation_function.transformation_fn), locals()
|
| 27 |
+
param_dict = dict([(param.name, param.default) for param in sig.parameters.values() if param.default != inspect._empty])
|
| 28 |
+
if td_transformation_function.name == "min_max_scaler":
|
| 29 |
+
df_res[feature_name] = df_res[feature_name].map(
|
| 30 |
+
lambda x: x * (param_dict["max_value"] - param_dict["min_value"]) + param_dict["min_value"])
|
| 31 |
+
|
| 32 |
+
elif td_transformation_function.name == "standard_scaler":
|
| 33 |
+
df_res[feature_name] = df_res[feature_name].map(
|
| 34 |
+
lambda x: x * param_dict['std_dev'] + param_dict["mean"])
|
| 35 |
+
elif td_transformation_function.name == "label_encoder":
|
| 36 |
+
dictionary = param_dict['value_to_index']
|
| 37 |
+
dictionary_ = {v: k for k, v in dictionary.items()}
|
| 38 |
+
df_res[feature_name] = df_res[feature_name].map(
|
| 39 |
+
lambda x: dictionary_[x])
|
| 40 |
+
return df_res
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_model(project, model_name, evaluation_metric, sort_metrics_by):
|
| 44 |
+
"""Retrieve desired model or download it from the Hopsworks Model Registry.
|
| 45 |
+
In second case, it will be physically downloaded to this directory"""
|
| 46 |
+
TARGET_FILE = "model.pkl"
|
| 47 |
+
list_of_files = [os.path.join(dirpath,filename) for dirpath, _, filenames \
|
| 48 |
+
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
|
| 49 |
+
|
| 50 |
+
if list_of_files:
|
| 51 |
+
model_path = list_of_files[0]
|
| 52 |
+
model = joblib.load(model_path)
|
| 53 |
+
else:
|
| 54 |
+
if not os.path.exists(TARGET_FILE):
|
| 55 |
+
mr = project.get_model_registry()
|
| 56 |
+
# get best model based on custom metrics
|
| 57 |
+
model = mr.get_best_model(model_name,
|
| 58 |
+
evaluation_metric,
|
| 59 |
+
sort_metrics_by)
|
| 60 |
+
model_dir = model.download()
|
| 61 |
+
model = joblib.load(model_dir + "/model.pkl")
|
| 62 |
+
|
| 63 |
+
return model
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def get_air_json(city_name, AIR_QUALITY_API_KEY):
|
| 67 |
+
return requests.get(f'https://api.waqi.info/feed/{city_name}/?token={AIR_QUALITY_API_KEY}').json()['data']
|
| 68 |
+
|
| 69 |
+
def get_air_quality_data(city_name):
|
| 70 |
+
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
| 71 |
+
json = get_air_json(city_name, AIR_QUALITY_API_KEY)
|
| 72 |
+
iaqi = json['iaqi']
|
| 73 |
+
forecast = json['forecast']['daily']
|
| 74 |
+
return [
|
| 75 |
+
city_name,
|
| 76 |
+
json['aqi'], # AQI
|
| 77 |
+
json['time']['s'][:10], # Date
|
| 78 |
+
iaqi['h']['v'],
|
| 79 |
+
iaqi['p']['v'],
|
| 80 |
+
iaqi['pm10']['v'],
|
| 81 |
+
iaqi['t']['v'],
|
| 82 |
+
forecast['o3'][0]['avg'],
|
| 83 |
+
forecast['o3'][0]['max'],
|
| 84 |
+
forecast['o3'][0]['min'],
|
| 85 |
+
forecast['pm10'][0]['avg'],
|
| 86 |
+
forecast['pm10'][0]['max'],
|
| 87 |
+
forecast['pm10'][0]['min'],
|
| 88 |
+
forecast['pm25'][0]['avg'],
|
| 89 |
+
forecast['pm25'][0]['max'],
|
| 90 |
+
forecast['pm25'][0]['min'],
|
| 91 |
+
# forecast['uvi'][0]['avg'],
|
| 92 |
+
# forecast['uvi'][0]['avg'],
|
| 93 |
+
# forecast['uvi'][0]['avg']
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
def get_air_quality_df(data):
|
| 97 |
+
col_names = [
|
| 98 |
+
'city',
|
| 99 |
+
'aqi',
|
| 100 |
+
'date&time',
|
| 101 |
+
'iaqi_h',
|
| 102 |
+
'iaqi_p',
|
| 103 |
+
'iaqi_pm10',
|
| 104 |
+
'iaqi_t',
|
| 105 |
+
'o3_avg',
|
| 106 |
+
'o3_max',
|
| 107 |
+
'o3_min',
|
| 108 |
+
'pm10_avg',
|
| 109 |
+
'pm10_max',
|
| 110 |
+
'pm10_min',
|
| 111 |
+
'pm25_avg',
|
| 112 |
+
'pm25_max',
|
| 113 |
+
'pm25_min',
|
| 114 |
+
# 'uvi_avg',
|
| 115 |
+
# 'uvi_max',
|
| 116 |
+
# 'uvi_min',
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
new_data = pd.DataFrame(
|
| 120 |
+
data,
|
| 121 |
+
columns=col_names
|
| 122 |
+
)
|
| 123 |
+
new_data.date = new_data.date.apply(timestamp_2_time)
|
| 124 |
+
|
| 125 |
+
return new_data
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def get_weather_json(city, date, WEATHER_API_KEY):
|
| 129 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{city.lower()}/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def get_weather_data(city_name, date):
|
| 133 |
+
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 134 |
+
json = get_weather_json(city_name, date, WEATHER_API_KEY)
|
| 135 |
+
data = json['days'][0]
|
| 136 |
+
|
| 137 |
+
return [
|
| 138 |
+
json['address'].capitalize(),
|
| 139 |
+
data['datetime'],
|
| 140 |
+
data['tempmax'],
|
| 141 |
+
data['tempmin'],
|
| 142 |
+
data['temp'],
|
| 143 |
+
data['feelslikemax'],
|
| 144 |
+
data['feelslikemin'],
|
| 145 |
+
data['feelslike'],
|
| 146 |
+
data['dew'],
|
| 147 |
+
data['humidity'],
|
| 148 |
+
data['precip'],
|
| 149 |
+
data['precipprob'],
|
| 150 |
+
data['precipcover'],
|
| 151 |
+
data['snow'],
|
| 152 |
+
data['snowdepth'],
|
| 153 |
+
data['windgust'],
|
| 154 |
+
data['windspeed'],
|
| 155 |
+
data['winddir'],
|
| 156 |
+
data['pressure'],
|
| 157 |
+
data['cloudcover'],
|
| 158 |
+
data['visibility'],
|
| 159 |
+
data['solarradiation'],
|
| 160 |
+
data['solarenergy'],
|
| 161 |
+
data['uvindex'],
|
| 162 |
+
data['conditions']
|
| 163 |
+
]
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def get_weather_df(data):
|
| 167 |
+
col_names = [
|
| 168 |
+
'city',
|
| 169 |
+
'date',
|
| 170 |
+
'tempmax',
|
| 171 |
+
'tempmin',
|
| 172 |
+
'temp',
|
| 173 |
+
'feelslikemax',
|
| 174 |
+
'feelslikemin',
|
| 175 |
+
'feelslike',
|
| 176 |
+
'dew',
|
| 177 |
+
'humidity',
|
| 178 |
+
'precip',
|
| 179 |
+
'precipprob',
|
| 180 |
+
'precipcover',
|
| 181 |
+
'snow',
|
| 182 |
+
'snowdepth',
|
| 183 |
+
'windgust',
|
| 184 |
+
'windspeed',
|
| 185 |
+
'winddir',
|
| 186 |
+
'pressure',
|
| 187 |
+
'cloudcover',
|
| 188 |
+
'visibility',
|
| 189 |
+
'solarradiation',
|
| 190 |
+
'solarenergy',
|
| 191 |
+
'uvindex',
|
| 192 |
+
'conditions'
|
| 193 |
+
]
|
| 194 |
+
|
| 195 |
+
new_data = pd.DataFrame(
|
| 196 |
+
data,
|
| 197 |
+
columns=col_names
|
| 198 |
+
)
|
| 199 |
+
new_data.date = new_data.date.apply(timestamp_2_time)
|
| 200 |
+
|
| 201 |
+
return new_data
|
| 202 |
+
|
| 203 |
+
def timestamp_2_time(x):
|
| 204 |
+
dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
|
| 205 |
+
dt_obj = dt_obj.timestamp() * 1000
|
| 206 |
+
return int(dt_obj)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
branca==0.6.0
|
| 2 |
+
folium==0.14.0
|
| 3 |
+
hopsworks==3.0.5
|
| 4 |
+
joblib==1.2.0
|
| 5 |
+
numpy==1.23.5
|
| 6 |
+
pandas==1.5.2
|
| 7 |
+
python-dotenv==0.21.0
|
| 8 |
+
requests==2.28.1
|
| 9 |
+
xgboost==0.90
|
| 10 |
+
gradio
|