Spaces:
Build error
Build error
| import os | |
| import streamlit as st | |
| from tariff_scraper import fetch_tariff_data, save_tariff_data | |
| # Predefined appliance list with typical loads in watts | |
| APPLIANCE_OPTIONS = { | |
| "Fan": 75, | |
| "Air Conditioner (1 Ton)": 1500, | |
| "Air Conditioner (1.5 Ton)": 2200, | |
| "Refrigerator": 150, | |
| "LED Bulb (20W)": 20, | |
| "Tube Light": 40, | |
| "Iron": 1000, | |
| "Microwave Oven": 1200, | |
| "Washing Machine": 500, | |
| "Electric Heater": 1500, | |
| "Laptop": 50, | |
| "Desktop Computer": 200, | |
| "Television (LCD/LED)": 120, | |
| "Water Pump": 1000, | |
| "Geyser (Electric)": 3000 | |
| } | |
| # Initialize session state | |
| if "appliance_data" not in st.session_state: | |
| st.session_state.appliance_data = [] | |
| if "tariff_data" not in st.session_state: | |
| st.session_state.tariff_data = None | |
| if "tariff_rate" not in st.session_state: | |
| st.session_state.tariff_rate = None | |
| # Function to calculate total units consumption | |
| def calculate_total_units(): | |
| total_energy_wh = 0 | |
| for data in st.session_state.appliance_data: | |
| total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"] | |
| return total_energy_wh / 1000 # Convert watt-hours to kilowatt-hours | |
| # Streamlit App | |
| st.title("Dynamic Tariff Electricity Bill Calculator") | |
| # Tariff Input | |
| st.sidebar.subheader("Fetch Tariff Data") | |
| tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:", | |
| value="https://iesco.com.pk/index.php/customer-services/tariff-guide") | |
| load_type = st.sidebar.selectbox("Select Load Type", ["A-1 GENERAL SUPPLY TARIFF - RESIDENTIAL", | |
| "A-2 GENERAL SUPPLY TARIFF - COMMERCIAL", | |
| "B - INDUSTRIAL SUPPLY TARIFFS", | |
| "D - AGRICULTURE TARIFF"]) | |
| if st.sidebar.button("Fetch Tariff"): | |
| try: | |
| # Fetch and save tariff data using the scraper | |
| tariff_data = fetch_tariff_data(tariff_url) | |
| save_tariff_data(tariff_data, "tariff_data.xlsx") | |
| st.session_state.tariff_data = tariff_data | |
| # Set the tariff rate for the selected load type | |
| if load_type in tariff_data: | |
| df = tariff_data[load_type] | |
| if "Variable Rs/kWh" in df.columns: | |
| st.session_state.tariff_rate = float(df["Variable Rs/kWh"].iloc[0]) | |
| st.sidebar.success(f"Tariff fetched for {load_type}: {st.session_state.tariff_rate} PKR/unit") | |
| else: | |
| st.sidebar.error(f"Variable Rs/kWh not found in {load_type}.") | |
| else: | |
| st.sidebar.warning(f"No data available for {load_type}.") | |
| except Exception as e: | |
| st.sidebar.error(f"Error fetching tariff data: {e}") | |
| # Add Appliances | |
| st.sidebar.subheader("Add Appliances") | |
| appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys())) | |
| default_load = APPLIANCE_OPTIONS[appliance] | |
| quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1) | |
| load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load) | |
| usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0) | |
| if st.sidebar.button("Add Appliance"): | |
| st.session_state.appliance_data.append({ | |
| "appliance": appliance, | |
| "load": load, | |
| "quantity": quantity, | |
| "usage_hours": usage_hours | |
| }) | |
| st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!") | |
| # Display Appliance List | |
| st.subheader("Appliance List") | |
| if st.session_state.appliance_data: | |
| for idx, data in enumerate(st.session_state.appliance_data, start=1): | |
| st.write(f"{idx}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day") | |
| else: | |
| st.write("No appliances added.") | |
| # Calculate Monthly Units and Electricity Bill | |
| if st.button("Calculate Monthly Bill"): | |
| if not st.session_state.tariff_rate: | |
| st.warning("Please fetch tariff data first.") | |
| else: | |
| total_units_kwh = calculate_total_units() | |
| monthly_units_kwh = total_units_kwh * 30 # Assuming 30 days in a month | |
| monthly_bill = monthly_units_kwh * st.session_state.tariff_rate | |
| st.subheader("Electricity Bill") | |
| st.write(f"**Total Monthly Bill: PKR {monthly_bill:.2f}**") | |