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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -------------------------------
|
| 4 |
+
# Appliance Wattage (Pakistan Common)
|
| 5 |
+
# -------------------------------
|
| 6 |
+
APPLIANCES = {
|
| 7 |
+
"LED Light": 15,
|
| 8 |
+
"Ceiling Fan": 80,
|
| 9 |
+
"Refrigerator": 200,
|
| 10 |
+
"LED TV": 120,
|
| 11 |
+
"Washing Machine": 500,
|
| 12 |
+
"Iron": 1000,
|
| 13 |
+
"Water Pump (1 HP)": 750,
|
| 14 |
+
"Air Conditioner (1 Ton)": 1500,
|
| 15 |
+
"Laptop": 100,
|
| 16 |
+
"Desktop Computer": 300
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
PEAK_SUN_HOURS = 5 # Pakistan average
|
| 20 |
+
|
| 21 |
+
# -------------------------------
|
| 22 |
+
# Core Calculation Function
|
| 23 |
+
# -------------------------------
|
| 24 |
+
def calculate_solar(
|
| 25 |
+
selected_appliances,
|
| 26 |
+
quantities,
|
| 27 |
+
hours,
|
| 28 |
+
city,
|
| 29 |
+
system_type,
|
| 30 |
+
battery_type,
|
| 31 |
+
backup_hours
|
| 32 |
+
):
|
| 33 |
+
if not selected_appliances:
|
| 34 |
+
return "β Please select at least one appliance."
|
| 35 |
+
|
| 36 |
+
total_daily_wh = 0
|
| 37 |
+
|
| 38 |
+
for app in selected_appliances:
|
| 39 |
+
qty = quantities.get(app, 1)
|
| 40 |
+
hrs = hours.get(app, 1)
|
| 41 |
+
watt = APPLIANCES[app]
|
| 42 |
+
total_daily_wh += watt * qty * hrs
|
| 43 |
+
|
| 44 |
+
daily_units = total_daily_wh / 1000 # kWh
|
| 45 |
+
|
| 46 |
+
# Solar system size (kW)
|
| 47 |
+
system_kw = round(daily_units / PEAK_SUN_HOURS, 2)
|
| 48 |
+
|
| 49 |
+
# Panels
|
| 50 |
+
panel_watt = 550
|
| 51 |
+
panels_required = int((system_kw * 1000) / panel_watt) + 1
|
| 52 |
+
|
| 53 |
+
# Inverter sizing
|
| 54 |
+
inverter_kw = round(system_kw + 1, 1)
|
| 55 |
+
|
| 56 |
+
# Battery sizing
|
| 57 |
+
battery_kwh = 0
|
| 58 |
+
if system_type != "On-Grid":
|
| 59 |
+
night_load_wh = (total_daily_wh / 24) * backup_hours
|
| 60 |
+
battery_kwh = round(night_load_wh / 1000, 2)
|
| 61 |
+
|
| 62 |
+
# -------------------------------
|
| 63 |
+
# Cost Estimation (PKR)
|
| 64 |
+
# -------------------------------
|
| 65 |
+
panel_cost = system_kw * 1000 * 45
|
| 66 |
+
inverter_cost = inverter_kw * 120000
|
| 67 |
+
|
| 68 |
+
if battery_type == "Lithium":
|
| 69 |
+
battery_cost = battery_kwh * 120000
|
| 70 |
+
else:
|
| 71 |
+
battery_cost = battery_kwh * 35000
|
| 72 |
+
|
| 73 |
+
subtotal = panel_cost + inverter_cost + battery_cost
|
| 74 |
+
installation = subtotal * 0.10
|
| 75 |
+
total_cost = int(subtotal + installation)
|
| 76 |
+
|
| 77 |
+
monthly_units = daily_units * 30
|
| 78 |
+
monthly_saving = int(monthly_units * 60) # Avg PKR/unit
|
| 79 |
+
|
| 80 |
+
# -------------------------------
|
| 81 |
+
# Output Summary
|
| 82 |
+
# -------------------------------
|
| 83 |
+
result = f"""
|
| 84 |
+
π **Solar System Recommendation (Pakistan)**
|
| 85 |
+
|
| 86 |
+
π City: {city}
|
| 87 |
+
β‘ System Type: {system_type}
|
| 88 |
+
|
| 89 |
+
π **Load Summary**
|
| 90 |
+
- Daily Consumption: {round(daily_units,2)} Units (kWh)
|
| 91 |
+
|
| 92 |
+
π **System Design**
|
| 93 |
+
- Solar System Size: {system_kw} kW
|
| 94 |
+
- Panels Required: {panels_required} Γ {panel_watt}W
|
| 95 |
+
- Inverter Size: {inverter_kw} kW
|
| 96 |
+
- Battery Capacity: {battery_kwh} kWh ({battery_type})
|
| 97 |
+
|
| 98 |
+
π° **Estimated Cost**
|
| 99 |
+
- Total System Cost: PKR {total_cost:,}
|
| 100 |
+
|
| 101 |
+
π **Savings**
|
| 102 |
+
- Monthly Units Generated: {round(monthly_units,1)}
|
| 103 |
+
- Estimated Monthly Saving: PKR {monthly_saving:,}
|
| 104 |
+
|
| 105 |
+
β οΈ *Prices are estimates and may vary by market.*
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
return result
|
| 109 |
+
|
| 110 |
+
# -------------------------------
|
| 111 |
+
# Gradio UI
|
| 112 |
+
# -------------------------------
|
| 113 |
+
with gr.Blocks(title="Solar Panel Calculator - Pakistan") as app:
|
| 114 |
+
gr.Markdown("## βοΈ Solar Panel Requirement Calculator (Pakistan)")
|
| 115 |
+
|
| 116 |
+
appliance_select = gr.CheckboxGroup(
|
| 117 |
+
choices=list(APPLIANCES.keys()),
|
| 118 |
+
label="Select Appliances"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
quantity_inputs = {}
|
| 122 |
+
hour_inputs = {}
|
| 123 |
+
|
| 124 |
+
for appliance in APPLIANCES:
|
| 125 |
+
with gr.Row():
|
| 126 |
+
quantity_inputs[appliance] = gr.Number(
|
| 127 |
+
label=f"{appliance} Quantity", value=1, visible=False
|
| 128 |
+
)
|
| 129 |
+
hour_inputs[appliance] = gr.Number(
|
| 130 |
+
label=f"{appliance} Daily Usage (Hours)", value=5, visible=False
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
def toggle_inputs(selected):
|
| 134 |
+
updates = []
|
| 135 |
+
for app in APPLIANCES:
|
| 136 |
+
visible = app in selected
|
| 137 |
+
updates.append(gr.update(visible=visible))
|
| 138 |
+
updates.append(gr.update(visible=visible))
|
| 139 |
+
return updates
|
| 140 |
+
|
| 141 |
+
appliance_select.change(
|
| 142 |
+
toggle_inputs,
|
| 143 |
+
appliance_select,
|
| 144 |
+
sum([[quantity_inputs[a], hour_inputs[a]] for a in APPLIANCES], [])
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
city = gr.Dropdown(
|
| 148 |
+
["Lahore", "Karachi", "Islamabad", "Faisalabad", "Multan", "Other"],
|
| 149 |
+
label="City"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
system_type = gr.Radio(
|
| 153 |
+
["On-Grid", "Off-Grid", "Hybrid"],
|
| 154 |
+
label="Solar System Type",
|
| 155 |
+
value="Hybrid"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
battery_type = gr.Radio(
|
| 159 |
+
["Lead Acid", "Lithium"],
|
| 160 |
+
label="Battery Type",
|
| 161 |
+
value="Lead Acid"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
backup_hours = gr.Slider(
|
| 165 |
+
2, 12, value=6, step=1, label="Backup Hours Required"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
calculate_btn = gr.Button("π Calculate Solar System")
|
| 169 |
+
|
| 170 |
+
output = gr.Textbox(lines=20, label="Result")
|
| 171 |
+
|
| 172 |
+
calculate_btn.click(
|
| 173 |
+
calculate_solar,
|
| 174 |
+
inputs=[
|
| 175 |
+
appliance_select,
|
| 176 |
+
quantity_inputs,
|
| 177 |
+
hour_inputs,
|
| 178 |
+
city,
|
| 179 |
+
system_type,
|
| 180 |
+
battery_type,
|
| 181 |
+
backup_hours
|
| 182 |
+
],
|
| 183 |
+
outputs=output
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
app.launch()
|