File size: 2,938 Bytes
d5a746f
 
 
 
 
 
 
 
 
 
 
a05dc62
 
 
 
1063fc9
 
 
 
 
 
a05dc62
 
1063fc9
9088dcb
 
1063fc9
 
a05dc62
1063fc9
9088dcb
1063fc9
 
9088dcb
1063fc9
9088dcb
 
a05dc62
1063fc9
 
 
 
 
9088dcb
a05dc62
1063fc9
a05dc62
1063fc9
 
 
 
 
 
 
 
 
 
 
 
 
a05dc62
1063fc9
a05dc62
1063fc9
 
 
 
 
9088dcb
 
a05dc62
1063fc9
 
 
9088dcb
 
1063fc9
 
 
 
 
 
9088dcb
 
1063fc9
a05dc62
1063fc9
a05dc62
 
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
# Dynamic installation safety net
def install_package(package):
    try:
        __import__(package.replace('2', '')) # Check for 'fpdf' namespace
    except ImportError:
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])

install_package("fpdf2")

# Now safe to import
from fpdf import FPDF
import gradio as gr
import pandas as pd
import datetime

# --- 2026 DYNAMIC MARKET DATA ---
MARKET_DATA = {
    "panel_pkr_w": 32,
    "sun_hrs": 5.2,
    "bank_rate": 0.14,  # Simulated Alfalah Rate
    "installation": 0.15
}

def generate_pdf(data):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Helvetica", 'B', 16)
    pdf.cell(0, 10, "Solar Feasibility Report - Pakistan 2026", center=True, new_x="LMARGIN", new_y="NEXT")
    
    pdf.set_font("Helvetica", size=12)
    pdf.ln(10)
    for key, value in data.items():
        pdf.cell(0, 10, f"{key}: {value}", new_x="LMARGIN", new_y="NEXT")
    
    report_path = "Solar_Insights_Report.pdf"
    pdf.output(report_path)
    return report_path

def solar_calculator(ac_count, fan_count, light_count, sys_type):
    # Engineering Math
    load = (ac_count * 1500) + (fan_count * 80) + (light_count * 12)
    daily_units = (load * 8) / 1000
    sys_kw = round((daily_units / MARKET_DATA["sun_hrs"]) * 1.2, 2)
    
    # Financials
    cost = int((sys_kw * 1000 * MARKET_DATA["panel_pkr_w"] + 250000) * (1 + MARKET_DATA["installation"]))
    
    # Bank EMI Calculation (P * r * (1+r)^n / ((1+r)^n - 1))
    p = cost * 0.7  # 30% Downpayment
    r = MARKET_DATA["bank_rate"] / 12
    n = 60
    emi = int(p * (r * (1+r)**n) / ((1+r)**n - 1))

    results = {
        "Total Load (W)": load,
        "System Size (kW)": sys_kw,
        "Total Cost (PKR)": f"{cost:,}",
        "Monthly EMI (PKR)": f"{emi:,}",
        "Date Generated": datetime.datetime.now().strftime("%Y-%m-%d")
    }
    
    pdf_file = generate_pdf(results)
    
    summary = f"""
    ## 📊 Estimation Results
    - **Recommended System:** {sys_kw} kW {sys_type}
    - **Estimated Investment:** PKR {cost:,}
    - **Financing:** PKR {emi:,}/month via Bank Alfalah
    """
    return summary, pdf_file

# --- UI DESIGN ---
with gr.Blocks(title="Solar PK Pro") as demo:
    gr.Markdown("# ☀️ SolarExpert Pakistan 2.0")
    with gr.Row():
        with gr.Column():
            ac = gr.Number(label="Inverter ACs (1.5 Ton)", value=1)
            fans = gr.Number(label="Ceiling Fans", value=4)
            lights = gr.Number(label="LED Lights", value=10)
            stype = gr.Dropdown(["On-Grid", "Hybrid"], label="System Architecture", value="Hybrid")
            calc_btn = gr.Button("Calculate & Generate Report", variant="primary")
        
        with gr.Column():
            out_md = gr.Markdown()
            out_file = gr.File(label="Download PDF Insights")

    calc_btn.click(solar_calculator, inputs=[ac, fans, lights, stype], outputs=[out_md, out_file])

demo.launch()