Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,88 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from tariff_scraper import TARIFF_URLS, scrape_tariff_data
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def main():
|
| 5 |
-
st.
|
| 6 |
-
st.
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
url = TARIFF_URLS[selected_company]
|
| 13 |
-
st.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from tariff_scraper import TARIFF_URLS, scrape_tariff_data
|
| 3 |
|
| 4 |
+
def calculate_bill(units_consumed, tariff_data):
|
| 5 |
+
"""
|
| 6 |
+
Calculate the bill based on units consumed and tariff rates.
|
| 7 |
+
Assumes the tariff data contains rates per unit in a structured format.
|
| 8 |
+
"""
|
| 9 |
+
for row in tariff_data:
|
| 10 |
+
if "to" in row.lower(): # Check if the row contains range information
|
| 11 |
+
try:
|
| 12 |
+
parts = row.split("|")
|
| 13 |
+
unit_range = parts[0].strip()
|
| 14 |
+
rate = float(parts[-1].strip()) # Assuming rate is the last column
|
| 15 |
+
|
| 16 |
+
# Parse the unit range (e.g., "1-100" or ">300")
|
| 17 |
+
if "-" in unit_range:
|
| 18 |
+
low, high = map(int, unit_range.split("-"))
|
| 19 |
+
if low <= units_consumed <= high:
|
| 20 |
+
return units_consumed * rate
|
| 21 |
+
elif ">" in unit_range:
|
| 22 |
+
threshold = int(unit_range.strip(">").strip())
|
| 23 |
+
if units_consumed > threshold:
|
| 24 |
+
return units_consumed * rate
|
| 25 |
+
except:
|
| 26 |
+
pass
|
| 27 |
+
return 0.0 # Default to 0 if no matching rate is found
|
| 28 |
+
|
| 29 |
def main():
|
| 30 |
+
st.set_page_config(page_title="Electricity Bill Calculator", layout="wide")
|
| 31 |
+
st.title("Electricity Bill Calculator")
|
| 32 |
|
| 33 |
+
# Sidebar for company selection
|
| 34 |
+
with st.sidebar:
|
| 35 |
+
st.header("Select Company")
|
| 36 |
+
selected_company = st.selectbox("Company", options=list(TARIFF_URLS.keys()))
|
| 37 |
+
st.write("Selected Company:", selected_company)
|
| 38 |
|
| 39 |
+
# Fetch tariff data from the selected company
|
| 40 |
url = TARIFF_URLS[selected_company]
|
| 41 |
+
with st.spinner("Fetching tariff data..."):
|
| 42 |
+
tariff_data = scrape_tariff_data(url)
|
| 43 |
+
|
| 44 |
+
# Main app interface for bill calculation
|
| 45 |
+
st.header("Appliances and Usage")
|
| 46 |
+
st.write("Add your appliances and usage details below:")
|
| 47 |
+
|
| 48 |
+
# Predefined appliances
|
| 49 |
+
appliances = {
|
| 50 |
+
"Fan": 75, # Power in watts
|
| 51 |
+
"Bulb": 40,
|
| 52 |
+
"Refrigerator": 150,
|
| 53 |
+
"Air Conditioner": 1500,
|
| 54 |
+
"Washing Machine": 500,
|
| 55 |
+
"Microwave": 1200
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Allow users to add appliances and customize
|
| 59 |
+
selected_appliances = st.multiselect("Select Appliances", options=list(appliances.keys()))
|
| 60 |
+
custom_appliance_name = st.text_input("Add Custom Appliance Name")
|
| 61 |
+
custom_appliance_power = st.number_input("Custom Appliance Power (Watts)", min_value=1, step=1)
|
| 62 |
+
|
| 63 |
+
if custom_appliance_name and custom_appliance_power:
|
| 64 |
+
appliances[custom_appliance_name] = custom_appliance_power
|
| 65 |
+
st.success(f"Added custom appliance: {custom_appliance_name} ({custom_appliance_power}W)")
|
| 66 |
+
|
| 67 |
+
# Input fields for usage details
|
| 68 |
+
total_units = 0
|
| 69 |
+
for appliance in selected_appliances:
|
| 70 |
+
st.subheader(f"{appliance} ({appliances[appliance]}W)")
|
| 71 |
+
num_units = st.number_input(f"How many {appliance}s?", min_value=1, step=1, key=f"{appliance}_units")
|
| 72 |
+
usage_hours = st.number_input(f"Daily usage hours for {appliance}:", min_value=1, step=1, key=f"{appliance}_hours")
|
| 73 |
+
monthly_units = (appliances[appliance] / 1000) * num_units * usage_hours * 30 # Convert watts to kWh
|
| 74 |
+
total_units += monthly_units
|
| 75 |
+
st.write(f"Estimated Monthly Units for {appliance}: {monthly_units:.2f} kWh")
|
| 76 |
+
|
| 77 |
+
st.write("### Total Estimated Monthly Units Consumed:", f"{total_units:.2f} kWh")
|
| 78 |
+
|
| 79 |
+
# Calculate the bill
|
| 80 |
+
if st.button("Calculate Bill"):
|
| 81 |
+
bill = calculate_bill(total_units, tariff_data)
|
| 82 |
+
if bill > 0:
|
| 83 |
+
st.success(f"Estimated Monthly Bill: Rs. {bill:.2f}")
|
| 84 |
+
else:
|
| 85 |
+
st.error("Could not calculate the bill. Please check the tariff data or try again.")
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
main()
|