Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,93 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from tariff_scraper import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Sidebar: Tariff Rates
|
| 8 |
-
st.sidebar.subheader("Tariff Rates")
|
| 9 |
-
url = st.sidebar.text_input("Tariff URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
tariff_rate = tariff_categories.get(load_type, 0.0)
|
| 25 |
-
else:
|
| 26 |
-
st.sidebar.warning("Using default rate. Fetch tariff rates to use live data.")
|
| 27 |
-
load_type = "Default"
|
| 28 |
-
tariff_rate = st.sidebar.number_input("Default Tariff Rate (PKR/unit)", min_value=0.0, value=20.0)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
appliance = st.sidebar.selectbox("Select Appliance", options=appliances.keys())
|
| 37 |
-
quantity = st.sidebar.number_input("Quantity", min_value=1, value=1)
|
| 38 |
-
usage_hours = st.sidebar.number_input("Usage Hours/Day", min_value=1.0, value=6.0)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
st.write("### Selected Appliances")
|
| 51 |
-
if "appliances" in st.session_state:
|
| 52 |
-
for app in st.session_state["appliances"]:
|
| 53 |
-
st.write(f"{app['quantity']} x {app['appliance']} ({app['usage_hours']} hours/day)")
|
| 54 |
-
|
| 55 |
-
# Calculate Bill
|
| 56 |
-
if st.button("Calculate Bill"):
|
| 57 |
-
total_units = sum(
|
| 58 |
-
app["quantity"] * app["load"] * app["usage_hours"] / 1000 for app in st.session_state["appliances"]
|
| 59 |
-
)
|
| 60 |
-
bill = total_units * 30 * tariff_rate
|
| 61 |
-
st.success(f"Estimated Monthly Bill for {load_type}: PKR {bill:.2f}")
|
| 62 |
-
else:
|
| 63 |
-
st.write("No appliances added yet.")
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from tariff_scraper import fetch_tariff_data
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from groq import Groq
|
| 6 |
|
| 7 |
+
# Initialize Groq client
|
| 8 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Predefined appliance list with typical loads in watts
|
| 11 |
+
APPLIANCE_OPTIONS = {
|
| 12 |
+
"Fan": 75,
|
| 13 |
+
"Air Conditioner (1 Ton)": 1500,
|
| 14 |
+
"Air Conditioner (1.5 Ton)": 2200,
|
| 15 |
+
"Refrigerator": 150,
|
| 16 |
+
"LED Bulb (20W)": 20,
|
| 17 |
+
"Tube Light": 40,
|
| 18 |
+
"Iron": 1000,
|
| 19 |
+
"Microwave Oven": 1200,
|
| 20 |
+
"Washing Machine": 500,
|
| 21 |
+
"Electric Heater": 1500,
|
| 22 |
+
"Laptop": 50,
|
| 23 |
+
"Desktop Computer": 200,
|
| 24 |
+
"Television (LCD/LED)": 120,
|
| 25 |
+
"Water Pump": 1000,
|
| 26 |
+
"Geyser (Electric)": 3000
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
if "appliance_data" not in st.session_state:
|
| 30 |
+
st.session_state.appliance_data = []
|
| 31 |
+
if "tariff_rate" not in st.session_state:
|
| 32 |
+
st.session_state.tariff_rate = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def calculate_total_units():
|
| 35 |
+
"""Calculate total units consumed in kWh."""
|
| 36 |
+
total_energy_wh = 0
|
| 37 |
+
for data in st.session_state.appliance_data:
|
| 38 |
+
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 39 |
+
return total_energy_wh / 1000 # Convert watt-hours to kilowatt-hours
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def fetch_tariff_from_groq(units):
|
| 42 |
+
"""Fetch tariff rate based on units consumed using Groq API."""
|
| 43 |
+
try:
|
| 44 |
+
response = client.chat.completions.create(
|
| 45 |
+
messages=[
|
| 46 |
+
{
|
| 47 |
+
"role": "user",
|
| 48 |
+
"content": f"Provide tariff rate for {units} units consumed in PKR."
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
model="llama3-8b-8192",
|
| 52 |
+
)
|
| 53 |
+
return float(response.choices[0].message.content.strip())
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(f"Error fetching tariff from Groq: {e}")
|
| 56 |
+
return None
|
| 57 |
|
| 58 |
+
st.title("Dynamic Tariff Electricity Bill Calculator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Add Appliances
|
| 61 |
+
st.sidebar.subheader("Add Appliances")
|
| 62 |
+
appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys()))
|
| 63 |
+
default_load = APPLIANCE_OPTIONS[appliance]
|
| 64 |
+
quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1)
|
| 65 |
+
load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load)
|
| 66 |
+
usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0)
|
| 67 |
+
|
| 68 |
+
if st.sidebar.button("Add Appliance"):
|
| 69 |
+
st.session_state.appliance_data.append({
|
| 70 |
+
"appliance": appliance,
|
| 71 |
+
"load": load,
|
| 72 |
+
"quantity": quantity,
|
| 73 |
+
"usage_hours": usage_hours
|
| 74 |
+
})
|
| 75 |
+
st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
|
| 76 |
+
|
| 77 |
+
# Display Appliance List
|
| 78 |
+
st.subheader("Appliance List")
|
| 79 |
+
if st.session_state.appliance_data:
|
| 80 |
+
for idx, data in enumerate(st.session_state.appliance_data, start=1):
|
| 81 |
+
st.write(f"{idx}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day")
|
| 82 |
+
else:
|
| 83 |
+
st.write("No appliances added.")
|
| 84 |
+
|
| 85 |
+
# Calculate Monthly Units and Electricity Bill
|
| 86 |
+
if st.button("Calculate Monthly Bill"):
|
| 87 |
+
total_units_kwh = calculate_total_units() * 30 # Assuming 30 days in a month
|
| 88 |
+
st.write(f"**Total Units Consumed (kWh): {total_units_kwh:.2f}**")
|
| 89 |
+
tariff_rate = fetch_tariff_from_groq(total_units_kwh)
|
| 90 |
+
if tariff_rate:
|
| 91 |
+
monthly_bill = total_units_kwh * tariff_rate
|
| 92 |
+
st.subheader("Electricity Bill")
|
| 93 |
+
st.write(f"**Total Monthly Bill: PKR {monthly_bill:.2f}**")
|