Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,33 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 6 |
from keras.models import load_model
|
| 7 |
-
import
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Load the
|
| 11 |
-
data = pd.read_csv('
|
| 12 |
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
|
| 13 |
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
| 14 |
-
loaded_model = load_model('solar_irradiance_model.h5')
|
| 15 |
|
| 16 |
-
#
|
| 17 |
encoder = OneHotEncoder(sparse=False, categories='auto')
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
#
|
| 26 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
| 27 |
-
# Encode and scale features
|
| 28 |
encoded_month_hour = encoder.transform([[month, hour]])
|
| 29 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
| 30 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
|
@@ -32,60 +35,36 @@ def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_e
|
|
| 32 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 33 |
return max(predicted_irradiance[0][0], 0.0)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
return data[data['Month'] == month]['Irradiance(W/m^2)'].values
|
| 37 |
-
|
| 38 |
-
# Streamlit UI
|
| 39 |
st.title("Solar Irradiance Prediction App")
|
| 40 |
st.sidebar.header("Input Parameters")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
month = st.sidebar.selectbox("Month",
|
| 44 |
-
hour = st.sidebar.slider("Hour
|
| 45 |
latitude = st.sidebar.number_input("Latitude", value=28.570633)
|
| 46 |
longitude = st.sidebar.number_input("Longitude", value=77.327215)
|
| 47 |
panel_capacity = st.sidebar.number_input("Panel Capacity (W)", value=500)
|
| 48 |
panel_efficiency = st.sidebar.number_input("Panel Efficiency", value=0.15)
|
| 49 |
wind_speed = st.sidebar.number_input("Wind Speed (km/h)", value=6.43988)
|
| 50 |
cloud_cover = st.sidebar.number_input("Cloud Cover (%)", value=17.7)
|
| 51 |
-
temperature = st.sidebar.number_input("Temperature (°F)", value=55)
|
| 52 |
|
| 53 |
-
# Predict and
|
| 54 |
if st.button("Predict"):
|
| 55 |
-
|
| 56 |
-
st.write(f"
|
| 57 |
|
| 58 |
# Visualization
|
| 59 |
-
st.
|
| 60 |
-
actual_irradiance =
|
| 61 |
predicted_irradiances = [predict_irradiance(month, h, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature) for h in range(24)]
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
st.pyplot(
|
| 71 |
-
|
| 72 |
-
# Hour vs. Irradiance
|
| 73 |
-
st.header("Hour vs. Irradiance for All Months")
|
| 74 |
-
hour_range = range(24)
|
| 75 |
-
predicted_irradiances_all = []
|
| 76 |
-
|
| 77 |
-
for h in hour_range:
|
| 78 |
-
if h in range(6) or h in range(18, 24):
|
| 79 |
-
predicted_irradiances_all.append(0)
|
| 80 |
-
else:
|
| 81 |
-
predicted_irradiances_all.append(
|
| 82 |
-
predict_irradiance(month, h, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature)
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
fig2, ax2 = plt.subplots(figsize=(10, 6))
|
| 86 |
-
ax2.bar(hour_range, predicted_irradiances_all, label="Predicted Irradiance", alpha=0.7)
|
| 87 |
-
ax2.set_xlabel("Hour")
|
| 88 |
-
ax2.set_ylabel("Irradiance (W/m²)")
|
| 89 |
-
ax2.set_title("Predicted Irradiance Across the Day")
|
| 90 |
-
ax2.legend()
|
| 91 |
-
st.pyplot(fig2)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 5 |
from keras.models import load_model
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
try:
|
| 10 |
+
loaded_model = load_model('solar_irradiance_model.h5')
|
| 11 |
+
except ValueError as e:
|
| 12 |
+
st.error(f"Error loading model: {e}")
|
| 13 |
|
| 14 |
+
# Load the dataset
|
| 15 |
+
data = pd.read_csv('solar_irradiance.csv')
|
| 16 |
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
|
| 17 |
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
|
|
|
| 18 |
|
| 19 |
+
# One-hot encoder setup
|
| 20 |
encoder = OneHotEncoder(sparse=False, categories='auto')
|
| 21 |
+
categorical_features = data[['Month', 'Hour']]
|
| 22 |
+
encoder.fit(categorical_features)
|
| 23 |
|
| 24 |
+
# Standard scaler setup
|
| 25 |
+
scaler = StandardScaler()
|
| 26 |
+
numerical_features = data[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
| 27 |
+
scaler.fit(numerical_features)
|
| 28 |
|
| 29 |
+
# Function to predict irradiance
|
| 30 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
|
|
|
| 31 |
encoded_month_hour = encoder.transform([[month, hour]])
|
| 32 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
| 33 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
|
|
|
| 35 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 36 |
return max(predicted_irradiance[0][0], 0.0)
|
| 37 |
|
| 38 |
+
# Streamlit app UI
|
|
|
|
|
|
|
|
|
|
| 39 |
st.title("Solar Irradiance Prediction App")
|
| 40 |
st.sidebar.header("Input Parameters")
|
| 41 |
|
| 42 |
+
# User inputs
|
| 43 |
+
month = st.sidebar.selectbox("Month", data['Month'].unique())
|
| 44 |
+
hour = st.sidebar.slider("Hour", 0, 23, 12)
|
| 45 |
latitude = st.sidebar.number_input("Latitude", value=28.570633)
|
| 46 |
longitude = st.sidebar.number_input("Longitude", value=77.327215)
|
| 47 |
panel_capacity = st.sidebar.number_input("Panel Capacity (W)", value=500)
|
| 48 |
panel_efficiency = st.sidebar.number_input("Panel Efficiency", value=0.15)
|
| 49 |
wind_speed = st.sidebar.number_input("Wind Speed (km/h)", value=6.43988)
|
| 50 |
cloud_cover = st.sidebar.number_input("Cloud Cover (%)", value=17.7)
|
| 51 |
+
temperature = st.sidebar.number_input("Temperature (°F)", value=55.0)
|
| 52 |
|
| 53 |
+
# Predict and display
|
| 54 |
if st.button("Predict"):
|
| 55 |
+
predicted_irradiance = predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature)
|
| 56 |
+
st.write(f"Predicted Solar Irradiance: {predicted_irradiance:.2f} W/m²")
|
| 57 |
|
| 58 |
# Visualization
|
| 59 |
+
st.subheader("Actual vs. Predicted Irradiance")
|
| 60 |
+
actual_irradiance = data[data['Month'] == month]['Irradiance(W/m^2)'].values
|
| 61 |
predicted_irradiances = [predict_irradiance(month, h, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature) for h in range(24)]
|
| 62 |
|
| 63 |
+
plt.figure(figsize=(10, 5))
|
| 64 |
+
plt.plot(range(24), actual_irradiance, label='Actual Irradiance')
|
| 65 |
+
plt.plot(range(24), predicted_irradiances, label='Predicted Irradiance')
|
| 66 |
+
plt.xlabel('Hour')
|
| 67 |
+
plt.ylabel('Irradiance (W/m²)')
|
| 68 |
+
plt.title(f'Actual vs. Predicted Irradiance for {month}')
|
| 69 |
+
plt.legend()
|
| 70 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|