Engineer786 commited on
Commit
4ab941e
Β·
verified Β·
1 Parent(s): 4f160f8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import streamlit as st
4
+ import pandas as pd
5
+ from scraper import scrape_tariffs
6
+
7
+ # Streamlit App: Electricity Bill & Carbon Footprint Estimator
8
+ st.title("πŸ”Œ Electricity Bill & Carbon Footprint Estimator")
9
+ st.sidebar.header("βš™οΈ User Input")
10
+
11
+ # Tariff URLs for scraping
12
+ tariff_urls = {
13
+ "IESCO": "https://iesco.com.pk/index.php/customer-services/tariff-guide",
14
+ "FESCO": "https://fesco.com.pk/tariff",
15
+ "HESCO": "http://www.hesco.gov.pk/htmls/tariffs.htm",
16
+ "KE": "https://www.ke.com.pk/customer-services/tariff-structure/",
17
+ "LESCO": "https://www.lesco.gov.pk/ElectricityTariffs",
18
+ "PESCO": "https://pesconlinebill.pk/pesco-tariff/",
19
+ "QESCO": "http://qesco.com.pk/Tariffs.aspx",
20
+ "TESCO": "https://tesco.gov.pk/index.php/electricity-traiff",
21
+ }
22
+
23
+ # Predefined appliances and their power in watts
24
+ appliances = {
25
+ "LED Bulb (10W)": 10,
26
+ "Ceiling Fan (75W)": 75,
27
+ "Refrigerator (150W)": 150,
28
+ "Air Conditioner (1.5 Ton, 1500W)": 1500,
29
+ "Washing Machine (500W)": 500,
30
+ "Television (100W)": 100,
31
+ "Laptop (65W)": 65,
32
+ "Iron (1000W)": 1000,
33
+ "Microwave Oven (1200W)": 1200,
34
+ "Water Heater (2000W)": 2000,
35
+ }
36
+
37
+ def scrape_data():
38
+ """
39
+ Scrapes tariff data from the provided URLs.
40
+ """
41
+ st.info("πŸ”„ Scraping tariff data... Please wait.")
42
+ scrape_tariffs(list(tariff_urls.values()))
43
+ st.success("βœ… Tariff data scraping complete.")
44
+
45
+ def calculate_carbon_footprint(monthly_energy_kwh):
46
+ """
47
+ Calculates the carbon footprint based on energy consumption in kWh.
48
+ """
49
+ carbon_emission_factor = 0.75 # kg CO2 per kWh
50
+ return monthly_energy_kwh * carbon_emission_factor
51
+
52
+ # Sidebar: Scrape Tariff Data
53
+ if st.sidebar.button("Scrape Tariff Data"):
54
+ scrape_data()
55
+
56
+ # Sidebar: Tariff Selection
57
+ st.sidebar.subheader("πŸ’‘ Select Tariff")
58
+ try:
59
+ tariff_data = pd.read_csv("data/tariffs.csv")
60
+ tariff_types = tariff_data["category"].unique()
61
+ selected_tariff = st.sidebar.selectbox("Select your tariff category:", tariff_types)
62
+ rate_per_kwh = tariff_data[tariff_data["category"] == selected_tariff]["rate"].iloc[0]
63
+ st.sidebar.write(f"Rate per kWh: **{rate_per_kwh} PKR**")
64
+ except FileNotFoundError:
65
+ st.sidebar.error("⚠️ Tariff data not found. Please scrape the data first.")
66
+ rate_per_kwh = 0
67
+
68
+ # Sidebar: User Inputs for Appliances
69
+ st.sidebar.subheader("🏠 Add Appliances")
70
+ selected_appliance = st.sidebar.selectbox("Select an appliance:", list(appliances.keys()))
71
+ appliance_power = appliances[selected_appliance]
72
+ appliance_quantity = st.sidebar.number_input(
73
+ "Enter quantity:", min_value=1, max_value=10, value=1
74
+ )
75
+ usage_hours = st.sidebar.number_input(
76
+ "Enter usage hours per day:", min_value=1, max_value=24, value=5
77
+ )
78
+
79
+ # Add appliance details to the main list
80
+ if "appliance_list" not in st.session_state:
81
+ st.session_state["appliance_list"] = []
82
+
83
+ if st.sidebar.button("Add Appliance"):
84
+ st.session_state["appliance_list"].append(
85
+ {
86
+ "appliance": selected_appliance,
87
+ "power": appliance_power,
88
+ "quantity": appliance_quantity,
89
+ "hours": usage_hours,
90
+ }
91
+ )
92
+
93
+ # Display the list of added appliances
94
+ st.subheader("πŸ“‹ Added Appliances")
95
+ if st.session_state["appliance_list"]:
96
+ for idx, appliance in enumerate(st.session_state["appliance_list"], start=1):
97
+ st.write(
98
+ f"{idx}. **{appliance['appliance']}** - "
99
+ f"{appliance['power']}W, {appliance['quantity']} unit(s), "
100
+ f"{appliance['hours']} hours/day"
101
+ )
102
+
103
+ # Electricity Bill and Carbon Footprint Calculation
104
+ if st.session_state["appliance_list"] and rate_per_kwh > 0:
105
+ total_daily_energy_kwh = sum(
106
+ (appliance["power"] * appliance["quantity"] * appliance["hours"]) / 1000
107
+ for appliance in st.session_state["appliance_list"]
108
+ )
109
+ monthly_energy_kwh = total_daily_energy_kwh * 30 # Assume 30 days in a month
110
+ bill_amount = monthly_energy_kwh * rate_per_kwh # Dynamic tariff rate
111
+ carbon_footprint = calculate_carbon_footprint(monthly_energy_kwh)
112
+
113
+ st.subheader("πŸ’΅ Electricity Bill & 🌍 Carbon Footprint")
114
+ st.write(f"πŸ’΅ **Estimated Electricity Bill**: **{bill_amount:.2f} PKR**")
115
+ st.write(f"🌍 **Estimated Carbon Footprint**: **{carbon_footprint:.2f} kg CO2 per month**")
116
+ else:
117
+ st.info("ℹ️ Add appliances to calculate the electricity bill and carbon footprint.")