Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,31 +8,56 @@ from darts.dataprocessing.transformers import Scaler
|
|
8 |
from sklearn.preprocessing import LabelEncoder
|
9 |
import numpy as np
|
10 |
import io
|
|
|
11 |
|
12 |
# ----------------------------
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# ----------------------------
|
15 |
-
df = pd.read_csv("dataset.csv") # <-- replace with your dataset file
|
16 |
df['datetime'] = pd.to_datetime(df['datetime'])
|
17 |
df = df.sort_values("datetime")
|
18 |
|
19 |
-
# Encode weather icons
|
20 |
encoder = LabelEncoder()
|
21 |
-
|
|
|
22 |
|
23 |
-
#
|
24 |
-
# Preprocessing for Forecasting
|
25 |
-
# ----------------------------
|
26 |
series = TimeSeries.from_dataframe(df, "datetime", "pv_output_kWh")
|
27 |
scaler = Scaler()
|
28 |
series_scaled = scaler.fit_transform(series)
|
29 |
|
30 |
-
# Pre-trained
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
if model is None:
|
35 |
-
# fallback: train small model (for demo)
|
36 |
model = NBEATSModel(input_chunk_length=30, output_chunk_length=7, n_epochs=10)
|
37 |
model.fit(series_scaled)
|
38 |
|
@@ -78,7 +103,7 @@ def forecast_pv(horizon, weather_condition):
|
|
78 |
forecast = model.predict(steps)
|
79 |
forecast = scaler.inverse_transform(forecast)
|
80 |
|
81 |
-
#
|
82 |
adjustment = {
|
83 |
"Clear": 1.0,
|
84 |
"Partly Cloudy": 0.85,
|
@@ -91,11 +116,11 @@ def forecast_pv(horizon, weather_condition):
|
|
91 |
adj_factor = adjustment.get(weather_condition, 1.0)
|
92 |
forecast_adj = forecast * adj_factor
|
93 |
|
94 |
-
# Plot
|
95 |
plt.figure(figsize=(10,4))
|
96 |
-
series[-7*24:].plot(label="History") # last
|
97 |
forecast.plot(label="Forecast (Base)")
|
98 |
-
forecast_adj.plot(label=f"Forecast (Adjusted
|
99 |
plt.legend()
|
100 |
plt.title(f"PV Forecast for {horizon}")
|
101 |
plt.tight_layout()
|
@@ -108,7 +133,7 @@ def forecast_pv(horizon, weather_condition):
|
|
108 |
return plt.gcf(), peak_info
|
109 |
|
110 |
# ----------------------------
|
111 |
-
# GRADIO
|
112 |
# ----------------------------
|
113 |
eda_tab = gr.TabbedInterface(
|
114 |
[
|
|
|
8 |
from sklearn.preprocessing import LabelEncoder
|
9 |
import numpy as np
|
10 |
import io
|
11 |
+
import os
|
12 |
|
13 |
# ----------------------------
|
14 |
+
# SAFE DATASET LOADER
|
15 |
+
# ----------------------------
|
16 |
+
def load_dataset(path="dataset.csv", url=None):
|
17 |
+
try:
|
18 |
+
# Try UTF-8
|
19 |
+
return pd.read_csv(path, encoding="utf-8")
|
20 |
+
except UnicodeDecodeError:
|
21 |
+
try:
|
22 |
+
# Fallback to Latin1
|
23 |
+
return pd.read_csv(path, encoding="latin1")
|
24 |
+
except Exception as e:
|
25 |
+
if url:
|
26 |
+
# If file missing, try URL
|
27 |
+
try:
|
28 |
+
return pd.read_csv(url, encoding="utf-8")
|
29 |
+
except UnicodeDecodeError:
|
30 |
+
return pd.read_csv(url, encoding="latin1")
|
31 |
+
else:
|
32 |
+
raise e
|
33 |
+
|
34 |
+
# Path or fallback URL
|
35 |
+
url = "https://raw.githubusercontent.com/yourusername/yourrepo/main/dataset.csv"
|
36 |
+
if os.path.exists("dataset.csv"):
|
37 |
+
df = load_dataset("dataset.csv")
|
38 |
+
else:
|
39 |
+
df = load_dataset(url=url)
|
40 |
+
|
41 |
+
# ----------------------------
|
42 |
+
# Preprocessing
|
43 |
# ----------------------------
|
|
|
44 |
df['datetime'] = pd.to_datetime(df['datetime'])
|
45 |
df = df.sort_values("datetime")
|
46 |
|
47 |
+
# Encode weather icons
|
48 |
encoder = LabelEncoder()
|
49 |
+
if "icon" in df.columns:
|
50 |
+
df['icon_encoded'] = encoder.fit_transform(df['icon'])
|
51 |
|
52 |
+
# Create timeseries
|
|
|
|
|
53 |
series = TimeSeries.from_dataframe(df, "datetime", "pv_output_kWh")
|
54 |
scaler = Scaler()
|
55 |
series_scaled = scaler.fit_transform(series)
|
56 |
|
57 |
+
# Pre-trained or fallback model
|
58 |
+
try:
|
59 |
+
model = TFTModel.load_from_checkpoint("tft_pretrained", work_dir="./")
|
60 |
+
except Exception:
|
|
|
|
|
61 |
model = NBEATSModel(input_chunk_length=30, output_chunk_length=7, n_epochs=10)
|
62 |
model.fit(series_scaled)
|
63 |
|
|
|
103 |
forecast = model.predict(steps)
|
104 |
forecast = scaler.inverse_transform(forecast)
|
105 |
|
106 |
+
# Weather impact adjustment
|
107 |
adjustment = {
|
108 |
"Clear": 1.0,
|
109 |
"Partly Cloudy": 0.85,
|
|
|
116 |
adj_factor = adjustment.get(weather_condition, 1.0)
|
117 |
forecast_adj = forecast * adj_factor
|
118 |
|
119 |
+
# Plot
|
120 |
plt.figure(figsize=(10,4))
|
121 |
+
series[-7*24:].plot(label="History") # last week history
|
122 |
forecast.plot(label="Forecast (Base)")
|
123 |
+
forecast_adj.plot(label=f"Forecast (Adjusted: {weather_condition})")
|
124 |
plt.legend()
|
125 |
plt.title(f"PV Forecast for {horizon}")
|
126 |
plt.tight_layout()
|
|
|
133 |
return plt.gcf(), peak_info
|
134 |
|
135 |
# ----------------------------
|
136 |
+
# GRADIO DASHBOARD
|
137 |
# ----------------------------
|
138 |
eda_tab = gr.TabbedInterface(
|
139 |
[
|