NaokiOkamoto's picture
Update app.py
c9e326b
raw
history blame
No virus
3.53 kB
import pandas as pd
import numpy as np
import gradio as gr
import datetime
import calendar
import matplotlib.pyplot as plt
import japanize_matplotlib
import matplotlib.dates as mdates
from dateutil.relativedelta import relativedelta
import datetime
from func import get_fish_qty, get_estat, dr_prediction_deployment, prediction_func, train_modeling
import yaml
with open('config.yaml') as file:
config = yaml.safe_load(file.read())
def retrain():
get_prediction_result(retrain = True)
def get_prediction_result(retrain = False):
today = datetime.datetime.now()
if retrain:
train_modeling.modeling()
prediction_month = (today+relativedelta(months=1)).strftime('%Y%m')
month_days = month_days = [pd.to_datetime(prediction_month + str(i+1).zfill(2)) for i in range(calendar.monthrange((today+relativedelta(months=1)).year, (today+relativedelta(months=1)).month)[1])]
dfc = pd.DataFrame({'target_date':month_days})
df = prediction_func.prediction_to_dr(config['oil_price_url'], config['fuel_procurement_cost_url'])
df = df.loc[df['target_date'].astype(str).str[:6]==prediction_month]
df['target_date'] = pd.to_datetime(df['target_date'].astype(str))
df['forecast_point'] = pd.to_datetime(df['forecast_point'].astype(str))
df = pd.merge(dfc,
df,
on='target_date',
how='left')
df.loc[df['forecast_point'].isnull(), 'forecast_point'] = df['target_date'].apply(lambda x:x-relativedelta(months=1))
df = df.loc[~((df['target_date']<(today+relativedelta(months=1)))&(df['電気代'].isnull()))]
return df[['forecast_point', 'target_date', '電気代']]
def plot_prediction_result():
update = gr.LinePlot.update(
value=get_prediction_result(),
x="target_date",
y="電気代",
title="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移",
width=500,
height=400,
)
return update
# def plot_prediction_result():
# df = get_prediction_result()
# fig, ax1 = plt.subplots(1, 1, figsize=(30, 10))
# fig.autofmt_xdate()
# ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
# ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1))
# plt.rcParams["font.size"] = 14
# ax1.grid(True)
# ax1.set_xlim(df['target_date'].min(), df['target_date'].max())
# ax1.set_xlabel('target_date')
# ax1.set_ylabel('電気代(円)')
# ax1.plot(df['target_date'], df['電気代'])
# ax1.legend(loc="upper left", bbox_to_anchor=(1.0, 1.0))
return fig
with gr.Blocks() as electoric_ploting:
gr.Markdown("# 📈 Real-Time Line Plot")
with gr.Row():
with gr.Column():
plot = gr.LinePlot(show_label=False)
# plot = gr.Plot(label="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移")
with gr.Row():
gr.DataFrame(get_prediction_result)
with gr.Column():
btn= gr.Button(value="再学習")
btn.click(retrain, inputs=None, outputs=None)
# demo.load(make_plot, inputs=[button], outputs=[plot])
electoric_ploting.load(lambda: datetime.datetime.now(),
None,
# c_time2,
every=3600)
dep = electoric_ploting.load(plot_prediction_result, None, plot, every=3600)
electoric_ploting.queue().launch()
plt.close()