Spaces:
Build error
Build error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,143 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from scraper import scrape_tariffs
|
| 6 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 7 |
-
import time
|
| 8 |
-
|
| 9 |
-
# Streamlit App: Electricity Bill & Carbon Footprint Estimator
|
| 10 |
-
st.title("π Electricity Bill & Carbon Footprint Estimator")
|
| 11 |
-
st.sidebar.header("βοΈ User Input")
|
| 12 |
-
|
| 13 |
-
# Tariff URLs for scraping
|
| 14 |
-
tariff_urls = {
|
| 15 |
-
"IESCO": "https://iesco.com.pk/index.php/customer-services/tariff-guide",
|
| 16 |
-
"FESCO": "https://fesco.com.pk/tariff",
|
| 17 |
-
"HESCO": "http://www.hesco.gov.pk/htmls/tariffs.htm",
|
| 18 |
-
"KE": "https://www.ke.com.pk/customer-services/tariff-structure/",
|
| 19 |
-
"LESCO": "https://www.lesco.gov.pk/ElectricityTariffs",
|
| 20 |
-
"PESCO": "https://pesconlinebill.pk/pesco-tariff/",
|
| 21 |
-
"QESCO": "http://qesco.com.pk/Tariffs.aspx",
|
| 22 |
-
"TESCO": "https://tesco.gov.pk/index.php/electricity-traiff",
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
# Predefined appliances and their power in watts
|
| 26 |
-
appliances = {
|
| 27 |
-
"LED Bulb (10W)": 10,
|
| 28 |
-
"Ceiling Fan (75W)": 75,
|
| 29 |
-
"Refrigerator (150W)": 150,
|
| 30 |
-
"Air Conditioner (1.5 Ton, 1500W)": 1500,
|
| 31 |
-
"Washing Machine (500W)": 500,
|
| 32 |
-
"Television (100W)": 100,
|
| 33 |
-
"Laptop (65W)": 65,
|
| 34 |
-
"Iron (1000W)": 1000,
|
| 35 |
-
"Microwave Oven (1200W)": 1200,
|
| 36 |
-
"Water Heater (2000W)": 2000,
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
def scrape_data():
|
| 40 |
-
"""
|
| 41 |
-
Scrapes tariff data from the provided URLs using parallel requests for efficiency.
|
| 42 |
-
"""
|
| 43 |
-
st.info("π Scraping tariff data... Please wait.")
|
| 44 |
-
|
| 45 |
-
def fetch_url(url):
|
| 46 |
-
"""
|
| 47 |
-
Fetches data from the given URL and returns the response.
|
| 48 |
-
"""
|
| 49 |
-
try:
|
| 50 |
-
response = requests.get(url, timeout=10)
|
| 51 |
-
return response.text
|
| 52 |
-
except requests.exceptions.RequestException as e:
|
| 53 |
-
st.warning(f"β οΈ Failed to fetch {url}: {e}")
|
| 54 |
-
return None
|
| 55 |
-
|
| 56 |
-
# Use ThreadPoolExecutor for parallel scraping
|
| 57 |
-
with ThreadPoolExecutor() as executor:
|
| 58 |
-
future_to_url = {executor.submit(fetch_url, url): url for url in tariff_urls.values()}
|
| 59 |
-
for future in as_completed(future_to_url):
|
| 60 |
-
url = future_to_url[future]
|
| 61 |
-
response = future.result()
|
| 62 |
-
if response:
|
| 63 |
-
st.write(f"β
Successfully fetched data from: {url}")
|
| 64 |
-
# Pass the response to your scraping function if necessary
|
| 65 |
-
# Example: parse_and_save_data(response)
|
| 66 |
-
else:
|
| 67 |
-
st.write(f"β Failed to fetch data from: {url}")
|
| 68 |
-
|
| 69 |
-
st.success("β
Tariff data scraping complete.")
|
| 70 |
-
|
| 71 |
-
def calculate_carbon_footprint(monthly_energy_kwh):
|
| 72 |
-
"""
|
| 73 |
-
Calculates the carbon footprint based on energy consumption in kWh.
|
| 74 |
-
"""
|
| 75 |
-
carbon_emission_factor = 0.75 # kg CO2 per kWh
|
| 76 |
-
return monthly_energy_kwh * carbon_emission_factor
|
| 77 |
-
|
| 78 |
-
# Sidebar: Scrape Tariff Data
|
| 79 |
-
if st.sidebar.button("Scrape Tariff Data"):
|
| 80 |
-
scrape_data()
|
| 81 |
-
|
| 82 |
-
# Sidebar: Tariff Selection
|
| 83 |
-
st.sidebar.subheader("π‘ Select Tariff")
|
| 84 |
-
try:
|
| 85 |
-
tariff_data = pd.read_csv("data/tariffs.csv")
|
| 86 |
-
tariff_types = tariff_data["category"].unique()
|
| 87 |
-
selected_tariff = st.sidebar.selectbox("Select your tariff category:", tariff_types)
|
| 88 |
-
rate_per_kwh = tariff_data[tariff_data["category"] == selected_tariff]["rate"].iloc[0]
|
| 89 |
-
st.sidebar.write(f"Rate per kWh: **{rate_per_kwh} PKR**")
|
| 90 |
-
except FileNotFoundError:
|
| 91 |
-
st.sidebar.error("β οΈ Tariff data not found. Please scrape the data first.")
|
| 92 |
-
rate_per_kwh = 0
|
| 93 |
-
|
| 94 |
-
# Sidebar: User Inputs for Appliances
|
| 95 |
-
st.sidebar.subheader("π Add Appliances")
|
| 96 |
-
selected_appliance = st.sidebar.selectbox("Select an appliance:", list(appliances.keys()))
|
| 97 |
-
appliance_power = appliances[selected_appliance]
|
| 98 |
-
appliance_quantity = st.sidebar.number_input(
|
| 99 |
-
"Enter quantity:", min_value=1, max_value=10, value=1
|
| 100 |
-
)
|
| 101 |
-
usage_hours = st.sidebar.number_input(
|
| 102 |
-
"Enter usage hours per day:", min_value=1, max_value=24, value=5
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# Add appliance details to the main list
|
| 106 |
-
if "appliance_list" not in st.session_state:
|
| 107 |
-
st.session_state["appliance_list"] = []
|
| 108 |
-
|
| 109 |
-
if st.sidebar.button("Add Appliance"):
|
| 110 |
-
st.session_state["appliance_list"].append(
|
| 111 |
-
{
|
| 112 |
-
"appliance": selected_appliance,
|
| 113 |
-
"power": appliance_power,
|
| 114 |
-
"quantity": appliance_quantity,
|
| 115 |
-
"hours": usage_hours,
|
| 116 |
-
}
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
# Display the list of added appliances
|
| 120 |
-
st.subheader("π Added Appliances")
|
| 121 |
-
if st.session_state["appliance_list"]:
|
| 122 |
-
for idx, appliance in enumerate(st.session_state["appliance_list"], start=1):
|
| 123 |
-
st.write(
|
| 124 |
-
f"{idx}. **{appliance['appliance']}** - "
|
| 125 |
-
f"{appliance['power']}W, {appliance['quantity']} unit(s), "
|
| 126 |
-
f"{appliance['hours']} hours/day"
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
# Electricity Bill and Carbon Footprint Calculation
|
| 130 |
-
if st.session_state["appliance_list"] and rate_per_kwh > 0:
|
| 131 |
-
total_daily_energy_kwh = sum(
|
| 132 |
-
(appliance["power"] * appliance["quantity"] * appliance["hours"]) / 1000
|
| 133 |
-
for appliance in st.session_state["appliance_list"]
|
| 134 |
-
)
|
| 135 |
-
monthly_energy_kwh = total_daily_energy_kwh * 30 # Assume 30 days in a month
|
| 136 |
-
bill_amount = monthly_energy_kwh * rate_per_kwh # Dynamic tariff rate
|
| 137 |
-
carbon_footprint = calculate_carbon_footprint(monthly_energy_kwh)
|
| 138 |
-
|
| 139 |
-
st.subheader("π΅ Electricity Bill & π Carbon Footprint")
|
| 140 |
-
st.write(f"π΅ **Estimated Electricity Bill**: **{bill_amount:.2f} PKR**")
|
| 141 |
-
st.write(f"π **Estimated Carbon Footprint**: **{carbon_footprint:.2f} kg CO2 per month**")
|
| 142 |
-
else:
|
| 143 |
-
st.info("βΉοΈ Add appliances to calculate the electricity bill and carbon footprint.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|