Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,17 @@ import plotly.graph_objects as go
|
|
9 |
# Load environment variables from .env file
|
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 |
-
# Initialize Gemini with the API key
|
16 |
api_key = os.getenv("GOOGLE_API_KEY")
|
17 |
if api_key:
|
18 |
genai.configure(api_key=api_key)
|
19 |
else:
|
20 |
st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.")
|
21 |
|
22 |
-
# Use
|
23 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
24 |
|
25 |
# Load solar data
|
@@ -30,23 +30,12 @@ def load_data():
|
|
30 |
|
31 |
df = load_data()
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
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 |
-
electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
|
43 |
-
|
44 |
-
submitted = st.form_submit_button("Get Estimate")
|
45 |
-
|
46 |
-
# Build the clean prompt for Gemini
|
47 |
-
def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw):
|
48 |
-
prompt = f"""
|
49 |
You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description:
|
|
|
50 |
Location: {location}
|
51 |
Roof size: {roof_size} sq meters
|
52 |
Monthly electricity bill: ₹{electricity_bill}
|
@@ -58,25 +47,66 @@ Estimated daily solar output in kWh: <value>
|
|
58 |
Total system cost in ₹: <value>
|
59 |
Monthly savings in ₹: <value>
|
60 |
Payback period in years: <value>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
"""
|
62 |
return prompt
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# Generate the solar project estimate via Gemini
|
65 |
-
if submitted and location
|
66 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
67 |
|
68 |
if state_data is not None:
|
69 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
70 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
71 |
-
|
72 |
-
prompt_text = build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw)
|
73 |
|
74 |
# Call Gemini API
|
75 |
with st.spinner("Generating solar estimate with Gemini..."):
|
76 |
response = model.generate_content(prompt_text)
|
77 |
|
78 |
-
# Display
|
79 |
-
st.subheader("Solar Project Estimate")
|
80 |
|
81 |
estimated_data = response.text.strip().split("\n")
|
82 |
|
@@ -93,7 +123,7 @@ if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
|
93 |
|
94 |
st.write(f"**{key}**: {value}")
|
95 |
|
96 |
-
if "Estimated solar system size" in key:
|
97 |
system_size_kw = float(value.split()[0])
|
98 |
if "Monthly savings" in key:
|
99 |
monthly_savings_rs = float(value.split()[0])
|
@@ -101,7 +131,7 @@ if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
|
101 |
total_system_cost = float(value.split()[0])
|
102 |
|
103 |
except ValueError:
|
104 |
-
st.warning("There was an issue processing the response.")
|
105 |
|
106 |
# Show Graph if values are available
|
107 |
if total_system_cost is not None and monthly_savings_rs is not None:
|
@@ -120,4 +150,5 @@ if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
|
120 |
else:
|
121 |
st.error("Sorry, the location entered does not match any available data.")
|
122 |
else:
|
123 |
-
st.
|
|
|
|
9 |
# Load environment variables from .env file
|
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 |
+
# Initialize Gemini with the API key
|
16 |
api_key = os.getenv("GOOGLE_API_KEY")
|
17 |
if api_key:
|
18 |
genai.configure(api_key=api_key)
|
19 |
else:
|
20 |
st.error("API key is missing. Please set the GOOGLE_API_KEY environment variable.")
|
21 |
|
22 |
+
# Use Gemini-1.5-Pro model
|
23 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
24 |
|
25 |
# Load solar data
|
|
|
30 |
|
31 |
df = load_data()
|
32 |
|
33 |
+
# Build prompt for Gemini
|
34 |
+
def build_prompt(location, project_type, roof_size=None, desired_kwh=None, electricity_bill=None, ghi=None, solar_cost_per_kw=None):
|
35 |
+
if project_type == "Rooftop Solar":
|
36 |
+
prompt = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description:
|
38 |
+
Project Type: Rooftop Solar
|
39 |
Location: {location}
|
40 |
Roof size: {roof_size} sq meters
|
41 |
Monthly electricity bill: ₹{electricity_bill}
|
|
|
47 |
Total system cost in ₹: <value>
|
48 |
Monthly savings in ₹: <value>
|
49 |
Payback period in years: <value>
|
50 |
+
"""
|
51 |
+
else: # Ground Mount Solar
|
52 |
+
prompt = f"""
|
53 |
+
You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description:
|
54 |
+
Project Type: Ground Mount Solar
|
55 |
+
Location: {location}
|
56 |
+
Desired monthly solar production: {desired_kwh} kWh
|
57 |
+
Monthly electricity bill: ₹{electricity_bill}
|
58 |
+
Average GHI: {ghi} kWh/m²/day
|
59 |
+
Solar system cost per kW: ₹{solar_cost_per_kw}
|
60 |
+
Respond strictly in this format (do not add anything extra):
|
61 |
+
Required solar system size in kW: <value>
|
62 |
+
Estimated daily solar output in kWh: <value>
|
63 |
+
Total system cost in ₹: <value>
|
64 |
+
Monthly savings in ₹: <value>
|
65 |
+
Payback period in years: <value>
|
66 |
"""
|
67 |
return prompt
|
68 |
|
69 |
+
# UI - Form for user input
|
70 |
+
st.title("☀️ AI-based Solar Project Estimation Tool")
|
71 |
+
st.write("### Enter Your Details Below:")
|
72 |
+
|
73 |
+
with st.form("solar_form"):
|
74 |
+
state_options = df['State'].dropna().unique()
|
75 |
+
|
76 |
+
location = st.selectbox("Select your State", options=sorted(state_options))
|
77 |
+
|
78 |
+
project_type = st.radio(
|
79 |
+
"Select Solar Project Type",
|
80 |
+
options=["Rooftop Solar", "Ground Mount Solar"]
|
81 |
+
)
|
82 |
+
|
83 |
+
if project_type == "Rooftop Solar":
|
84 |
+
roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
|
85 |
+
electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
|
86 |
+
desired_kwh = None
|
87 |
+
else:
|
88 |
+
desired_kwh = st.number_input("Enter desired monthly solar electricity production (kWh)", min_value=1)
|
89 |
+
electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
|
90 |
+
roof_size = None
|
91 |
+
|
92 |
+
submitted = st.form_submit_button("Get Estimate")
|
93 |
+
|
94 |
# Generate the solar project estimate via Gemini
|
95 |
+
if submitted and location:
|
96 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
97 |
|
98 |
if state_data is not None:
|
99 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
100 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
101 |
+
|
102 |
+
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)
|
103 |
|
104 |
# Call Gemini API
|
105 |
with st.spinner("Generating solar estimate with Gemini..."):
|
106 |
response = model.generate_content(prompt_text)
|
107 |
|
108 |
+
# Display structured output
|
109 |
+
st.subheader("🔹 Solar Project Estimate")
|
110 |
|
111 |
estimated_data = response.text.strip().split("\n")
|
112 |
|
|
|
123 |
|
124 |
st.write(f"**{key}**: {value}")
|
125 |
|
126 |
+
if "Estimated solar system size" in key or "Required solar system size" in key:
|
127 |
system_size_kw = float(value.split()[0])
|
128 |
if "Monthly savings" in key:
|
129 |
monthly_savings_rs = float(value.split()[0])
|
|
|
131 |
total_system_cost = float(value.split()[0])
|
132 |
|
133 |
except ValueError:
|
134 |
+
st.warning("There was an issue processing the response. Please try again.")
|
135 |
|
136 |
# Show Graph if values are available
|
137 |
if total_system_cost is not None and monthly_savings_rs is not None:
|
|
|
150 |
else:
|
151 |
st.error("Sorry, the location entered does not match any available data.")
|
152 |
else:
|
153 |
+
st.info("Please fill out all fields to get your solar project estimate.")
|
154 |
+
|