Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -------------------------------
|
| 4 |
+
# Appliance Data (Pakistan)
|
| 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
|
| 20 |
+
|
| 21 |
+
# -------------------------------
|
| 22 |
+
# Calculation Logic
|
| 23 |
+
# -------------------------------
|
| 24 |
+
def calculate_solar(
|
| 25 |
+
selected_appliances,
|
| 26 |
+
city,
|
| 27 |
+
system_type,
|
| 28 |
+
battery_type,
|
| 29 |
+
backup_hours,
|
| 30 |
+
*values
|
| 31 |
+
):
|
| 32 |
+
quantities = values[:len(APPLIANCES)]
|
| 33 |
+
hours = values[len(APPLIANCES):]
|
| 34 |
+
|
| 35 |
+
total_daily_wh = 0
|
| 36 |
+
|
| 37 |
+
for i, (name, watt) in enumerate(APPLIANCES):
|
| 38 |
+
if name in selected_appliances:
|
| 39 |
+
total_daily_wh += watt * quantities[i] * hours[i]
|
| 40 |
+
|
| 41 |
+
if total_daily_wh == 0:
|
| 42 |
+
return "β Please select at least one appliance."
|
| 43 |
+
|
| 44 |
+
daily_units = total_daily_wh / 1000
|
| 45 |
+
system_kw = round(daily_units / PEAK_SUN_HOURS, 2)
|
| 46 |
+
|
| 47 |
+
panel_watt = 550
|
| 48 |
+
panels_required = int((system_kw * 1000) / panel_watt) + 1
|
| 49 |
+
inverter_kw = round(system_kw + 1, 1)
|
| 50 |
+
|
| 51 |
+
battery_kwh = 0
|
| 52 |
+
if system_type != "On-Grid":
|
| 53 |
+
battery_kwh = round((daily_units / 24) * backup_hours, 2)
|
| 54 |
+
|
| 55 |
+
panel_cost = system_kw * 1000 * 45
|
| 56 |
+
inverter_cost = inverter_kw * 120000
|
| 57 |
+
battery_cost = battery_kwh * (120000 if battery_type == "Lithium" else 35000)
|
| 58 |
+
|
| 59 |
+
total_cost = int((panel_cost + inverter_cost + battery_cost) * 1.1)
|
| 60 |
+
|
| 61 |
+
monthly_units = daily_units * 30
|
| 62 |
+
monthly_saving = int(monthly_units * 60)
|
| 63 |
+
|
| 64 |
+
return f"""
|
| 65 |
+
π Solar System Recommendation (Pakistan)
|
| 66 |
+
|
| 67 |
+
π City: {city}
|
| 68 |
+
β‘ System Type: {system_type}
|
| 69 |
+
|
| 70 |
+
π Daily Load
|
| 71 |
+
β’ {daily_units:.2f} Units
|
| 72 |
+
|
| 73 |
+
π System Design
|
| 74 |
+
β’ Solar Size: {system_kw} kW
|
| 75 |
+
β’ Panels: {panels_required} Γ {panel_watt}W
|
| 76 |
+
β’ Inverter: {inverter_kw} kW
|
| 77 |
+
β’ Battery: {battery_kwh} kWh ({battery_type})
|
| 78 |
+
|
| 79 |
+
π° Estimated Cost
|
| 80 |
+
β’ PKR {total_cost:,}
|
| 81 |
+
|
| 82 |
+
π Savings
|
| 83 |
+
β’ Monthly Units: {monthly_units:.1f}
|
| 84 |
+
β’ Monthly Saving: PKR {monthly_saving:,}
|
| 85 |
+
|
| 86 |
+
β οΈ Estimated values for Pakistan market.
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# -------------------------------
|
| 90 |
+
# UI
|
| 91 |
+
# -------------------------------
|
| 92 |
+
with gr.Blocks() as app:
|
| 93 |
+
gr.Markdown("## βοΈ Solar Panel Calculator (Pakistan)")
|
| 94 |
+
|
| 95 |
+
appliance_names = [a[0] for a in APPLIANCES]
|
| 96 |
+
|
| 97 |
+
selected_appliances = gr.CheckboxGroup(
|
| 98 |
+
appliance_names, label="Select Appliances"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
qty_inputs = []
|
| 102 |
+
hour_inputs = []
|
| 103 |
+
|
| 104 |
+
for name, _ in APPLIANCES:
|
| 105 |
+
with gr.Row():
|
| 106 |
+
qty_inputs.append(gr.Number(label=f"{name} Quantity", value=1))
|
| 107 |
+
hour_inputs.append(gr.Number(label=f"{name} Usage Hours", value=5))
|
| 108 |
+
|
| 109 |
+
city = gr.Dropdown(
|
| 110 |
+
["Lahore", "Karachi", "Islamabad", "Faisalabad", "Multan", "Other"],
|
| 111 |
+
label="City",
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
system_type = gr.Radio(
|
| 115 |
+
["On-Grid", "Off-Grid", "Hybrid"], value="Hybrid", label="System Type"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
battery_type = gr.Radio(
|
| 119 |
+
["Lead Acid", "Lithium"], value="Lead Acid", label="Battery Type"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
backup_hours = gr.Slider(2, 12, value=6, step=1, label="Backup Hours")
|
| 123 |
+
|
| 124 |
+
btn = gr.Button("π Calculate")
|
| 125 |
+
|
| 126 |
+
output = gr.Textbox(lines=18, label="Result")
|
| 127 |
+
|
| 128 |
+
btn.click(
|
| 129 |
+
calculate_solar,
|
| 130 |
+
inputs=[
|
| 131 |
+
selected_appliances,
|
| 132 |
+
city,
|
| 133 |
+
system_type,
|
| 134 |
+
battery_type,
|
| 135 |
+
backup_hours,
|
| 136 |
+
*qty_inputs,
|
| 137 |
+
*hour_inputs,
|
| 138 |
+
],
|
| 139 |
+
outputs=output,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
app.launch()
|