Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import math | |
| import matplotlib.pyplot as plt | |
| import numpy_financial as npf | |
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer | |
| from reportlab.lib.pagesizes import A4 | |
| from reportlab.lib.styles import getSampleStyleSheet | |
| # =============================== | |
| # PAGE CONFIG | |
| # =============================== | |
| st.set_page_config(layout="wide") | |
| st.title("π΅π° AI Solar Intelligence Platform (Ultra Premium)") | |
| # =============================== | |
| # ADVANCED CONFIG | |
| # =============================== | |
| SYSTEM_LOSSES = 0.20 | |
| DEMAND_FACTOR_INDUSTRIAL = 0.75 | |
| CITY_IRRADIANCE_INDEX = { | |
| "Karachi": 6.2, | |
| "Lahore": 5.5, | |
| "Islamabad": 5.2, | |
| "Peshawar": 5.6, | |
| "Quetta": 6.5, | |
| } | |
| # Pricing | |
| PANEL_COST_PER_WATT = 55 | |
| INSTALLATION_COST_PER_WATT = 35 | |
| BATTERY_COST_5KWH = 95000 | |
| # Appliance Database (Expanded) | |
| LOAD_LIBRARY = { | |
| "Home": { | |
| "Lighting": 60, | |
| "Fans": 400, | |
| "Fridge": 200, | |
| "AC": 1500 | |
| }, | |
| "Industrial": { | |
| "Motors": 5000, | |
| "Compressors": 8000, | |
| "CNC Machines": 2000 | |
| } | |
| } | |
| # =============================== | |
| # AI RECOMMENDATION ENGINE | |
| # =============================== | |
| def ai_solar_recommendation(load_kw, sunlight): | |
| optimal_system = (load_kw / sunlight) * 1.2 | |
| return round(optimal_system, 2) | |
| def financial_projection(cost, daily_kwh): | |
| cashflows = [] | |
| for year in range(1, 26): | |
| price_escalation = (1 + 0.07) ** year | |
| savings = daily_kwh * 30 * 55 * price_escalation | |
| cashflows.append(savings * 12) | |
| npv = npf.npv(0.05, [-cost] + cashflows) | |
| irr = npf.irr([-cost] + cashflows) | |
| payback = next((i for i, cf in enumerate(np.cumsum(cashflows), 1) | |
| if cf >= cost), None) | |
| return cashflows, npv, irr, payback, np.cumsum(cashflows) | |
| # =============================== | |
| # USER INPUT LAYER | |
| # =============================== | |
| user_type = st.selectbox( | |
| "Select Client Type", | |
| ["Homeowner", "Commercial Business", "Industrial Investor"] | |
| ) | |
| city = st.selectbox("Select City", list(CITY_IRRADIANCE_INDEX.keys())) | |
| sunlight = CITY_IRRADIANCE_INDEX[city] | |
| # Load Selection | |
| if user_type == "Homeowner": | |
| load_choice = st.multiselect( | |
| "Select Home Loads", | |
| ["Lighting", "Fans", "Fridge", "AC"] | |
| ) | |
| elif user_type == "Commercial Business": | |
| load_choice = st.multiselect( | |
| "Select Business Loads", | |
| ["Lighting", "Computers", "AC", "Machinery"] | |
| ) | |
| else: | |
| load_choice = st.multiselect( | |
| "Select Industrial Loads", | |
| ["Motors", "Compressors", "CNC Machines"] | |
| ) | |
| hours = st.slider("Daily Usage Hours", 1, 24, 8) | |
| # =============================== | |
| # CALCULATIONS | |
| # =============================== | |
| if st.button("π Run AI Solar Analysis"): | |
| if len(load_choice) == 0: | |
| st.error("Please select at least one load") | |
| else: | |
| # Load estimation | |
| load_watts = 0 | |
| for item in load_choice: | |
| if item in ["Lighting", "Fans"]: | |
| load_watts += 200 | |
| elif item == "Fridge": | |
| load_watts += 200 | |
| elif item == "AC": | |
| load_watts += 1500 | |
| elif item in ["Motors", "Compressors", "CNC Machines"]: | |
| load_watts += 3000 | |
| daily_kwh = (load_watts * hours) / 1000 | |
| daily_kwh = daily_kwh / (1 - SYSTEM_LOSSES) | |
| # AI Recommendation | |
| system_kw = ai_solar_recommendation(daily_kwh, sunlight) | |
| system_cost = system_kw * 1000 * (PANEL_COST_PER_WATT + INSTALLATION_COST_PER_WATT) | |
| # Financials | |
| cashflows, npv, irr, payback, cumulative = financial_projection(system_cost, daily_kwh) | |
| # Results Display | |
| st.subheader("π AI Analysis Results") | |
| st.write(f"Recommended Solar System Size: {system_kw} kW") | |
| st.write(f"Daily Energy Consumption: {round(daily_kwh,2)} kWh") | |
| st.write(f"Estimated Installation Cost: PKR {round(system_cost):,}") | |
| st.write(f"NPV (25 Years): PKR {round(npv,2):,}") | |
| st.write(f"IRR: {round(irr*100,2)} %") | |
| st.write(f"Payback Period: {payback} Years") | |
| # Charts | |
| st.subheader("π Investment Growth Projection") | |
| plt.figure(figsize=(10,4)) | |
| plt.plot(range(1,26), cumulative) | |
| plt.axhline(system_cost, linestyle="--") | |
| st.pyplot(plt) | |
| # WhatsApp Style Summary | |
| st.subheader("π± Proposal Summary") | |
| summary_text = f""" | |
| Solar Proposal Summary | |
| City: {city} | |
| Recommended System: {system_kw} kW | |
| Cost Estimate: PKR {round(system_cost):,} | |
| Payback Period: {payback} Years | |
| IRR: {round(irr*100,2)}% | |
| """ | |
| st.text_area("Copy for WhatsApp / Proposal", summary_text, height=200) |