Spaces:
Sleeping
Sleeping
File size: 5,955 Bytes
6c5f2fa 9fbbc09 6c5f2fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import streamlit as st
import pandas as pd
import pickle
import os
from pathlib import Path
# Set page title and configuration
st.set_page_config(page_title="ISA414: Energy Consumption Estimator", layout="wide")
st.title("ISA 414: Energy Consumption Estimator")
# Create a function to load models
@st.cache_resource
def load_models():
# Check if model files exist in the current directory
model_path = Path("model.pickle")
encoder_path = Path("encoder.pickle")
if not model_path.exists() or not encoder_path.exists():
st.error("Model files not found. Please make sure 'model.pickle' and 'encoder.pickle' are in the same directory as this script.")
st.stop()
# Load model and encoding from files
model = pickle.load(open("model.pickle", 'rb'))
enc = pickle.load(open("encoder.pickle", 'rb'))
return model, enc
# Try to load models
try:
model, enc = load_models()
except Exception as e:
st.error(f"Error loading models: {str(e)}")
st.info("For demonstration purposes, you can continue using the app without the models.")
model, enc = None, None
# Create sidebar with form inputs
with st.form("consumption_form"):
st.subheader("Enter Client Information")
# Create two columns for form layout
col1, col2 = st.columns(2)
with col1:
age = st.number_input("Age", min_value=0, max_value=120, value=30)
marital_status_options = ["Single", "Married", "Separated", "Divorced", "Widowed", "Other"]
marital_status = st.selectbox("Marital Status", options=marital_status_options, index=0)
marital_status_value = str(marital_status_options.index(marital_status) + 1)
consumption = st.number_input("Day/Night Consumption", min_value=0.0, value=1.0, format="%.2f")
income_options = ["Less than 10,000", "Between 10,000 and 30,000",
"Between 40,000 and 60,000", "Between 60,000 and 100,000", "More than 100,000"]
income = st.selectbox("Income Level", options=income_options, index=0)
income_value = str(income_options.index(income) + 1)
area_options = ["City centre", "City outskirts", "Rural", "Remote"]
area = st.selectbox("Dwelling Area", options=area_options, index=0)
area_value = str(area_options.index(area) + 1)
with col2:
children_options = ["No", "Yes"]
children = st.selectbox("Children", options=children_options, index=0)
children_value = False if children_options.index(children) == 0 else True
solar_options = ["No", "Yes"]
solar = st.selectbox("Solar Panel", options=solar_options, index=0)
solar_value = False if solar_options.index(solar) == 0 else True
sustainability_options = ["Favorable", "Undecided", "Unfavorable"]
sustainability = st.selectbox("Attitude Towards Sustainability", options=sustainability_options, index=0)
sustainability_value = str(sustainability_options.index(sustainability) + 1)
tariff_options = ["Flat", "Time-of-Use", "Dynamic"]
tariff = st.selectbox("Tariff", options=tariff_options, index=0)
tariff_value = "Tariff " + str(tariff_options.index(tariff) + 1)
# Submit button
submitted = st.form_submit_button("Estimate Consumption")
# Function to make prediction
def forecast(age, marital_status, consumption, income, area, children, solar, sustainability, tariff):
# Creating a DataFrame having a new client
new_client = [age, marital_status, consumption, income, area, children, solar, sustainability, tariff]
new_client = pd.DataFrame([new_client],
columns=("Age", "MaritalStatus", "DayNightConsumption", "IncomeLevel",
"DwellingArea", "HasChildren", "SolarRoof", "AttitudeSustainability", "Tariff"))
# Categorical columns
cat_columns = ["MaritalStatus", "IncomeLevel", "DwellingArea", "AttitudeSustainability", "Tariff"]
# Using the previously created encoding and list of categorical variables to generate dummies
dummies = enc.transform(new_client[cat_columns])
# Creating a DataFrame of the dummies
dummies_df = pd.DataFrame(dummies, columns=enc.get_feature_names_out(input_features=cat_columns))
# Concatenating with the original data
new_client = pd.concat([new_client, dummies_df], axis=1)
# Dropping old columns
new_client = new_client.drop(columns=cat_columns, axis=1)
# Making prediction
prediction = model.predict(new_client)[0]
return prediction
# Display results if form is submitted
if submitted:
try:
if model is not None and enc is not None:
# Make prediction
prediction = forecast(
age,
marital_status_value,
consumption,
income_value,
area_value,
children_value,
solar_value,
sustainability_value,
tariff_value
)
# Display result
st.success(f"Estimated annual consumption: {prediction:.2f} kWh")
else:
st.warning("This is a demo mode. Models are not loaded.")
st.info("In a real deployment, the prediction would be calculated here.")
except Exception as e:
st.error(f"Error making prediction: {str(e)}")
# Add information about the app
with st.expander("About this app"):
st.write("""
This app estimates energy consumption based on various client attributes.
It uses a machine learning model to make predictions.
To use the app:
1. Enter the client information in the form
2. Click 'Estimate Consumption' to see the prediction
Note: This app requires 'model.pickle' and 'encoder.pickle' files to make actual predictions.
""")
|