Spaces:
Runtime error
Runtime error
| # 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() |