amberroohee's picture
Create app.py
d0f46b1 verified
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()