Spaces:
Sleeping
Sleeping
time series for gradio
Browse files- .gitignore +2 -1
- app.py +16 -4
- debug.ipynb +22 -0
- img/pm25_forecast.png +0 -0
- infer.py +52 -2
- scheduler.py +7 -0
.gitignore
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
.venv
|
| 2 |
.env
|
| 3 |
-
.cache.sqlite
|
|
|
|
|
|
| 1 |
.venv
|
| 2 |
.env
|
| 3 |
+
.cache.sqlite
|
| 4 |
+
__pycache__/
|
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
| 5 |
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
now = datetime.now()
|
| 8 |
|
| 9 |
+
df = pd.DataFrame({
|
| 10 |
+
'time': [now - timedelta(minutes=5*i) for i in range(25)],
|
| 11 |
+
'price': np.random.randint(100, 1000, 25),
|
| 12 |
+
'origin': [random.choice(["DFW", "DAL", "HOU"]) for _ in range(25)],
|
| 13 |
+
'destination': [random.choice(["JFK", "LGA", "EWR"]) for _ in range(25)],
|
| 14 |
+
})
|
| 15 |
|
| 16 |
+
with gr.Blocks() as demo:
|
| 17 |
+
gr.LinePlot(df, x="time", y="price")
|
| 18 |
+
gr.ScatterPlot(df, x="time", y="price", color="origin")
|
| 19 |
+
|
| 20 |
+
demo.launch()
|
debug.ipynb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {
|
| 7 |
+
"vscode": {
|
| 8 |
+
"languageId": "plaintext"
|
| 9 |
+
}
|
| 10 |
+
},
|
| 11 |
+
"outputs": [],
|
| 12 |
+
"source": []
|
| 13 |
+
}
|
| 14 |
+
],
|
| 15 |
+
"metadata": {
|
| 16 |
+
"language_info": {
|
| 17 |
+
"name": "python"
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
"nbformat": 4,
|
| 21 |
+
"nbformat_minor": 2
|
| 22 |
+
}
|
img/pm25_forecast.png
ADDED
|
infer.py
CHANGED
|
@@ -14,6 +14,11 @@ project_name = os.getenv('HOPSWORKS_PROJECT')
|
|
| 14 |
project = hopsworks.login(project=project_name, api_key_value=api_key)
|
| 15 |
fs = project.get_feature_store()
|
| 16 |
secrets = util.secrets_api(project.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
AQI_API_KEY = secrets.get_secret("AQI_API_KEY").value
|
| 19 |
location_str = secrets.get_secret("SENSOR_LOCATION_JSON").value
|
|
@@ -26,7 +31,7 @@ feature_view = fs.get_feature_view(
|
|
| 26 |
version=1,
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
mr = project.get_model_registry()
|
| 32 |
|
|
@@ -39,7 +44,7 @@ saved_model_dir = retrieved_model.download()
|
|
| 39 |
retrieved_xgboost_model = XGBRegressor()
|
| 40 |
retrieved_xgboost_model.load_model(saved_model_dir + "/model.json")
|
| 41 |
|
| 42 |
-
|
| 43 |
|
| 44 |
weather_fg = fs.get_feature_group(
|
| 45 |
name='weather',
|
|
@@ -48,5 +53,50 @@ weather_fg = fs.get_feature_group(
|
|
| 48 |
|
| 49 |
today_timestamp = pd.to_datetime(today)
|
| 50 |
batch_data = weather_fg.filter(weather_fg.date >= today_timestamp ).read()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
batch_data['predicted_pm25'] = retrieved_xgboost_model.predict(
|
| 52 |
batch_data[['temperature_2m_mean', 'precipitation_sum', 'wind_speed_10m_max', 'wind_direction_10m_dominant']])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
project = hopsworks.login(project=project_name, api_key_value=api_key)
|
| 15 |
fs = project.get_feature_store()
|
| 16 |
secrets = util.secrets_api(project.name)
|
| 17 |
+
location_str = secrets.get_secret("SENSOR_LOCATION_JSON").value
|
| 18 |
+
location = json.loads(location_str)
|
| 19 |
+
country=location['country']
|
| 20 |
+
city=location['city']
|
| 21 |
+
street=location['street']
|
| 22 |
|
| 23 |
AQI_API_KEY = secrets.get_secret("AQI_API_KEY").value
|
| 24 |
location_str = secrets.get_secret("SENSOR_LOCATION_JSON").value
|
|
|
|
| 31 |
version=1,
|
| 32 |
)
|
| 33 |
|
| 34 |
+
### Retreive model
|
| 35 |
|
| 36 |
mr = project.get_model_registry()
|
| 37 |
|
|
|
|
| 44 |
retrieved_xgboost_model = XGBRegressor()
|
| 45 |
retrieved_xgboost_model.load_model(saved_model_dir + "/model.json")
|
| 46 |
|
| 47 |
+
### Retrieve features
|
| 48 |
|
| 49 |
weather_fg = fs.get_feature_group(
|
| 50 |
name='weather',
|
|
|
|
| 53 |
|
| 54 |
today_timestamp = pd.to_datetime(today)
|
| 55 |
batch_data = weather_fg.filter(weather_fg.date >= today_timestamp ).read()
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
### Predict and upload
|
| 59 |
+
|
| 60 |
batch_data['predicted_pm25'] = retrieved_xgboost_model.predict(
|
| 61 |
batch_data[['temperature_2m_mean', 'precipitation_sum', 'wind_speed_10m_max', 'wind_direction_10m_dominant']])
|
| 62 |
+
|
| 63 |
+
batch_data['street'] = street
|
| 64 |
+
batch_data['city'] = city
|
| 65 |
+
batch_data['country'] = country
|
| 66 |
+
# Fill in the number of days before the date on which you made the forecast (base_date)
|
| 67 |
+
batch_data['days_before_forecast_day'] = range(1, len(batch_data)+1)
|
| 68 |
+
batch_data = batch_data.sort_values(by=['date'])
|
| 69 |
+
#batch_data['date'] = batch_data['date'].dt.tz_convert(None).astype('datetime64[ns]')
|
| 70 |
+
|
| 71 |
+
plt = util.plot_air_quality_forecast(city, street, batch_data, file_path="./img/pm25_forecast.png")
|
| 72 |
+
|
| 73 |
+
monitor_fg = fs.get_or_create_feature_group(
|
| 74 |
+
name='aq_predictions',
|
| 75 |
+
description='Air Quality prediction monitoring',
|
| 76 |
+
version=1,
|
| 77 |
+
primary_key=['city','street','date','days_before_forecast_day'],
|
| 78 |
+
event_time="date"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
print(f"Batch data: {batch_data}")
|
| 82 |
+
|
| 83 |
+
monitor_fg.insert(batch_data, write_options={"wait_for_job": True})
|
| 84 |
+
monitoring_df = monitor_fg.filter(monitor_fg.days_before_forecast_day == 1).read()
|
| 85 |
+
|
| 86 |
+
# Hindcast monitoring
|
| 87 |
+
|
| 88 |
+
air_quality_fg = fs.get_feature_group(
|
| 89 |
+
name='air_quality',
|
| 90 |
+
version=1,
|
| 91 |
+
)
|
| 92 |
+
air_quality_df = air_quality_fg.read()
|
| 93 |
+
|
| 94 |
+
outcome_df = air_quality_df[['date', 'pm25']]
|
| 95 |
+
preds_df = monitoring_df[['date', 'predicted_pm25']]
|
| 96 |
+
hindcast_df = pd.merge(preds_df, outcome_df, on="date")
|
| 97 |
+
hindcast_df = hindcast_df.sort_values(by=['date'])
|
| 98 |
+
|
| 99 |
+
if len(hindcast_df) == 0:
|
| 100 |
+
hindcast_df = util.backfill_predictions_for_monitoring(weather_fg, air_quality_df, monitor_fg, retrieved_xgboost_model)
|
| 101 |
+
|
| 102 |
+
plt = util.plot_air_quality_forecast(city, street, hindcast_df, file_path="./img/pm25_hindcast_1day.png", hindcast=True)
|
scheduler.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import modal
|
| 2 |
+
|
| 3 |
+
app = modal.App('scheduler')
|
| 4 |
+
|
| 5 |
+
@app.function(schedule=modal.Period(seconds=15))
|
| 6 |
+
def update():
|
| 7 |
+
print('Updating...')
|