Update app.py
Browse files
app.py
CHANGED
@@ -5,22 +5,13 @@ import os
|
|
5 |
from dotenv import load_dotenv
|
6 |
import plotly.graph_objects as go
|
7 |
|
8 |
-
|
|
|
9 |
load_dotenv()
|
10 |
|
11 |
# Set page configuration
|
12 |
st.set_page_config(page_title="☀️AI-Based Solar Project Estimation Tool", layout="centered")
|
13 |
|
14 |
-
# Initialize Gemini with the API key
|
15 |
-
api_key = os.getenv("GOOGLE_API_KEY")
|
16 |
-
if api_key:
|
17 |
-
genai.configure(api_key=api_key)
|
18 |
-
else:
|
19 |
-
st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.")
|
20 |
-
|
21 |
-
# Use Gemini-1.5-Pro model
|
22 |
-
model = genai.GenerativeModel("gemini-1.5-pro")
|
23 |
-
|
24 |
# Load solar data
|
25 |
@st.cache_data
|
26 |
def load_data():
|
@@ -29,68 +20,16 @@ def load_data():
|
|
29 |
|
30 |
df = load_data()
|
31 |
|
32 |
-
#
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
You are an AI-based solar project estimator.
|
38 |
-
Use the following calculation methods:
|
39 |
-
- Estimated system size (kW) = Roof size (sq meters) × 0.10
|
40 |
-
- Estimated daily solar output (kWh) = System size (kW) × Average GHI (kWh/m²/day)
|
41 |
-
- Total system cost (₹) = System size (kW) × Solar system cost per kW
|
42 |
-
- Assume tariff rate = ₹7/kWh
|
43 |
-
- Monthly savings (₹) = Estimated daily output × 30 × 7
|
44 |
-
- Payback period (years) = Total system cost ÷ (Monthly savings × 12)
|
45 |
-
Inputs:
|
46 |
-
- Project Type: Rooftop Solar
|
47 |
-
- Location: {location}
|
48 |
-
- Roof Size: {roof_size} sq meters
|
49 |
-
- Monthly Electricity Bill: ₹{electricity_bill}
|
50 |
-
- Average GHI: {ghi} kWh/m²/day
|
51 |
-
- Solar System Cost per kW: ₹{solar_cost_per_kw}
|
52 |
-
Now, calculate and return strictly in this format:
|
53 |
-
Estimated solar system size in kW: <value>
|
54 |
-
Estimated daily solar output in kWh: <value>
|
55 |
-
Total system cost in ₹: <value>
|
56 |
-
Monthly savings in ₹: <value>
|
57 |
-
Payback period in years: <value>
|
58 |
-
"""
|
59 |
-
else:
|
60 |
-
prompt = f"""
|
61 |
-
You are an AI-based solar project estimator.
|
62 |
-
Use the following calculation methods:
|
63 |
-
- Required system size (kW) = Desired monthly solar production ÷ (30 × Average GHI)
|
64 |
-
- Estimated daily solar output (kWh) = System size (kW) × Average GHI (kWh/m²/day)
|
65 |
-
- Total system cost (₹) = System size (kW) × Solar system cost per kW
|
66 |
-
- Assume tariff rate = ₹7/kWh
|
67 |
-
- Monthly savings (₹) = Estimated daily output × 30 × 7
|
68 |
-
- Payback period (years) = Total system cost ÷ (Monthly savings × 12)
|
69 |
-
Inputs:
|
70 |
-
- Project Type: Ground Mount Solar
|
71 |
-
- Location: {location}
|
72 |
-
- Desired Monthly Solar Production: {desired_kwh} kWh
|
73 |
-
- Monthly Electricity Bill: ₹{electricity_bill}
|
74 |
-
- Average GHI: {ghi} kWh/m²/day
|
75 |
-
- Solar System Cost per kW: ₹{solar_cost_per_kw}
|
76 |
-
Now, calculate and return strictly in this format:
|
77 |
-
Required solar system size in kW: <value>
|
78 |
-
Estimated daily solar output in kWh: <value>
|
79 |
-
Total system cost in ₹: <value>
|
80 |
-
Monthly savings in ₹: <value>
|
81 |
-
Payback period in years: <value>
|
82 |
-
"""
|
83 |
-
return prompt
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# UI - Form for user input
|
88 |
st.title("☀️AI-Based Solar Project Estimation Tool")
|
89 |
st.write("### Enter Your Details Below:")
|
90 |
|
91 |
with st.form("solar_form"):
|
92 |
state_options = df['State'].dropna().unique()
|
93 |
-
|
94 |
location = st.selectbox("Select your State", options=sorted(state_options))
|
95 |
|
96 |
project_type = st.radio(
|
@@ -109,116 +48,53 @@ with st.form("solar_form"):
|
|
109 |
|
110 |
submitted = st.form_submit_button("Get Estimate")
|
111 |
|
112 |
-
#
|
113 |
if submitted and location:
|
114 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
115 |
-
|
116 |
if state_data is not None:
|
117 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
118 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
119 |
|
120 |
-
# Check roof size limit for rooftop solar
|
121 |
-
if project_type == "Rooftop Solar" and roof_size:
|
122 |
-
max_allowed_kw = roof_size * 0.15
|
123 |
-
if max_allowed_kw > 5: # Maximum 5 kW limit
|
124 |
-
st.warning(f"Roof size exceeds the maximum allowed capacity of 5 kW. The system size will be limited to 5 kW.")
|
125 |
-
max_allowed_kw = 5
|
126 |
-
roof_size = max_allowed_kw / 0.15 # Adjust roof size to fit within the limit
|
127 |
-
|
128 |
-
prompt_text = build_prompt(location, project_type, roof_size=roof_size, desired_kwh=desired_kwh, electricity_bill=electricity_bill, ghi=ghi, solar_cost_per_kw=solar_cost_per_kw)
|
129 |
-
|
130 |
-
# Call Gemini API
|
131 |
-
with st.spinner("Generating solar estimate with Gemini..."):
|
132 |
-
response = model.generate_content(prompt_text)
|
133 |
-
|
134 |
-
# Display structured output
|
135 |
-
st.subheader("🔹 Solar Project Estimate")
|
136 |
-
|
137 |
-
estimated_data = response.text.strip().split("\n")
|
138 |
-
|
139 |
-
system_size_kw = None
|
140 |
-
monthly_savings_rs = None
|
141 |
-
total_system_cost = None
|
142 |
-
payback_period_years = None
|
143 |
-
daily_output_kwh = None
|
144 |
-
|
145 |
-
for point in estimated_data:
|
146 |
-
if ":" in point:
|
147 |
-
try:
|
148 |
-
key, value = point.split(":", 1)
|
149 |
-
key = key.strip()
|
150 |
-
value = value.strip()
|
151 |
-
|
152 |
-
st.write(f"**{key}**: {value}")
|
153 |
-
|
154 |
-
if "Estimated solar system size" in key or "Required solar system size" in key:
|
155 |
-
system_size_kw = float(value.split()[0])
|
156 |
-
if "Monthly savings" in key:
|
157 |
-
monthly_savings_rs = float(value.split()[0])
|
158 |
-
if "Total system cost" in key:
|
159 |
-
total_system_cost = float(value.split()[0])
|
160 |
-
if "Payback period" in key:
|
161 |
-
payback_period_years = float(value.split()[0])
|
162 |
-
|
163 |
-
except ValueError:
|
164 |
-
st.warning("There was an issue processing the response. Please try again.")
|
165 |
-
|
166 |
-
# Calculate Daily Output in kWh
|
167 |
-
if system_size_kw is not None and ghi is not None:
|
168 |
-
daily_output_kwh = system_size_kw * ghi # Estimate daily output in kWh
|
169 |
-
|
170 |
-
# Show the formulas
|
171 |
-
st.subheader("🧮 Formulas Used:")
|
172 |
-
|
173 |
if project_type == "Rooftop Solar":
|
174 |
-
|
175 |
-
|
176 |
-
- Estimated Solar System Size (kW) = Roof Size (m²) × 0.15
|
177 |
-
- Estimated Daily Solar Output (kWh) = System Size (kW) × Average GHI (kWh/m²/day)
|
178 |
-
- Total System Cost (₹) = System Size (kW) × Solar Cost per kW (₹)
|
179 |
-
- Monthly Savings (₹) = (Estimated Daily Output × 30 × Tariff Rate per kWh) [Approximate]
|
180 |
-
- Payback Period (years) = Total System Cost ÷ (Monthly Savings × 12)
|
181 |
-
""")
|
182 |
else:
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
st.
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
name="Financials",
|
207 |
-
x=["Solar System Size", "Daily Output", "Total Cost", "Monthly Savings", "Payback Period"],
|
208 |
-
y=[0, 0, total_system_cost, monthly_savings_rs, payback_period_years],
|
209 |
-
marker_color='#00CC96'
|
210 |
-
)
|
211 |
-
])
|
212 |
-
|
213 |
-
fig.update_layout(
|
214 |
-
barmode='group',
|
215 |
-
title="Comparison of Solar System Parameters",
|
216 |
-
yaxis_title="Values",
|
217 |
-
xaxis_title="Parameters"
|
218 |
)
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
else:
|
222 |
-
st.error("
|
223 |
else:
|
224 |
-
st.warning("Please
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
import plotly.graph_objects as go
|
7 |
|
8 |
+
|
9 |
+
# Load environment variables
|
10 |
load_dotenv()
|
11 |
|
12 |
# Set page configuration
|
13 |
st.set_page_config(page_title="☀️AI-Based Solar Project Estimation Tool", layout="centered")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Load solar data
|
16 |
@st.cache_data
|
17 |
def load_data():
|
|
|
20 |
|
21 |
df = load_data()
|
22 |
|
23 |
+
# Constants
|
24 |
+
TARIFF_RATE = 7 # ₹7 per kWh
|
25 |
+
ROOFTOP_CONVERSION_FACTOR = 0.10 # 0.10 kW per sq meter
|
26 |
+
|
27 |
+
# UI - Form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
st.title("☀️AI-Based Solar Project Estimation Tool")
|
29 |
st.write("### Enter Your Details Below:")
|
30 |
|
31 |
with st.form("solar_form"):
|
32 |
state_options = df['State'].dropna().unique()
|
|
|
33 |
location = st.selectbox("Select your State", options=sorted(state_options))
|
34 |
|
35 |
project_type = st.radio(
|
|
|
48 |
|
49 |
submitted = st.form_submit_button("Get Estimate")
|
50 |
|
51 |
+
# Calculate directly
|
52 |
if submitted and location:
|
53 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
54 |
+
|
55 |
if state_data is not None:
|
56 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
57 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
if project_type == "Rooftop Solar":
|
60 |
+
system_size_kw = round(roof_size * ROOFTOP_CONVERSION_FACTOR, 2)
|
61 |
+
estimated_daily_output = round(system_size_kw * ghi, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
else:
|
63 |
+
system_size_kw = round(desired_kwh / (30 * ghi), 2)
|
64 |
+
estimated_daily_output = round(system_size_kw * ghi, 2)
|
65 |
+
|
66 |
+
total_system_cost = round(system_size_kw * solar_cost_per_kw, 2)
|
67 |
+
monthly_savings = round(estimated_daily_output * 30 * TARIFF_RATE, 2)
|
68 |
+
payback_period = round(total_system_cost / (monthly_savings * 12), 2)
|
69 |
+
|
70 |
+
# Display Results
|
71 |
+
st.subheader("🔹 Solar Project Estimate")
|
72 |
+
st.write(f"**Estimated solar system size in kW**: {system_size_kw}")
|
73 |
+
st.write(f"**Estimated daily solar output in kWh**: {estimated_daily_output}")
|
74 |
+
st.write(f"**Total system cost in ₹**: {total_system_cost}")
|
75 |
+
st.write(f"**Monthly savings in ₹**: {monthly_savings}")
|
76 |
+
st.write(f"**Payback period in years**: {payback_period}")
|
77 |
+
|
78 |
+
# Visual Summary
|
79 |
+
st.subheader("📊 Visual Summary")
|
80 |
+
fig = go.Figure(data=[
|
81 |
+
go.Bar(
|
82 |
+
name="System Parameters",
|
83 |
+
x=["System Size (kW)", "Daily Output (kWh)", "Total Cost (₹)", "Monthly Savings (₹)", "Payback (Years)"],
|
84 |
+
y=[system_size_kw, estimated_daily_output, total_system_cost, monthly_savings, payback_period],
|
85 |
+
marker_color='#636EFA'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
)
|
87 |
+
])
|
88 |
+
fig.update_layout(
|
89 |
+
title="Solar System Estimation Overview",
|
90 |
+
yaxis_title="Values",
|
91 |
+
xaxis_title="Parameters"
|
92 |
+
)
|
93 |
+
st.plotly_chart(fig, use_container_width=True)
|
94 |
+
|
95 |
+
st.info("Note: Tariff assumed ₹7/kWh. Actual payback may vary based on location, grid policy, and maintenance.")
|
96 |
|
97 |
else:
|
98 |
+
st.error("State data not found. Please try a valid state.")
|
99 |
else:
|
100 |
+
st.warning("Please complete all fields to get your estimate.")
|