Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,15 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import google.generativeai as genai
|
| 4 |
import os
|
| 5 |
-
from io import StringIO
|
| 6 |
-
import csv
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# Load environment variables from .env file
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
# Set page configuration
|
| 14 |
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
|
| 15 |
|
| 16 |
-
# Initialize Gemini
|
| 17 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 18 |
if api_key:
|
| 19 |
genai.configure(api_key=api_key)
|
|
@@ -30,67 +27,85 @@ def load_data():
|
|
| 30 |
|
| 31 |
df = load_data()
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
st.title("AI-based Solar Project Estimation Tool")
|
| 35 |
-
st.write("### Enter Your Details
|
| 36 |
|
| 37 |
with st.form("solar_form"):
|
| 38 |
state_options = df['State'].dropna().unique()
|
| 39 |
|
| 40 |
location = st.selectbox("Select your State", options=sorted(state_options))
|
| 41 |
roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
|
| 42 |
-
|
| 43 |
|
| 44 |
submitted = st.form_submit_button("Get Estimate")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw):
|
| 48 |
-
prompt = f"""
|
| 49 |
-
Estimate the solar system for the location '{location}' based on the following details:
|
| 50 |
-
- Roof size: {roof_size} sq meters
|
| 51 |
-
- Monthly electricity bill: ₹{electricity_bill}
|
| 52 |
-
- Average GHI (solar radiation) for {location}: {ghi} kWh/m²/day
|
| 53 |
-
- Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
|
| 54 |
-
Provide the following:
|
| 55 |
-
1. Estimated solar system size in kW
|
| 56 |
-
2. Estimated daily solar output in kWh
|
| 57 |
-
3. Total system cost in ₹
|
| 58 |
-
4. Monthly savings in ₹
|
| 59 |
-
5. Payback period in years
|
| 60 |
-
"""
|
| 61 |
-
return prompt
|
| 62 |
-
|
| 63 |
-
# Generate the solar project estimate via Gemini
|
| 64 |
-
if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
| 65 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
| 66 |
|
| 67 |
if state_data is not None:
|
| 68 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
| 69 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.subheader("Solar Project Estimate")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Display only the required points: system size, cost, savings, and payback period
|
| 84 |
-
for point in estimated_data:
|
| 85 |
-
if ":" in point: # Only process lines with a colon
|
| 86 |
-
try:
|
| 87 |
-
# Extract the value after the colon
|
| 88 |
-
key, value = point.split(":", 1) # Split into two parts only
|
| 89 |
-
st.write(f"{key.strip()}: {value.strip()}")
|
| 90 |
-
except ValueError:
|
| 91 |
-
# Handle cases where the line doesn't split into two parts
|
| 92 |
-
st.warning("There was an issue processing the response.")
|
| 93 |
else:
|
| 94 |
-
st.error("
|
| 95 |
else:
|
| 96 |
-
st.warning("Please fill
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
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)
|
|
|
|
| 27 |
|
| 28 |
df = load_data()
|
| 29 |
|
| 30 |
+
# Solar Calculation Function (not relying on Gemini for calculation)
|
| 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 |
+
monthly_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
|
| 67 |
|
| 68 |
submitted = st.form_submit_button("Get Estimate")
|
| 69 |
|
| 70 |
+
if submitted and location and roof_size > 0 and monthly_bill >= 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Calculate estimates
|
| 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 |
+
Roof size = {roof_size} sq meters
|
| 91 |
+
Monthly bill = ₹{monthly_bill}
|
| 92 |
+
GHI = {ghi} kWh/m²/day
|
| 93 |
+
Solar system cost per kW = ₹{solar_cost_per_kw}
|
| 94 |
|
| 95 |
+
Use no description. Only numeric values clearly.
|
| 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 |
+
for key, value in estimates.items():
|
| 107 |
+
st.write(f"{key}: {value}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
+
st.error("Location data not found. Please select a valid state.")
|
| 110 |
else:
|
| 111 |
+
st.warning("Please fill all the fields.")
|