Spaces:
Sleeping
Sleeping
Create app_backend.py
Browse files- app_backend.py +39 -0
app_backend.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"load_demand_kwh": np.random.randint(200, 500, len(time_index)),
|
| 25 |
+
"solar_output_kw": np.random.randint(50, 150, len(time_index)),
|
| 26 |
+
"wind_output_kw": np.random.randint(30, 120, len(time_index)),
|
| 27 |
+
"grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
# Load optimization recommendation
|
| 31 |
+
def optimize_load(demand, solar, wind):
|
| 32 |
+
renewable_supply = solar + wind
|
| 33 |
+
if renewable_supply >= demand:
|
| 34 |
+
return "Grid Stable"
|
| 35 |
+
return "Use Backup or Adjust Load"
|
| 36 |
+
|
| 37 |
+
# Export functions for use in Streamlit
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
print("Backend ready!")
|