Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,24 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
import os
|
6 |
-
import
|
7 |
-
from dotenv import load_dotenv
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
|
17 |
|
18 |
-
#
|
19 |
-
df = pd.read_csv('https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/main/solar_data_india_2024.csv')
|
20 |
-
|
21 |
-
# Streamlit UI
|
22 |
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
|
23 |
st.title("AI-based Solar Project Estimation Tool")
|
24 |
-
|
25 |
st.write("### Enter Your Details Below:")
|
26 |
|
27 |
with st.form("solar_form"):
|
@@ -33,6 +30,25 @@ with st.form("solar_form"):
|
|
33 |
|
34 |
submitted = st.form_submit_button("Get Estimate")
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
37 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
38 |
|
@@ -40,50 +56,46 @@ if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
|
40 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
41 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
42 |
|
43 |
-
prompt_text =
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
- Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
|
49 |
-
|
50 |
-
Provide:
|
51 |
-
1. Estimated solar system size in kW
|
52 |
-
2. Estimated daily solar output in kWh
|
53 |
-
3. Total system cost in ₹
|
54 |
-
4. Monthly savings in ₹
|
55 |
-
5. Payback period in years
|
56 |
-
"""
|
57 |
-
|
58 |
-
# Define the headers for the API request
|
59 |
-
headers = {
|
60 |
-
"Authorization": f"Bearer {GEMINI_API_KEY}",
|
61 |
-
"Content-Type": "application/json"
|
62 |
-
}
|
63 |
-
|
64 |
-
# Define the request payload
|
65 |
-
payload = {
|
66 |
-
"model": "gemini-1.5-flash",
|
67 |
-
"messages": [{"role": "system", "content": "You are a helpful assistant."},
|
68 |
-
{"role": "user", "content": prompt_text}],
|
69 |
-
"temperature": 0.7
|
70 |
-
}
|
71 |
-
|
72 |
-
# Make the API request
|
73 |
-
response = requests.post(GEMINI_API_URL, json=payload, headers=headers)
|
74 |
-
|
75 |
-
# Check the response from the API
|
76 |
-
if response.status_code == 200:
|
77 |
-
result = response.json()
|
78 |
-
generated_content = result['choices'][0]['message']['content']
|
79 |
-
|
80 |
-
# Display the generated content (solar estimates)
|
81 |
st.subheader("Solar Project Estimate")
|
82 |
-
st.
|
83 |
else:
|
84 |
-
st.
|
85 |
-
|
86 |
else:
|
87 |
st.error("Sorry, the location entered does not match any available data.")
|
88 |
else:
|
89 |
st.warning("Please fill out all fields to see your solar project estimate.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import google.generativeai as genai
|
|
|
4 |
import os
|
5 |
+
from io import StringIO
|
|
|
6 |
|
7 |
+
# Initialize Gemini
|
8 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
9 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
10 |
|
11 |
+
# Load solar data
|
12 |
+
@st.cache_data
|
13 |
+
def load_data():
|
14 |
+
df = pd.read_csv('https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/main/solar_data_india_2024.csv')
|
15 |
+
return df
|
16 |
|
17 |
+
df = load_data()
|
|
|
18 |
|
19 |
+
# UI - Form for user input
|
|
|
|
|
|
|
20 |
st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
|
21 |
st.title("AI-based Solar Project Estimation Tool")
|
|
|
22 |
st.write("### Enter Your Details Below:")
|
23 |
|
24 |
with st.form("solar_form"):
|
|
|
30 |
|
31 |
submitted = st.form_submit_button("Get Estimate")
|
32 |
|
33 |
+
# Build the prompt for Gemini
|
34 |
+
def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw):
|
35 |
+
prompt = f"""
|
36 |
+
Estimate the solar system for the location '{location}' based on the following details:
|
37 |
+
- Roof size: {roof_size} sq meters
|
38 |
+
- Monthly electricity bill: ₹{electricity_bill}
|
39 |
+
- Average GHI (solar radiation) for {location}: {ghi} kWh/m²/day
|
40 |
+
- Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
|
41 |
+
|
42 |
+
Provide:
|
43 |
+
1. Estimated solar system size in kW
|
44 |
+
2. Estimated daily solar output in kWh
|
45 |
+
3. Total system cost in ₹
|
46 |
+
4. Monthly savings in ₹
|
47 |
+
5. Payback period in years
|
48 |
+
"""
|
49 |
+
return prompt
|
50 |
+
|
51 |
+
# Generate the solar project estimate via Gemini
|
52 |
if submitted and location and roof_size > 0 and electricity_bill >= 0:
|
53 |
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
54 |
|
|
|
56 |
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
57 |
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
58 |
|
59 |
+
prompt_text = build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw)
|
60 |
+
|
61 |
+
if prompt_text:
|
62 |
+
with st.spinner("Generating solar estimate with Gemini..."):
|
63 |
+
response = model.generate_content(prompt_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
st.subheader("Solar Project Estimate")
|
65 |
+
st.text_area("Generated Estimate", response.text, height=200)
|
66 |
else:
|
67 |
+
st.warning("Please check the inputs again.")
|
|
|
68 |
else:
|
69 |
st.error("Sorry, the location entered does not match any available data.")
|
70 |
else:
|
71 |
st.warning("Please fill out all fields to see your solar project estimate.")
|
72 |
+
|
73 |
+
# Batch CSV Export
|
74 |
+
st.markdown("### Export Solar Estimates")
|
75 |
+
batch = st.number_input("How many estimates to generate?", min_value=1, max_value=100, value=5)
|
76 |
+
|
77 |
+
if st.button("Generate Batch & Download CSV"):
|
78 |
+
if location and roof_size > 0 and electricity_bill >= 0:
|
79 |
+
csv_buffer = StringIO()
|
80 |
+
writer = csv.writer(csv_buffer)
|
81 |
+
writer.writerow(["Sequence_no", "Solar Estimate"])
|
82 |
+
|
83 |
+
with st.spinner("Generating estimates..."):
|
84 |
+
for i in range(1, batch + 1):
|
85 |
+
state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
|
86 |
+
ghi = state_data['Avg_GHI (kWh/m²/day)']
|
87 |
+
solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
|
88 |
+
prompt_text = build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw)
|
89 |
+
|
90 |
+
# Generate the estimate using Gemini
|
91 |
+
response = model.generate_content(prompt_text)
|
92 |
+
writer.writerow([i, response.text.strip()])
|
93 |
+
|
94 |
+
st.download_button(
|
95 |
+
label="Download Solar Estimates CSV",
|
96 |
+
data=csv_buffer.getvalue(),
|
97 |
+
file_name="solar_estimates.csv",
|
98 |
+
mime="text/csv"
|
99 |
+
)
|
100 |
+
else:
|
101 |
+
st.warning("Please fill out all fields to generate batch estimates.")
|