Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +149 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Huggingface_Prototype.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1i--A21QuJPKdv-HM2kUrFSwj89Qfv4cb
|
8 |
+
"""
|
9 |
+
|
10 |
+
#! mkdir ~/.kaggle
|
11 |
+
#! cp kaggle.json ~/.kaggle/
|
12 |
+
#! chmod 600 ~/.kaggle/kaggle.json
|
13 |
+
|
14 |
+
#!kaggle datasets download -d sercandikici/merged-dataset-electricty-weather-for-modelling
|
15 |
+
#! unzip merged-dataset-electricty-weather-for-modelling.zip
|
16 |
+
|
17 |
+
pip install gradio
|
18 |
+
|
19 |
+
from google.colab import files
|
20 |
+
uploaded = files.upload()
|
21 |
+
|
22 |
+
#df = pd.read_csv("merged_data.csv")
|
23 |
+
#df.drop('is_holiday', axis=1, inplace=True)
|
24 |
+
#df.to_csv('merged_data_huggingface.csv', index=False)
|
25 |
+
#from google.colab import files
|
26 |
+
#files.download('merged_data_huggingface.csv')
|
27 |
+
|
28 |
+
from prophet import Prophet
|
29 |
+
import numpy as np
|
30 |
+
import pandas as pd
|
31 |
+
import matplotlib.pyplot as plt
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
def forecast_plot(forecast_days,test_days, days):
|
35 |
+
|
36 |
+
fig, ax = plt.subplots(figsize=(14, 4))
|
37 |
+
ax.plot(forecast_days['ds'], forecast_days['yhat'], label='Forecast', color='green')
|
38 |
+
ax.scatter(test_days['ds'], test_days['y'], label='Actual', color='orange')
|
39 |
+
ax.set_xlabel('Date')
|
40 |
+
ax.set_ylabel('MGW')
|
41 |
+
plt.title(f'Prophet Forecast - Model 3 - {days} days horizon')
|
42 |
+
plt.legend()
|
43 |
+
|
44 |
+
return fig
|
45 |
+
|
46 |
+
def mean_absolute_percentage_error(y_true, y_pred):
|
47 |
+
y_true, y_pred = np.array(y_true), np.array(y_pred)
|
48 |
+
mape = np.mean(np.abs((y_true - y_pred) / y_true))
|
49 |
+
return mape
|
50 |
+
|
51 |
+
def root_mean_squared_error(y_true, y_pred):
|
52 |
+
y_true, y_pred = np.array(y_true), np.array(y_pred)
|
53 |
+
mse = np.mean((y_true - y_pred) ** 2)
|
54 |
+
rmse = np.sqrt(mse)
|
55 |
+
return rmse
|
56 |
+
|
57 |
+
def r_squared(y_true, y_pred):
|
58 |
+
y_true, y_pred = np.array(y_true), np.array(y_pred)
|
59 |
+
mean_y_true = np.mean(y_true)
|
60 |
+
ss_total = np.sum((y_true - mean_y_true) ** 2)
|
61 |
+
ss_residual = np.sum((y_true - y_pred) ** 2)
|
62 |
+
r2 = 1 - (ss_residual / ss_total)
|
63 |
+
return r2
|
64 |
+
|
65 |
+
def predict_and_evaluate(csv_file, days_to_predict,freq, country_name):
|
66 |
+
|
67 |
+
df_model = pd.read_csv(csv_file)
|
68 |
+
df_model.columns = ["ds", "y", "temp"]
|
69 |
+
df_model['ds'] = pd.to_datetime(df_model['ds'])
|
70 |
+
|
71 |
+
|
72 |
+
split_from = 90 * 12
|
73 |
+
train_data = df_model[:-split_from]
|
74 |
+
test_data = df_model[-split_from:]
|
75 |
+
freq = freq
|
76 |
+
seasonality_prior_scale = 0.01
|
77 |
+
changepoint_prior_scale = 0.05
|
78 |
+
mcmc_samples = 50
|
79 |
+
periods = days_to_predict * 12
|
80 |
+
|
81 |
+
m = Prophet(mcmc_samples=mcmc_samples, changepoint_prior_scale=changepoint_prior_scale,
|
82 |
+
seasonality_prior_scale=seasonality_prior_scale)
|
83 |
+
m.add_country_holidays(country_name=country_name)
|
84 |
+
m.add_regressor("temp", mode="additive")
|
85 |
+
m.fit(train_data)
|
86 |
+
|
87 |
+
future = m.make_future_dataframe(periods=periods, freq=freq)
|
88 |
+
train_idx = future["ds"].isin(train_data.ds)
|
89 |
+
test_idx = ~train_idx
|
90 |
+
|
91 |
+
reg = ["temp"]
|
92 |
+
for r in reg:
|
93 |
+
future.loc[train_idx, r] = train_data[r].to_list()
|
94 |
+
for r in reg:
|
95 |
+
future.loc[test_idx, r] = test_data.iloc[:periods][r].to_list()
|
96 |
+
|
97 |
+
forecast = m.predict(future)
|
98 |
+
forecast_days = forecast[forecast["ds"] >= test_data["ds"].iloc[0]]
|
99 |
+
test_days = test_data[(test_data["ds"] >= test_data["ds"].iloc[0]) & (
|
100 |
+
test_data["ds"] <= forecast_days["ds"].iloc[-1])]
|
101 |
+
|
102 |
+
plot = forecast_plot(forecast_days, test_days, days_to_predict)
|
103 |
+
|
104 |
+
mape = mean_absolute_percentage_error(test_days["y"], forecast_days["yhat"])
|
105 |
+
rmse = root_mean_squared_error(test_days["y"], forecast_days["yhat"])
|
106 |
+
rsqr = r_squared(test_days["y"], forecast_days["yhat"])
|
107 |
+
|
108 |
+
metrics = {
|
109 |
+
"MAPE": round(mape,3),
|
110 |
+
"RMSE": round(rmse,1),
|
111 |
+
"R-squared": round(rsqr,3)
|
112 |
+
}
|
113 |
+
|
114 |
+
return metrics,plot
|
115 |
+
|
116 |
+
csv_name = "merged_data_huggingface.csv"
|
117 |
+
#df_merged['settlement_date'] = pd.to_datetime(df_merged['settlement_date'])
|
118 |
+
#df_model = df_merged[["tsd", "settlement_date", "temp"]]
|
119 |
+
#df_model.columns = ["y", "ds", "temp"]
|
120 |
+
|
121 |
+
days_to_predict = 15 # Set the default value for days to predict
|
122 |
+
country_name = "UK" # Set the default value for country to predict
|
123 |
+
freq = "2H" # Set the default value for country to predict
|
124 |
+
|
125 |
+
predict_and_evaluate(csv_name, days_to_predict, freq, country_name)
|
126 |
+
|
127 |
+
iface = gr.Interface(
|
128 |
+
fn=predict_and_evaluate,
|
129 |
+
inputs=[
|
130 |
+
gr.File(label="CSV File"),
|
131 |
+
gr.Slider(1, 90, value=30, step=1, label="Days to Predict"),
|
132 |
+
gr.Textbox(label="Data Frequency", placeholder="Enter frequency (e.g., 2H for 2 hourly)"),
|
133 |
+
gr.Textbox(label="Country Code", placeholder="Enter country code (e.g., UK)")
|
134 |
+
],
|
135 |
+
outputs=[
|
136 |
+
gr.Textbox(label=" Evaluation Metrics"),
|
137 |
+
"plot"
|
138 |
+
|
139 |
+
],
|
140 |
+
title="Prophet Electricty Load Forecasting Model",
|
141 |
+
description="Upload a CSV file of time series data to generate electricty demand forecasts using Prophet. Update country code(eg UK or DE) for holidays and data frequency",
|
142 |
+
|
143 |
+
|
144 |
+
examples=[
|
145 |
+
["merged_data_huggingface.csv", 30, "2H", "UK"]
|
146 |
+
]
|
147 |
+
)
|
148 |
+
|
149 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
prophet
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
matplotlib
|