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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pickle
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Set page configuration
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Electric Bill Predictor",
|
| 9 |
+
page_icon="⚡",
|
| 10 |
+
layout="centered",
|
| 11 |
+
initial_sidebar_state="expanded"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Custom CSS
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.title {
|
| 18 |
+
color: #2c3e50;
|
| 19 |
+
text-align: center;
|
| 20 |
+
margin-bottom: 30px;
|
| 21 |
+
}
|
| 22 |
+
.input-section {
|
| 23 |
+
background-color: #f8f9fa;
|
| 24 |
+
padding: 20px;
|
| 25 |
+
border-radius: 10px;
|
| 26 |
+
margin-bottom: 20px;
|
| 27 |
+
border-left: 5px solid #27ae60;
|
| 28 |
+
}
|
| 29 |
+
.result-box {
|
| 30 |
+
background-color: #e8f5e9;
|
| 31 |
+
padding: 25px;
|
| 32 |
+
border-radius: 10px;
|
| 33 |
+
margin-top: 20px;
|
| 34 |
+
text-align: center;
|
| 35 |
+
font-size: 1.5em;
|
| 36 |
+
font-weight: bold;
|
| 37 |
+
border: 2px solid #27ae60;
|
| 38 |
+
}
|
| 39 |
+
.stButton>button {
|
| 40 |
+
background-color: #27ae60;
|
| 41 |
+
color: white;
|
| 42 |
+
border-radius: 8px;
|
| 43 |
+
padding: 12px 24px;
|
| 44 |
+
width: 100%;
|
| 45 |
+
transition: all 0.3s;
|
| 46 |
+
font-size: 1.1em;
|
| 47 |
+
}
|
| 48 |
+
.stButton>button:hover {
|
| 49 |
+
background-color: #2ecc71;
|
| 50 |
+
transform: scale(1.02);
|
| 51 |
+
}
|
| 52 |
+
.stSelectbox, .stNumberInput {
|
| 53 |
+
margin-bottom: 15px;
|
| 54 |
+
}
|
| 55 |
+
.footer {
|
| 56 |
+
text-align: center;
|
| 57 |
+
margin-top: 30px;
|
| 58 |
+
color: #777;
|
| 59 |
+
font-size: 0.9em;
|
| 60 |
+
}
|
| 61 |
+
</style>
|
| 62 |
+
""", unsafe_allow_html=True)
|
| 63 |
+
|
| 64 |
+
# Load model with caching and error handling
|
| 65 |
+
@st.cache_resource
|
| 66 |
+
def load_model():
|
| 67 |
+
try:
|
| 68 |
+
with open("final_model_.pkl", "rb") as f:
|
| 69 |
+
model = pickle.load(f)
|
| 70 |
+
st.success("✅ Model loaded successfully!")
|
| 71 |
+
return model
|
| 72 |
+
except FileNotFoundError:
|
| 73 |
+
st.error("❌ Model file not found! Please ensure 'final_model_.pkl' is in the correct directory.")
|
| 74 |
+
return None
|
| 75 |
+
except Exception as e:
|
| 76 |
+
st.error(f"❌ Error loading model: {str(e)}")
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
model = load_model()
|
| 80 |
+
|
| 81 |
+
# App title and header
|
| 82 |
+
st.markdown("<h1 class='title'>⚡ Smart Electric Bill Predictor</h1>", unsafe_allow_html=True)
|
| 83 |
+
st.markdown("Predict your monthly electricity bill based on appliance usage patterns and location.")
|
| 84 |
+
|
| 85 |
+
# Main input section
|
| 86 |
+
with st.expander("🏠 **Property & Usage Details**", expanded=True):
|
| 87 |
+
st.markdown("<div class='input-section'>", unsafe_allow_html=True)
|
| 88 |
+
|
| 89 |
+
col1, col2 = st.columns(2)
|
| 90 |
+
|
| 91 |
+
with col1:
|
| 92 |
+
st.subheader("Appliance Usage (Hours)")
|
| 93 |
+
fan = st.slider("Fan Hours per Day", 5.0, 23.0, 13.0, 0.5,
|
| 94 |
+
help="Daily usage hours of ceiling/table fans")
|
| 95 |
+
fridge = st.slider("Refrigerator Hours", 17.0, 23.0, 18.0, 0.5,
|
| 96 |
+
help="Refrigerator running hours (typically 18-24 hours)")
|
| 97 |
+
ac = st.slider("Air Conditioner Hours", 0.0, 3.0, 1.0, 0.5,
|
| 98 |
+
help="Daily AC usage hours (0 if not used)")
|
| 99 |
+
tv = st.slider("Television Hours", 3.0, 22.0, 12.0, 0.5,
|
| 100 |
+
help="Daily TV viewing hours")
|
| 101 |
+
monitor = st.slider("Computer Monitor Hours", 1.0, 12.0, 8.0, 0.5,
|
| 102 |
+
help="Daily computer usage hours")
|
| 103 |
+
|
| 104 |
+
with col2:
|
| 105 |
+
st.subheader("Location & Billing")
|
| 106 |
+
city = st.selectbox("City",
|
| 107 |
+
['Hyderabad', 'Vadodara', 'Shimla', 'Mumbai', 'Ratnagiri',
|
| 108 |
+
'New Delhi', 'Dahej', 'Ahmedabad', 'Noida', 'Nagpur', 'Chennai',
|
| 109 |
+
'Faridabad', 'Kolkata', 'Pune', 'Gurgaon', 'Navi Mumbai'],
|
| 110 |
+
help="Select your city for regional tariff rates")
|
| 111 |
+
|
| 112 |
+
company = st.selectbox("Electricity Provider",
|
| 113 |
+
['Tata Power Company Ltd.', 'NHPC', 'Jyoti Structure',
|
| 114 |
+
'Power Grid Corp', 'Ratnagiri Gas and Power Pvt. Ltd. (RGPPL)',
|
| 115 |
+
'Adani Power Ltd.', 'Kalpataru Power', 'Orient Green',
|
| 116 |
+
'Sterlite Power Transmission Ltd',
|
| 117 |
+
'Neueon Towers / Sujana Towers Ltd.', 'KEC International',
|
| 118 |
+
'Indowind Energy', 'Unitech Power Transmission Ltd.',
|
| 119 |
+
'Bonfiglioli Transmission Pvt. Ltd.', 'SJVN Ltd.',
|
| 120 |
+
'Maha Transco – Maharashtra State Electricity Transmission Co, Ltd.',
|
| 121 |
+
'L&T Transmission & Distribution', 'Guj Ind Power',
|
| 122 |
+
'Torrent Power Ltd.', 'Reliance Energy', 'GE T&D India Limited',
|
| 123 |
+
'NTPC Pvt. Ltd.',
|
| 124 |
+
'Optibelt Power Transmission India Private Limited', 'CESC',
|
| 125 |
+
'Ringfeder Power Transmission India Pvt. Ltd.', 'Reliance Power',
|
| 126 |
+
'JSW Energy Ltd.', 'Sunil Hitech Eng',
|
| 127 |
+
'Toshiba Transmission & Distribution Systems (India) Pvt. Ltd.',
|
| 128 |
+
'Jaiprakash Power', 'TransRail Lighting', 'NLC India'],
|
| 129 |
+
help="Select your electricity provider company")
|
| 130 |
+
|
| 131 |
+
month = st.selectbox("Month",
|
| 132 |
+
["January", "February", "March", "April", "May", "June",
|
| 133 |
+
"July", "August", "September", "October", "November", "December"],
|
| 134 |
+
index=7, # Default to August
|
| 135 |
+
help="Select month for seasonal variation")
|
| 136 |
+
|
| 137 |
+
monthly_hours = st.slider("Total Monthly Usage Hours", 95.0, 926.0, 826.0, 10.0,
|
| 138 |
+
help="Sum of all appliance usage hours for the month")
|
| 139 |
+
|
| 140 |
+
tariff_rate = st.slider("Electricity Tariff Rate (₹/kWh)", 7.4, 9.3, 8.2, 0.1,
|
| 141 |
+
help="Current electricity rate per unit")
|
| 142 |
+
|
| 143 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 144 |
+
|
| 145 |
+
# Prediction button and results
|
| 146 |
+
if st.button("🔍 Predict Monthly Bill", use_container_width=True):
|
| 147 |
+
if model is None:
|
| 148 |
+
st.error("Cannot make prediction - model not loaded.")
|
| 149 |
+
else:
|
| 150 |
+
try:
|
| 151 |
+
# Convert month name to number
|
| 152 |
+
month_num = ["January", "February", "March", "April", "May", "June",
|
| 153 |
+
"July", "August", "September", "October", "November", "December"].index(month) + 1
|
| 154 |
+
|
| 155 |
+
input_data = pd.DataFrame([[fan, fridge, ac, tv, monitor, month_num,
|
| 156 |
+
city, company, monthly_hours, tariff_rate]],
|
| 157 |
+
columns=['Fan', 'Refrigerator', 'AirConditioner',
|
| 158 |
+
'Television', 'Monitor', 'Month',
|
| 159 |
+
'City', 'Company', 'MonthlyHours',
|
| 160 |
+
'TariffRate'])
|
| 161 |
+
|
| 162 |
+
predicted_price = model.predict(input_data)[0]
|
| 163 |
+
|
| 164 |
+
# Display result with visual impact
|
| 165 |
+
st.markdown(f"""
|
| 166 |
+
<div class='result-box'>
|
| 167 |
+
<div style='font-size: 1.2em; margin-bottom: 10px;'>Estimated Monthly Bill</div>
|
| 168 |
+
<div style='font-size: 2em; color: #27ae60;'>₹ {predicted_price:,.2f} INR</div>
|
| 169 |
+
<div style='margin-top: 15px; font-size: 0.8em; color: #555;'>
|
| 170 |
+
Based on your usage patterns in {city} ({month})
|
| 171 |
+
</div>
|
| 172 |
+
</div>
|
| 173 |
+
""", unsafe_allow_html=True)
|
| 174 |
+
|
| 175 |
+
# Add energy saving tips based on prediction
|
| 176 |
+
if predicted_price > 5000:
|
| 177 |
+
st.warning("💡 High Bill Alert: Consider reducing AC usage and switching to energy-efficient appliances.")
|
| 178 |
+
elif predicted_price > 3000:
|
| 179 |
+
st.info("💡 Moderate Bill: You might save by using fans instead of AC when possible.")
|
| 180 |
+
else:
|
| 181 |
+
st.success("💡 Efficient Usage: Your electricity consumption is well managed!")
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
st.error(f"❌ Prediction error: {str(e)}")
|
| 185 |
+
|
| 186 |
+
# Additional information section
|
| 187 |
+
with st.expander("ℹ️ About This Prediction"):
|
| 188 |
+
st.markdown("""
|
| 189 |
+
**How this prediction works:**
|
| 190 |
+
- The model analyzes your appliance usage patterns, location, and local electricity rates
|
| 191 |
+
- Calculations consider seasonal variations in energy consumption
|
| 192 |
+
- Predictions are based on machine learning models trained on historical billing data
|
| 193 |
+
|
| 194 |
+
**For more accurate results:**
|
| 195 |
+
- Provide exact usage hours from your electricity meter if available
|
| 196 |
+
- Update tariff rates according to your latest electricity bill
|
| 197 |
+
- Consider seasonal adjustments for AC/heating usage
|
| 198 |
+
""")
|
| 199 |
+
|
| 200 |
+
# Footer
|
| 201 |
+
st.markdown("""
|
| 202 |
+
<div class='footer'>
|
| 203 |
+
Energy Conservation Starts With Awareness • Powered by Machine Learning
|
| 204 |
+
</div>
|
| 205 |
+
""", unsafe_allow_html=True)
|