Download app.py from MLDeveloper/AI_based_Solar_Project_Estimation_Tool: direct link, hf CLI and curl.
- Browser
- Download file 4.21 kB
-
https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/e3741a5b83dbb02bf9f1a207e02e40f957eb2b52/app.py
- Command line
-
hf download hf://spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool@e3741a5b83dbb02bf9f1a207e02e40f957eb2b52/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/e3741a5b83dbb02bf9f1a207e02e40f957eb2b52/app.py
4.21 kB
| import streamlit as st | |
| import pandas as pd | |
| import google.generativeai as genai | |
| import os | |
| from io import StringIO | |
| import csv | |
| import streamlit as st | |
| import pandas as pd | |
| import google.generativeai as genai | |
| import os | |
| import csv | |
| from io import StringIO | |
| # Set page configuration first | |
| st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered") | |
| # Initialize Gemini | |
| genai.configure(api_key=os.getenv("GEMINI_API_KEY")) | |
| model = genai.GenerativeModel("gemini-1.5-flash") | |
| # 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 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: | |
| 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 | |
| if prompt_text: | |
| with st.spinner("Generating solar estimate with Gemini..."): | |
| response = model.generate_content(prompt_text) | |
| st.subheader("Solar Project Estimate") | |
| st.text_area("Generated Estimate", response.text, height=200) | |
| else: | |
| st.warning("Please check the inputs again.") | |
| 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.") | |
| # Batch CSV Export | |
| st.markdown("### Export Solar Estimates") | |
| batch = st.number_input("How many estimates to generate?", min_value=1, max_value=100, value=5) | |
| if st.button("Generate Batch & Download CSV"): | |
| if location and roof_size > 0 and electricity_bill >= 0: | |
| csv_buffer = StringIO() | |
| writer = csv.writer(csv_buffer) | |
| writer.writerow(["Sequence_no", "Solar Estimate"]) | |
| # Call Gemini API once for the batch generation | |
| with st.spinner("Generating batch estimates..."): | |
| batch_prompts = [build_prompt(location, roof_size, electricity_bill, ghi, solar_cost_per_kw) for _ in range(batch)] | |
| batch_responses = [model.generate_content(prompt) for prompt in batch_prompts] | |
| # Process the batch responses and write to CSV | |
| for i, response in enumerate(batch_responses, 1): | |
| writer.writerow([i, response.text.strip()]) | |
| st.download_button( | |
| label="Download Solar Estimates CSV", | |
| data=csv_buffer.getvalue(), | |
| file_name="solar_estimates.csv", | |
| mime="text/csv" | |
| ) | |
| else: | |
| st.warning("Please fill out all fields to generate batch estimates.") | |