File size: 1,916 Bytes
ee3256e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar

import hopsworks
import joblib
import datetime
import os
import requests

project = hopsworks.login()
fs = project.get_feature_store()

mr = project.get_model_registry()
model = mr.get_model("ny_elec_model", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/ny_elec_model.pkl")


def predict():
    today = get_date()
    temp = get_temp(today)
    df = pd.DataFrame({"date": [today], "temperature": [temp]})
    df['date'] = pd.to_datetime(df['date'], infer_datetime_format=True)
    df['day'] = df['date'].dt.dayofweek
    df['month'] = df['date'].dt.month
    holidays = calendar().holidays(start=df['date'].min(), end=df['date'].max())
    df['holiday'] = df['date'].isin(holidays).astype(int)

    demand = model.predict(df.drop(columns=['date']))[0]
    return [today, temp, demand]


def get_date():
    today = datetime.datetime.today()
    return today.date()


def get_temp(date):
    weather_api_key = os.environ.get('WEATHER_API_KEY')
    weather_url = ('http://api.weatherapi.com/v1/history.json'
                   '?key={}'
                   '&q=New%20York,%20USA'
                   '&dt={}').format(weather_api_key, date)
    return requests.get(weather_url).json()['forecast']['forecastday'][0]['day']['avgtemp_c']


demo = gr.Interface(
    fn = predict,
    title = "NY Electricity Demand Prediction",
    description ="Daily NY Electricity Demand Prediction",
    allow_flagging = "never",
    inputs = [],
    outputs = [
        gr.Textbox(label="Date"),
        gr.Textbox(label="Temperature forecast [℃]"),
        gr.Textbox(label="Predicted demand [MWh]"),
    ]
)

# TODO: we have only the demand predictions for two days ago, so we have two options
#  - skip EIA demand forecast (no comparison)
#  - show prediction for two days ago
demo.launch()