Spaces:
Sleeping
Sleeping
Update app_backend.py
Browse files- app_backend.py +77 -77
app_backend.py
CHANGED
@@ -148,87 +148,87 @@
|
|
148 |
# code 4
|
149 |
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
#
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
#
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
#
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
|
189 |
|
190 |
|
191 |
# code 5
|
192 |
|
193 |
|
194 |
-
import random
|
195 |
-
import pandas as pd
|
196 |
-
def fetch_data():
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
def generate_recommendations(data):
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
def grid_health_status(data):
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
def generate_trading_options(data):
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
|
|
|
148 |
# code 4
|
149 |
|
150 |
|
151 |
+
import pandas as pd
|
152 |
+
import numpy as np
|
153 |
+
import requests
|
154 |
+
from datetime import datetime
|
155 |
+
|
156 |
+
# Function to fetch real-time weather data
|
157 |
+
def fetch_weather(api_key, location):
|
158 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
|
159 |
+
response = requests.get(url).json()
|
160 |
+
if response["cod"] == 200:
|
161 |
+
return {
|
162 |
+
"temperature": response["main"]["temp"],
|
163 |
+
"wind_speed": response["wind"]["speed"],
|
164 |
+
"weather": response["weather"][0]["description"]
|
165 |
+
}
|
166 |
+
return None
|
167 |
+
|
168 |
+
# Generate synthetic grid data
|
169 |
+
def generate_synthetic_data():
|
170 |
+
time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
|
171 |
+
return pd.DataFrame({
|
172 |
+
"timestamp": time_index,
|
173 |
+
"load_demand_kwh": np.random.randint(200, 500, len(time_index)),
|
174 |
+
"solar_output_kw": np.random.randint(50, 150, len(time_index)),
|
175 |
+
"wind_output_kw": np.random.randint(30, 120, len(time_index)),
|
176 |
+
"grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
|
177 |
+
})
|
178 |
+
|
179 |
+
# Load optimization recommendation
|
180 |
+
def optimize_load(demand, solar, wind):
|
181 |
+
renewable_supply = solar + wind
|
182 |
+
if renewable_supply >= demand:
|
183 |
+
return "Grid Stable"
|
184 |
+
return "Use Backup or Adjust Load"
|
185 |
+
|
186 |
+
if __name__ == "__main__":
|
187 |
+
print("Backend ready!")
|
188 |
|
189 |
|
190 |
|
191 |
# code 5
|
192 |
|
193 |
|
194 |
+
# import random
|
195 |
+
# import pandas as pd
|
196 |
+
# def fetch_data():
|
197 |
+
# # Simulating fetching data from a database or API
|
198 |
+
# data = {
|
199 |
+
# 'temperature': random.uniform(-10, 30),
|
200 |
+
# 'wind_speed': random.uniform(0, 20),
|
201 |
+
# 'weather_condition': random.choice(['Clear', 'Overcast Clouds', 'Thunderstorm', 'Rain']),
|
202 |
+
# 'timestamps': pd.date_range("2025-01-01", periods=10, freq='H'),
|
203 |
+
# 'total_consumption': [random.uniform(50, 100) for _ in range(10)],
|
204 |
+
# 'grid_generation': [random.uniform(30, 80) for _ in range(10)],
|
205 |
+
# 'storage_usage': [random.uniform(10, 30) for _ in range(10)],
|
206 |
+
# 'solar_storage': random.uniform(10, 30),
|
207 |
+
# 'wind_storage': random.uniform(10, 30),
|
208 |
+
# 'hydro_storage': random.uniform(10, 30),
|
209 |
+
# 'total_storage': random.uniform(50, 100),
|
210 |
+
# }
|
211 |
+
# return data
|
212 |
+
|
213 |
+
# def generate_recommendations(data):
|
214 |
+
# recommendations = []
|
215 |
+
# if data['total_consumption'][-1] > data['grid_generation'][-1]:
|
216 |
+
# recommendations.append("Consider integrating additional renewable sources to meet the current demand.")
|
217 |
+
# if data['storage_usage'][-1] > data['total_storage'] * 0.8:
|
218 |
+
# recommendations.append("Energy storage is running low. Consider optimizing the grid or adding more storage.")
|
219 |
+
# return recommendations
|
220 |
+
|
221 |
+
# def grid_health_status(data):
|
222 |
+
# status = "Grid is operating normally."
|
223 |
+
# if data['total_consumption'][-1] > 90:
|
224 |
+
# status = "Warning: High consumption detected!"
|
225 |
+
# if data['wind_speed'] > 15:
|
226 |
+
# status = "Warning: High wind speeds, may affect wind turbine output."
|
227 |
+
# return status
|
228 |
+
|
229 |
+
# def generate_trading_options(data):
|
230 |
+
# if data['total_storage'] > 60:
|
231 |
+
# return "Energy is available for export to neighboring countries."
|
232 |
+
# else:
|
233 |
+
# return "Energy reserves are low. Trading is not recommended at this moment."
|
234 |
|