-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
222 lines (166 loc) · 6.71 KB
/
Copy pathapp.py
File metadata and controls
222 lines (166 loc) · 6.71 KB
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
import streamlit as st
from PIL import Image
import os
import numpy as np
import matplotlib.pyplot as plt
from sam_model import load_sam_model, get_rooftop_mask
from utils import calculate_area_from_mask, estimate_solar_metrics
@st.cache_resource
def load_cached_sam():
return load_sam_model()
def display_results(metrics, monthly_generation, monthly_bill=None, tariff=8.0):
st.markdown("## Solar Feasibility Summary")
system_size = metrics["system_size_kw"]
annual_gen = metrics["annual_generation_kwh"]
annual_savings = metrics["annual_savings"]
payback = metrics["payback_years"]
roi = metrics["roi_percent"]
net_cost = metrics["net_cost"]
# --- Bill Offset ---
if monthly_bill:
annual_consumption = (monthly_bill * 12) / tariff
bill_offset = min((annual_gen / annual_consumption) * 100, 100)
estimated_new_bill = max(
(annual_consumption - annual_gen) * tariff / 12, 0
)
else:
bill_offset = None
estimated_new_bill = None
# WHAT THIS MEANS FOR YOU
st.markdown("### What This Means For You")
col1, col2, col3 = st.columns(3)
col1.metric("System Size", f"{system_size} kW")
col2.metric("Annual Savings", f"₹{annual_savings:,.0f}")
col3.metric("Payback Period", f"{payback} years")
if bill_offset:
st.success(
f"Your solar system can offset **{bill_offset:.0f}%** of your yearly electricity usage."
)
st.info(
f"Your ₹{monthly_bill:,.0f} monthly bill could reduce to approximately **₹{estimated_new_bill:,.0f}/month**."
)
# LONG TERM VALUE
st.markdown("### 10-Year Financial Impact")
total_10yr = metrics["cumulative_savings"][-1]
st.write(
f"Over 10 years, you could save approximately **₹{total_10yr:,.0f}**."
)
if roi > 25:
st.success("This investment significantly outperforms fixed deposits and low-risk instruments.")
elif roi > 15:
st.warning("This provides moderate long-term financial benefit.")
else:
st.error("Financial return is relatively modest under current assumptions.")
with st.expander("Technical Details (Advanced Users)"):
st.write(f"Annual Generation: {annual_gen} kWh")
st.write(f"Performance Ratio: {metrics['performance_ratio']}")
st.write(f"Installation Cost: ₹{metrics['installation_cost']}")
st.write(f"Subsidy (PM Surya Ghar): ₹{metrics['subsidy']}")
st.write(f"Net Cost After Subsidy: ₹{net_cost}")
# Side-by-side plots
st.subheader("Energy & Financial Projection")
col1, col2 = st.columns(2)
with col1:
st.markdown("### Monthly Energy Output")
fig1, ax1 = plt.subplots(figsize=(5, 3))
ax1.bar(monthly_generation.index.strftime("%b"),
monthly_generation.values)
ax1.set_ylabel("Energy (kWh)")
ax1.set_xlabel("Month")
plt.tight_layout()
st.pyplot(fig1)
with col2:
st.markdown("### 10-Year Savings Projection")
fig2, ax2 = plt.subplots(figsize=(5, 3))
ax2.plot(range(1, 11), metrics["cumulative_savings"], marker="o")
ax2.set_ylabel("Cumulative Savings (₹)")
ax2.set_xlabel("Year")
plt.tight_layout()
st.pyplot(fig2)
st.set_page_config(page_title="SolarAIAssistant", layout="wide")
st.title("SolarAIAssistant")
st.subheader("Rooftop Solar Feasibility and Savings Estimator for Indian Households")
st.markdown(
"""
Evaluate rooftop solar installation under PM Surya Ghar Yojana
using physics-based energy simulation and financial modeling.
"""
)
# Input Method Selection
st.header("Input Method")
input_mode = st.radio(
"Select evaluation method:",
["Monthly Electricity Bill", "Rooftop Image (AI Detection)"]
)
# Location Settings
st.header("Location Settings")
latitude = st.number_input("Latitude", value=28.6139, format="%.6f")
longitude = st.number_input("Longitude", value=77.2090, format="%.6f")
tilt = st.slider("Panel Tilt (degrees)", 0, 45, 20)
azimuth = st.slider("Panel Azimuth (degrees)", 0, 360, 180)
optimize = st.checkbox("Automatically optimize tilt")
tariff = st.number_input(
"Electricity Tariff (₹ per kWh)",
min_value=0.1,
value=8.0
)
# BILL MODE
if input_mode == "Monthly Electricity Bill":
st.header("Electricity Bill Details")
monthly_bill = st.number_input("Monthly Electricity Bill (₹)", min_value=0.0)
if st.button("Run Feasibility Analysis"):
if monthly_bill <= 0:
st.error("Please enter a valid monthly electricity bill.")
else:
annual_bill = monthly_bill * 12
annual_consumption = annual_bill / tariff
# Approximate system size estimation
average_specific_yield = 1500 # kWh per kW per year (India average)
estimated_system_size = annual_consumption / average_specific_yield
utilization_factor = 0.7
estimated_area = (estimated_system_size / 0.18) / utilization_factor
metrics, monthly_generation = estimate_solar_metrics(
area_m2=estimated_area,
latitude=latitude,
longitude=longitude,
tilt=tilt,
azimuth=azimuth,
optimize=optimize,
tariff=tariff
)
display_results(
metrics,
monthly_generation,
monthly_bill=monthly_bill,
tariff=tariff
)
# ROOFTOP MODE
elif input_mode == "Rooftop Image (AI Detection)":
st.header("Upload Rooftop Image")
uploaded_file = st.file_uploader(
"Upload a satellite or aerial rooftop image",
type=["png", "jpg", "jpeg"]
)
if uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_container_width=True)
with st.spinner("Detecting rooftop area..."):
sam = load_cached_sam()
mask = get_rooftop_mask(image, sam)
if mask is None:
st.error("Rooftop could not be detected. Please upload a clearer image.")
else:
masked_image_np = np.array(image)
masked_image_np[~mask] = [0, 0, 0]
st.image(masked_image_np, caption="Detected Rooftop Area", use_container_width=True)
area_m2 = calculate_area_from_mask(mask)
metrics, monthly_generation = estimate_solar_metrics(
area_m2=area_m2,
latitude=latitude,
longitude=longitude,
tilt=tilt,
azimuth=azimuth,
optimize=optimize,
tariff=tariff
)
display_results(metrics, monthly_generation)