An end-to-end demo for retail supply chain analytics: generate realistic dummy data, engineer features, train a weekly demand-forecast model, and optimize replenishment under budget using linear programming. Exposed via a FastAPI service.
This project is driven by a clear problem contract that defines what is forecasted and what the optimizer decides:
- Forecast target: weekly
demand_qtyper SKU × location, with horizon (H = 4) weeks ahead. - Optimizer output:
order_qtyper SKU–location–period, minimizing total inventory cost (holding + stockout + ordering) under budget/capacity. - Key constraints: lead time, MOQ, capacity, optional shelf-life, and budget cap.
- Pass/fail KPIs:
- Forecasting: WAPE and MASE vs naive baseline.
- Inventory/ops: fill-rate, stockout days, and total cost vs baseline or target.
For a more detailed description, see docs/problem_contract.md.
Streamlit dashboard screenshots (example):
- Landing page (metrics + charts):
assets/landingpage1.pngassets/landingpage2.png
- SKU-location details table:
assets/detail.png
- Language: Python 3.11
- API: FastAPI + Uvicorn
- ML: pandas, numpy, scikit-learn, LightGBM (fallback to RandomForest)
- Optimization: SciPy linprog
- Packaging: Docker
app/ FastAPI app & services
main.py
services/
inference.py
optimizer.py
etl/ Dummy data + features + processed
generate_dummy.py
build_features.py
models/ Training code & artifacts
train_forecast.py
artifacts/
scripts/ Utilities (e.g., backtest placeholder)
tests/ Pytest for API
docker/ Dockerfile
- Python 3.11
- Windows PowerShell or a POSIX shell
- Optional: Docker (24+) for container runs
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install --upgrade pip
pip install -r requirements.txtpython3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt- Generate data and build features
python etl/generate_dummy.py
python etl/build_features.py- Train forecasting model (with time-based split)
python -m src.forecasting.train- Evaluate and generate predictions
python -m src.forecasting.evaluateSetelah menjalankan pipeline di atas, jalankan dashboard:
streamlit run streamlit_app.pyDashboard akan terbuka di browser (default: http://localhost:8501).
Fitur Dashboard:
- 📈 Summary metrics (WAPE untuk naive, seasonal, model)
- 📉 Forecast vs Actual comparison chart (interaktif)
- 📊 Error distribution histograms
- 🔍 Detail metrics per SKU-location (opsional)
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload- Smoke tests (untuk API)
Invoke-RestMethod -Uri "http://localhost:8000/health" -Method Get
Invoke-RestMethod -Uri "http://localhost:8000/forecast" -Method Post -ContentType "application/json" -Body '{"horizon_weeks":4,"pairs":[{"store_id":"S001","product_id":"P001"}]}'
Invoke-RestMethod -Uri "http://localhost:8000/replenish" -Method Post -ContentType "application/json" -Body '{"target_service":0.95,"capacity":50000}'Alternatively, using Make (macOS/Linux or Windows with make installed):
make setup && make data && make train && make run-
GET
/health→{ "status": "ok" } -
POST
/forecast- Request
{ "horizon_weeks": 8, "pairs": [{"store_id":"S001","product_id":"P001"}] } - Response (excerpt)
{ "horizon_weeks": 8, "forecasts": [ {"store_id":"S001","product_id":"P001","forecast":[6.2,6.1,6.0, ...]} ] }
- Request
-
POST
/replenish- Request
{ "target_service": 0.95, "capacity": 50000 } - Response (excerpt)
{ "target_service": 0.95, "capacity": 50000.0, "orders": [ {"store_id":"S001","product_id":"P001","order_qty":12,"unit_price":50.0,"cost":600} ] }
- Request
Notes:
- If no trained model is found,
/forecastreturns a reasonable naive forecast. - Replenishment solves a linear program; if the solver fails, it falls back to needs.
pytest -qdocker build -t scm-ml .
docker run -p 8000:8000 scm-ml
curl http://localhost:8000/health- Parquet errors: ensure
pyarrowis installed (included inrequirements.txt). - Windows without
make: use the PowerShell commands above or install make viachoco install make. - Port already in use: change
--port 8001when running Uvicorn.
- Push repo ke GitHub
- Login ke streamlit.io
- Connect GitHub repo
- Set main file:
streamlit_app.py - Deploy!
Note: Pastikan data/processed/predictions.csv sudah ada di repo atau generate via GitHub Actions.
Untuk deployment production dengan custom UI, bisa build Next.js frontend yang call FastAPI backend.
- ✅ Streamlit dashboard untuk visualisasi
- ETA/lead-time module
- Anomaly detection
- Deployment notes for AWS (ECS/Lambda)
- Next.js frontend untuk production


