Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 warnings
|
| 8 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 9 |
+
|
| 10 |
+
# Load the data and pre-trained model
|
| 11 |
+
data = pd.read_csv('solar_irradiance.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 |
+
# Preprocessing
|
| 17 |
+
encoder = OneHotEncoder(sparse=False, categories='auto')
|
| 18 |
+
scaler = StandardScaler()
|
| 19 |
+
|
| 20 |
+
# Encode categorical and scale numerical features
|
| 21 |
+
encoder.fit(data[['Month', 'Hour']])
|
| 22 |
+
scaler.fit(data[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency',
|
| 23 |
+
'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']])
|
| 24 |
+
|
| 25 |
+
# Helper functions
|
| 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)
|
| 31 |
+
reshaped_features = np.reshape(processed_features, (1, 1, processed_features.shape[1]))
|
| 32 |
+
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 33 |
+
return max(predicted_irradiance[0][0], 0.0)
|
| 34 |
+
|
| 35 |
+
def get_actual_irradiance(month):
|
| 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 |
+
# Sidebar Inputs
|
| 43 |
+
month = st.sidebar.selectbox("Month", options=data['Month'].unique())
|
| 44 |
+
hour = st.sidebar.slider("Hour of the Day", min_value=0, max_value=23, value=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)
|
| 52 |
+
|
| 53 |
+
# Predict and Display
|
| 54 |
+
if st.button("Predict"):
|
| 55 |
+
predicted = predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature)
|
| 56 |
+
st.write(f"**Predicted Solar Irradiance:** {predicted:.2f} W/m²")
|
| 57 |
+
|
| 58 |
+
# Visualization
|
| 59 |
+
st.header(f"Actual vs. Predicted Irradiance for {month}")
|
| 60 |
+
actual_irradiance = get_actual_irradiance(month)
|
| 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 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 64 |
+
ax.plot(range(24), actual_irradiance, label="Actual Irradiance", marker='o')
|
| 65 |
+
ax.plot(range(24), predicted_irradiances, label="Predicted Irradiance", marker='x')
|
| 66 |
+
ax.set_xlabel("Hour")
|
| 67 |
+
ax.set_ylabel("Irradiance (W/m²)")
|
| 68 |
+
ax.set_title(f"Actual vs. Predicted Irradiance for {month}")
|
| 69 |
+
ax.legend()
|
| 70 |
+
st.pyplot(fig)
|
| 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)
|