-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
318 lines (258 loc) · 12.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from flask import Flask, render_template, request
import matplotlib
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
import xgboost as xgb
import requests
import matplotlib.pyplot as plt
from io import BytesIO
import base64
matplotlib.use('Agg')
import openai
openai.api_key = 'sk-********************************************'
# Replace 'YOUR_API_KEY' with your actual API key
api_key = 'b0ff80d841cb45b28e8194148231409'
weather_api_url = f'http://api.weatherapi.com/v1/current.json?key=b0ff80d841cb45b28e8194148231409&q=thiruvananthapuram&aqi=no'
# Make an API request
response = requests.get(weather_api_url)
# Parse the JSON response to extract weather data
weather_data = response.json()
# Extract temperature in Celsius and rainfall in mm from the JSON response
u_input_temperature = weather_data['current']['temp_c']
u_input_rainfall = weather_data['current']['precip_mm']
app = Flask(__name__)
# Load the dataset
data = pd.read_csv("new_dataset.csv", parse_dates=["Date"])
# Extract year and month as features
data['Year'] = data['Date'].dt.year
data['Month'] = data['Date'].dt.month
# Define the target variables
target_rainfall = 'Rainfall_mm'
target_temperature = 'Temperature_Celsius'
target_weather = 'Weather'
target_energy_demand = 'Energy Demand (GWh)'
# Features for prediction
features = ['Year', 'Month', 'Solar Energy (GWh)', 'Hydroelectric Power (GWh)', 'Energy Demand (GWh)', 'Total_maxpower (gwh)']
# Split the dataset into training data
train_data = data[data['Year'] < 2023]
# Predict Weather (Rainy/Sunny)
X = train_data[features]
y = train_data[target_weather]
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
rf_classifier.fit(X, y)
# Predict Energy Demand
X = train_data[features]
y = train_data[target_energy_demand]
xgb_regressor = xgb.XGBRegressor(objective="reg:squarederror", random_state=42)
xgb_regressor.fit(X, y)
# Predict Temperature
X = train_data[features]
y = train_data[target_temperature]
temperature_regressor = RandomForestRegressor(n_estimators=100, random_state=42)
temperature_regressor.fit(X, y)
# Predict Rainfall
X = train_data[features]
y = train_data[target_rainfall]
rainfall_regressor = RandomForestRegressor(n_estimators=100, random_state=42)
rainfall_regressor.fit(X, y)
def plot_bar_chart(data, xlabel, ylabel, title):
plt.figure(figsize=(8, 4))
plt.bar(data.keys(), data.values(), color=['blue', 'green', 'orange', 'red'])
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid(axis='y', linestyle='--', alpha=0.7)
img = BytesIO()
plt.savefig(img, format='png')
img.seek(0)
plt.close()
return base64.b64encode(img.getvalue()).decode()
# Function to predict for user input date
def predict_for_user_input(input_date):
# Extract year and month from the input date
input_year = input_date.year
input_month = input_date.month
# Use the previous year's data to predict
previous_year_data = train_data[(train_data['Year'] == (input_year - 1)) & (train_data['Month'] == input_month)]
if previous_year_data.empty:
# If there's no data for the specified month and year, use the most recent data for the same month
previous_year_data = train_data[train_data['Month'] == input_month].tail(1)
input_features = previous_year_data[features].values.reshape(1, -1)
# Predict Weather
predicted_weather = rf_classifier.predict(input_features)[0]
# Predict Energy Demand
predicted_energy_demand = xgb_regressor.predict(input_features)[0]
# Predict Temperature
predicted_temperature = temperature_regressor.predict(input_features)[0]
# Predict Rainfall
predicted_rainfall = rainfall_regressor.predict(input_features)[0]
# Suggest Power Generation Source
suggested_power_source = 'Solar' if predicted_weather == 'Sunny' else 'Hydro'
# Calculate Total Power Needed to be Produced (more than demand)
total_power_needed = predicted_energy_demand * 1.1
# Calculate Power to be Produced by Solar and Hydro
if suggested_power_source == 'Solar':
power_hydro = total_power_needed / 5
power_solar = total_power_needed - power_hydro
else:
power_solar = total_power_needed / 5
power_hydro = total_power_needed - power_solar
charts = {
'Predicted_Energy_Demand': plot_bar_chart(
{'Solar': power_solar, 'Hydro': power_hydro,'Total-Power-Needed':total_power_needed},
'Power Source',
'Power (MWh)',
'Predicted Power Generation'
),
}
return {
'Predicted_Weather': predicted_weather,
'Predicted_Energy_Demand_GWh': predicted_energy_demand,
'Suggested_Power_Source': suggested_power_source,
'Total_Power_Needed_GWh': total_power_needed,
'Power_Produced_by_Solar_GWh': power_solar,
'Power_Produced_by_Hydro_GWh': power_hydro,
'Predicted_Temperature_Celsius': predicted_temperature,
'Predicted_Rainfall_mm': predicted_rainfall,
'Charts': charts
}
def generate_report(predicted_weather, suggested_power_source):
# Define a prompt for the report generation
prompt = f"Based on the prediction, the weather is expected to be {predicted_weather}. The suggested power source is {suggested_power_source}. Generate a report summarizing the prediction."
# Call OpenAI API to generate a report
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=150
)
# Extract the generated report from the OpenAI API response
generated_report = response['choices'][0]['text'].strip()
return generated_report
# Function to predict for user input date, temperature, and rainfall
def predict_for_user_input_temp_rain(input_date, input_temperature, input_rainfall):
# Extract year and month from the input date
input_year = input_date.year
input_month = input_date.month
# Find the most recent available data for the same month and year
recent_data = train_data[(train_data['Year'] == input_year) & (train_data['Month'] == input_month)]
if recent_data.empty:
# If there's no data for the specified month and year, use the most recent data for the same month
recent_data = train_data[train_data['Month'] == input_month].tail(1)
input_features = recent_data[features].values.reshape(1, -1)
# Predict Energy Demand
predicted_energy_demand = xgb_regressor.predict(input_features)[0]
# Suggest Power Generation Source
suggested_power_source = 'Solar' if input_temperature > 30 and input_rainfall < 100 else 'Hydro'
# Predict Weather
predicted_weather = rf_classifier.predict(input_features)[0] if input_temperature and input_rainfall is None else 'Rainy' if input_rainfall >= 100 else 'Sunny'
# Calculate Total Power Needed to be Produced (more than demand)
total_power_needed = predicted_energy_demand * 1.1
# Calculate Power to be Produced by Solar and Hydro
if suggested_power_source == 'Solar':
power_hydro = total_power_needed / 5
power_solar = total_power_needed - power_hydro
else:
power_solar = total_power_needed / 5
power_hydro = total_power_needed - power_solar
charts = {
'Predicted_Energy_Demand': plot_bar_chart(
{'Solar': power_solar, 'Hydro': power_hydro,'Total-Power-Needed':total_power_needed},
'Power Source',
'Power (MWh)',
'Predicted Power Generation'
),
}
return {
'Predicted_Weather': predicted_weather,
'Predicted_Energy_Demand_GWh': predicted_energy_demand,
'Suggested_Power_Source': suggested_power_source,
'Total_Power_Needed_GWh': total_power_needed,
'Power_Produced_by_Solar_GWh': power_solar,
'Power_Produced_by_Hydro_GWh': power_hydro,
'Charts': charts
}
def predict_for_user_input_current(input_date, input_temperature, input_rainfall):
u_input_temperature = weather_data['current']['temp_c']
u_input_rainfall = weather_data['current']['precip_mm']
# Extract year and month from the input date
input_year = input_date.year
input_month = input_date.month
# Find the most recent available data for the same month and year
recent_data = train_data[(train_data['Year'] == input_year) & (train_data['Month'] == input_month)]
if recent_data.empty:
# If there's no data for the specified month and year, use the most recent data for the same month
recent_data = train_data[train_data['Month'] == input_month].tail(1)
input_features = recent_data[features].values.reshape(1, -1)
# Predict Energy Demand
predicted_energy_demand = xgb_regressor.predict(input_features)[0]
# Suggest Power Generation Source
suggested_power_source = 'Solar' if input_temperature > 25 and input_rainfall < 50 else 'Hydro'
# Predict Weather
sunny_prob = rf_classifier.predict_proba(input_features)[0][1] # Probability of being sunny
predicted_weather = 'Sunny' if sunny_prob > 0.5 else 'Rainy'
# Calculate Total Power Needed to be Produced (more than demand)
total_power_needed = predicted_energy_demand * 1.1
# Calculate Power to be Produced by Solar and Hydro
if suggested_power_source == 'Solar':
power_hydro = total_power_needed / 5
power_solar = total_power_needed - power_hydro
else:
power_solar = total_power_needed / 5
power_hydro = total_power_needed - power_solar
charts = {
'Predicted_Energy_Demand': plot_bar_chart(
{'Solar': power_solar, 'Hydro': power_hydro,'Total-Power-Needed':total_power_needed},
'Power Source',
'Power (MWh)',
'Predicted Power Generation'
),
# 'Predicted_Weather': plot_bar_chart(
# {'Sunny': sunny_prob, 'Rainy': 1 - sunny_prob},
# 'Weather',
# 'Probability',
# 'Predicted Weather Probability'
# ),
}
return {
'Predicted_Weather': predicted_weather,
'Predicted_Energy_Demand_GWh': predicted_energy_demand,
'Suggested_Power_Source': suggested_power_source,
'Total_Power_Needed_GWh': total_power_needed,
'Power_Produced_by_Solar_GWh': power_solar,
'Power_Produced_by_Hydro_GWh': power_hydro,
'Predicted_Temperature_Celsius' : u_input_temperature,
'Predicted_Rainfall_mm':u_input_rainfall,
'Charts': charts
}
@app.route('/', methods=['GET', 'POST'])
def index():
prediction = None
report = None
if request.method == 'POST':
user_input_date_str = request.form['input_date']
user_input_date = pd.to_datetime(user_input_date_str, format='%Y-%m')
prediction = predict_for_user_input(user_input_date)
# Generate a report based on the prediction
report = generate_report(prediction['Predicted_Weather'], prediction['Suggested_Power_Source'])
return render_template('index.html', prediction=prediction, report=report)
@app.route('/predict_temperature_rainfall', methods=['GET', 'POST'])
def predict_with_temperature_rainfall():
prediction = None
if request.method == 'POST':
user_input_date_str = request.form['input_date']
user_input_date = pd.to_datetime(user_input_date_str, format='%Y-%m')
user_input_temperature = float(request.form['input_temperature'])
user_input_rainfall = float(request.form['input_rainfall'])
prediction = predict_for_user_input_temp_rain(user_input_date, user_input_temperature, user_input_rainfall)
return render_template('predict_temperature_rainfall.html', prediction=prediction)
@app.route('/predict_on_current', methods=['GET', 'POST'])
def predict_with_current():
prediction = None
if request.method == 'POST':
user_input_date_str = request.form['input_date']
user_input_date = pd.to_datetime(user_input_date_str, format='%Y-%m')
prediction = predict_for_user_input_current(user_input_date, u_input_temperature, u_input_rainfall)
return render_template('predict_on_current.html', prediction=prediction)
if __name__ == '__main__':
app.run(debug=True)