A full-stack application for forecasting retail inventory using machine learning. The application visualizes forecasts with confidence intervals and historical accuracy metrics.
- Frontend: ReactJS with Recharts for visualization
- Backend: Python + Flask
- Database: PostgreSQL (time-series database)
- ML Model: Prophet (Facebook's forecasting library)
- Data Processing: Pandas
- ML-powered inventory forecasting using Prophet
- Interactive dashboard with time-series visualization
- 95% confidence intervals for predictions
- Historical accuracy metrics (MAE, MAPE)
- Support for multiple products and categories
- Configurable forecast periods (7-90 days)
- Historical data analysis (30-730 days)
- Python 3.8+
- Node.js 14+
- PostgreSQL 12+
Create a PostgreSQL database:
createdb retail_forecasterInitialize the database schema:
cd backend
psql retail_forecaster < ../database/schema.sqlCreate a virtual environment and install dependencies:
cd backend
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txtConfigure environment variables:
cp .env.example .env
# Edit .env with your database credentialsInitialize the database and generate sample data:
python database.py
python generate_sample_data.pyStart the Flask server:
python app.pyThe backend API will be available at http://localhost:5000
Install dependencies:
cd frontend
npm installStart the development server:
npm startThe frontend will be available at http://localhost:3000
GET /api/products- Get list of all productsGET /api/historical?product_id=1&days_back=90- Get historical sales dataPOST /api/forecast- Generate forecast for a productGET /api/forecast/:product_id- Get saved forecastGET /api/accuracy/:product_id- Get accuracy metricsGET /api/health- Health check
- Select a product from the dropdown
- Configure the historical days and forecast period
- Click "Generate Forecast" to create predictions
- View the interactive chart with:
- Blue line: Actual historical sales
- Purple dashed line: Forecasted sales
- Shaded area: 95% confidence interval
- Review accuracy metrics (MAE and MAPE)
product_id(SERIAL PRIMARY KEY)product_name(VARCHAR)category(VARCHAR)unit_price(DECIMAL)
sale_id(SERIAL PRIMARY KEY)product_id(FOREIGN KEY)sale_date(DATE) - indexed for time-series queriesquantity_sold(INTEGER)total_amount(DECIMAL)
forecast_id(SERIAL PRIMARY KEY)product_id(FOREIGN KEY)forecast_date(DATE)predicted_quantity(DECIMAL)lower_bound(DECIMAL)upper_bound(DECIMAL)confidence_level(DECIMAL)
# Backend tests
cd backend
python -m pytest
# Frontend tests
cd frontend
npm test# Build frontend
cd frontend
npm run build
# The build folder will contain optimized production filesThe application includes a data generator that creates realistic sample data with:
- 10 sample products across different categories
- 2 years of historical sales data
- Seasonal trends and weekly patterns
- Random variations to simulate real-world data
┌─────────────────────────────────────────┐
│ FRONTEND (ReactJS) │
│ ├─ Dashboard UI │
│ ├─ Recharts visualization │
│ └─ Product selection controls │
└────────────────┬────────────────────────┘
│ HTTP/REST
▼
┌─────────────────────────────────────────┐
│ BACKEND (Python + Flask) │
│ ├─ API endpoints │
│ ├─ Prophet forecasting model │
│ └─ Data processing (Pandas) │
└────────────────┬────────────────────────┘
│ SQL
▼
┌─────────────────────────────────────────┐
│ DATABASE (PostgreSQL) │
│ ├─ sales_data (time-series) │
│ ├─ forecasts (predictions) │
│ └─ products (metadata) │
└─────────────────────────────────────────┘
Ensure PostgreSQL is running and the DATABASE_URL in .env is correct:
# Check PostgreSQL status
sudo systemctl status postgresql
# Test connection
psql retail_forecasterProphet requires additional dependencies. If installation fails:
# On Ubuntu/Debian
sudo apt-get install python3-dev
# On macOS
brew install gcc
# Then retry
pip install prophetIf ports 5000 or 3000 are in use, modify:
- Backend: Change
FLASK_PORTin.env - Frontend: Set
PORTenvironment variable before runningnpm start