akazmi commited on
Commit
ff70353
Β·
verified Β·
1 Parent(s): 1b59e15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import streamlit as st
4
+ import pandas as pd
5
+ from scraper import scrape_tariffs
6
+ from groq import Groq
7
+
8
+ # Initialize Groq client
9
+ client = Groq(api_key=os.environ.get('GroqApi'))
10
+
11
+ # Streamlit App: ⚑ EnergyGuru_PowerCalc: AI-Driven Bill & Carbon Footprint Tracker
12
+ st.title("⚑ EnergyGuru_PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")
13
+ st.sidebar.header("βš™οΈ User Input")
14
+
15
+ # Tariff URLs for scraping
16
+ tariff_urls = {
17
+ "IESCO": "https://iesco.com.pk/index.php/customer-services/tariff-guide",
18
+ "FESCO": "https://fesco.com.pk/tariff",
19
+ "HESCO": "http://www.hesco.gov.pk/htmls/tariffs.htm",
20
+ "KE": "https://www.ke.com.pk/customer-services/tariff-structure/",
21
+ "LESCO": "https://www.lesco.gov.pk/ElectricityTariffs",
22
+ "PESCO": "https://pesconlinebill.pk/pesco-tariff/",
23
+ "QESCO": "http://qesco.com.pk/Tariffs.aspx",
24
+ "TESCO": "https://tesco.gov.pk/index.php/electricity-traiff",
25
+ }
26
+
27
+ # Predefined appliances and their power in watts
28
+ appliances = {
29
+ "LED Bulb (10W)": 10,
30
+ "Ceiling Fan (75W)": 75,
31
+ "Refrigerator (150W)": 150,
32
+ "Air Conditioner (1.5 Ton, 1500W)": 1500,
33
+ "Washing Machine (500W)": 500,
34
+ "Television (100W)": 100,
35
+ "Laptop (65W)": 65,
36
+ "Iron (1000W)": 1000,
37
+ "Microwave Oven (1200W)": 1200,
38
+ "Water Heater (2000W)": 2000,
39
+ }
40
+
41
+ def scrape_data():
42
+ """
43
+ Scrapes tariff data from the provided URLs.
44
+ """
45
+ st.info("πŸ”„ Scraping tariff data... Please wait.")
46
+ scrape_tariffs(list(tariff_urls.values()))
47
+ st.success("βœ… Tariff data scraping complete.")
48
+
49
+ def calculate_carbon_footprint(monthly_energy_kwh):
50
+ """
51
+ Calculates the carbon footprint based on energy consumption in kWh.
52
+ """
53
+ carbon_emission_factor = 0.75 # kg CO2 per kWh
54
+ return monthly_energy_kwh * carbon_emission_factor
55
+
56
+ # Sidebar: Scrape Tariff Data
57
+ if st.sidebar.button("Scrape Tariff Data"):
58
+ scrape_data()
59
+
60
+ # Sidebar: Tariff Selection
61
+ st.sidebar.subheader("πŸ’‘ Select Tariff")
62
+ try:
63
+ tariff_data = pd.read_csv("data/tariffs.csv")
64
+ tariff_types = tariff_data["category"].unique()
65
+ selected_tariff = st.sidebar.selectbox("Select your tariff category:", tariff_types)
66
+ rate_per_kwh = tariff_data[tariff_data["category"] == selected_tariff]["rate"].iloc[0]
67
+ st.sidebar.write(f"Rate per kWh: **{rate_per_kwh} PKR**")
68
+ except FileNotFoundError:
69
+ st.sidebar.error("⚠️ Tariff data not found. Please scrape the data first.")
70
+ rate_per_kwh = 0
71
+
72
+ # Sidebar: User Inputs for Appliances
73
+ st.sidebar.subheader("🏠 Add Appliances")
74
+ selected_appliance = st.sidebar.selectbox("Select an appliance:", list(appliances.keys()))
75
+ appliance_power = appliances[selected_appliance]
76
+ appliance_quantity = st.sidebar.number_input(
77
+ "Enter quantity:", min_value=1, max_value=10, value=1
78
+ )
79
+ usage_hours = st.sidebar.number_input(
80
+ "Enter usage hours per day:", min_value=1, max_value=24, value=5
81
+ )
82
+
83
+ # Add appliance details to the main list
84
+ if "appliance_list" not in st.session_state:
85
+ st.session_state["appliance_list"] = []
86
+
87
+ if st.sidebar.button("Add Appliance"):
88
+ st.session_state["appliance_list"].append(
89
+ {
90
+ "appliance": selected_appliance,
91
+ "power": appliance_power,
92
+ "quantity": appliance_quantity,
93
+ "hours": usage_hours,
94
+ }
95
+ )
96
+
97
+ # Display the list of added appliances
98
+ st.subheader("πŸ“‹ Added Appliances")
99
+ if st.session_state["appliance_list"]:
100
+ for idx, appliance in enumerate(st.session_state["appliance_list"], start=1):
101
+ st.write(
102
+ f"{idx}. **{appliance['appliance']}** - "
103
+ f"{appliance['power']}W, {appliance['quantity']} unit(s), "
104
+ f"{appliance['hours']} hours/day"
105
+ )
106
+
107
+ # Electricity Bill and Carbon Footprint Calculation
108
+ if st.session_state["appliance_list"] and rate_per_kwh > 0:
109
+ total_daily_energy_kwh = sum(
110
+ (appliance["power"] * appliance["quantity"] * appliance["hours"]) / 1000
111
+ for appliance in st.session_state["appliance_list"]
112
+ )
113
+ monthly_energy_kwh = total_daily_energy_kwh * 30 # Assume 30 days in a month
114
+ bill_amount = monthly_energy_kwh * rate_per_kwh # Dynamic tariff rate
115
+ carbon_footprint = calculate_carbon_footprint(monthly_energy_kwh)
116
+
117
+ st.subheader("πŸ’΅ Electricity Bill & 🌍 Carbon Footprint")
118
+ st.write(f"πŸ’΅ **Estimated Electricity Bill**: **{bill_amount:.2f} PKR**")
119
+ st.write(f"🌍 **Estimated Carbon Footprint**: **{carbon_footprint:.2f} kg CO2 per month**")
120
+
121
+ # Generate Groq-based advice to reduce carbon footprint
122
+ appliance_names = [appliance["appliance"] for appliance in st.session_state["appliance_list"]]
123
+ appliance_list_str = ", ".join(appliance_names)
124
+
125
+ chat_completion = client.chat.completions.create(
126
+ messages=[{
127
+ "role": "user",
128
+ "content": (
129
+ f"Provide exactly 3 to 4 short, one-line tips to reduce the carbon footprint caused "
130
+ f"by the usage of the following appliances: {appliance_list_str}. The tips should be "
131
+ f"practical and relevant to these appliances only."
132
+ )
133
+ }],
134
+ model="llama3-8b-8192",
135
+ )
136
+
137
+ advice = chat_completion.choices[0].message.content
138
+ st.subheader("πŸ’‘ Tips to Reduce Carbon Footprint")
139
+ st.write(advice)
140
+
141
+
142
+ else:
143
+ st.info("ℹ️ Add appliances to calculate the electricity bill and carbon footprint.")
144
+
145
+ # Footer
146
+ st.markdown("---")
147
+ st.markdown(
148
+ "<div style='text-align: center; padding: 10px;'>"
149
+ "Designed by EnergyGuru - Powered by AI Driven Tracker<br>"
150
+ "</div>",
151
+ unsafe_allow_html=True
152
+ )