File size: 3,544 Bytes
18a3a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import gradio as gr

# -------------------------------
# Appliance Data (Pakistan)
# -------------------------------
APPLIANCES = [
    ("LED Light", 15),
    ("Ceiling Fan", 80),
    ("Refrigerator", 200),
    ("LED TV", 120),
    ("Washing Machine", 500),
    ("Iron", 1000),
    ("Water Pump (1 HP)", 750),
    ("Air Conditioner (1 Ton)", 1500),
    ("Laptop", 100),
    ("Desktop Computer", 300),
]

PEAK_SUN_HOURS = 5

# -------------------------------
# Calculation Logic
# -------------------------------
def calculate_solar(
    selected_appliances,
    city,
    system_type,
    battery_type,
    backup_hours,
    *values
):
    quantities = values[:len(APPLIANCES)]
    hours = values[len(APPLIANCES):]

    total_daily_wh = 0

    for i, (name, watt) in enumerate(APPLIANCES):
        if name in selected_appliances:
            total_daily_wh += watt * quantities[i] * hours[i]

    if total_daily_wh == 0:
        return "❌ Please select at least one appliance."

    daily_units = total_daily_wh / 1000
    system_kw = round(daily_units / PEAK_SUN_HOURS, 2)

    panel_watt = 550
    panels_required = int((system_kw * 1000) / panel_watt) + 1
    inverter_kw = round(system_kw + 1, 1)

    battery_kwh = 0
    if system_type != "On-Grid":
        battery_kwh = round((daily_units / 24) * backup_hours, 2)

    panel_cost = system_kw * 1000 * 45
    inverter_cost = inverter_kw * 120000
    battery_cost = battery_kwh * (120000 if battery_type == "Lithium" else 35000)

    total_cost = int((panel_cost + inverter_cost + battery_cost) * 1.1)

    monthly_units = daily_units * 30
    monthly_saving = int(monthly_units * 60)

    return f"""
🌞 Solar System Recommendation (Pakistan)

πŸ“ City: {city}
⚑ System Type: {system_type}

πŸ”Œ Daily Load
β€’ {daily_units:.2f} Units

πŸ”‹ System Design
β€’ Solar Size: {system_kw} kW
β€’ Panels: {panels_required} Γ— {panel_watt}W
β€’ Inverter: {inverter_kw} kW
β€’ Battery: {battery_kwh} kWh ({battery_type})

πŸ’° Estimated Cost
β€’ PKR {total_cost:,}

πŸ“‰ Savings
β€’ Monthly Units: {monthly_units:.1f}
β€’ Monthly Saving: PKR {monthly_saving:,}

⚠️ Estimated values for Pakistan market.
"""

# -------------------------------
# UI
# -------------------------------
with gr.Blocks() as app:
    gr.Markdown("## β˜€οΈ Solar Panel Calculator (Pakistan)")

    appliance_names = [a[0] for a in APPLIANCES]

    selected_appliances = gr.CheckboxGroup(
        appliance_names, label="Select Appliances"
    )

    qty_inputs = []
    hour_inputs = []

    for name, _ in APPLIANCES:
        with gr.Row():
            qty_inputs.append(gr.Number(label=f"{name} Quantity", value=1))
            hour_inputs.append(gr.Number(label=f"{name} Usage Hours", value=5))

    city = gr.Dropdown(
        ["Lahore", "Karachi", "Islamabad", "Faisalabad", "Multan", "Other"],
        label="City",
    )

    system_type = gr.Radio(
        ["On-Grid", "Off-Grid", "Hybrid"], value="Hybrid", label="System Type"
    )

    battery_type = gr.Radio(
        ["Lead Acid", "Lithium"], value="Lead Acid", label="Battery Type"
    )

    backup_hours = gr.Slider(2, 12, value=6, step=1, label="Backup Hours")

    btn = gr.Button("πŸ” Calculate")

    output = gr.Textbox(lines=18, label="Result")

    btn.click(
        calculate_solar,
        inputs=[
            selected_appliances,
            city,
            system_type,
            battery_type,
            backup_hours,
            *qty_inputs,
            *hour_inputs,
        ],
        outputs=output,
    )

app.launch()