Skip to content

lilnurik/prophet_ERP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Prophet ERP - AI-Powered Backend Application

An AI-powered backend application built with Flask that provides demand forecasting and production recommendations using Facebook Prophet.

πŸš€ Quick Start

Run with Docker (Recommended)

git clone <repository-url>
cd prophet_ERP
docker-compose up -d

The API will be available at http://localhost:5000

Local Development

# Install dependencies
pip install -r requirements.txt

# Initialize database
export DATABASE_URL="sqlite:///prophet_erp.db"
python init_db.py

# Start the application
python app/main.py

🎯 Live Demo

# Run the interactive demo (server must be running)
python demo.py

Features

  • Sales, Production, and Inventory Management: Store and track business data in PostgreSQL/SQLite
  • AI-Powered Forecasting: Uses Facebook Prophet to predict demand for each SKU
  • Production Recommendations: Calculates optimal production quantities using EOQ and safety stock formulas
  • RESTful API: Clean JSON API endpoints for all operations
  • Docker Support: Complete containerization with docker-compose
  • Model Persistence: Automatic model saving/loading per SKU
  • Real-time Health Monitoring: Database connectivity and system status checks

Tech Stack

  • Backend: Flask with SQLAlchemy ORM
  • Database: PostgreSQL (production) / SQLite (development)
  • AI/ML: Facebook Prophet, pandas, numpy, scikit-learn
  • Validation: Pydantic
  • Deployment: Docker & docker-compose
  • Testing: pytest

API Endpoints

Data Input

Add Sales Record

curl -X POST http://localhost:5000/sales \
  -H "Content-Type: application/json" \
  -d '{
    "sku": "PROD001",
    "date": "2024-01-01",
    "quantity": 50
  }'

Add Production Record

curl -X POST http://localhost:5000/production \
  -H "Content-Type: application/json" \
  -d '{
    "sku": "PROD001", 
    "date": "2024-01-01",
    "quantity": 100
  }'

Update Inventory

curl -X POST http://localhost:5000/inventory \
  -H "Content-Type: application/json" \
  -d '{
    "sku": "PROD001",
    "date": "2024-01-01", 
    "quantity": 150
  }'

AI Forecasting & Recommendations

Get Demand Forecast (with AI)

curl "http://localhost:5000/forecast/PROD001?days_ahead=30"

Response includes:

  • Raw predictions with confidence intervals
  • Chart-ready data arrays
  • Forecast period information

Get Production Recommendations (Smart)

curl "http://localhost:5000/recommendations/PROD001?days_ahead=30"

Response includes:

  • Recommended production quantity
  • Current stock levels
  • Forecasted demand
  • Safety stock calculation
  • Detailed reasoning

Health Check

curl http://localhost:5000/status

AI Forecasting Engine

The forecasting module (app/services/forecasting.py) implements:

  • πŸ€– Prophet Model Training: Automatically trains time series models on historical sales data
  • πŸ’Ύ Model Persistence: Saves/loads trained models to reduce computation time
  • πŸ“ˆ Demand Forecasting: Predicts future demand with confidence intervals
  • πŸ›‘οΈ Safety Stock Calculation: Uses historical demand variability (95% service level)
  • 🏭 Smart Production Recommendations: Forecasted Demand + Safety Stock - Current Stock

Forecasting Algorithm

  1. Data Preparation: Aggregates sales data by date for each SKU
  2. Model Training: Uses Facebook Prophet with yearly and weekly seasonality
  3. Prediction: Generates forecasts for specified time horizon
  4. Safety Stock: Z-score calculation with demand variability analysis
  5. Production Recommendation: AI-driven optimal production quantities

πŸ“Š Example Results

Real Demo Output:

πŸ“ˆ FORECAST SUMMARY:
  β€’ Forecast Period: 14 days
  β€’ Total Predicted Demand: 1,822.8 units
  β€’ Average Daily Demand: 130.2 units
  β€’ Range: 116.1 - 126.1 units

🏭 PRODUCTION RECOMMENDATION:  
  β€’ Recommended Production: 1,704.93 units
  β€’ Current Stock: 200.0 units
  β€’ Forecasted Demand: 1,822.8 units
  β€’ Safety Stock: 82.13 units

Project Structure

prophet_ERP/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py              # Application factory and entry point
β”‚   β”œβ”€β”€ config.py            # Configuration settings
β”‚   β”œβ”€β”€ database.py          # Database initialization
β”‚   β”œβ”€β”€ models.py            # SQLAlchemy models (sales, production, inventory, deficits)
β”‚   β”œβ”€β”€ schemas.py           # Pydantic validation schemas
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── __init__.py      # API route definitions
β”‚   └── services/
β”‚       └── forecasting.py   # Prophet forecasting service
β”œβ”€β”€ models/                  # Stored Prophet models (one per SKU)
β”œβ”€β”€ sample_data/            # Sample CSV data for testing
β”œβ”€β”€ tests/                  # Unit tests
β”œβ”€β”€ demo.py                 # Interactive demo script
β”œβ”€β”€ Dockerfile              # Container definition
β”œβ”€β”€ docker-compose.yml      # Multi-service deployment
β”œβ”€β”€ requirements.txt        # Python dependencies
└── init_db.py             # Database initialization script

Loading Sample Data

Load the provided sample data for testing:

import pandas as pd
import requests

# Load sales data
sales_df = pd.read_csv('sample_data/sales_data.csv')
for _, row in sales_df.iterrows():
    response = requests.post('http://localhost:5000/sales', json={
        'sku': row['sku'],
        'date': row['date'],
        'quantity': row['quantity']
    })

# Load inventory data  
inventory_df = pd.read_csv('sample_data/inventory_data.csv')
for _, row in inventory_df.iterrows():
    response = requests.post('http://localhost:5000/inventory', json={
        'sku': row['sku'],
        'date': row['date'],
        'quantity': row['quantity']
    })

How Model Training Works

Automatic Training: Models are automatically trained when:

  1. A forecast is requested for a SKU with no existing model
  2. You manually delete the model file in models/{sku}_prophet_model.pkl

Model Storage: Models are persisted in the models/ directory with naming: {SKU}_prophet_model.pkl

Minimum Data: Requires at least 2 data points (recommended: 30+ for better accuracy)

Testing

Run the test suite:

# Install test dependencies  
pip install pytest pytest-flask

# Run tests
pytest tests/ -v

Tests cover:

  • Forecasting service functionality
  • API endpoint validation
  • Data model operations
  • Error handling scenarios

Environment Variables

  • DATABASE_URL: PostgreSQL/SQLite connection string
  • SECRET_KEY: Flask secret key for session management

Deployment Options

Development

export DATABASE_URL="sqlite:///prophet_erp.db"
python app/main.py

Production with Docker

docker-compose up -d

Production with PostgreSQL

export DATABASE_URL="postgresql://user:password@localhost:5432/prophet_erp"
export SECRET_KEY="your-secure-secret-key"
gunicorn --bind 0.0.0.0:5000 --workers 4 "app.main:create_app()"

Troubleshooting

Common Issues

  1. Database Connection Error: Ensure PostgreSQL is running and connection details are correct
  2. Prophet Installation Issues: May require additional system dependencies on some platforms
  3. Model Training Failures: Ensure sufficient historical data (minimum 2 data points, recommended 30+)
  4. Memory Issues: Prophet models can be memory-intensive for large datasets

Logs

Application logs include:

  • Model training status
  • API request/response details
  • Error messages with stack traces
  • Performance metrics

Performance

  • Training Time: ~1-3 seconds for 30 days of data
  • Prediction Time: ~100ms for 30-day forecasts
  • Memory Usage: ~50MB per trained model
  • Concurrent Requests: Supports multiple simultaneous API calls

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details


Built with ❀️ using Flask, Prophet, and modern Python practices

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors