Spaces:
Build error
Build error
Upload app (2).py
Browse files- app (2).py +109 -0
app (2).py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import requests
|
| 5 |
+
from utils import calculate_bill
|
| 6 |
+
|
| 7 |
+
# Predefined appliance list with typical loads in watts
|
| 8 |
+
APPLIANCE_OPTIONS = {
|
| 9 |
+
"Fan": 75,
|
| 10 |
+
"Air Conditioner (1 Ton)": 1500,
|
| 11 |
+
"Air Conditioner (1.5 Ton)": 2200,
|
| 12 |
+
"Refrigerator": 150,
|
| 13 |
+
"LED Bulb (20W)": 20,
|
| 14 |
+
"Tube Light": 40,
|
| 15 |
+
"Iron": 1000,
|
| 16 |
+
"Microwave Oven": 1200,
|
| 17 |
+
"Washing Machine": 500,
|
| 18 |
+
"Electric Heater": 1500,
|
| 19 |
+
"Laptop": 50,
|
| 20 |
+
"Desktop Computer": 200,
|
| 21 |
+
"Television (LCD/LED)": 120,
|
| 22 |
+
"Water Pump": 1000,
|
| 23 |
+
"Geyser (Electric)": 3000
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Initialize session state for appliance data
|
| 27 |
+
if "appliance_data" not in st.session_state:
|
| 28 |
+
st.session_state.appliance_data = []
|
| 29 |
+
|
| 30 |
+
# Groq Client Setup
|
| 31 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 32 |
+
|
| 33 |
+
# App Configuration
|
| 34 |
+
st.title("Pakistani Electricity Bill Calculator")
|
| 35 |
+
st.sidebar.header("Add Appliances")
|
| 36 |
+
st.sidebar.write("Select appliances, quantity, and usage details to calculate electricity consumption.")
|
| 37 |
+
|
| 38 |
+
# Tariff Data
|
| 39 |
+
def fetch_tariff_data():
|
| 40 |
+
"""Fetch tariff data from the WAPDA website."""
|
| 41 |
+
url = "https://nepra.org.pk/consumer%20affairs/Electricity%20Bill.php" # Replace with actual URL
|
| 42 |
+
try:
|
| 43 |
+
response = requests.get(url, timeout=10)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
return response.json()
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
print(f"Error fetching tariff data: {e}")
|
| 48 |
+
return {"rate_per_unit": 25} # Return mock data as fallback
|
| 49 |
+
|
| 50 |
+
st.write("Fetching real-time tariff data...")
|
| 51 |
+
tariff_data = fetch_tariff_data()
|
| 52 |
+
st.success("Tariff data fetched successfully!")
|
| 53 |
+
|
| 54 |
+
# Appliance Selection
|
| 55 |
+
appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys()))
|
| 56 |
+
default_load = APPLIANCE_OPTIONS[appliance]
|
| 57 |
+
quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1)
|
| 58 |
+
load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load)
|
| 59 |
+
usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0)
|
| 60 |
+
|
| 61 |
+
# Add Appliance Button
|
| 62 |
+
if st.sidebar.button("Add Appliance"):
|
| 63 |
+
st.session_state.appliance_data.append({
|
| 64 |
+
"appliance": appliance,
|
| 65 |
+
"load": load,
|
| 66 |
+
"quantity": quantity,
|
| 67 |
+
"usage_hours": usage_hours
|
| 68 |
+
})
|
| 69 |
+
st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
|
| 70 |
+
|
| 71 |
+
# Display Appliance List
|
| 72 |
+
st.header("Appliance List")
|
| 73 |
+
if st.session_state.appliance_data:
|
| 74 |
+
total_load = 0
|
| 75 |
+
total_usage_hours = 0
|
| 76 |
+
total_energy_wh = 0 # Total energy in watt-hours
|
| 77 |
+
for idx, data in enumerate(st.session_state.appliance_data):
|
| 78 |
+
st.write(f"{idx+1}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day")
|
| 79 |
+
total_load += data["quantity"] * data["load"]
|
| 80 |
+
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 81 |
+
st.write(f"**Total Load: {total_load} Watts**")
|
| 82 |
+
else:
|
| 83 |
+
st.write("No appliances added yet.")
|
| 84 |
+
|
| 85 |
+
# Calculate Monthly Bill Button
|
| 86 |
+
if st.button("Calculate Monthly Bill"):
|
| 87 |
+
# Convert total energy consumption from watt-hours (Wh) to kilowatt-hours (kWh)
|
| 88 |
+
total_energy_kwh = total_energy_wh / 1000 # Convert energy to kWh
|
| 89 |
+
|
| 90 |
+
# Calculate total monthly energy consumption by multiplying by 30
|
| 91 |
+
monthly_energy_kwh = total_energy_kwh * 30
|
| 92 |
+
|
| 93 |
+
# Get the rate from tariff data or use a default rate if not found
|
| 94 |
+
rate_per_unit = tariff_data.get("rate_per_unit", 25) # Default rate of 25 PKR/kWh
|
| 95 |
+
|
| 96 |
+
# Correct Monthly Bill Calculation: Total Monthly Energy in kWh * Rate per kWh (Rate in PKR)
|
| 97 |
+
monthly_bill = monthly_energy_kwh * rate_per_unit
|
| 98 |
+
|
| 99 |
+
# Query AI Model (optional, if needed for insights)
|
| 100 |
+
query = f"Calculate the monthly electricity bill for appliances with a total energy consumption of {monthly_energy_kwh:.2f} kWh per month."
|
| 101 |
+
response = client.chat.completions.create(
|
| 102 |
+
messages=[{"role": "user", "content": query}],
|
| 103 |
+
model="llama3-8b-8192",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Display Results
|
| 107 |
+
st.write(f"**Total Monthly Electricity Bill: PKR {monthly_bill:.2f}**")
|
| 108 |
+
st.write("**AI Model Insight:**")
|
| 109 |
+
st.write(response.choices[0].message.content)
|