Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,21 @@ import google.generativeai as genai
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Load environment variables
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# Set page configuration
|
| 11 |
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
|
| 12 |
|
| 13 |
-
# Initialize Gemini
|
| 14 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 15 |
if api_key:
|
| 16 |
genai.configure(api_key=api_key)
|
| 17 |
else:
|
| 18 |
st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.")
|
| 19 |
|
| 20 |
-
model
|
|
|
|
| 21 |
|
| 22 |
# Load solar data
|
| 23 |
@st.cache_data
|
|
@@ -27,85 +28,64 @@ def load_data():
|
|
| 27 |
|
| 28 |
df = load_data()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
def calculate_solar_estimate(roof_size, monthly_bill, electricity_price, ghi, cost_per_kw):
|
| 32 |
-
daily_consumption_inr = monthly_bill / 30
|
| 33 |
-
daily_consumption_kwh = daily_consumption_inr / electricity_price
|
| 34 |
-
|
| 35 |
-
# Assume system size based on consumption
|
| 36 |
-
estimated_system_size_kw = 3 # Fixed for now as realistic estimate
|
| 37 |
-
peak_sun_hours = ghi # Use GHI as sun hours (approximation)
|
| 38 |
-
|
| 39 |
-
daily_solar_output_kwh = estimated_system_size_kw * peak_sun_hours * 0.75 # considering derating factor
|
| 40 |
-
|
| 41 |
-
total_system_cost = estimated_system_size_kw * cost_per_kw
|
| 42 |
-
|
| 43 |
-
monthly_generation_kwh = daily_solar_output_kwh * 30
|
| 44 |
-
monthly_savings_inr = monthly_generation_kwh * electricity_price
|
| 45 |
-
|
| 46 |
-
annual_savings_inr = monthly_savings_inr * 12
|
| 47 |
-
payback_period_years = total_system_cost / annual_savings_inr
|
| 48 |
-
|
| 49 |
-
return {
|
| 50 |
-
"Estimated solar system size in kW": round(estimated_system_size_kw, 2),
|
| 51 |
-
"Estimated daily solar output in kWh": round(daily_solar_output_kwh, 2),
|
| 52 |
-
"Total system cost in ₹": int(total_system_cost),
|
| 53 |
-
"Monthly savings in ₹": int(monthly_savings_inr),
|
| 54 |
-
"Payback period in years": round(payback_period_years, 2)
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
# UI - Form
|
| 58 |
st.title("AI-based Solar Project Estimation Tool")
|
| 59 |
-
st.write("### Enter Your Details:")
|
| 60 |
|
| 61 |
with st.form("solar_form"):
|
| 62 |
state_options = df['State'].dropna().unique()
|
| 63 |
|
| 64 |
location = st.selectbox("Select your State", options=sorted(state_options))
|
| 65 |
roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
|
| 66 |
-
|
| 67 |
|
| 68 |
submitted = st.form_submit_button("Get Estimate")
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
| 72 |
|
| 73 |
if state_data is not None:
|
| 74 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
| 75 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
| 76 |
-
electricity_price = 8 # Assume ₹8/kWh
|
| 77 |
|
| 78 |
-
|
| 79 |
-
estimates = calculate_solar_estimate(roof_size, monthly_bill, electricity_price, ghi, solar_cost_per_kw)
|
| 80 |
-
|
| 81 |
-
# Build clean prompt for Gemini to verify the calculation
|
| 82 |
-
prompt = f"""
|
| 83 |
-
ONLY output these 5 points based on inputs:
|
| 84 |
-
1. Estimated solar system size in kW
|
| 85 |
-
2. Estimated daily solar output in kWh
|
| 86 |
-
3. Total system cost in ₹
|
| 87 |
-
4. Monthly savings in ₹
|
| 88 |
-
5. Payback period in years
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
Solar system cost per kW = ₹{solar_cost_per_kw}
|
| 94 |
|
| 95 |
-
|
| 96 |
-
"""
|
| 97 |
-
|
| 98 |
-
# Call Gemini API (just to double-check/validate if you want)
|
| 99 |
-
with st.spinner("Generating final estimate..."):
|
| 100 |
-
gemini_response = model.generate_content(prompt)
|
| 101 |
-
final_response = gemini_response.text.strip()
|
| 102 |
-
|
| 103 |
-
# Display calculated values directly (without trusting Gemini text output)
|
| 104 |
st.subheader("Solar Project Estimate")
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
-
st.error("
|
| 110 |
else:
|
| 111 |
-
st.warning("Please fill all
|
|
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
# Set page configuration first
|
| 11 |
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
|
| 12 |
|
| 13 |
+
# Initialize Gemini with the API key loaded from the .env file
|
| 14 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 15 |
if api_key:
|
| 16 |
genai.configure(api_key=api_key)
|
| 17 |
else:
|
| 18 |
st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.")
|
| 19 |
|
| 20 |
+
# Use better model: gemini-1.5-pro
|
| 21 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 22 |
|
| 23 |
# Load solar data
|
| 24 |
@st.cache_data
|
|
|
|
| 28 |
|
| 29 |
df = load_data()
|
| 30 |
|
| 31 |
+
# UI - Form for user input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
st.title("AI-based Solar Project Estimation Tool")
|
| 33 |
+
st.write("### Enter Your Details Below:")
|
| 34 |
|
| 35 |
with st.form("solar_form"):
|
| 36 |
state_options = df['State'].dropna().unique()
|
| 37 |
|
| 38 |
location = st.selectbox("Select your State", options=sorted(state_options))
|
| 39 |
roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
|
| 40 |
+
electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
|
| 41 |
|
| 42 |
submitted = st.form_submit_button("Get Estimate")
|
| 43 |
|
| 44 |
+
# Build the clean prompt for Gemini
|
| 45 |
+
def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw):
|
| 46 |
+
prompt = f"""
|
| 47 |
+
You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description:
|
| 48 |
+
|
| 49 |
+
Location: {location}
|
| 50 |
+
Roof size: {roof_size} sq meters
|
| 51 |
+
Monthly electricity bill: ₹{electricity_bill}
|
| 52 |
+
Average GHI: {ghi} kWh/m²/day
|
| 53 |
+
Solar system cost per kW: ₹{solar_cost_per_kw}
|
| 54 |
+
|
| 55 |
+
Respond strictly in this format (do not add anything extra):
|
| 56 |
+
|
| 57 |
+
Estimated solar system size in kW: <value>
|
| 58 |
+
Estimated daily solar output in kWh: <value>
|
| 59 |
+
Total system cost in ₹: <value>
|
| 60 |
+
Monthly savings in ₹: <value>
|
| 61 |
+
Payback period in years: <value>
|
| 62 |
+
"""
|
| 63 |
+
return prompt
|
| 64 |
+
|
| 65 |
+
# Generate the solar project estimate via Gemini
|
| 66 |
+
if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
| 67 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
| 68 |
|
| 69 |
if state_data is not None:
|
| 70 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
| 71 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
|
|
|
| 72 |
|
| 73 |
+
prompt_text = build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Call Gemini API
|
| 76 |
+
with st.spinner("Generating solar estimate with Gemini..."):
|
| 77 |
+
response = model.generate_content(prompt_text)
|
|
|
|
| 78 |
|
| 79 |
+
# Display clean structured output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
st.subheader("Solar Project Estimate")
|
| 81 |
+
|
| 82 |
+
estimated_data = response.text.strip().split("\n")
|
| 83 |
+
|
| 84 |
+
for point in estimated_data:
|
| 85 |
+
if ":" in point:
|
| 86 |
+
key, value = point.split(":", 1)
|
| 87 |
+
st.write(f"**{key.strip()}**: {value.strip()}")
|
| 88 |
else:
|
| 89 |
+
st.error("Sorry, the location entered does not match any available data.")
|
| 90 |
else:
|
| 91 |
+
st.warning("Please fill out all fields to see your solar project estimate.")
|