This project implements a production-ready demand forecasting system using historical time-series data. The final solution is exposed as a REST API and deployed on Google Cloud Run.
The focus of this project is on:
- Time-series thinking
- Proper model evaluation
- Production deployment using cloud services
Data → Feature Engineering → Model → FastAPI → Docker → Cloud Run → Client Request
Forecast weekly demand using historical demand patterns.
Such forecasting problems are common in:
- Inventory planning
- Supply chain optimization
- Retail operations
- Capacity planning
The goal is to predict the next period’s demand based on past behavior.
- Raw demand data was aggregated to weekly demand
- Ensured time continuity (no missing weeks)
- Handled missing and inconsistent values
- Created a clean time-series dataset suitable for forecasting
Core columns: center_id | week | total_demand
--
Time-based feature engineering was applied to capture temporal patterns.
- Week of year
- Month (derived from week index)
lag_1: demand from the previous weeklag_4: demand from four weeks ago
rolling_mean_4: 4-week rolling average (shifted to avoid data leakage)
These features help capture:
- Short-term demand memory
- Trends
- Seasonality
- Time-based split (no random shuffling)
- Most recent weeks used for validation
- Prevents data leakage
- Mimics real-world forecasting conditions
Multiple models were evaluated:
| Model | Result |
|---|---|
| Naive Baseline (lag-1) | ✅ Best |
| Random Forest | Higher error |
| Boosting models | No improvement |
The baseline lag-based model performed best due to strong temporal persistence in demand.
Why baseline was chosen:
- Lower validation error
- Simple and stable
- Easy to explain and maintain in production
This reflects real industry practice where simpler models are preferred when they perform well.
The forecasting logic is exposed via a REST API.
POST /predict
- Input validation and explicit failure handling (negative, zero, unrealistic values)
- Lightweight data drift awareness using training distribution statistics
- Inference latency logging
- Model metadata endpoint (/model-info)
- Cloud-native deployment on Google Cloud Run
- Containerized with Docker
The API explicitly handles:
- Negative inputs
- Zero values
- Unrealistic large values
- Drift detection warnings
If a new model version underperforms in production:
- Revert to previous
model_info.jsonversion. - Redeploy the last stable container image from Artifact Registry.
- Validate using the
/model-infoendpoint. - Confirm health via
/healthand sample/predictrequests.
This ensures safe rollback without service interruption.
- Automated retraining pipeline
- Statistical drift detection using distribution comparison
- CI/CD integration for model deployment
- Monitoring dashboard for real-time performance tracking
{
"lag_1": 25000
}
Response
{
"predicted_demand": 25000.0
}