A simple machine learning project to predict future sales totals based on product, customer, quantity, and price using a Random Forest Regressor and Linear Regression. This project includes:
- Data preprocessing & model training
- Flask API for real-time predictions
- Visualization of prediction performance
- Easy setup & deployment-ready code
- Upload
.csvor.xlsxExcel files containing sales data - Instantly get predicted totals using a trained machine learning model
- View results in a clean HTML table
- Download results as a CSV file
- View a live bar chart of predictions at the bottom of the page
Your file must have the following columns:
productcustomerquantityprice
Example:
| product | customer | quantity | price |
|---|---|---|---|
| pen | Alice | 10 | 2 |
| book | Bob | 5 | 8 |
- Select a
.csvor.xlsxfile from your system. - The model will automatically predict totals for each row.
- The results will be shown in a table.
- Click “Download Results as CSV” to save predictions.
- View a bar chart of predicted totals at the bottom of the page.
The chart shows the predicted sales total for each row visually using a bar graph.
- Backend: Python, Flask
- Machine Learning: scikit-learn, pandas
- Frontend: HTML, JavaScript (PapaParse, XLSX.js, Chart.js)
- Deployment: Replit
https://3c96b8b2-4475-48a0-941c-69ab9e588cff-00-1m3398d51iq58.pike.replit.dev:5000
OR
https://sales-predictor-ai-engine-production.up.railway.app/api/v1/predict
curl -X POST https://3c96b8b2-4475-48a0-941c-69ab9e588cff-00-1m3398d51iq58.pike.replit.dev:5000/api/v1/predict -H "Content-Type: application/json" -d '[
{"product": "Laptop", "customer": "John", "quantity": 2, "price": 800},
{"product": "Tablet", "customer": "Alice", "quantity": 3, "price": 400},
{"product": "Phone", "customer": "Mark", "quantity": 0, "price": 900}
]'Response:
[
{
"index": 0,
"predicted_total": 1520.0
},
{
"index": 1,
"predicted_total": 1205.0
},
{
"index": 2,
"error": "Quantity must be a positive number."
}
]✅ Valid records will also be appended to data/new_sales.csv for future retraining purposes.
predict_sales_ai_model/
├── api/
│ ├── routes
| | └──__init__.py
| | └──v1.py
| ├── _init_.py
| └── app.py
├── charts/
│ └── random_forest_prediction_vs_actual.png
├── data/
│ └── sales_50.csv
│ └── sales_1000.csv
├── model/
│ └──random_forest
| ├── model.pkl
| ├── model_columns.pkl
| └── train_and_save.py
├── tests/
│ └── test_random_forest_model.py (optional)
├── requirements.txt
├── .gitignore
└── README.md
product: Product name (categorical)customer: Customer name (categorical)quantity: Number of units soldprice: Price per unit
total = quantity × price is used as the prediction target.
-
Algorithm: Random Forest Regressor
-
Framework: scikit-learn
-
Evaluation:
- Mean Absolute Error: ~19.82 AED
- R² Score: 1.0 (excellent fit)
# Activate your virtual environment
python -m venv venv
venv\Scripts\activate # On Windows
# Install dependencies
python -m pip install -r requirements.txt
# Train and save the model
python -m model.random_forest.train_and_save# Test the model
python -m tests.test_random_forest_model
python -m api.appThe API will start at: http://127.0.0.1:5000/
curl -X POST http://127.0.0.1:5000/api/v1/predict -H "Content-Type: application/json" -d '{"product": "Laptop", "customer": "John", "quantity": 2, "price": 800}'Response:
[
{
"index": 0,
"predicted_total": 1520.0
}
]curl -X POST http://127.0.0.1:5000/api/v1/predict -H "Content-Type: application/json" -d '[
{"product": "Laptop", "customer": "John", "quantity": 2, "price": 800},
{"product": "Tablet", "customer": "Alice", "quantity": 3, "price": 400},
{"product": "Phone", "customer": "Mark", "quantity": 0, "price": 900}
]'Response:
[
{
"index": 0,
"predicted_total": 1520.0
},
{
"index": 1,
"predicted_total": 1205.0
},
{
"index": 2,
"error": "Quantity must be a positive number."
}
]✅ Valid records will also be appended to data/new_sales.csv for future retraining purposes.
This chart compares actual and predicted sales totals on the test set.
Flask
pandas
scikit-learn
joblib
matplotlib
__pycache__/
*.pkl
.env
venv/
*.pyc
.DS_Store
MIT License
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Shahzad Software Engineer | ML Enthusiast
For any questions or suggestions, feel free to reach out!
