eforse01 commited on
Commit
0fe02ae
·
verified ·
1 Parent(s): c6ce0ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -19
app.py CHANGED
@@ -5,11 +5,13 @@ 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
 
9
  # API Endpoints
10
  WEATHER_API = "https://api.open-meteo.com/v1/forecast"
11
  ELECTRICITY_PRICE_API = "https://www.elprisetjustnu.se/api/v1/prices"
12
- ENERGY_PRODUCTION_API = "https://transparency.entsoe.eu/api"
13
 
14
  # Fetch weather data
15
  def fetch_weather_data():
@@ -26,16 +28,28 @@ def fetch_weather_data():
26
 
27
  # Fetch electricity price data
28
  def fetch_electricity_prices():
29
- response = requests.get(ELECTRICITY_PRICE_API + "/json/sv")
 
 
30
  response.raise_for_status()
31
  return pd.DataFrame(response.json())
32
 
33
- # Fetch energy production prices
34
  def fetch_energy_production_data():
35
- # Placeholder for actual API parameters
36
- response = requests.get(ENERGY_PRODUCTION_API, params={})
 
 
 
 
37
  response.raise_for_status()
38
- return pd.DataFrame(response.json())
 
 
 
 
 
 
39
 
40
  # Prepare the dataset
41
  def prepare_dataset(weather_data, electricity_data, energy_data):
@@ -64,6 +78,28 @@ def predict_price(features):
64
  prediction = model.predict(pd.DataFrame([features]))
65
  return prediction[0]
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  # Gradio Interface
68
  def gradio_interface():
69
  def wrapper(temp, precip, wind_speed, humidity, energy_price, electricity_price):
@@ -95,16 +131,19 @@ def gradio_interface():
95
  interface.launch()
96
 
97
  if __name__ == "__main__":
98
- # Fetch data
99
- weather_data = fetch_weather_data()
100
- electricity_data = fetch_electricity_prices()
101
- energy_data = fetch_energy_production_data()
102
-
103
- # Prepare dataset and train the model
104
- dataset = prepare_dataset(weather_data, electricity_data, energy_data)
105
- rmse = train_model(dataset)
106
- print(f"Model trained with RMSE: {rmse}")
107
-
108
- # Launch Gradio interface
109
- gradio_interface()
110
-
 
 
 
 
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():
 
28
 
29
  # Fetch electricity price data
30
  def fetch_electricity_prices():
31
+ today = datetime.datetime.now().strftime('%Y/%m-%d')
32
+ url = f"{ELECTRICITY_PRICE_API}/{today}_SE3.json" # Replace 'SE3' with the desired price category
33
+ response = requests.get(url)
34
  response.raise_for_status()
35
  return pd.DataFrame(response.json())
36
 
37
+ # Fetch energy production data using Energy-Charts API
38
  def fetch_energy_production_data():
39
+ params = {
40
+ "country": "se", # Sweden country code
41
+ "start": "2023-01-01",
42
+ "end": "2023-12-31"
43
+ }
44
+ response = requests.get(ENERGY_CHARTS_API, params=params)
45
  response.raise_for_status()
46
+ data = response.json()
47
+ production_data = {
48
+ "unix_seconds": data["unix_seconds"],
49
+ "production": {ptype["name"]: ptype["data"] for ptype in data["production_types"]}
50
+ }
51
+ production_df = pd.DataFrame(production_data)
52
+ return production_df
53
 
54
  # Prepare the dataset
55
  def prepare_dataset(weather_data, electricity_data, energy_data):
 
78
  prediction = model.predict(pd.DataFrame([features]))
79
  return prediction[0]
80
 
81
+ # Update predictions and save to file
82
+ def update_predictions():
83
+ # Fetch the latest data
84
+ weather_data = fetch_weather_data()
85
+ electricity_data = fetch_electricity_prices()
86
+ energy_data = fetch_energy_production_data()
87
+
88
+ # Prepare dataset
89
+ dataset = prepare_dataset(weather_data, electricity_data, energy_data)
90
+
91
+ # Load the model
92
+ model = xgb.XGBRegressor()
93
+ model.load_model("electricity_price_model.json")
94
+
95
+ # Generate predictions
96
+ predictions = model.predict(dataset.drop("price", axis=1))
97
+
98
+ # Save predictions to file
99
+ predictions_output = dataset.copy()
100
+ predictions_output["predicted_price"] = predictions
101
+ predictions_output.to_json("predictions.json", orient="records")
102
+
103
  # Gradio Interface
104
  def gradio_interface():
105
  def wrapper(temp, precip, wind_speed, humidity, energy_price, electricity_price):
 
131
  interface.launch()
132
 
133
  if __name__ == "__main__":
134
+ import sys
135
+ if len(sys.argv) > 1 and sys.argv[1] == "update":
136
+ update_predictions()
137
+ else:
138
+ # Fetch data
139
+ weather_data = fetch_weather_data()
140
+ electricity_data = fetch_electricity_prices()
141
+ energy_data = fetch_energy_production_data()
142
+
143
+ # Prepare dataset and train the model
144
+ dataset = prepare_dataset(weather_data, electricity_data, energy_data)
145
+ rmse = train_model(dataset)
146
+ print(f"Model trained with RMSE: {rmse}")
147
+
148
+ # Launch Gradio interface
149
+ gradio_interface()