| import streamlit as st |
| import pandas as pd |
| import google.generativeai as genai |
| import os |
| from dotenv import load_dotenv |
| import plotly.graph_objects as go |
|
|
|
|
| |
| load_dotenv() |
|
|
| |
| st.set_page_config(page_title="☀️AI-Based Solar Project Estimation Tool", layout="centered") |
|
|
| |
| api_key = os.getenv("GOOGLE_API_KEY") |
| if api_key: |
| genai.configure(api_key=api_key) |
| else: |
| st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.") |
|
|
| |
| model = genai.GenerativeModel("gemini-1.5-pro") |
|
|
| |
| @st.cache_data |
| def load_data(): |
| df = pd.read_csv('https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/main/solar_data_india_2024.csv') |
| return df |
|
|
| df = load_data() |
|
|
| |
| def build_prompt(location, project_type, roof_size=None, desired_kwh=None, electricity_bill=None, ghi=None, solar_cost_per_kw=None): |
| if project_type == "Rooftop Solar": |
| prompt = f""" |
| You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description: |
| Project Type: Rooftop Solar |
| Location: {location} |
| Roof size: {roof_size} sq meters |
| Monthly electricity bill: ₹{electricity_bill} |
| Average GHI: {ghi} kWh/m²/day |
| Solar system cost per kW: ₹{solar_cost_per_kw} |
| Respond strictly in this format (do not add anything extra): |
| Estimated solar system size in kW: <value> |
| Estimated daily solar output in kWh: <value> |
| Total system cost in ₹: <value> |
| Monthly savings in ₹: <value> |
| Payback period in years: <value> |
| """ |
| else: |
| prompt = f""" |
| You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description: |
| Project Type: Ground Mount Solar |
| Location: {location} |
| Desired monthly solar production: {desired_kwh} kWh |
| Monthly electricity bill: ₹{electricity_bill} |
| Average GHI: {ghi} kWh/m²/day |
| Solar system cost per kW: ₹{solar_cost_per_kw} |
| Respond strictly in this format (do not add anything extra): |
| Required solar system size in kW: <value> |
| Estimated daily solar output in kWh: <value> |
| Total system cost in ₹: <value> |
| Monthly savings in ₹: <value> |
| Payback period in years: <value> |
| """ |
| return prompt |
|
|
| |
| st.title("☀️ AI-Based Solar Project Estimation Tool") |
| st.write("### Enter Your Details Below:") |
|
|
| with st.form("solar_form"): |
| state_options = df['State'].dropna().unique() |
| |
| location = st.selectbox("Select your State", options=sorted(state_options)) |
| |
| project_type = st.radio( |
| "Select Solar Project Type", |
| options=["Rooftop Solar", "Ground Mount Solar"] |
| ) |
|
|
| if project_type == "Rooftop Solar": |
| roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1) |
| electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0) |
| desired_kwh = None |
| else: |
| desired_kwh = st.number_input("Enter desired monthly solar electricity production (kWh)", min_value=1) |
| electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0) |
| roof_size = None |
|
|
| submitted = st.form_submit_button("Get Estimate") |
|
|
| |
| if submitted and location: |
| state_data = df[df['State'].str.contains(location, case=False)].iloc[0] |
| |
| if state_data is not None: |
| ghi = state_data['Avg_GHI (kWh/m²/day)'] |
| solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)'] |
|
|
| 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) |
| |
| |
| with st.spinner("Generating solar estimate with Gemini..."): |
| response = model.generate_content(prompt_text) |
| |
| |
| st.subheader("🔹 Solar Project Estimate") |
| |
| estimated_data = response.text.strip().split("\n") |
| |
| system_size_kw = None |
| monthly_savings_rs = None |
| total_system_cost = None |
| payback_period_years = None |
| |
| for point in estimated_data: |
| if ":" in point: |
| try: |
| key, value = point.split(":", 1) |
| key = key.strip() |
| value = value.strip() |
| |
| st.write(f"**{key}**: {value}") |
|
|
| if "Estimated solar system size" in key or "Required solar system size" in key: |
| system_size_kw = float(value.split()[0]) |
| if "Monthly savings" in key: |
| monthly_savings_rs = float(value.split()[0]) |
| if "Total system cost" in key: |
| total_system_cost = float(value.split()[0]) |
| if "Payback period" in key: |
| payback_period_years = float(value.split()[0]) |
|
|
| except ValueError: |
| st.warning("There was an issue processing the response. Please try again.") |
|
|
| |
| |
| if system_size_kw is not None and total_system_cost is not None: |
| st.subheader("📊 Visual Summary") |
| |
| |
| fig = go.Figure(data=[ |
| go.Bar( |
| name="System Size (kW)", |
| x=["Solar System Size", "Daily Output", "Total Cost", "Monthly Savings", "Payback Period"], |
| y=[system_size_kw, daily_output_kwh, total_system_cost, monthly_savings_rs, payback_period_years], |
| marker_color='#636EFA' |
| ), |
| go.Bar( |
| name="Financials", |
| x=["Solar System Size", "Daily Output", "Total Cost", "Monthly Savings", "Payback Period"], |
| y=[0, 0, total_system_cost, monthly_savings_rs, payback_period_years], |
| marker_color='#00CC96' |
| ) |
| ]) |
| |
| |
| fig.update_layout( |
| barmode='group', |
| title="Comparison of Solar System Parameters", |
| yaxis_title="Values", |
| xaxis_title="Parameters" |
| ) |
| st.plotly_chart(fig, use_container_width=True) |
|
|
| else: |
| st.error("Sorry, the location entered does not match any available data.") |
| else: |
| st.warning("Please fill out all fields to see your solar project estimate.") |
| |