Skip to content

abz-mhd/ai-based-demand-forcasting

Repository files navigation

🤖 AI-Based Demand Forecasting

GitHub stars GitHub forks GitHub issues GitHub license

An end-to-end time-series forecasting pipeline for accurate product demand predictions in retail and supply chain scenarios.

📖 Overview

This repository presents a comprehensive AI-based demand forecasting solution designed to predict product demand with high accuracy. It encapsulates an entire time-series forecasting pipeline, from exploratory data analysis (EDA) and feature engineering to model training, evaluation, and deployment. The system is particularly tailored for retail and supply chain contexts, enabling businesses to optimize inventory, streamline operations, and make informed strategic decisions based on precise demand predictions.

The core of the project is a deployed machine learning model that, given relevant input features, can provide future demand estimates, crucial for proactive planning and resource allocation.

✨ Features

  • End-to-End Forecasting Pipeline: A complete workflow covering data ingestion, preprocessing, modeling, and deployment.
  • Automated Feature Engineering: Utilizes various time-series features (e.g., lagged variables, rolling statistics, calendar features) to enhance model accuracy.
  • Advanced Machine Learning Model: Employs a robust XGBoost model for high-performance demand prediction.
  • Model Persistence: Pre-trained models, scalers, and label encoders are serialized (.pkl files) for efficient loading and deployment.
  • API for Predictions: The app.py script serves as an API endpoint to receive new data and return demand forecasts.
  • Performance Tracking: Includes model_performance_metrics.csv to track and evaluate model performance.
  • Scalable Data Preprocessing: Leverages scikit-learn for data scaling and categorical encoding, ensuring consistency in feature transformation.
  • Real-world Data Integration: Designed to work with real-world inventory data, as exemplified by inventory_data.json.

🛠️ Tech Stack

Programming Language: Python

Machine Learning & Data Science Libraries: Scikit-learn XGBoost Pandas NumPy

Web Framework (Inferred): Flask

Data Storage/Serialization: JSON Pickle

🚀 Quick Start

Follow these steps to set up and run the AI-based demand forecasting application locally.

Prerequisites

  • Python 3.8+
  • It's recommended to use a virtual environment.

Installation

  1. Clone the repository

    git clone https://github.com/abz-mhd/ai-based-demand-forcasting.git
    cd ai-based-demand-forcasting
  2. Create and activate a virtual environment

    python -m venv venv
    # On macOS/Linux
    source venv/bin/activate
    # On Windows
    .\venv\Scripts\activate
  3. Install dependencies Since there is no requirements.txt provided, you will need to manually install the required libraries. It's highly recommended to create a requirements.txt file first.

    Option A: Create requirements.txt (Recommended) Create a file named requirements.txt in the project root with the following content:

    Flask
    pandas
    numpy
    scikit-learn
    xgboost
    

    Then, install:

    pip install -r requirements.txt

    Option B: Install directly

    pip install Flask pandas numpy scikit-learn xgboost

Data & Model Setup

The project relies on several data files and pre-trained model artifacts, which are already included in the repository:

  • Dataset/: Directory for input data.
  • inventory_data.json: The primary dataset used for forecasting.
  • feature_names.pkl: Pickled list of feature names used by the model.
  • label_encoders.pkl: Pickled dictionary of label encoders for categorical features.
  • scaler.pkl: Pickled data scaler (e.g., StandardScaler) for numerical features.
  • xgboost_demand_forecasting_model.pkl: The serialized XGBoost model.
  • model_performance_metrics.csv: Stores evaluation metrics of the trained model.

Ensure these files are present in the root directory or their respective locations as per the Project Structure.

Start the Application

The App Run Commend.txt suggests running app.py directly.

  1. Run the Flask application:
    python app.py
    The application will typically start on http://localhost:5000 (or another port if configured within app.py).

📁 Project Structure

ai-based-demand-forcasting/
├── .idea/                            # IDE specific configurations (e.g., PyCharm)
├── App Run Commend.txt               # Instructions to run the application
├── Dataset/                          # Directory intended for datasets
├── app.py                            # Main application file, likely hosting the prediction API
├── feature_names.pkl                 # Pickled list of feature names used by the model
├── inventory_data.json               # Primary dataset containing inventory information
├── label_encoders.pkl                # Pickled label encoders for categorical features
├── model_performance_metrics.csv     # CSV containing performance metrics of the trained model
├── scaler.pkl                        # Pickled data scaler (e.g., StandardScaler)
├── xgboost_demand_forecasting_model.pkl # The pre-trained XGBoost demand forecasting model
└── README.md                         # This README file

📚 API Reference

The app.py file likely exposes a prediction endpoint. Below is an inferred structure for how to interact with the deployed model.

POST /predict

Makes a demand prediction based on provided input features.

  • URL: /predict
  • Method: POST
  • Content-Type: application/json

Request Body Example:

{
  "date": "2026-03-01",
  "store": "StoreA",
  "item": "Item1",
  "price": 10.50,
  "promotion": 0,
  "holiday": 0
  // ... other features required by the model
}

Note: The actual required features depend on the feature_names.pkl and the model's training data. Ensure all necessary features are provided in the correct format.

Response Body Example (Success):

{
  "predicted_demand": 125.75,
  "unit": "units"
}

Response Body Example (Error):

{
  "error": "Missing input feature: 'promotion'",
  "status_code": 400
}

🔧 Development

Available Scripts

  • python app.py: Starts the Flask application and makes the prediction API available.

Development Workflow

  1. Ensure all prerequisites are installed and the virtual environment is activated.
  2. Make changes to app.py or any related scripts.
  3. Restart the application using python app.py to apply changes.
  4. Test the API endpoint using tools like curl, Postman, or a custom script.

🧪 Testing

This project includes a model_performance_metrics.csv file which records the evaluation metrics of the trained model. For real-time testing of the deployed prediction API:

  1. Ensure the application is running (python app.py).
  2. Send POST requests to the /predict endpoint with various input data.
  3. Verify that the API returns predictions in the expected format.

Note: There are no explicit unit/integration test files (tests/) detected in the repository structure. It is recommended to add these for robust development.

🚀 Deployment

The app.py script serves as a standalone Flask application that can be deployed as a web service.

  • For local development, simply run python app.py.
  • For production deployment, it is recommended to use a production-ready WSGI server like Gunicorn or uWSGI, typically behind a reverse proxy (e.g., Nginx), or deploy to cloud platforms such as AWS Elastic Beanstalk, Google Cloud Run, or Azure App Service.
  • Consider containerization with Docker for consistent environments across development and production. (No Dockerfile detected in current structure).

🤝 Contributing

We welcome contributions to enhance this AI-based demand forecasting pipeline! Please consider contributing by:

  • Improving the demand forecasting model (e.g., trying new algorithms, hyperparameter tuning).
  • Adding more sophisticated feature engineering techniques.
  • Enhancing the API with additional endpoints or error handling.
  • Adding a proper requirements.txt file for easier setup.
  • Implementing unit and integration tests.
  • Creating a Dockerfile for containerization.
  • Improving documentation or providing examples.

📄 License

This project is open-source. Please see the repository for specific licensing information. If no explicit LICENSE file is present, standard open-source practices apply.

🙏 Acknowledgments

  • This project leverages powerful open-source libraries like Pandas, NumPy, Scikit-learn, XGBoost, and Flask.
  • Inspired by best practices in time-series forecasting and MLOps.

📞 Support & Contact

  • 🐛 Issues: Feel free to open an issue on the GitHub Issues page for bug reports or feature requests.

⭐ Star this repo if you find it helpful!

Made with ❤️ by abz-mhd

About

AI‑Based Demand Forecasting — End‑to‑end time‑series forecasting pipeline (EDA → feature engineering → model training → evaluation → deployment) for accurate product demand predictions in retail/supply chain scenarios.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages