Skip to content

Repository files navigation

AI-Powered Sales Forecasting System

A professional, full-stack web application built with Django and Machine Learning (Random Forest) to predict future product sales based on historical transaction datasets.

This application allows users to upload sales datasets (CSV/Excel format), processes the data dynamically, performs multi-product recursive forecasting, generates customized trend charts, and enables exporting the predicted sales tables back into CSV or Excel files.


🚀 Key Features

  • Interactive Web Interface: A modern, responsive frontend built with CSS3 animations, Bootstrap 5, and Django Crispy Forms.
  • User Authentication: Secure signup, login, password reset, and session-based route protection using a custom decorator.
  • Smart Dataset Processing: Seamless parsing of uploaded CSV and Excel (.xlsx) files.
  • Profit & Loss Calculations: Automatic calculations of profit/loss fields on the uploaded dataset.
  • Multi-Product Forecasting Engine: Uses a pre-trained Random Forest model for recursive forecasting (default horizon: 10 months) customized for multiple products.
  • Dynamic Data Visualization: Generates high-quality Matplotlib line charts comparing historical data with forecasts, rendered directly in the UI as base64 images.
  • Export Utilities: Quick export options to download predictions as CSV or formatted Excel files.
  • Feedback System: Integrates a "Contact Us" form that saves user submissions to the database for administrative review.

🛠️ Tech Stack

  • Backend: Django 5.x, Python 3
  • Frontend: HTML5, CSS3, Bootstrap 5, FontAwesome, Django Crispy Forms (Bootstrap 5 pack)
  • Machine Learning & Analytics: Scikit-learn (Random Forest Regressor), Pandas, NumPy, Joblib
  • Data Visualization: Matplotlib
  • Database: SQLite3 (Development)

📁 Repository Structure

├── config/                 # Django project configurations (settings, urls, wsgi)
├── main/                   # Core Django application containing views, models, forms, templates
│   ├── migrations/         # Database migrations
│   ├── static/             # CSS, JS, and image assets
│   ├── templates/          # HTML views (home, prediction, login, signup, base layout)
│   ├── models.py           # CustomUser and ContactMessage models
│   ├── views.py            # Core views, login/registration controllers, forecasting algorithms
│   └── forms.py            # User registration and authentication forms
├── ml/                     # Machine learning module
│   └── artifacts/          # Pre-trained Random Forest models and metadata (PKL, JSON)
├── requirements.txt        # Python package dependencies
├── manage.py               # Django management script
└── db.sqlite3              # Local SQLite database (ignored by git in production)

⚙️ Installation & Local Setup

Follow these steps to run the project locally on your machine:

Prerequisites

Make sure you have Python 3.10+ installed on your system.

1. Clone the Repository

git clone https://github.com/Awaisghan/Sales-Prediction-System.git
cd Sales-Prediction-System

2. Create and Activate a Virtual Environment

On macOS/Linux:

python3 -m venv venv
source venv/bin/activate

On Windows:

python -m venv venv
venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

4. Run Database Migrations

Initialize the database schemas:

python manage.py migrate

5. Create a Superuser (Optional)

To access the Django admin panel, create an admin account:

python manage.py createsuperuser

6. Run the Development Server

python manage.py runserver

Open your browser and navigate to http://127.0.0.1:8000/.


🧠 Machine Learning & Forecasting Pipeline

The forecasting pipeline is executed in the following steps:

  1. Data Ingestion: The user uploads a CSV/Excel file containing transaction records (date, product, sales, price, cost).
  2. Feature Engineering: For each product group, historical lag features are generated:
    • sales_lag1, sales_lag2, sales_lag3 (Previous 3 months of sales)
    • sales_roll3 (3-month rolling mean of sales)
    • month (Extracted calendar month)
  3. Recursive Forecasting:
    • A pre-trained RandomForestRegressor model (loaded from models_by_product.pkl) dynamically predicts the sales for the next month.
    • This predicted value is fed back recursively to build lag features for subsequent months, forecasting up to 10 months ahead.
  4. Data Visualization: Matplotlib plots a combined view of historical sales (green line) and forecasted sales (magenta line), showing clear trends and seasonal patterns.

About

An AI-powered Sales Forecasting web application built with Django and Machine Learning (Random Forest) to predict product sales from uploaded datasets

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages