A production-grade web-based system for residential communities to analyze, predict, and reduce electricity consumption using historical bill data, statistical machine learning models, and rule-based recommendations.
This system helps residential communities:
- Analyze electricity consumption patterns with deterministic analytics
- Predict future consumption using classical ML models
- Reduce energy waste through actionable recommendations
- Compare household performance against community averages
- Track consumption trends over time
- FastAPI - Modern, fast web framework
- SQLAlchemy - SQL toolkit and ORM
- PostgreSQL - Relational database
- scikit-learn - Machine learning library
- APScheduler - Background job scheduling
- pytesseract - OCR for bill processing
- HTML5/CSS3 - Structure and styling
- Vanilla JavaScript - No framework dependencies
- Chart.js - Data visualization
- Community Admin: Register community, create households
- Household User: Upload bills, view analytics, get recommendations
- JWT-based authentication with role-based access control
- Upload bill images/PDFs with OCR extraction
- Manual entry option
- Automatic data validation and correction
- Monthly consumption trends
- Year-over-year comparisons
- Community average comparisons
- Cost per unit analysis
- Spike and anomaly detection
- Linear Regression: Trend-based forecasting
- Moving Average: Recent pattern analysis
- Exponential Smoothing: Weighted historical data
- Ensemble predictions combining all models
- Rule-based energy-saving suggestions
- Triggers:
- High consumption detected
- Rising consumption trend
- Predicted spike
- Above community average
- Seasonal inefficiency
- Actionable suggestions with estimated savings
- Python 3.9+
- PostgreSQL 12+
- Tesseract OCR
- Node.js (optional, for development tools)
git clone <repository-url>
cd SmartCommunitiPlannercd backend
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
pip install -r requirements.txtWindows:
- Download from: https://github.com/UB-Mannheim/tesseract/wiki
- Install and note the installation path
- Update
.envwith Tesseract path
Linux:
sudo apt-get install tesseract-ocrMac:
brew install tesseract# Create database
psql -U postgres
CREATE DATABASE smart_community;
CREATE USER smartuser WITH PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE smart_community TO smartuser;
\qcp .env.example .env
# Edit .env with your database credentials and secret key# Run Python shell
python
>>> from app.models import init_db
>>> init_db()
>>> exit()# Development mode
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Production mode
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4- API Documentation: http://localhost:8000/docs
- Frontend: http://localhost:8000/login.html
SmartCommunitiPlanner/
βββ backend/
β βββ app/
β β βββ __init__.py
β β βββ main.py # FastAPI application
β β βββ scheduler.py # Background jobs
β β βββ models/ # SQLAlchemy models
β β β βββ base.py
β β β βββ community.py
β β β βββ household.py
β β β βββ user.py
β β β βββ electricity_bill.py
β β β βββ analytics_result.py
β β β βββ prediction_result.py
β β β βββ recommendation.py
β β βββ routes/ # API endpoints
β β β βββ auth.py
β β β βββ community.py
β β β βββ household.py
β β β βββ bills.py
β β β βββ analytics.py
β β β βββ predictions.py
β β β βββ recommendations.py
β β βββ services/ # Business logic
β β β βββ ocr_service.py
β β β βββ analytics_service.py
β β β βββ prediction_service.py
β β β βββ recommendation_service.py
β β βββ schemas/ # Pydantic schemas
β β βββ utils/ # Helper functions
β β βββ security.py
β β βββ auth.py
β βββ requirements.txt
β βββ .env.example
βββ frontend/
β βββ login.html
β βββ household-dashboard.html
β βββ static/
β βββ css/
β β βββ style.css
β βββ js/
β βββ app.js
β βββ auth.js
β βββ dashboard.js
βββ README.md
After setup, create your first community through the registration page.
- communities: Community information
- households: Household details
- users: Authentication and role management
- electricity_bills: Bill data with OCR results
- analytics_results: Computed analytics
- prediction_results: ML predictions
- recommendations: Energy-saving recommendations
All tables use UUID primary keys and proper foreign key relationships.
- Register a community
- Create households as community admin
- Login as household user
- Upload electricity bills
- View analytics and predictions
- Generate recommendations
Use the interactive API documentation at /docs
The system runs periodic background jobs:
- Daily (2 AM): Recalculate analytics for all households
- Monthly (1st, 3 AM): Generate next month's predictions
- Weekly (Monday, 6 AM): Generate new recommendations
- Uses historical data to fit a linear trend
- Good for stable consumption patterns
- Provides RΒ² accuracy score
- Averages last 3 months for prediction
- Responds quickly to recent changes
- Simple and interpretable
- Weighted average favoring recent data
- Balances historical patterns and recent trends
- Smooth predictions with less noise
- Overview with key metrics
- Bill upload and management
- Interactive analytics charts
- Prediction visualizations
- Recommendation cards
- Household management
- Aggregated community analytics
- Household performance distribution
- Passwords hashed with bcrypt
- JWT tokens for authentication
- Role-based access control
- Input validation on all endpoints
- SQL injection protection via ORM
- CORS configuration
- Use strong
SECRET_KEY - Set
DEBUG=False - Configure proper
CORS_ORIGINS - Use production PostgreSQL server
- Set up SSL/TLS certificates
- Use gunicorn/uvicorn workers
- Enable database connection pooling
- Implement rate limiting
- Set up caching (Redis)
- Use CDN for static files
- Application logging
- Error tracking (Sentry)
- Performance monitoring
- Database query optimization
Interactive API documentation available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
This is an academic/demonstration project. For improvements:
- Follow existing code structure
- Add proper documentation
- Write tests for new features
- Follow PEP 8 style guide
[Specify License]
[Your Name/Team]
- FastAPI documentation
- scikit-learn community
- Chart.js library
- Tesseract OCR project
For issues or questions:
- Open an issue in the repository
- Contact: [your-email]
Note: This system is designed for educational and demonstration purposes. For production deployment, conduct proper security audits and load testing.
Login Credentials:
Default credentials have been removed for security. Please set up your own users after deployment.