surahj commited on
Commit
9ccab89
·
1 Parent(s): 9dff778

Update readme

Browse files
Files changed (1) hide show
  1. README.md +258 -112
README.md CHANGED
@@ -1,79 +1,58 @@
1
- ---
2
- title: 'Electricity Consumption Predictor'
3
- emoji: '⚡'
4
- colorFrom: 'blue'
5
- colorTo: 'purple'
6
- sdk: gradio
7
- sdk_version: 3.40.1
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- tags:
12
- - machine-learning
13
- - regression
14
- - electricity
15
- - consumption
16
- - prediction
17
- - gradio
18
- - scikit-learn
19
- datasets:
20
- - synthetic-electricity-data
21
- metrics:
22
- - mse
23
- - rmse
24
- - mae
25
- - r2
26
- library_name: scikit-learn
27
- ---
28
-
29
- # ⚡ Electricity Consumption Predictor
30
-
31
- A machine learning application that predicts daily electricity consumption based on various factors like temperature, day of the week, and special events.
32
-
33
- ## 🚀 Live Demo
34
-
35
- [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/surahj/electricity-consumption-predictor)
36
-
37
- ## 📊 Features
38
-
39
- - **Temperature-based predictions**: Considers how temperature affects electricity usage
40
- - **Day of week analysis**: Accounts for different consumption patterns on weekdays vs weekends
41
- - **Special events**: Factors in holidays and major events
42
- - **Interactive interface**: User-friendly Gradio web interface
43
- - **Model insights**: Detailed explanation of prediction factors
44
-
45
- ## 🛠️ Technology Stack
46
-
47
- - **Machine Learning**: scikit-learn (Linear Regression)
48
- - **Data Processing**: pandas, numpy
49
- - **Web Interface**: Gradio
50
- - **Model Persistence**: joblib
51
 
52
  ## 🏗️ Project Structure
53
 
54
  ```
 
55
  ├── src/
56
- │ ├── app.py # Main Gradio application
57
- │ ├── model.py # ML model implementation
58
- └── data_generator.py # Synthetic data generation
 
59
  ├── tests/
60
- │ ├── test_model.py # Model unit tests
61
- │ ├── test_app.py # App unit tests
62
- └── test_integration.py # Integration tests
63
- ├── app.py # Hugging Face Spaces entry point
64
- ├── requirements.txt # Python dependencies
65
- └── README.md # This file
 
 
 
66
  ```
67
 
68
- ## 🧪 Usage
 
 
69
 
70
- ### Local Development
 
71
 
72
- 1. **Clone the repository**:
 
 
73
 
74
  ```bash
75
- git clone https://github.com/YOUR_USERNAME/electricity-consumption-predictor.git
76
- cd electricity-consumption-predictor
77
  ```
78
 
79
  2. **Install dependencies**:
@@ -85,67 +64,235 @@ A machine learning application that predicts daily electricity consumption based
85
  3. **Run the application**:
86
 
87
  ```bash
88
- python app.py
89
  ```
90
 
91
- 4. **Run tests**:
92
- ```bash
93
- pytest tests/
94
- ```
95
 
96
- ### Hugging Face Spaces
97
 
98
- The app is automatically deployed on Hugging Face Spaces. Simply visit the live demo link above to use the application.
99
 
100
- ## 📈 How It Works
 
 
 
101
 
102
- 1. **Data Generation**: Creates synthetic electricity consumption data with realistic patterns
103
- 2. **Model Training**: Trains a linear regression model on historical data
104
- 3. **Feature Engineering**: Extracts relevant features (temperature, day of week, events)
105
- 4. **Prediction**: Uses the trained model to predict consumption for new scenarios
106
- 5. **Interpretation**: Provides detailed breakdown of prediction factors
107
 
108
- ## 🎯 Model Features
109
 
110
- - **Temperature Effect**: Higher temperatures increase AC usage
111
- - **Day of Week**: Weekends typically have different consumption patterns
112
- - **Base Consumption**: Minimum daily electricity usage
113
- - **Event Impact**: Special events can significantly affect consumption
114
 
115
- ## 📊 Example Predictions
 
116
 
117
- | Temperature | Day | Event | Predicted Consumption |
118
- | ----------- | --------- | ------- | --------------------- |
119
- | 25°C | Monday | None | 16.5 kWh |
120
- | 35°C | Saturday | Holiday | 22.3 kWh |
121
- | 15°C | Wednesday | None | 14.1 kWh |
122
 
123
- ## 🔧 Configuration
124
 
125
- The model can be customized by modifying parameters in `src/model.py`:
 
 
126
 
127
- - Training data size
128
- - Feature weights
129
- - Model hyperparameters
130
 
131
- ## 🧪 Testing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- Run the test suite to ensure everything works correctly:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  ```bash
136
- # Run all tests
137
- pytest tests/
138
 
139
- # Run with coverage
140
- pytest tests/ --cov=src
141
 
142
- # Run specific test file
143
- pytest tests/test_model.py
 
 
 
144
  ```
145
 
146
- ## 📝 License
147
 
148
- This project is licensed under the MIT License - see the LICENSE file for details.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  ## 🤝 Contributing
151
 
@@ -153,16 +300,15 @@ This project is licensed under the MIT License - see the LICENSE file for detail
153
  2. Create a feature branch
154
  3. Make your changes
155
  4. Add tests for new functionality
156
- 5. Submit a pull request
 
157
 
158
- ## 📞 Support
159
 
160
- If you encounter any issues or have questions:
161
-
162
- - Open an issue on GitHub
163
- - Check the Hugging Face Spaces discussion
164
- - Review the test files for usage examples
165
 
166
- ---
167
 
168
- **Built with ❤️ using Gradio and scikit-learn**
 
 
 
1
+ # Daily Household Electricity Consumption Predictor
2
+
3
+ A web-based application designed to help Nigerian households estimate their daily electricity usage in Kilowatt-hours (kWh). This project serves as a practical learning vehicle for Machine Learning Operations (MLOps), covering the full lifecycle from data preparation and model training to deployment, monitoring, and continuous improvement.
4
+
5
+ ## 🎯 Project Goals
6
+
7
+ ### Business Goals
8
+
9
+ - **Empower Households**: Provide users with a simple, accessible tool to understand and predict their daily electricity consumption
10
+ - **Promote Energy Awareness**: Help users identify factors influencing their electricity usage, encouraging more efficient energy habits
11
+ - **Inform Budgeting**: Enable users to better estimate their electricity bills, reducing financial surprises
12
+ - **Foundational MLOps Learning**: Serve as a concrete project to apply and understand core MLOps principles
13
+
14
+ ### Machine Learning & Technical Goals
15
+
16
+ - **Accurate Prediction**: Develop a regression model capable of predicting daily kWh consumption with acceptable accuracy
17
+ - **User-Friendly Interface**: Create an intuitive web interface that allows easy input of features and clear display of predictions
18
+ - **Deployable Application**: Build a self-contained application that can be deployed to a public platform
19
+ - **MLOps Readiness**: Design the application with modularity and best practices that facilitate future MLOps implementation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  ## 🏗️ Project Structure
22
 
23
  ```
24
+ lin-re-model/
25
  ├── src/
26
+ │ ├── __init__.py
27
+ │ ├── data_generator.py # Synthetic data generation
28
+ ├── model.py # ML model training and prediction
29
+ │ └── app.py # Gradio web interface
30
  ├── tests/
31
+ │ ├── __init__.py
32
+ │ ├── test_data_generator.py # Data generator tests
33
+ ├── test_model.py # Model tests
34
+ ├── test_app.py # Application tests
35
+ │ └── test_integration.py # Integration tests
36
+ ├── requirements.txt # Python dependencies
37
+ ├── pytest.ini # Pytest configuration
38
+ ├── run_tests.py # Test runner script
39
+ └── README.md # This file
40
  ```
41
 
42
+ ## 🚀 Quick Start
43
+
44
+ ### Prerequisites
45
 
46
+ - Python 3.8 or higher
47
+ - pip (Python package installer)
48
 
49
+ ### Installation
50
+
51
+ 1. **Clone the repository** (if not already done):
52
 
53
  ```bash
54
+ git clone <repository-url>
55
+ cd lin-re-model
56
  ```
57
 
58
  2. **Install dependencies**:
 
64
  3. **Run the application**:
65
 
66
  ```bash
67
+ python src/app.py
68
  ```
69
 
70
+ 4. **Open your browser** and navigate to `http://localhost:7860`
 
 
 
71
 
72
+ ## 🧪 Testing
73
 
74
+ This project includes comprehensive tests to ensure code quality and functionality. The test suite covers:
75
 
76
+ - **Unit Tests**: Individual component testing
77
+ - **Integration Tests**: End-to-end workflow testing
78
+ - **Data Quality Tests**: Validation of synthetic data generation
79
+ - **Model Performance Tests**: Verification of model accuracy and consistency
80
 
81
+ ### Running Tests
 
 
 
 
82
 
83
+ #### Option 1: Using the test runner script
84
 
85
+ ```bash
86
+ # Run all tests with coverage
87
+ python run_tests.py
 
88
 
89
+ # Run only unit tests
90
+ python run_tests.py unit
91
 
92
+ # Run only integration tests
93
+ python run_tests.py integration
94
+ ```
 
 
95
 
96
+ #### Option 2: Using pytest directly
97
 
98
+ ```bash
99
+ # Run all tests
100
+ pytest
101
 
102
+ # Run with verbose output
103
+ pytest -v
 
104
 
105
+ # Run with coverage report
106
+ pytest --cov=src --cov-report=html
107
+
108
+ # Run specific test file
109
+ pytest tests/test_model.py
110
+
111
+ # Run specific test class
112
+ pytest tests/test_model.py::TestElectricityConsumptionModel
113
+
114
+ # Run specific test method
115
+ pytest tests/test_model.py::TestElectricityConsumptionModel::test_train_model
116
+ ```
117
+
118
+ ### Test Coverage
119
+
120
+ The test suite provides comprehensive coverage including:
121
+
122
+ - **Data Generator Tests**:
123
+
124
+ - Data generation with different parameters
125
+ - Data splitting functionality
126
+ - Data persistence (save/load)
127
+ - Data quality validation
128
+ - Reproducibility checks
129
+
130
+ - **Model Tests**:
131
+
132
+ - Model initialization and training
133
+ - Feature preparation and validation
134
+ - Prediction functionality
135
+ - Model evaluation metrics
136
+ - Model persistence (save/load)
137
+ - Error handling
138
+
139
+ - **Application Tests**:
140
+
141
+ - Web interface functionality
142
+ - User interaction flows
143
+ - Error handling in UI
144
+ - State management
145
+
146
+ - **Integration Tests**:
147
+ - Complete workflow testing
148
+ - End-to-end functionality
149
+ - Performance consistency
150
+ - Data quality across components
151
+
152
+ ### Expected Test Results
153
+
154
+ When all tests pass, you should see output similar to:
155
+
156
+ ```
157
+ 🧪 Running Daily Household Electricity Consumption Predictor Tests
158
+ ======================================================================
159
+ ============================= test session starts ==============================
160
+ platform linux -- Python 3.8.x, pytest-7.4.0, pluggy-1.0.0
161
+ rootdir: /path/to/lin-re-model
162
+ plugins: cov-4.1.0
163
+ collected 45 tests
164
+
165
+ tests/test_app.py ................... [ 42%]
166
+ tests/test_data_generator.py ................... [ 78%]
167
+ tests/test_integration.py .......... [100%]
168
+
169
+ ---------- coverage: platform linux, python 3.8.x-final-0 -----------
170
+ Name Stmts Miss Cover Missing
171
+ ------------------------------------------------------------
172
+ src/__init__.py 1 0 100%
173
+ src/app.py 180 5 97% 180-185
174
+ src/data_generator.py 95 2 98% 95-97
175
+ src/model.py 180 8 96% 180-188
176
+ ------------------------------------------------------------
177
+ TOTAL 456 15 97%
178
+
179
+ ============================== 45 passed in 5.23s ==============================
180
+
181
+ ✅ All tests passed!
182
+ ```
183
 
184
+ ## 📊 Model Features
185
+
186
+ The electricity consumption prediction model uses the following features:
187
+
188
+ 1. **Average Daily Temperature** (°C): Numerical input (15-35°C range)
189
+ 2. **Day of the Week**: Categorical input (Monday through Sunday)
190
+ 3. **Major Event**: Boolean input (Holiday, Power Outage, etc.)
191
+
192
+ ### Model Algorithm
193
+
194
+ - **Algorithm**: Linear Regression
195
+ - **Preprocessing**: StandardScaler for numerical features, OneHotEncoder for categorical features
196
+ - **Evaluation Metrics**: MSE, RMSE, MAE, R²
197
+
198
+ ## 🎮 Using the Application
199
+
200
+ ### Step 1: Generate Data & Train Model
201
+
202
+ 1. Navigate to the "Data Generation & Training" tab
203
+ 2. Adjust parameters as desired:
204
+ - Number of Data Points (100-5000)
205
+ - Noise Level (0.01-0.5)
206
+ - Training/Validation/Test Set Proportions
207
+ 3. Click "Generate Data & Train Model"
208
+ 4. Review the training metrics and evaluation results
209
+
210
+ ### Step 2: Make Predictions
211
+
212
+ 1. Navigate to the "Prediction" tab
213
+ 2. Enter your parameters:
214
+ - Average Daily Temperature (15-35°C)
215
+ - Day of the Week
216
+ - Major Event (checkbox)
217
+ 3. Click "Predict Consumption"
218
+ 4. View your estimated daily electricity consumption
219
+
220
+ ### Step 3: Understand the Model
221
+
222
+ 1. Navigate to the "Model Information" tab
223
+ 2. Click "Show Model Information"
224
+ 3. Review feature coefficients and model interpretation
225
+
226
+ ## 🔧 Development
227
+
228
+ ### Adding New Tests
229
+
230
+ To add new tests:
231
+
232
+ 1. **Unit Tests**: Add to appropriate test file in `tests/`
233
+ 2. **Integration Tests**: Add to `tests/test_integration.py`
234
+ 3. **Follow naming convention**: `test_<functionality>`
235
+ 4. **Use descriptive docstrings**: Explain what the test validates
236
+
237
+ ### Test Best Practices
238
+
239
+ - **Isolation**: Each test should be independent
240
+ - **Descriptive names**: Test names should clearly indicate what they test
241
+ - **Assertions**: Use specific assertions with meaningful messages
242
+ - **Coverage**: Aim for high test coverage (>95%)
243
+ - **Performance**: Tests should run quickly (<10 seconds total)
244
+
245
+ ### Running Tests in Development
246
+
247
+ During development, you can run tests in different ways:
248
 
249
  ```bash
250
+ # Quick test run (no coverage)
251
+ pytest -x # Stop on first failure
252
 
253
+ # Run tests in parallel (if pytest-xdist installed)
254
+ pytest -n auto
255
 
256
+ # Run tests with detailed output
257
+ pytest -v -s
258
+
259
+ # Run tests and watch for changes
260
+ pytest-watch # Requires pytest-watch package
261
  ```
262
 
263
+ ## 🚀 Deployment
264
 
265
+ ### Local Deployment
266
+
267
+ ```bash
268
+ python src/app.py
269
+ ```
270
+
271
+ ### Hugging Face Spaces Deployment
272
+
273
+ 1. Create a new Space on Hugging Face
274
+ 2. Upload the project files
275
+ 3. Configure the Space to run `python src/app.py`
276
+ 4. The application will be available at your Space URL
277
+
278
+ ## 📈 Future Enhancements
279
+
280
+ ### MLOps Features (Future Phases)
281
+
282
+ - **Data Versioning**: Implement DVC for data version control
283
+ - **Experiment Tracking**: Integrate MLflow or Weights & Biases
284
+ - **Model Registry**: Use MLflow Model Registry for model lifecycle management
285
+ - **Containerization**: Create Dockerfile for reproducible environments
286
+ - **CI/CD**: Set up GitHub Actions for automated testing and deployment
287
+ - **Model Monitoring**: Implement monitoring for data drift and performance degradation
288
+ - **Continuous Training**: Define triggers for automated retraining
289
+
290
+ ### Model Improvements
291
+
292
+ - **Feature Engineering**: Add more complex features (historical averages, time of day, etc.)
293
+ - **Advanced Models**: Experiment with Random Forest, Gradient Boosting, etc.
294
+ - **Hyperparameter Tuning**: Implement automated hyperparameter optimization
295
+ - **Ensemble Methods**: Combine multiple models for better predictions
296
 
297
  ## 🤝 Contributing
298
 
 
300
  2. Create a feature branch
301
  3. Make your changes
302
  4. Add tests for new functionality
303
+ 5. Ensure all tests pass
304
+ 6. Submit a pull request
305
 
306
+ ## 📄 License
307
 
308
+ This project is licensed under the MIT License - see the LICENSE file for details.
 
 
 
 
309
 
310
+ ## 🙏 Acknowledgments
311
 
312
+ - Gradio team for the excellent web interface framework
313
+ - Scikit-learn team for the machine learning library
314
+ - The MLOps community for best practices and guidance