File size: 4,817 Bytes
d0f46b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import gradio as gr

# -------------------------------
# Appliance Wattage (Pakistan Common)
# -------------------------------
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  # Pakistan average

# -------------------------------
# Core Calculation Function
# -------------------------------
def calculate_solar(
    selected_appliances,
    quantities,
    hours,
    city,
    system_type,
    battery_type,
    backup_hours
):
    if not selected_appliances:
        return "❌ Please select at least one appliance."

    total_daily_wh = 0

    for app in selected_appliances:
        qty = quantities.get(app, 1)
        hrs = hours.get(app, 1)
        watt = APPLIANCES[app]
        total_daily_wh += watt * qty * hrs

    daily_units = total_daily_wh / 1000  # kWh

    # Solar system size (kW)
    system_kw = round(daily_units / PEAK_SUN_HOURS, 2)

    # Panels
    panel_watt = 550
    panels_required = int((system_kw * 1000) / panel_watt) + 1

    # Inverter sizing
    inverter_kw = round(system_kw + 1, 1)

    # Battery sizing
    battery_kwh = 0
    if system_type != "On-Grid":
        night_load_wh = (total_daily_wh / 24) * backup_hours
        battery_kwh = round(night_load_wh / 1000, 2)

    # -------------------------------
    # Cost Estimation (PKR)
    # -------------------------------
    panel_cost = system_kw * 1000 * 45
    inverter_cost = inverter_kw * 120000

    if battery_type == "Lithium":
        battery_cost = battery_kwh * 120000
    else:
        battery_cost = battery_kwh * 35000

    subtotal = panel_cost + inverter_cost + battery_cost
    installation = subtotal * 0.10
    total_cost = int(subtotal + installation)

    monthly_units = daily_units * 30
    monthly_saving = int(monthly_units * 60)  # Avg PKR/unit

    # -------------------------------
    # Output Summary
    # -------------------------------
    result = f"""
🌞 **Solar System Recommendation (Pakistan)**

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

πŸ”Œ **Load Summary**
- Daily Consumption: {round(daily_units,2)} Units (kWh)

πŸ”‹ **System Design**
- Solar System Size: {system_kw} kW
- Panels Required: {panels_required} Γ— {panel_watt}W
- Inverter Size: {inverter_kw} kW
- Battery Capacity: {battery_kwh} kWh ({battery_type})

πŸ’° **Estimated Cost**
- Total System Cost: PKR {total_cost:,}

πŸ“‰ **Savings**
- Monthly Units Generated: {round(monthly_units,1)}
- Estimated Monthly Saving: PKR {monthly_saving:,}

⚠️ *Prices are estimates and may vary by market.*
    """

    return result

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

    appliance_select = gr.CheckboxGroup(
        choices=list(APPLIANCES.keys()),
        label="Select Appliances"
    )

    quantity_inputs = {}
    hour_inputs = {}

    for appliance in APPLIANCES:
        with gr.Row():
            quantity_inputs[appliance] = gr.Number(
                label=f"{appliance} Quantity", value=1, visible=False
            )
            hour_inputs[appliance] = gr.Number(
                label=f"{appliance} Daily Usage (Hours)", value=5, visible=False
            )

    def toggle_inputs(selected):
        updates = []
        for app in APPLIANCES:
            visible = app in selected
            updates.append(gr.update(visible=visible))
            updates.append(gr.update(visible=visible))
        return updates

    appliance_select.change(
        toggle_inputs,
        appliance_select,
        sum([[quantity_inputs[a], hour_inputs[a]] for a in APPLIANCES], [])
    )

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

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

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

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

    calculate_btn = gr.Button("πŸ” Calculate Solar System")

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

    calculate_btn.click(
        calculate_solar,
        inputs=[
            appliance_select,
            quantity_inputs,
            hour_inputs,
            city,
            system_type,
            battery_type,
            backup_hours
        ],
        outputs=output
    )

app.launch()