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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import math
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy_financial as npf
|
| 7 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 8 |
+
from reportlab.lib.pagesizes import A4
|
| 9 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 10 |
+
|
| 11 |
+
# ===============================
|
| 12 |
+
# PAGE CONFIG
|
| 13 |
+
# ===============================
|
| 14 |
+
st.set_page_config(layout="wide")
|
| 15 |
+
st.title("π΅π° AI Solar Intelligence Platform (Ultra Premium)")
|
| 16 |
+
|
| 17 |
+
# ===============================
|
| 18 |
+
# ADVANCED CONFIG
|
| 19 |
+
# ===============================
|
| 20 |
+
|
| 21 |
+
SYSTEM_LOSSES = 0.20
|
| 22 |
+
DEMAND_FACTOR_INDUSTRIAL = 0.75
|
| 23 |
+
|
| 24 |
+
CITY_IRRADIANCE_INDEX = {
|
| 25 |
+
"Karachi": 6.2,
|
| 26 |
+
"Lahore": 5.5,
|
| 27 |
+
"Islamabad": 5.2,
|
| 28 |
+
"Peshawar": 5.6,
|
| 29 |
+
"Quetta": 6.5,
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Pricing
|
| 33 |
+
PANEL_COST_PER_WATT = 55
|
| 34 |
+
INSTALLATION_COST_PER_WATT = 35
|
| 35 |
+
BATTERY_COST_5KWH = 95000
|
| 36 |
+
|
| 37 |
+
# Appliance Database (Expanded)
|
| 38 |
+
LOAD_LIBRARY = {
|
| 39 |
+
"Home": {
|
| 40 |
+
"Lighting": 60,
|
| 41 |
+
"Fans": 400,
|
| 42 |
+
"Fridge": 200,
|
| 43 |
+
"AC": 1500
|
| 44 |
+
},
|
| 45 |
+
"Industrial": {
|
| 46 |
+
"Motors": 5000,
|
| 47 |
+
"Compressors": 8000,
|
| 48 |
+
"CNC Machines": 2000
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# ===============================
|
| 53 |
+
# AI RECOMMENDATION ENGINE
|
| 54 |
+
# ===============================
|
| 55 |
+
|
| 56 |
+
def ai_solar_recommendation(load_kw, sunlight):
|
| 57 |
+
optimal_system = (load_kw / sunlight) * 1.2
|
| 58 |
+
return round(optimal_system, 2)
|
| 59 |
+
|
| 60 |
+
def financial_projection(cost, daily_kwh):
|
| 61 |
+
cashflows = []
|
| 62 |
+
for year in range(1, 26):
|
| 63 |
+
price_escalation = (1 + 0.07) ** year
|
| 64 |
+
savings = daily_kwh * 30 * 55 * price_escalation
|
| 65 |
+
cashflows.append(savings * 12)
|
| 66 |
+
|
| 67 |
+
npv = npf.npv(0.05, [-cost] + cashflows)
|
| 68 |
+
irr = npf.irr([-cost] + cashflows)
|
| 69 |
+
|
| 70 |
+
payback = next((i for i, cf in enumerate(np.cumsum(cashflows), 1)
|
| 71 |
+
if cf >= cost), None)
|
| 72 |
+
|
| 73 |
+
return cashflows, npv, irr, payback, np.cumsum(cashflows)
|
| 74 |
+
|
| 75 |
+
# ===============================
|
| 76 |
+
# USER INPUT LAYER
|
| 77 |
+
# ===============================
|
| 78 |
+
|
| 79 |
+
user_type = st.selectbox(
|
| 80 |
+
"Select Client Type",
|
| 81 |
+
["Homeowner", "Commercial Business", "Industrial Investor"]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
city = st.selectbox("Select City", list(CITY_IRRADIANCE_INDEX.keys()))
|
| 85 |
+
sunlight = CITY_IRRADIANCE_INDEX[city]
|
| 86 |
+
|
| 87 |
+
# Load Selection
|
| 88 |
+
if user_type == "Homeowner":
|
| 89 |
+
load_choice = st.multiselect(
|
| 90 |
+
"Select Home Loads",
|
| 91 |
+
["Lighting", "Fans", "Fridge", "AC"]
|
| 92 |
+
)
|
| 93 |
+
elif user_type == "Commercial Business":
|
| 94 |
+
load_choice = st.multiselect(
|
| 95 |
+
"Select Business Loads",
|
| 96 |
+
["Lighting", "Computers", "AC", "Machinery"]
|
| 97 |
+
)
|
| 98 |
+
else:
|
| 99 |
+
load_choice = st.multiselect(
|
| 100 |
+
"Select Industrial Loads",
|
| 101 |
+
["Motors", "Compressors", "CNC Machines"]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
hours = st.slider("Daily Usage Hours", 1, 24, 8)
|
| 105 |
+
|
| 106 |
+
# ===============================
|
| 107 |
+
# CALCULATIONS
|
| 108 |
+
# ===============================
|
| 109 |
+
|
| 110 |
+
if st.button("π Run AI Solar Analysis"):
|
| 111 |
+
|
| 112 |
+
if len(load_choice) == 0:
|
| 113 |
+
st.error("Please select at least one load")
|
| 114 |
+
else:
|
| 115 |
+
|
| 116 |
+
# Load estimation
|
| 117 |
+
load_watts = 0
|
| 118 |
+
|
| 119 |
+
for item in load_choice:
|
| 120 |
+
if item in ["Lighting", "Fans"]:
|
| 121 |
+
load_watts += 200
|
| 122 |
+
elif item == "Fridge":
|
| 123 |
+
load_watts += 200
|
| 124 |
+
elif item == "AC":
|
| 125 |
+
load_watts += 1500
|
| 126 |
+
elif item in ["Motors", "Compressors", "CNC Machines"]:
|
| 127 |
+
load_watts += 3000
|
| 128 |
+
|
| 129 |
+
daily_kwh = (load_watts * hours) / 1000
|
| 130 |
+
daily_kwh = daily_kwh / (1 - SYSTEM_LOSSES)
|
| 131 |
+
|
| 132 |
+
# AI Recommendation
|
| 133 |
+
system_kw = ai_solar_recommendation(daily_kwh, sunlight)
|
| 134 |
+
|
| 135 |
+
system_cost = system_kw * 1000 * (PANEL_COST_PER_WATT + INSTALLATION_COST_PER_WATT)
|
| 136 |
+
|
| 137 |
+
# Financials
|
| 138 |
+
cashflows, npv, irr, payback, cumulative = financial_projection(system_cost, daily_kwh)
|
| 139 |
+
|
| 140 |
+
# Results Display
|
| 141 |
+
st.subheader("π AI Analysis Results")
|
| 142 |
+
|
| 143 |
+
st.write(f"Recommended Solar System Size: {system_kw} kW")
|
| 144 |
+
st.write(f"Daily Energy Consumption: {round(daily_kwh,2)} kWh")
|
| 145 |
+
st.write(f"Estimated Installation Cost: PKR {round(system_cost):,}")
|
| 146 |
+
st.write(f"NPV (25 Years): PKR {round(npv,2):,}")
|
| 147 |
+
st.write(f"IRR: {round(irr*100,2)} %")
|
| 148 |
+
st.write(f"Payback Period: {payback} Years")
|
| 149 |
+
|
| 150 |
+
# Charts
|
| 151 |
+
st.subheader("π Investment Growth Projection")
|
| 152 |
+
|
| 153 |
+
plt.figure(figsize=(10,4))
|
| 154 |
+
plt.plot(range(1,26), cumulative)
|
| 155 |
+
plt.axhline(system_cost, linestyle="--")
|
| 156 |
+
st.pyplot(plt)
|
| 157 |
+
|
| 158 |
+
# WhatsApp Style Summary
|
| 159 |
+
st.subheader("π± Proposal Summary")
|
| 160 |
+
|
| 161 |
+
summary_text = f"""
|
| 162 |
+
Solar Proposal Summary
|
| 163 |
+
|
| 164 |
+
City: {city}
|
| 165 |
+
Recommended System: {system_kw} kW
|
| 166 |
+
Cost Estimate: PKR {round(system_cost):,}
|
| 167 |
+
Payback Period: {payback} Years
|
| 168 |
+
IRR: {round(irr*100,2)}%
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
st.text_area("Copy for WhatsApp / Proposal", summary_text, height=200)
|