Skip to content

swastik500/Smart_community_planner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Smart Community Energy Saving Planner

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.

🎯 Project Overview

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

πŸ—οΈ Technology Stack

Backend

  • 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

Frontend

  • HTML5/CSS3 - Structure and styling
  • Vanilla JavaScript - No framework dependencies
  • Chart.js - Data visualization

πŸ“‹ Features

1. User Roles & Authentication

  • Community Admin: Register community, create households
  • Household User: Upload bills, view analytics, get recommendations
  • JWT-based authentication with role-based access control

2. Electricity Bill Processing

  • Upload bill images/PDFs with OCR extraction
  • Manual entry option
  • Automatic data validation and correction

3. Analytics Module

  • Monthly consumption trends
  • Year-over-year comparisons
  • Community average comparisons
  • Cost per unit analysis
  • Spike and anomaly detection

4. Machine Learning Predictions

  • Linear Regression: Trend-based forecasting
  • Moving Average: Recent pattern analysis
  • Exponential Smoothing: Weighted historical data
  • Ensemble predictions combining all models

5. Recommendation Engine

  • Rule-based energy-saving suggestions
  • Triggers:
    • High consumption detected
    • Rising consumption trend
    • Predicted spike
    • Above community average
    • Seasonal inefficiency
  • Actionable suggestions with estimated savings

πŸš€ Installation & Setup

Prerequisites

  • Python 3.9+
  • PostgreSQL 12+
  • Tesseract OCR
  • Node.js (optional, for development tools)

Step 1: Clone Repository

git clone <repository-url>
cd SmartCommunitiPlanner

Step 2: Setup Python Environment

cd backend
python -m venv venv

# Windows
venv\Scripts\activate

# Linux/Mac
source venv/bin/activate

pip install -r requirements.txt

Step 3: Install Tesseract OCR

Windows:

  1. Download from: https://github.com/UB-Mannheim/tesseract/wiki
  2. Install and note the installation path
  3. Update .env with Tesseract path

Linux:

sudo apt-get install tesseract-ocr

Mac:

brew install tesseract

Step 4: Setup PostgreSQL Database

# Create database
psql -U postgres
CREATE DATABASE smart_community;
CREATE USER smartuser WITH PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE smart_community TO smartuser;
\q

Step 5: Configure Environment

cp .env.example .env
# Edit .env with your database credentials and secret key

Step 6: Initialize Database

# Run Python shell
python
>>> from app.models import init_db
>>> init_db()
>>> exit()

Step 7: Run Application

# 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

Step 8: Access Application

πŸ“ Project Structure

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

πŸ” Default Credentials

After setup, create your first community through the registration page.

πŸ“Š Database Schema

Core Tables:

  • 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.

πŸ§ͺ Testing

Manual Testing

  1. Register a community
  2. Create households as community admin
  3. Login as household user
  4. Upload electricity bills
  5. View analytics and predictions
  6. Generate recommendations

API Testing

Use the interactive API documentation at /docs

πŸ”„ Background Jobs

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

πŸ“ˆ Machine Learning Models

Linear Regression

  • Uses historical data to fit a linear trend
  • Good for stable consumption patterns
  • Provides RΒ² accuracy score

Moving Average (3-month window)

  • Averages last 3 months for prediction
  • Responds quickly to recent changes
  • Simple and interpretable

Exponential Smoothing (Ξ±=0.3)

  • Weighted average favoring recent data
  • Balances historical patterns and recent trends
  • Smooth predictions with less noise

🎨 Frontend Features

Household Dashboard

  • Overview with key metrics
  • Bill upload and management
  • Interactive analytics charts
  • Prediction visualizations
  • Recommendation cards

Community Dashboard (Admin)

  • Household management
  • Aggregated community analytics
  • Household performance distribution

πŸ”’ Security

  • Passwords hashed with bcrypt
  • JWT tokens for authentication
  • Role-based access control
  • Input validation on all endpoints
  • SQL injection protection via ORM
  • CORS configuration

🚧 Production Deployment

Environment

  • Use strong SECRET_KEY
  • Set DEBUG=False
  • Configure proper CORS_ORIGINS
  • Use production PostgreSQL server
  • Set up SSL/TLS certificates

Performance

  • Use gunicorn/uvicorn workers
  • Enable database connection pooling
  • Implement rate limiting
  • Set up caching (Redis)
  • Use CDN for static files

Monitoring

  • Application logging
  • Error tracking (Sentry)
  • Performance monitoring
  • Database query optimization

πŸ“ API Documentation

Interactive API documentation available at:

🀝 Contributing

This is an academic/demonstration project. For improvements:

  1. Follow existing code structure
  2. Add proper documentation
  3. Write tests for new features
  4. Follow PEP 8 style guide

πŸ“„ License

[Specify License]

πŸ‘₯ Authors

[Your Name/Team]

πŸ™ Acknowledgments

  • FastAPI documentation
  • scikit-learn community
  • Chart.js library
  • Tesseract OCR project

πŸ“ž Support

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.

About

A cummunity energy saving, analyzation planner

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors