Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,8 +31,9 @@ if "appliance_data" not in st.session_state:
|
|
| 31 |
client = Groq(api_key=os.getenv("EnergyGuru_PowerCalc"))
|
| 32 |
|
| 33 |
# App Configuration
|
| 34 |
-
st.
|
| 35 |
-
st.
|
|
|
|
| 36 |
st.sidebar.write("Select appliances, quantity, and usage details to calculate electricity consumption.")
|
| 37 |
|
| 38 |
# Tariff Data
|
|
@@ -44,11 +45,11 @@ def fetch_tariff_data():
|
|
| 44 |
response.raise_for_status()
|
| 45 |
return response.json()
|
| 46 |
except requests.exceptions.RequestException as e:
|
| 47 |
-
|
| 48 |
return {"rate_per_unit": 25} # Return mock data as fallback
|
| 49 |
|
| 50 |
-
st.
|
| 51 |
-
tariff_data = fetch_tariff_data()
|
| 52 |
st.success("Tariff data fetched successfully!")
|
| 53 |
|
| 54 |
# Appliance Selection
|
|
@@ -69,10 +70,9 @@ if st.sidebar.button("Add Appliance"):
|
|
| 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")
|
|
@@ -80,7 +80,7 @@ if st.session_state.appliance_data:
|
|
| 80 |
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 81 |
st.write(f"**Total Load: {total_load} Watts**")
|
| 82 |
else:
|
| 83 |
-
st.
|
| 84 |
|
| 85 |
# Function to calculate carbon footprint (in kg CO2)
|
| 86 |
def calculate_carbon_footprint(monthly_energy_kwh):
|
|
@@ -104,8 +104,8 @@ if st.button("Calculate Monthly Bill"):
|
|
| 104 |
# Calculate the carbon footprint based on monthly energy consumption
|
| 105 |
carbon_footprint = calculate_carbon_footprint(monthly_energy_kwh)
|
| 106 |
|
| 107 |
-
# Query AI Model
|
| 108 |
-
query = f"
|
| 109 |
response = client.chat.completions.create(
|
| 110 |
messages=[{"role": "user", "content": query}],
|
| 111 |
model="llama3-8b-8192",
|
|
@@ -114,6 +114,10 @@ if st.button("Calculate Monthly Bill"):
|
|
| 114 |
# Display Results
|
| 115 |
st.write(f"**Total Monthly Electricity Bill: PKR {monthly_bill:.2f}**")
|
| 116 |
st.write(f"**Total Carbon Footprint: {carbon_footprint:.2f} kg CO2**")
|
| 117 |
-
st.
|
| 118 |
st.write(response.choices[0].message.content)
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
client = Groq(api_key=os.getenv("EnergyGuru_PowerCalc"))
|
| 32 |
|
| 33 |
# App Configuration
|
| 34 |
+
st.set_page_config(page_title="EnergyGuru_PowerCalc", page_icon="⚡", layout="wide")
|
| 35 |
+
st.title("⚡ EnergyGuru_PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")
|
| 36 |
+
st.sidebar.header("🔌 Add Appliances")
|
| 37 |
st.sidebar.write("Select appliances, quantity, and usage details to calculate electricity consumption.")
|
| 38 |
|
| 39 |
# Tariff Data
|
|
|
|
| 45 |
response.raise_for_status()
|
| 46 |
return response.json()
|
| 47 |
except requests.exceptions.RequestException as e:
|
| 48 |
+
st.error(f"Error fetching tariff data: {e}")
|
| 49 |
return {"rate_per_unit": 25} # Return mock data as fallback
|
| 50 |
|
| 51 |
+
with st.spinner("Fetching real-time tariff data..."):
|
| 52 |
+
tariff_data = fetch_tariff_data()
|
| 53 |
st.success("Tariff data fetched successfully!")
|
| 54 |
|
| 55 |
# Appliance Selection
|
|
|
|
| 70 |
st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
|
| 71 |
|
| 72 |
# Display Appliance List
|
| 73 |
+
st.header("📋 Appliance List")
|
| 74 |
if st.session_state.appliance_data:
|
| 75 |
total_load = 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")
|
|
|
|
| 80 |
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 81 |
st.write(f"**Total Load: {total_load} Watts**")
|
| 82 |
else:
|
| 83 |
+
st.info("No appliances added yet.")
|
| 84 |
|
| 85 |
# Function to calculate carbon footprint (in kg CO2)
|
| 86 |
def calculate_carbon_footprint(monthly_energy_kwh):
|
|
|
|
| 104 |
# Calculate the carbon footprint based on monthly energy consumption
|
| 105 |
carbon_footprint = calculate_carbon_footprint(monthly_energy_kwh)
|
| 106 |
|
| 107 |
+
# Query AI Model for suggestions to reduce carbon footprint
|
| 108 |
+
query = f"How can I reduce the carbon footprint of {carbon_footprint:.2f} kg CO2 for a monthly electricity consumption of {monthly_energy_kwh:.2f} kWh?"
|
| 109 |
response = client.chat.completions.create(
|
| 110 |
messages=[{"role": "user", "content": query}],
|
| 111 |
model="llama3-8b-8192",
|
|
|
|
| 114 |
# Display Results
|
| 115 |
st.write(f"**Total Monthly Electricity Bill: PKR {monthly_bill:.2f}**")
|
| 116 |
st.write(f"**Total Carbon Footprint: {carbon_footprint:.2f} kg CO2**")
|
| 117 |
+
st.header("🌿 Tips to Reduce Carbon Footprint")
|
| 118 |
st.write(response.choices[0].message.content)
|
| 119 |
+
|
| 120 |
+
# Footer
|
| 121 |
+
st.markdown("---")
|
| 122 |
+
st.markdown("### Designed by EnergyGuru ---- Powered by PowerCalc - AI Driven Tracker")
|
| 123 |
+
|