Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,59 +4,63 @@ import xgboost as xgb
|
|
4 |
from sklearn.model_selection import train_test_split
|
5 |
from sklearn.metrics import mean_squared_error
|
6 |
import gradio as gr
|
7 |
-
import os
|
8 |
-
import json
|
9 |
import datetime
|
|
|
10 |
|
11 |
# API Endpoints
|
12 |
WEATHER_API = "https://api.open-meteo.com/v1/forecast"
|
13 |
ELECTRICITY_PRICE_API = "https://www.elprisetjustnu.se/api/v1/prices"
|
14 |
ENERGY_CHARTS_API = "https://energy-charts.info/api/public_power"
|
15 |
|
16 |
-
# Fetch weather data
|
17 |
-
def fetch_weather_data():
|
|
|
|
|
|
|
18 |
params = {
|
19 |
"latitude": 59.3293, # Stockholm latitude
|
20 |
"longitude": 18.0686, # Stockholm longitude
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
|
|
24 |
}
|
25 |
response = requests.get(WEATHER_API, params=params)
|
26 |
response.raise_for_status()
|
27 |
-
|
28 |
-
|
29 |
-
return pd.DataFrame(hourly_data)
|
30 |
|
31 |
-
# Fetch electricity
|
32 |
def fetch_electricity_prices():
|
33 |
today = datetime.datetime.now().strftime('%Y/%m-%d')
|
34 |
-
url = f"{ELECTRICITY_PRICE_API}/{today}_SE3.json"
|
35 |
response = requests.get(url)
|
36 |
response.raise_for_status()
|
37 |
return pd.DataFrame(response.json())
|
38 |
|
39 |
# Fetch energy production data using Energy-Charts API
|
40 |
-
def fetch_energy_production_data():
|
|
|
|
|
|
|
41 |
params = {
|
42 |
"country": "se", # Sweden country code
|
43 |
-
"start":
|
44 |
-
"end":
|
45 |
}
|
46 |
response = requests.get(ENERGY_CHARTS_API, params=params)
|
47 |
response.raise_for_status()
|
48 |
data = response.json()
|
49 |
production_data = {
|
50 |
"unix_seconds": data["unix_seconds"],
|
51 |
-
|
52 |
}
|
53 |
-
|
54 |
-
return production_df
|
55 |
|
56 |
# Prepare the dataset
|
57 |
def prepare_dataset(weather_data, electricity_data, energy_data):
|
58 |
dataset = pd.concat([weather_data, electricity_data, energy_data], axis=1)
|
59 |
-
dataset = dataset.dropna()
|
60 |
return dataset
|
61 |
|
62 |
# Train the model
|
@@ -71,7 +75,8 @@ def train_model(dataset):
|
|
71 |
|
72 |
rmse = mean_squared_error(y_test, predictions, squared=False)
|
73 |
model.save_model("electricity_price_model.json")
|
74 |
-
|
|
|
75 |
|
76 |
# Load the model and make predictions
|
77 |
def predict_price(features):
|
@@ -101,6 +106,7 @@ def update_predictions():
|
|
101 |
predictions_output = dataset.copy()
|
102 |
predictions_output["predicted_price"] = predictions
|
103 |
predictions_output.to_json("predictions.json", orient="records")
|
|
|
104 |
|
105 |
# Gradio Interface
|
106 |
def gradio_interface():
|
@@ -144,8 +150,8 @@ if __name__ == "__main__":
|
|
144 |
|
145 |
# Prepare dataset and train the model
|
146 |
dataset = prepare_dataset(weather_data, electricity_data, energy_data)
|
147 |
-
|
148 |
-
print(f"Model trained with RMSE: {rmse}")
|
149 |
|
150 |
# Launch Gradio interface
|
151 |
gradio_interface()
|
|
|
|
4 |
from sklearn.model_selection import train_test_split
|
5 |
from sklearn.metrics import mean_squared_error
|
6 |
import gradio as gr
|
|
|
|
|
7 |
import datetime
|
8 |
+
import os
|
9 |
|
10 |
# API Endpoints
|
11 |
WEATHER_API = "https://api.open-meteo.com/v1/forecast"
|
12 |
ELECTRICITY_PRICE_API = "https://www.elprisetjustnu.se/api/v1/prices"
|
13 |
ENERGY_CHARTS_API = "https://energy-charts.info/api/public_power"
|
14 |
|
15 |
+
# Fetch historical weather data
|
16 |
+
def fetch_weather_data(start_date="2023-01-01", end_date=None):
|
17 |
+
if end_date is None:
|
18 |
+
end_date = datetime.datetime.now().strftime('%Y-%m-%d')
|
19 |
+
|
20 |
params = {
|
21 |
"latitude": 59.3293, # Stockholm latitude
|
22 |
"longitude": 18.0686, # Stockholm longitude
|
23 |
+
"daily": "temperature_2m_mean,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant",
|
24 |
+
"start_date": start_date,
|
25 |
+
"end_date": end_date,
|
26 |
+
"timezone": "Europe/Stockholm"
|
27 |
}
|
28 |
response = requests.get(WEATHER_API, params=params)
|
29 |
response.raise_for_status()
|
30 |
+
daily_data = response.json()["daily"]
|
31 |
+
return pd.DataFrame(daily_data)
|
|
|
32 |
|
33 |
+
# Fetch historical electricity prices
|
34 |
def fetch_electricity_prices():
|
35 |
today = datetime.datetime.now().strftime('%Y/%m-%d')
|
36 |
+
url = f"{ELECTRICITY_PRICE_API}/{today}_SE3.json"
|
37 |
response = requests.get(url)
|
38 |
response.raise_for_status()
|
39 |
return pd.DataFrame(response.json())
|
40 |
|
41 |
# Fetch energy production data using Energy-Charts API
|
42 |
+
def fetch_energy_production_data(start_date="2023-01-01", end_date=None):
|
43 |
+
if end_date is None:
|
44 |
+
end_date = datetime.datetime.now().strftime('%Y-%m-%d')
|
45 |
+
|
46 |
params = {
|
47 |
"country": "se", # Sweden country code
|
48 |
+
"start": start_date,
|
49 |
+
"end": end_date
|
50 |
}
|
51 |
response = requests.get(ENERGY_CHARTS_API, params=params)
|
52 |
response.raise_for_status()
|
53 |
data = response.json()
|
54 |
production_data = {
|
55 |
"unix_seconds": data["unix_seconds"],
|
56 |
+
**{ptype["name"]: ptype["data"] for ptype in data["production_types"]}
|
57 |
}
|
58 |
+
return pd.DataFrame(production_data)
|
|
|
59 |
|
60 |
# Prepare the dataset
|
61 |
def prepare_dataset(weather_data, electricity_data, energy_data):
|
62 |
dataset = pd.concat([weather_data, electricity_data, energy_data], axis=1)
|
63 |
+
dataset = dataset.dropna()
|
64 |
return dataset
|
65 |
|
66 |
# Train the model
|
|
|
75 |
|
76 |
rmse = mean_squared_error(y_test, predictions, squared=False)
|
77 |
model.save_model("electricity_price_model.json")
|
78 |
+
print(f"Model trained with RMSE: {rmse}")
|
79 |
+
return model
|
80 |
|
81 |
# Load the model and make predictions
|
82 |
def predict_price(features):
|
|
|
106 |
predictions_output = dataset.copy()
|
107 |
predictions_output["predicted_price"] = predictions
|
108 |
predictions_output.to_json("predictions.json", orient="records")
|
109 |
+
print("Predictions updated and saved.")
|
110 |
|
111 |
# Gradio Interface
|
112 |
def gradio_interface():
|
|
|
150 |
|
151 |
# Prepare dataset and train the model
|
152 |
dataset = prepare_dataset(weather_data, electricity_data, energy_data)
|
153 |
+
train_model(dataset)
|
|
|
154 |
|
155 |
# Launch Gradio interface
|
156 |
gradio_interface()
|
157 |
+
|