File size: 4,511 Bytes
b555fe1 a851214 372b3f3 a851214 c565f89 0cdc019 23fc541 0c4f7e8 0cdc019 618f85b 0c4f7e8 c565f89 27cc353 0cdc019 618f85b 0cdc019 372b3f3 a851214 b555fe1 a851214 27cc353 a851214 b555fe1 b0848d8 b555fe1 b0848d8 2d29e28 b0848d8 a851214 e799f84 a851214 b0848d8 2d29e28 5aee751 54c70ee b555fe1 b003a37 b555fe1 a851214 d36a0d6 bcfe0fa e799f84 0c4f7e8 9beb932 e799f84 0c4f7e8 e799f84 9beb932 24c1abd e799f84 24c1abd 0c4f7e8 24c1abd b555fe1 fdd0b88 |
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 |
import streamlit as st
import pandas as pd
import google.generativeai as genai
import os
from io import StringIO
import csv
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Set page configuration
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
# Initialize Gemini with the API key loaded from the .env file
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-flash")
# Load solar data
@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()
# UI - Form for user input
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))
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)
submitted = st.form_submit_button("Get Estimate")
# Build the prompt for Gemini
def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw):
prompt = f"""
Estimate the solar system for the location '{location}' based on the following details:
- Roof size: {roof_size} sq meters
- Monthly electricity bill: ₹{electricity_bill}
- Average GHI (solar radiation) for {location}: {ghi} kWh/m²/day
- Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
Provide the following:
1. Estimated solar system size in kW
2. Estimated daily solar output in kWh
3. Total system cost in ₹
4. Monthly savings in ₹
5. Payback period in years
"""
return prompt
# Generate the solar project estimate via Gemini
if submitted and location and roof_size > 0 and electricity_bill >= 0:
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, roof_size, electricity_bill, ghi, solar_cost_per_kw)
# Call Gemini API once for all the batch generation
with st.spinner("Generating solar estimate with Gemini..."):
response = model.generate_content(prompt_text)
# Debug: Print the raw response to see the structure
st.write("Raw Response:", response.text)
# Display structured output with only the requested points
st.subheader("Solar Project Estimate")
# Break down the response into structured points
estimated_data = response.text.strip().split("\n")
# Initialize values for each field
system_size = None
total_cost = None
monthly_savings = None
payback_period = None
# Parse the response to find and assign the correct values
for point in estimated_data:
if "solar system size" in point.lower():
system_size = point.split(":")[1].strip()
elif "total system cost" in point.lower():
total_cost = point.split(":")[1].strip()
elif "monthly savings" in point.lower():
monthly_savings = point.split(":")[1].strip()
elif "payback period" in point.lower():
payback_period = point.split(":")[1].strip()
# Ensure that we only display numeric values for the fields
if system_size and total_cost and monthly_savings and payback_period:
st.write(f"1. Estimated solar system size in kW: {system_size}")
st.write(f"2. Total system cost in ₹: {total_cost}")
st.write(f"3. Monthly savings in ₹: {monthly_savings}")
st.write(f"4. Payback period in years: {payback_period}")
else:
st.error("Unable to extract valid data from the response.")
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.")
|