Talha812's picture
Create app.py
18a3a0c verified
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()