Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- README-HF.md +22 -0
- README.md +61 -13
- encoder.pickle +3 -0
- main.py +145 -0
- model.pickle +3 -0
- requirements.txt +3 -0
README-HF.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Energy Consumption Estimator
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: blue
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.30.0
|
8 |
+
app_file: main.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
# Energy Consumption Estimator
|
13 |
+
|
14 |
+
This Streamlit app estimates energy consumption based on client attributes using a machine learning model.
|
15 |
+
|
16 |
+
## Important Note
|
17 |
+
|
18 |
+
For the app to function properly, you need to upload the following files to this Space:
|
19 |
+
- `model.pickle` (your trained model)
|
20 |
+
- `encoder.pickle` (your encoder)
|
21 |
+
|
22 |
+
These files should be placed in the same directory as `main.py`.
|
README.md
CHANGED
@@ -1,13 +1,61 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Energy Consumption Estimator
|
2 |
+
|
3 |
+
A Streamlit web application for estimating energy consumption based on various client attributes. This is a web-based version of the original PyQt5 desktop application.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
|
7 |
+
- User-friendly web interface
|
8 |
+
- Form-based input for client information
|
9 |
+
- Instant energy consumption estimation
|
10 |
+
- Responsive design that works on desktop and mobile
|
11 |
+
|
12 |
+
## Requirements
|
13 |
+
|
14 |
+
- Python 3.7+
|
15 |
+
- Streamlit
|
16 |
+
- Pandas
|
17 |
+
- Scikit-learn (for the model)
|
18 |
+
|
19 |
+
## Setup and Running Locally
|
20 |
+
|
21 |
+
1. Clone this repository
|
22 |
+
2. Install the required packages:
|
23 |
+
```
|
24 |
+
pip install -r requirements.txt
|
25 |
+
```
|
26 |
+
3. Place your `model.pickle` and `encoder.pickle` files in the same directory as `main.py`
|
27 |
+
4. Run the Streamlit app:
|
28 |
+
```
|
29 |
+
streamlit run main.py
|
30 |
+
```
|
31 |
+
|
32 |
+
## Deploying to Hugging Face Spaces
|
33 |
+
|
34 |
+
1. Create a new Space on [Hugging Face Spaces](https://huggingface.co/spaces)
|
35 |
+
2. Choose Streamlit as the SDK
|
36 |
+
3. Upload the following files to your Space:
|
37 |
+
- `main.py`
|
38 |
+
- `requirements.txt`
|
39 |
+
- `model.pickle` (your trained model)
|
40 |
+
- `encoder.pickle` (your encoder)
|
41 |
+
4. The app will automatically deploy and be available at your Space's URL
|
42 |
+
|
43 |
+
## Input Parameters
|
44 |
+
|
45 |
+
- **Age**: Client's age
|
46 |
+
- **Marital Status**: Single, Married, Separated, Divorced, Widowed, or Other
|
47 |
+
- **Day/Night Consumption**: Current consumption value
|
48 |
+
- **Income Level**: Income bracket
|
49 |
+
- **Dwelling Area**: Type of area where the client lives
|
50 |
+
- **Children**: Whether the client has children
|
51 |
+
- **Solar Panel**: Whether the client has solar panels
|
52 |
+
- **Attitude Towards Sustainability**: Client's attitude towards sustainability
|
53 |
+
- **Tariff**: Type of tariff plan
|
54 |
+
|
55 |
+
## How It Works
|
56 |
+
|
57 |
+
The application takes the input parameters, processes them using the same encoding and transformation logic as the original application, and then uses a machine learning model to predict the estimated annual energy consumption in kWh.
|
58 |
+
|
59 |
+
## Note
|
60 |
+
|
61 |
+
This application requires the same model and encoder files as the original PyQt5 application. Make sure to include these files when deploying.
|
encoder.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5d03d4112920faba49b8800885e1433baa6549d5df5ed6b7c438f8e7fb69694c
|
3 |
+
size 936
|
main.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import pickle
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
# Set page title and configuration
|
8 |
+
st.set_page_config(page_title="ISA414: Energy Consumption Estimator", layout="wide")
|
9 |
+
st.title("ISA 414: Energy Consumption Estimator")
|
10 |
+
|
11 |
+
# Create a function to load models
|
12 |
+
@st.cache_resource
|
13 |
+
def load_models():
|
14 |
+
# Check if model files exist in the current directory
|
15 |
+
model_path = Path("model.pickle")
|
16 |
+
encoder_path = Path("encoder.pickle")
|
17 |
+
|
18 |
+
if not model_path.exists() or not encoder_path.exists():
|
19 |
+
st.error("Model files not found. Please make sure 'model.pickle' and 'encoder.pickle' are in the same directory as this script.")
|
20 |
+
st.stop()
|
21 |
+
|
22 |
+
# Load model and encoding from files
|
23 |
+
model = pickle.load(open("model.pickle", 'rb'))
|
24 |
+
enc = pickle.load(open("encoder.pickle", 'rb'))
|
25 |
+
return model, enc
|
26 |
+
|
27 |
+
# Try to load models
|
28 |
+
try:
|
29 |
+
model, enc = load_models()
|
30 |
+
except Exception as e:
|
31 |
+
st.error(f"Error loading models: {str(e)}")
|
32 |
+
st.info("For demonstration purposes, you can continue using the app without the models.")
|
33 |
+
model, enc = None, None
|
34 |
+
|
35 |
+
# Create sidebar with form inputs
|
36 |
+
with st.form("consumption_form"):
|
37 |
+
st.subheader("Enter Client Information")
|
38 |
+
|
39 |
+
# Create two columns for form layout
|
40 |
+
col1, col2 = st.columns(2)
|
41 |
+
|
42 |
+
with col1:
|
43 |
+
age = st.number_input("Age", min_value=0, max_value=120, value=30)
|
44 |
+
|
45 |
+
marital_status_options = ["Single", "Married", "Separated", "Divorced", "Widowed", "Other"]
|
46 |
+
marital_status = st.selectbox("Marital Status", options=marital_status_options, index=0)
|
47 |
+
marital_status_value = str(marital_status_options.index(marital_status) + 1)
|
48 |
+
|
49 |
+
consumption = st.number_input("Day/Night Consumption", min_value=0.0, value=100.0, format="%.2f")
|
50 |
+
|
51 |
+
income_options = ["Less than 10,000", "Between 10,000 and 30,000",
|
52 |
+
"Between 40,000 and 60,000", "Between 60,000 and 100,000", "More than 100,000"]
|
53 |
+
income = st.selectbox("Income Level", options=income_options, index=0)
|
54 |
+
income_value = str(income_options.index(income) + 1)
|
55 |
+
|
56 |
+
area_options = ["City centre", "City outskirts", "Rural", "Remote"]
|
57 |
+
area = st.selectbox("Dwelling Area", options=area_options, index=0)
|
58 |
+
area_value = str(area_options.index(area) + 1)
|
59 |
+
|
60 |
+
with col2:
|
61 |
+
children_options = ["No", "Yes"]
|
62 |
+
children = st.selectbox("Children", options=children_options, index=0)
|
63 |
+
children_value = False if children_options.index(children) == 0 else True
|
64 |
+
|
65 |
+
solar_options = ["No", "Yes"]
|
66 |
+
solar = st.selectbox("Solar Panel", options=solar_options, index=0)
|
67 |
+
solar_value = False if solar_options.index(solar) == 0 else True
|
68 |
+
|
69 |
+
sustainability_options = ["Favorable", "Undecided", "Unfavorable"]
|
70 |
+
sustainability = st.selectbox("Attitude Towards Sustainability", options=sustainability_options, index=0)
|
71 |
+
sustainability_value = str(sustainability_options.index(sustainability) + 1)
|
72 |
+
|
73 |
+
tariff_options = ["Flat", "Time-of-Use", "Dynamic"]
|
74 |
+
tariff = st.selectbox("Tariff", options=tariff_options, index=0)
|
75 |
+
tariff_value = "Tariff " + str(tariff_options.index(tariff) + 1)
|
76 |
+
|
77 |
+
# Submit button
|
78 |
+
submitted = st.form_submit_button("Estimate Consumption")
|
79 |
+
|
80 |
+
# Function to make prediction
|
81 |
+
def forecast(age, marital_status, consumption, income, area, children, solar, sustainability, tariff):
|
82 |
+
# Creating a DataFrame having a new client
|
83 |
+
new_client = [age, marital_status, consumption, income, area, children, solar, sustainability, tariff]
|
84 |
+
|
85 |
+
new_client = pd.DataFrame([new_client],
|
86 |
+
columns=("Age", "MaritalStatus", "DayNightConsumption", "IncomeLevel",
|
87 |
+
"DwellingArea", "HasChildren", "SolarRoof", "AttitudeSustainability", "Tariff"))
|
88 |
+
|
89 |
+
# Categorical columns
|
90 |
+
cat_columns = ["MaritalStatus", "IncomeLevel", "DwellingArea", "AttitudeSustainability", "Tariff"]
|
91 |
+
|
92 |
+
# Using the previously created encoding and list of categorical variables to generate dummies
|
93 |
+
dummies = enc.transform(new_client[cat_columns])
|
94 |
+
|
95 |
+
# Creating a DataFrame of the dummies
|
96 |
+
dummies_df = pd.DataFrame(dummies, columns=enc.get_feature_names_out(input_features=cat_columns))
|
97 |
+
|
98 |
+
# Concatenating with the original data
|
99 |
+
new_client = pd.concat([new_client, dummies_df], axis=1)
|
100 |
+
|
101 |
+
# Dropping old columns
|
102 |
+
new_client = new_client.drop(columns=cat_columns, axis=1)
|
103 |
+
|
104 |
+
# Making prediction
|
105 |
+
prediction = model.predict(new_client)[0]
|
106 |
+
|
107 |
+
return prediction
|
108 |
+
|
109 |
+
# Display results if form is submitted
|
110 |
+
if submitted:
|
111 |
+
try:
|
112 |
+
if model is not None and enc is not None:
|
113 |
+
# Make prediction
|
114 |
+
prediction = forecast(
|
115 |
+
age,
|
116 |
+
marital_status_value,
|
117 |
+
consumption,
|
118 |
+
income_value,
|
119 |
+
area_value,
|
120 |
+
children_value,
|
121 |
+
solar_value,
|
122 |
+
sustainability_value,
|
123 |
+
tariff_value
|
124 |
+
)
|
125 |
+
|
126 |
+
# Display result
|
127 |
+
st.success(f"Estimated annual consumption: {prediction:.2f} kWh")
|
128 |
+
else:
|
129 |
+
st.warning("This is a demo mode. Models are not loaded.")
|
130 |
+
st.info("In a real deployment, the prediction would be calculated here.")
|
131 |
+
except Exception as e:
|
132 |
+
st.error(f"Error making prediction: {str(e)}")
|
133 |
+
|
134 |
+
# Add information about the app
|
135 |
+
with st.expander("About this app"):
|
136 |
+
st.write("""
|
137 |
+
This app estimates energy consumption based on various client attributes.
|
138 |
+
It uses a machine learning model to make predictions.
|
139 |
+
|
140 |
+
To use the app:
|
141 |
+
1. Enter the client information in the form
|
142 |
+
2. Click 'Estimate Consumption' to see the prediction
|
143 |
+
|
144 |
+
Note: This app requires 'model.pickle' and 'encoder.pickle' files to make actual predictions.
|
145 |
+
""")
|
model.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1ba98225bf59ddf12e20e48af8809116e5e2bc9e92e54bdaa1bccdddbcf7e55d
|
3 |
+
size 12044898
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.30.0
|
2 |
+
pandas==2.1.3
|
3 |
+
scikit-learn==1.3.2
|