CoderHassan commited on
Commit
3a152cf
·
verified ·
1 Parent(s): 6051b19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -46
app.py CHANGED
@@ -1,29 +1,44 @@
1
  # app.py
2
  import os
3
  import streamlit as st
4
- from scraper import fetch_tariff_from_url # Import the scraper function
5
- from bs4 import BeautifulSoup
6
- import urllib3
7
  import requests
8
  import plotly.graph_objects as go
9
 
10
- # ... (Other imports and constants like APPLIANCE_OPTIONS, EMISSION_FACTORS, CITIES)
11
 
12
- # Combine the calculate_total_units and get_heating_degree_days functions here
13
-
14
- def calculate_footprint(distance_car, distance_bus, distance_plane, electricity_usage, diet_type, shopping_spent_pkr, city, house_area, exchange_rate):
15
- # ... (Existing footprint calculation logic)
16
 
17
  def get_exchange_rate():
18
- # ... (Existing exchange rate function)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Streamlit App
21
  st.title("PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")
22
 
23
- # Tariff Input Section
24
  st.sidebar.subheader("Fetch Tariff Data")
25
  tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:")
26
- load_type = st.sidebar.selectbox("Select Load Type", ["Residential", "Commercial", "Industrial", "Agriculture"])
27
 
28
  if st.sidebar.button("Fetch Tariff"):
29
  rate = fetch_tariff_from_url(tariff_url, load_type)
@@ -33,42 +48,14 @@ if st.sidebar.button("Fetch Tariff"):
33
  else:
34
  st.sidebar.warning("Failed to fetch tariff rate. Ensure the URL and load type are correct.")
35
 
36
- # Appliance Input Section
37
- # ... (Existing code for adding appliances and displaying the list)
38
 
39
- # Monthly Bill Calculation Section
40
- if st.button("Calculate Monthly Bill"):
41
- # ... (Existing code for calculating and displaying the bill)
42
-
43
- # Carbon Footprint Calculation Section
44
- st.header("Carbon Footprint Calculator")
45
  city = st.selectbox("Select the Nearest City", options=list(CITIES.keys()))
46
- distance_car = st.number_input("Monthly Distance Traveled by Car (km)", min_value=0.0)
47
- distance_bus = st.number_input("Monthly Distance Traveled by Bus (km)", min_value=0.0)
48
- distance_plane = st.number_input("Monthly Distance Traveled by Plane (km)", min_value=0.0)
49
- electricity_usage = st.number_input("Monthly Electricity Usage (kWh)", min_value=0.0)
50
- diet_type = st.radio("Diet Type", options=["meat_diet", "veg_diet"], index=0)
51
- shopping_spent_pkr = st.number_input("Monthly Shopping Expenditure (PKR)", min_value=0.0)
52
- house_area = st.number_input("House Area (m²)", min_value=0.0)
53
-
54
- exchange_rate = get_exchange_rate()
55
- st.info(f"Real-time USD to PKR exchange rate: {exchange_rate:.2f} PKR/USD")
56
-
57
  if st.button("Calculate Carbon Footprint"):
58
- result = calculate_footprint(
59
- distance_car,
60
- distance_bus,
61
- distance_plane,
62
- electricity_usage,
63
- diet_type,
64
- shopping_spent_pkr,
65
- city,
66
- house_area,
67
- exchange_rate,
68
- )
69
  st.subheader(f"Your Estimated Monthly Carbon Footprint: {result['total_emissions']:.2f} kg CO2")
70
- breakdown = result["breakdown"]
71
- labels = list(breakdown.keys())
72
- values = list(breakdown.values())
73
- fig = go.Figure(data=[go.Pie(labels=labels, values=values, title="Carbon Footprint Breakdown")])
74
- st.plotly_chart(fig)
 
1
  # app.py
2
  import os
3
  import streamlit as st
4
+ from scraper import fetch_tariff_from_url
 
 
5
  import requests
6
  import plotly.graph_objects as go
7
 
8
+ APPLIANCE_OPTIONS = { ... } # Your existing appliance list
9
 
10
+ # Tariff and Carbon Footprint related functions
11
+ def calculate_total_units():
12
+ ...
 
13
 
14
  def get_exchange_rate():
15
+ """Fetch the real-time USD to PKR conversion rate."""
16
+ try:
17
+ response = requests.get("https://open.er-api.com/v6/latest/PKR")
18
+ response.raise_for_status()
19
+ data = response.json()
20
+ if "rates" in data and "USD" in data["rates"]:
21
+ return 1 / data["rates"]["USD"]
22
+ else:
23
+ st.error("Exchange rate data is missing expected fields.")
24
+ return 300
25
+ except Exception as e:
26
+ st.error(f"Error fetching exchange rate: {e}")
27
+ return 300
28
+
29
+ def get_heating_degree_days(latitude, longitude):
30
+ ...
31
+
32
+ def calculate_footprint(distance_car, distance_bus, distance_plane, electricity_usage, diet_type, shopping_spent_pkr, city, house_area, exchange_rate):
33
+ ...
34
 
35
  # Streamlit App
36
  st.title("PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")
37
 
38
+ # Tariff Input and Appliance Section
39
  st.sidebar.subheader("Fetch Tariff Data")
40
  tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:")
41
+ load_type = st.sidebar.selectbox("Select Load Type", ["Residential", "Commercial", "Industrial", "Agriculture"])
42
 
43
  if st.sidebar.button("Fetch Tariff"):
44
  rate = fetch_tariff_from_url(tariff_url, load_type)
 
48
  else:
49
  st.sidebar.warning("Failed to fetch tariff rate. Ensure the URL and load type are correct.")
50
 
51
+ # Add Appliances Section
52
+ ...
53
 
54
+ # Calculate Carbon Footprint Section
55
+ st.header("Calculate Carbon Footprint")
 
 
 
 
56
  city = st.selectbox("Select the Nearest City", options=list(CITIES.keys()))
57
+ ...
 
 
 
 
 
 
 
 
 
 
58
  if st.button("Calculate Carbon Footprint"):
59
+ result = calculate_footprint(...)
 
 
 
 
 
 
 
 
 
 
60
  st.subheader(f"Your Estimated Monthly Carbon Footprint: {result['total_emissions']:.2f} kg CO2")
61
+ ...