Spaces:
Sleeping
Sleeping
Create app_backend.py
Browse files- app_backend.py +41 -0
app_backend.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import plotly.express as px
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import requests
|
6 |
+
|
7 |
+
# Function to fetch real-time weather data
|
8 |
+
def fetch_weather(api_key, location):
|
9 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
|
10 |
+
response = requests.get(url).json()
|
11 |
+
if response["cod"] == 200:
|
12 |
+
return {
|
13 |
+
"temperature": response["main"]["temp"],
|
14 |
+
"wind_speed": response["wind"]["speed"],
|
15 |
+
"weather": response["weather"][0]["description"]
|
16 |
+
}
|
17 |
+
return None
|
18 |
+
|
19 |
+
# Generate synthetic grid data
|
20 |
+
def generate_synthetic_data():
|
21 |
+
time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
|
22 |
+
return pd.DataFrame({
|
23 |
+
"timestamp": time_index,
|
24 |
+
"total_consumption_kwh": np.random.randint(200, 500, len(time_index)),
|
25 |
+
"grid_generation_kwh": np.random.randint(150, 400, len(time_index)),
|
26 |
+
"storage_usage_kwh": np.random.randint(50, 150, len(time_index)),
|
27 |
+
"solar_output_kw": np.random.randint(50, 150, len(time_index)),
|
28 |
+
"wind_output_kw": np.random.randint(30, 120, len(time_index)),
|
29 |
+
"grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
|
30 |
+
})
|
31 |
+
|
32 |
+
# Load optimization recommendation
|
33 |
+
def optimize_load(demand, solar, wind):
|
34 |
+
renewable_supply = solar + wind
|
35 |
+
if renewable_supply >= demand:
|
36 |
+
return "Grid Stable"
|
37 |
+
return "Use Backup or Adjust Load"
|
38 |
+
|
39 |
+
# Export functions for use in Streamlit
|
40 |
+
if __name__ == "__main__":
|
41 |
+
print("Backend ready!")
|