Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293 unmodified lines333435363738394041424344454647484950515253545556575859606162636465666768697071727374# app.pyimport osimport streamlit as stfrom scraper import fetch_tariff_from_url # Import the scraper functionfrom bs4 import BeautifulSoupimport urllib3import requestsimport plotly.graph_objects as go# ... (Other imports and constants like APPLIANCE_OPTIONS, EMISSION_FACTORS, CITIES)# Combine the calculate_total_units and get_heating_degree_days functions heredef calculate_footprint(distance_car, distance_bus, distance_plane, electricity_usage, diet_type, shopping_spent_pkr, city, house_area, exchange_rate):# ... (Existing footprint calculation logic)def get_exchange_rate():# ... (Existing exchange rate function)# Streamlit Appst.title("PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")# Tariff Input Sectionst.sidebar.subheader("Fetch Tariff Data")tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:")load_type = st.sidebar.selectbox("Select Load Type", ["Residential", "Commercial", "Industrial", "Agriculture"])if st.sidebar.button("Fetch Tariff"):rate = fetch_tariff_from_url(tariff_url, load_type)3 unmodified lineselse:st.sidebar.warning("Failed to fetch tariff rate. Ensure the URL and load type are correct.")# Appliance Input Section# ... (Existing code for adding appliances and displaying the list)# Monthly Bill Calculation Sectionif st.button("Calculate Monthly Bill"):# ... (Existing code for calculating and displaying the bill)# Carbon Footprint Calculation Sectionst.header("Carbon Footprint Calculator")city = st.selectbox("Select the Nearest City", options=list(CITIES.keys()))distance_car = st.number_input("Monthly Distance Traveled by Car (km)", min_value=0.0)distance_bus = st.number_input("Monthly Distance Traveled by Bus (km)", min_value=0.0)distance_plane = st.number_input("Monthly Distance Traveled by Plane (km)", min_value=0.0)electricity_usage = st.number_input("Monthly Electricity Usage (kWh)", min_value=0.0)diet_type = st.radio("Diet Type", options=["meat_diet", "veg_diet"], index=0)shopping_spent_pkr = st.number_input("Monthly Shopping Expenditure (PKR)", min_value=0.0)house_area = st.number_input("House Area (m²)", min_value=0.0)exchange_rate = get_exchange_rate()st.info(f"Real-time USD to PKR exchange rate: {exchange_rate:.2f} PKR/USD")if st.button("Calculate Carbon Footprint"):result = calculate_footprint(distance_car,distance_bus,distance_plane,electricity_usage,diet_type,shopping_spent_pkr,city,house_area,exchange_rate,)st.subheader(f"Your Estimated Monthly Carbon Footprint: {result['total_emissions']:.2f} kg CO2")breakdown = result["breakdown"]labels = list(breakdown.keys())values = list(breakdown.values())fig = go.Figure(data=[go.Pie(labels=labels, values=values, title="Carbon Footprint Breakdown")])st.plotly_chart(fig)12345678910111213141516171819202122232425262728293031323334353637383940414243443 unmodified lines4849505152535455565758596061# app.pyimport osimport streamlit as stfrom scraper import fetch_tariff_from_urlimport requestsimport plotly.graph_objects as goAPPLIANCE_OPTIONS = { ... } # Your existing appliance list# Tariff and Carbon Footprint related functionsdef calculate_total_units():...def get_exchange_rate():"""Fetch the real-time USD to PKR conversion rate."""try:response = requests.get("https://open.er-api.com/v6/latest/PKR")response.raise_for_status()data = response.json()if "rates" in data and "USD" in data["rates"]:return 1 / data["rates"]["USD"]else:st.error("Exchange rate data is missing expected fields.")return 300except Exception as e:st.error(f"Error fetching exchange rate: {e}")return 300def get_heating_degree_days(latitude, longitude):...def calculate_footprint(distance_car, distance_bus, distance_plane, electricity_usage, diet_type, shopping_spent_pkr, city, house_area, exchange_rate):...# Streamlit Appst.title("PowerCalc: AI-Driven Bill & Carbon Footprint Tracker")# Tariff Input and Appliance Sectionst.sidebar.subheader("Fetch Tariff Data")tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:")load_type = st.sidebar.selectbox("Select Load Type", ["Residential", "Commercial", "Industrial", "Agriculture"])if st.sidebar.button("Fetch Tariff"):rate = fetch_tariff_from_url(tariff_url, load_type)3 unmodified lineselse:st.sidebar.warning("Failed to fetch tariff rate. Ensure the URL and load type are correct.")# Add Appliances Section...# Calculate Carbon Footprint Sectionst.header("Calculate Carbon Footprint")city = st.selectbox("Select the Nearest City", options=list(CITIES.keys()))...if st.button("Calculate Carbon Footprint"):result = calculate_footprint(...)st.subheader(f"Your Estimated Monthly Carbon Footprint: {result['total_emissions']:.2f} kg CO2")...
app.py+33-46