|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
with gr.Row(): |
|
gr.DataFrame(get_prediction_result) |
|
|
|
with gr.Column(): |
|
btn= gr.Button(value="再学習") |
|
btn.click(retrain, inputs=None, outputs=None) |
|
|
|
|
|
|
|
|
|
electoric_ploting.load(lambda: datetime.datetime.now(), |
|
None, |
|
|
|
every=3600) |
|
dep = electoric_ploting.load(plot_prediction_result, None, plot, every=3600) |
|
|
|
electoric_ploting.queue().launch() |
|
|
|
plt.close() |