Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
+
import xgboost as xgb
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
import os
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Scheduler
|
| 13 |
+
scheduler = BackgroundScheduler()
|
| 14 |
+
scheduler.start()
|
| 15 |
+
|
| 16 |
+
# Load the trained model
|
| 17 |
+
model = xgb.XGBRegressor()
|
| 18 |
+
model.load_model("electricity_price_model.json")
|
| 19 |
+
|
| 20 |
+
# Constants
|
| 21 |
+
WEATHER_API = "https://api.open-meteo.com/v1/forecast"
|
| 22 |
+
ELECTRICITY_PRICE_API = "https://www.elprisetjustnu.se/api/v1/prices"
|
| 23 |
+
ENERGY_CHARTS_API = "https://api.energy-charts.info/public_power?"
|
| 24 |
+
PREDICTIONS_FILE = "predicted_prices.csv"
|
| 25 |
+
|
| 26 |
+
# Fetch weather data for the next day
|
| 27 |
+
def fetch_weather_data_for_tomorrow():
|
| 28 |
+
tomorrow = (datetime.now() + timedelta(days=1)).strftime('%Y-%m-%d')
|
| 29 |
+
params = {
|
| 30 |
+
"latitude": 59.3293,
|
| 31 |
+
"longitude": 18.0686,
|
| 32 |
+
"daily": "temperature_2m_mean,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant",
|
| 33 |
+
"start_date": tomorrow,
|
| 34 |
+
"end_date": tomorrow,
|
| 35 |
+
"timezone": "Europe/Stockholm"
|
| 36 |
+
}
|
| 37 |
+
response = requests.get(WEATHER_API, params=params)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
data = response.json()["daily"]
|
| 40 |
+
return pd.DataFrame(data)
|
| 41 |
+
|
| 42 |
+
# Fetch energy production data for the current day
|
| 43 |
+
def fetch_energy_production_data():
|
| 44 |
+
today = datetime.now().strftime('%Y-%m-%d')
|
| 45 |
+
params = {"country": "se", "start": today, "end": today}
|
| 46 |
+
response = requests.get(ENERGY_CHARTS_API, params=params)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
data = response.json()
|
| 49 |
+
|
| 50 |
+
if "production_types" in data:
|
| 51 |
+
production_data = {
|
| 52 |
+
"unix_seconds": data["unix_seconds"],
|
| 53 |
+
**{ptype["name"]: ptype["data"] for ptype in data["production_types"]}
|
| 54 |
+
}
|
| 55 |
+
energy_df = pd.DataFrame(production_data)
|
| 56 |
+
energy_df = energy_df.rename(columns={"unix_seconds": "time"})
|
| 57 |
+
energy_df["time"] = pd.to_datetime(energy_df["time"], unit="s", errors="coerce").dt.tz_localize(None)
|
| 58 |
+
return energy_df
|
| 59 |
+
else:
|
| 60 |
+
return pd.DataFrame()
|
| 61 |
+
|
| 62 |
+
# Fetch electricity prices for the current day
|
| 63 |
+
def fetch_current_electricity_prices():
|
| 64 |
+
today = datetime.now().strftime('%Y/%m-%d')
|
| 65 |
+
url = f"{ELECTRICITY_PRICE_API}/{today}_SE3.json"
|
| 66 |
+
response = requests.get(url)
|
| 67 |
+
response.raise_for_status()
|
| 68 |
+
data = response.json()
|
| 69 |
+
|
| 70 |
+
electricity_df = pd.DataFrame(data)
|
| 71 |
+
electricity_df = electricity_df.rename(columns={"time_start": "time"})
|
| 72 |
+
electricity_df["time"] = pd.to_datetime(electricity_df["time"], errors="coerce").dt.tz_localize(None)
|
| 73 |
+
return electricity_df
|
| 74 |
+
|
| 75 |
+
# Prepare dataset for prediction
|
| 76 |
+
def prepare_prediction_data():
|
| 77 |
+
energy_data = fetch_energy_production_data()
|
| 78 |
+
electricity_data = fetch_current_electricity_prices()
|
| 79 |
+
weather_data = fetch_weather_data_for_tomorrow()
|
| 80 |
+
|
| 81 |
+
dataset = pd.merge(energy_data, electricity_data, on="time", how="inner")
|
| 82 |
+
dataset = pd.merge(dataset, weather_data, on="time", how="outer")
|
| 83 |
+
dataset = dataset.dropna()
|
| 84 |
+
return dataset
|
| 85 |
+
|
| 86 |
+
# Predict electricity prices for the next day
|
| 87 |
+
def predict_next_day_price():
|
| 88 |
+
dataset = prepare_prediction_data()
|
| 89 |
+
X = dataset.drop(["SEK_per_kWh", "time"], axis=1, errors="ignore")
|
| 90 |
+
predictions = model.predict(X)
|
| 91 |
+
dataset["predicted_price"] = predictions
|
| 92 |
+
dataset.to_csv(PREDICTIONS_FILE, index=False)
|
| 93 |
+
generate_dashboard(dataset)
|
| 94 |
+
print("Predictions saved to 'predicted_prices.csv'.")
|
| 95 |
+
|
| 96 |
+
# Generate a dashboard for visualization
|
| 97 |
+
def generate_dashboard(data):
|
| 98 |
+
plt.figure(figsize=(10, 6))
|
| 99 |
+
plt.plot(data["time"], data["predicted_price"], label="Predicted Price", linestyle="--")
|
| 100 |
+
if "SEK_per_kWh" in data.columns:
|
| 101 |
+
plt.plot(data["time"], data["SEK_per_kWh"], label="Actual Price")
|
| 102 |
+
plt.xlabel("Time")
|
| 103 |
+
plt.ylabel("Electricity Price (SEK/kWh)")
|
| 104 |
+
plt.title("Electricity Prices: Predicted vs Actual")
|
| 105 |
+
plt.legend()
|
| 106 |
+
plt.grid()
|
| 107 |
+
plt.savefig("dashboard.png")
|
| 108 |
+
plt.close()
|
| 109 |
+
|
| 110 |
+
# Schedule daily updates
|
| 111 |
+
scheduler.add_job(predict_next_day_price, "cron", hour=23, minute=59)
|
| 112 |
+
|
| 113 |
+
# API: Get predictions
|
| 114 |
+
@app.get("/predictions")
|
| 115 |
+
def get_predictions():
|
| 116 |
+
if not os.path.exists(PREDICTIONS_FILE):
|
| 117 |
+
return {"error": "Predictions not available"}
|
| 118 |
+
predictions = pd.read_csv(PREDICTIONS_FILE)
|
| 119 |
+
return predictions.to_dict()
|
| 120 |
+
|
| 121 |
+
# API: Get dashboard
|
| 122 |
+
@app.get("/dashboard")
|
| 123 |
+
def get_dashboard():
|
| 124 |
+
if not os.path.exists("dashboard.png"):
|
| 125 |
+
return {"error": "Dashboard not available"}
|
| 126 |
+
return {"dashboard_url": "/dashboard.png"}
|