Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from fpdf import FPDF | |
| # --- Appliance power ratings (in watts) --- | |
| appliance_power = { | |
| "Fan": 75, | |
| "LED Light": 15, | |
| "Refrigerator": 150, | |
| "TV": 100, | |
| "Air Conditioner": 1000, | |
| "Washing Machine": 500, | |
| "Computer": 200 | |
| } | |
| st.set_page_config(page_title="Solar Energy Planner", layout="wide") | |
| st.title("โ๏ธ Solar Energy Consumption & Planning App") | |
| # --- Sidebar: User Inputs --- | |
| st.sidebar.header("๐ Appliance Load Input") | |
| appliance_data = [] | |
| for appliance, watt in appliance_power.items(): | |
| qty = st.sidebar.number_input(f"{appliance} Quantity", 0, 20, 0, key=f"{appliance}_qty") | |
| hours = st.sidebar.number_input(f"{appliance} Daily Hours", 0, 24, 0, key=f"{appliance}_hours") | |
| if qty > 0 and hours > 0: | |
| appliance_data.append({ | |
| "Appliance": appliance, | |
| "Qty": qty, | |
| "Hours": hours, | |
| "Watt": watt, | |
| "Daily kWh": round(qty * hours * watt / 1000, 2) | |
| }) | |
| # Default values | |
| total_daily_kwh = 0 | |
| total_monthly_kwh = 0 | |
| num_panels = 0 | |
| # --- Show appliance usage table --- | |
| if appliance_data: | |
| st.subheader("๐งฎ Appliance-wise Energy Consumption") | |
| df = pd.DataFrame(appliance_data) | |
| df["Monthly kWh"] = df["Daily kWh"] * 30 | |
| st.dataframe(df, use_container_width=True) | |
| total_daily_kwh = df["Daily kWh"].sum() | |
| total_monthly_kwh = df["Monthly kWh"].sum() | |
| st.metric("๐ Total Daily Consumption (kWh)", round(total_daily_kwh, 2)) | |
| st.metric("๐ Total Monthly Consumption (kWh)", round(total_monthly_kwh, 2)) | |
| else: | |
| st.info("Please enter appliance details in the sidebar to start.") | |
| # --- Solar Panel Calculator --- | |
| st.subheader("โ๏ธ Solar Panel Requirement Calculator") | |
| avg_sunlight_hours = st.number_input("Average Sunlight Hours/Day", 1.0, 12.0, 5.5) | |
| panel_watt = st.number_input("Panel Wattage (W)", 100, 600, 300) | |
| if total_daily_kwh > 0: | |
| kwh_per_panel = round((panel_watt * avg_sunlight_hours) / 1000, 2) | |
| num_panels = int(np.ceil(total_daily_kwh / kwh_per_panel)) | |
| st.success(f"You need approximately **{num_panels}** panels of {panel_watt}W to cover {round(total_daily_kwh, 2)} kWh/day.") | |
| st.caption(f"Each panel generates approx. {kwh_per_panel} kWh/day.") | |
| # --- Tilt Angle Calculator --- | |
| st.subheader("๐ Recommended Tilt Angle") | |
| latitude = st.number_input("Enter Latitude of Your Location", -90.0, 90.0, 30.0) | |
| tilt_year = round(latitude * 0.9, 1) | |
| tilt_summer = round(latitude * 0.7, 1) | |
| tilt_winter = round(latitude * 1.1, 1) | |
| st.markdown(f""" | |
| - ๐ **Year-round Tilt Angle**: `{tilt_year}ยฐ` | |
| - ๐ **Summer Tilt**: `{tilt_summer}ยฐ` | |
| - โ๏ธ **Winter Tilt**: `{tilt_winter}ยฐ` | |
| """) | |
| # --- Graphs: Weekly and Monthly Consumption --- | |
| if total_daily_kwh > 0: | |
| st.subheader("๐ Energy Consumption Overview") | |
| week_days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] | |
| weekly_kwh = [round(total_daily_kwh + np.random.uniform(-0.3, 0.3), 2) for _ in range(7)] | |
| fig1, ax1 = plt.subplots() | |
| ax1.bar(week_days, weekly_kwh, color='skyblue') | |
| ax1.set_ylabel("kWh") | |
| ax1.set_title("Weekly Consumption") | |
| st.pyplot(fig1) | |
| months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] | |
| monthly_kwh = [round(total_daily_kwh * 30 + np.random.uniform(-5, 5), 2) for _ in range(12)] | |
| fig2, ax2 = plt.subplots() | |
| ax2.plot(months, monthly_kwh, marker='o', color='green') | |
| ax2.set_ylabel("kWh") | |
| ax2.set_title("Monthly Consumption") | |
| st.pyplot(fig2) | |
| # --- Roof Area Estimator --- | |
| st.subheader("๐ Roof Area & Panel Capacity") | |
| roof_area = st.number_input("Enter Available Roof Area (sq. ft)", 0, 1000, 200) | |
| panel_area = 18 # Average panel size (sq. ft) | |
| max_panels_fit = int(roof_area / panel_area) | |
| max_capacity_kw = round((max_panels_fit * panel_watt) / 1000, 2) | |
| st.markdown(f""" | |
| - Max panels installable: `{max_panels_fit}` | |
| - Max capacity: `{max_capacity_kw} kW` | |
| """) | |
| # --- Battery Estimator --- | |
| st.subheader("๐ Battery Backup Estimator") | |
| if total_daily_kwh > 0: | |
| backup_hours = st.slider("Backup Hours Required", 1, 24, 6) | |
| avg_load_kw = total_daily_kwh / 24 | |
| battery_size_kwh = round(avg_load_kw * backup_hours, 2) | |
| battery_ah_12v = round((battery_size_kwh * 1000) / 12, 0) | |
| st.markdown(f""" | |
| - Required Battery Size: **{battery_size_kwh} kWh** | |
| - Battery (12V): **{battery_ah_12v} Ah** | |
| """) | |
| else: | |
| st.info("Add appliance details first to calculate battery backup.") | |
| # --- Cost and ROI Estimator --- | |
| st.subheader("๐ฐ Cost & ROI Estimator") | |
| unit_cost = st.number_input("Grid Cost per Unit (kWh)", 5.0, 50.0, 20.0) | |
| panel_cost = st.number_input("Cost per Panel (PKR)", 10000, 100000, 40000) | |
| installation_cost = st.number_input("Installation Cost (PKR)", 0, 100000, 20000) | |
| if num_panels > 0: | |
| total_cost = int(num_panels) * panel_cost + installation_cost | |
| monthly_saving = round(total_daily_kwh * 30 * unit_cost, 0) | |
| roi_months = round(total_cost / monthly_saving, 1) | |
| st.markdown(f""" | |
| - ๐ธ Total System Cost: **PKR {total_cost:,}** | |
| - ๐ต Estimated Monthly Savings: **PKR {monthly_saving:,}** | |
| - ๐ ROI / Break-even in: **{roi_months} months** | |
| """) | |
| else: | |
| st.info("Add appliances and sunlight hours to calculate panel requirements.") | |
| # --- Footer --- | |
| st.markdown("---") | |
| st.caption("Developed with โค๏ธ using Streamlit | Ready for Hugging Face Deployment") | |