Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Importierung Pakete
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd #Verarbeitung excel/csv
|
4 |
+
|
5 |
+
from darts.models import LinearRegressionModel #Importierung Klasse LinearRegressionModel von darts
|
6 |
+
from darts import TimeSeries #In darts wird für Datensätze das TimeSeries Format verwendet
|
7 |
+
|
8 |
+
from sklearn.metrics import mean_absolute_error, r2_score
|
9 |
+
|
10 |
+
import os
|
11 |
+
import plotly.express as px #Erstellung von Diagrammen (dynamisch --> bspw. zoom)
|
12 |
+
|
13 |
+
# Funktion, um die hochgeladene Datei zu lesen
|
14 |
+
def upload_excel(file):
|
15 |
+
if file is not None:
|
16 |
+
# Excel-Datei lesen
|
17 |
+
df = pd.read_excel(file.name, index_col='time')
|
18 |
+
return df
|
19 |
+
|
20 |
+
# Funktion, um Checkbox-Optionen automatisch zu erstellen
|
21 |
+
def create_feature_checkbox(df):
|
22 |
+
if df is not None:
|
23 |
+
new_choices = list(df.columns)
|
24 |
+
return gr.update(choices=new_choices)
|
25 |
+
else:
|
26 |
+
return gr.update(choices=[])
|
27 |
+
|
28 |
+
# Funktion, um Trainingsstartdatum automatisch zu laden
|
29 |
+
def fill_start_date(df):
|
30 |
+
if df is not None:
|
31 |
+
start_date = str(df.index.min())
|
32 |
+
return gr.update(value=start_date)
|
33 |
+
else:
|
34 |
+
return gr.update(value='')
|
35 |
+
|
36 |
+
# Funktion, um Teststartdatum automatisch zu laden
|
37 |
+
def fill_end_date(df):
|
38 |
+
if df is not None:
|
39 |
+
end_date = str(df.index.max())
|
40 |
+
return gr.update(value=end_date)
|
41 |
+
else:
|
42 |
+
return gr.update(value='')
|
43 |
+
|
44 |
+
#Funktion, läd die excel Datei hoch und führt außerdem die Funktionen create_feature_checkbox, fill_start_date, fill_end_date
|
45 |
+
def process_excel(file):
|
46 |
+
df = upload_excel(file)
|
47 |
+
return df, create_feature_checkbox(df), create_feature_checkbox(df), fill_start_date(df), fill_end_date(df) #create_feature_checkbox steht zwei mal drinnen wegen target und covariate
|
48 |
+
|
49 |
+
#Funktion training und training/test-split
|
50 |
+
def data_split_and_training(df,start_train,end_train,start_test,end_test,target,covariates,lags_target,lags_covariates):
|
51 |
+
#Aufteilen in Train Test
|
52 |
+
train_df = df[start_train:end_train]
|
53 |
+
test_df = df[start_test:end_test]
|
54 |
+
|
55 |
+
#Umwandeln in darts.TimeSeries
|
56 |
+
train = TimeSeries.from_dataframe(train_df)
|
57 |
+
test = TimeSeries.from_dataframe(test_df)
|
58 |
+
|
59 |
+
#Lags Eingabe in Liste umwandeln für Darts Bibliothek
|
60 |
+
lags_target_list = list(range(-1,-lags_target-1,-1))
|
61 |
+
lags_covariates_list = list(range(-1,-lags_covariates-1,-1))
|
62 |
+
|
63 |
+
#model instantiation
|
64 |
+
model = LinearRegressionModel(lags=lags_target_list, lags_future_covariates=lags_covariates_list)
|
65 |
+
|
66 |
+
#model training
|
67 |
+
model_fit = model.fit(train[target], future_covariates= train[covariates])
|
68 |
+
return train, test, model_fit, f'Training is complete'
|
69 |
+
|
70 |
+
# Funktion zur Speicherung des trainierten Modells
|
71 |
+
def create_file(state_value):
|
72 |
+
file_path = "model_fit.pt"
|
73 |
+
with open(file_path, "w") as f:
|
74 |
+
f.write(f"State Value: {state_value}")
|
75 |
+
return file_path
|
76 |
+
|
77 |
+
# Funktion für Prediction
|
78 |
+
def test(model_fit,test,train,covariates):
|
79 |
+
#prediction
|
80 |
+
prediction = model_fit.predict(n=len(test), future_covariates= train.append(test)[covariates])
|
81 |
+
|
82 |
+
#converting to pandas
|
83 |
+
df_prediction = prediction.pd_dataframe()
|
84 |
+
|
85 |
+
# Visualisierung mit Plotly
|
86 |
+
fig = px.line()
|
87 |
+
fig.add_scatter(x=df_prediction.index, y=test.pd_dataframe()[df_prediction.columns[0]], mode='lines', name='test', line_color='red')
|
88 |
+
fig.add_scatter(x=df_prediction.index, y=df_prediction[df_prediction.columns[0]], mode='lines', name='predict', line_color='blue')
|
89 |
+
fig.update_yaxes(title_text=df_prediction.columns[0])
|
90 |
+
|
91 |
+
#check metrics
|
92 |
+
target_variable = []
|
93 |
+
MAE = []
|
94 |
+
R2 = []
|
95 |
+
for i in df_prediction.columns:
|
96 |
+
target_variable.append(i)
|
97 |
+
MAE.append(mean_absolute_error(df_prediction[i], test.pd_dataframe()[i]))
|
98 |
+
R2.append(r2_score(df_prediction[i], test.pd_dataframe()[i]))
|
99 |
+
metrics = pd.DataFrame({'target':target_variable,'MAE': MAE, 'R2': R2})
|
100 |
+
return metrics, fig
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# Gradio-Interface erstellen
|
105 |
+
with gr.Blocks() as demo:
|
106 |
+
gr.Markdown("## ML-based Building Simulation") #Überschrift
|
107 |
+
|
108 |
+
# Datei-Upload-Komponente
|
109 |
+
file_input = gr.File(label="Drop an excel file", file_types=[".xls", ".xlsx"]) #Hochladebereich
|
110 |
+
|
111 |
+
#Upload Button
|
112 |
+
upload_button = gr.Button("Upload Excel") #Button für Funktion proceed_excel()
|
113 |
+
|
114 |
+
#Feature checkboxen erstellen
|
115 |
+
checkbox_group_target = gr.CheckboxGroup(label="Select target variable", choices=[], interactive=True) #Auswahl target variable
|
116 |
+
checkbox_group_covariate = gr.CheckboxGroup(label="Select exogenous variables", choices=[], interactive=True) #Auswahl covariate variables
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
# Eingabe Teilung Training und Testdaten
|
120 |
+
start_train = gr.Textbox(label='start_train:', lines=1, interactive=True)
|
121 |
+
end_train = gr.Textbox(label='end_train:', lines=1, interactive=True, value = '2019-06-04 02:15:00')
|
122 |
+
start_test = gr.Textbox(label='start_test:', lines=1, interactive=True, value = '2019-06-04 02:30:00')
|
123 |
+
end_test = gr.Textbox(label='end_test:', lines=1, interactive=True)
|
124 |
+
|
125 |
+
# Eingabe Verzögerung
|
126 |
+
with gr.Row():
|
127 |
+
lags_target = gr.Number(value=1, label='Number of laged values for target', interactive=True)
|
128 |
+
lags_covariates = gr.Number(value=1, label='Number of laged values for exogenous variables', interactive=True)
|
129 |
+
|
130 |
+
# DataFrame df wird als Zustand gespeichert, damit er in in anderen Funktionen weiter verwendet werden kann
|
131 |
+
df_state = gr.State(None)
|
132 |
+
train_state = gr.State(None)
|
133 |
+
test_state = gr.State(None)
|
134 |
+
model_fit_state = gr.State(None)
|
135 |
+
|
136 |
+
#Training Button
|
137 |
+
training_button = gr.Button("Training") #Button für Funktion data_split_and_training()
|
138 |
+
|
139 |
+
with gr.Row():
|
140 |
+
#Training Status
|
141 |
+
status_output = gr.Textbox(label="Training status")
|
142 |
+
with gr.Column():
|
143 |
+
#Download Button
|
144 |
+
download_button = gr.DownloadButton("Download model")
|
145 |
+
test_button = gr.Button("Test")
|
146 |
+
|
147 |
+
# Tabellenausgabe Metrik
|
148 |
+
metrics_table = gr.DataFrame(label="Metrics", headers=['target','MAE','R2'])
|
149 |
+
|
150 |
+
# Plotausgabe Vorhersagen
|
151 |
+
plot = gr.Plot()
|
152 |
+
|
153 |
+
# Event-Verknüpfungen
|
154 |
+
upload_button.click(process_excel, inputs=file_input, outputs=[df_state, checkbox_group_target, checkbox_group_covariate, start_train, end_test])
|
155 |
+
training_button.click(data_split_and_training,
|
156 |
+
inputs=[df_state, start_train, end_train, start_test, end_test, checkbox_group_target, checkbox_group_covariate, lags_target, lags_covariates],
|
157 |
+
outputs=[train_state, test_state, model_fit_state, status_output])
|
158 |
+
download_button.click(create_file,inputs=[model_fit_state],outputs=[download_button])
|
159 |
+
test_button.click(test,inputs=[model_fit_state,test_state,train_state,checkbox_group_covariate],outputs=[metrics_table,plot])
|
160 |
+
|
161 |
+
# Demo starten
|
162 |
+
demo.launch(show_error=True)
|