Download app.py from MLDeveloper/AI_based_Solar_Project_Estimation_Tool: direct link, hf CLI and curl.
- Browser
- Download file 4.79 kB
-
https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/0529bdc10fc4d4359ea10e5bb10eaddf736817e1/app.py
- Command line
-
hf download hf://spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool@0529bdc10fc4d4359ea10e5bb10eaddf736817e1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/0529bdc10fc4d4359ea10e5bb10eaddf736817e1/app.py
4.79 kB
| 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 environment variables from .env file | |
| load_dotenv() | |
| # Set page configuration first | |
| 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.") | |
| # Use better model: gemini-1.5-pro | |
| model = genai.GenerativeModel("gemini-1.5-pro") | |
| # Load solar 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 clean prompt for Gemini | |
| def build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw): | |
| prompt = f""" | |
| You are a solar project estimator tool. Based on the following details, calculate and return only the values without any extra description: | |
| 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> | |
| """ | |
| 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 | |
| with st.spinner("Generating solar estimate with Gemini..."): | |
| response = model.generate_content(prompt_text) | |
| # Display clean structured output | |
| st.subheader("Solar Project Estimate") | |
| estimated_data = response.text.strip().split("\n") | |
| for point in estimated_data: | |
| if ":" in point: | |
| key, value = point.split(":", 1) | |
| st.write(f"**{key.strip()}**: {value.strip()}") | |
| # Variables to store extracted values for graph | |
| system_size_kw = None | |
| monthly_savings_rs = 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: | |
| system_size_kw = float(value.split()[0]) | |
| if "Monthly savings" in key: | |
| monthly_savings_rs = float(value.split()[0]) | |
| except ValueError: | |
| st.warning("There was an issue processing the response.") | |
| # Show Graph if available | |
| if system_size_kw is not None and monthly_savings_rs is not None: | |
| st.subheader("📊 Visual Summary") | |
| fig = go.Figure(data=[ | |
| go.Bar(name='Estimate', | |
| x=['Solar System Size (kW)', 'Monthly Savings (₹)'], | |
| y=[system_size_kw, monthly_savings_rs], | |
| marker_color=['#636EFA', '#00CC96']) | |
| ]) | |
| fig.update_layout(title="Solar Project Estimates", yaxis_title="Values", xaxis_title="Parameters") | |
| st.plotly_chart(fig) | |
| 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.") | |