Commodity price forecasting web app built with Flask, Keras/TensorFlow, and scikit-learn.
- Backend:
Flaskapp (app.py) serving a single route/for rendering predictions. - Model: Keras model file
myModel.kerasthat predicts next-day prices for a fixed set of commodities using a sliding window of the last 7 days. - Scaling: scikit-learn scaler
scaler.pklused to scale inputs (and inverse-transform predictions). - Data: Historical daily series in
data/data_mean.csv(primary), with alternatedata/data_median.csv, plus the original datasetsdata.csv(wider feature set) andraw_data.csv(compact schema). - Frontend: Jinja2 template
templates/index.htmlwithstatic/script.jsandstatic/style.cssfor UI, date picker, crop selection, and a Chart.js placeholder.
app.py
README.md
requirements.txt
myModel.keras
scaler.pkl
static/
script.js
style.css
templates/
index.html
data/
data_mean.csv
data_median.csv
raw_data.csv
data.csv
data/data_mean.csvanddata/data_median.csv(used by app):- Columns:
Day, Month, Year, Rice, Wheat, Atta (Wheat), Gram Dal, Tur/Arhar Dal, Date Dateis a full timestamp string (e.g.,2016-01-01 00:00:00.000000000), parsed into pandasdatetime64.- App uses only:
Date+ feature columns defined inMODEL_COLUMNS.
- Columns:
raw_data.csv: compact format withDay, Month, Yearplus the five commodities used by the model.data.csv: wide format with 20+ commodities and staples; not directly used by the current app.
- Imports: Flask, joblib, Keras load_model, pandas, numpy, json.
- Loads:
model = load_model('myModel.keras')scaler = joblib.load('scaler.pkl')- Historical data
data = pd.read_csv('data/data_mean.csv')+ parseDate.
- Constants:
MODEL_COLUMNS = ['Rice', 'Wheat', 'Atta (Wheat)', 'Gram Dal', 'Tur/Arhar Dal']LOOK_BACK = 7
- Core function:
predict_future_date(data, target_date_str, model, scaler, look_back, model_columns)- If
target_date <= end_date(last date in historical data):- Returns the row for that date, restricted to
MODEL_COLUMNS, formatted to 2 decimals.
- Returns the row for that date, restricted to
- Else (future date):
- Takes the last
look_backdays of the selected columns, scales them, reshapes to(1, look_back, n_features). - Iteratively predicts next day in scaled space, inverse transforms to price units, and appends to a working copy of the data; slides the window until reaching the target date.
- Returns a single-row DataFrame formatted to 4 decimals.
- Takes the last
- Notes:
- Caller passes
data.copy()so requests don’t mutate the global DataFrame. - If model/scaler/data fail to load, returns an error message string.
- Caller passes
- If
- Route
/(GET/POST):- Renders
index.htmlwith:default_date= max historical date (asYYYY-MM-DD).available_crops=MODEL_COLUMNS.
- On POST, reads
future_dateand a JSON list of selected crops (crops), runs prediction, and sends a subset DataFrame (or error string) back to the template.
- Renders
templates/index.html:- Left panel: Flatpickr inline date picker bound to hidden input
future_date. - Crop selection: Toggle buttons for each commodity with “Select All” control; selected list is serialized to hidden input
cropsas JSON string. - Result area: Renders a table if
prediction_resultis a DataFrame; otherwise shows error text. - Chart.js canvas present; actual
chartDatais not yet provided from the backend, so the chart is currently a placeholder.
- Left panel: Flatpickr inline date picker bound to hidden input
static/script.js:- Manages crop button selection, select-all toggle, hidden inputs, and loader visibility on form submit.
- Initializes Flatpickr with
defaultDate = {{ default_date }}. - Chart.js code expects a global
chartDataobject; not provided yet, so no chart shows by default.
static/style.css:- Theme/typography, responsive grid, styled buttons, date picker theming, table styles, and chart container.
- Create a Python environment (Python 3.10+ recommended for TensorFlow 2.19):
- Windows (PowerShell):
- Optional:
py -3.10 -m venv .venv; .venv\Scripts\Activate.ps1
- Optional:
- Windows (PowerShell):
- Install dependencies:
pip install -r requirements.txt
- Start the server:
python app.py
- Open http://127.0.0.1:5000/ in your browser.
- Model/scaler load failures:
- Ensure
myModel.kerasandscaler.pklexist at project root and were created with compatible library versions. - If
scaler.pklwas saved with a different scikit-learn version, you may hit deserialization errors. Pin to the original version if possible.
- Ensure
- TensorFlow/Keras compatibility:
- Requirements pin
tensorflow==2.19.0andkeras==3.9.2. If the model was trained with older TF/Keras, consider aligning versions used during training and serving.
- Requirements pin
- Date selection:
- Past dates must exist in
data/data_mean.csvto return historical rows. Missing dates will result in empty selections; the code doesn’t explicitly handle “gap” days.
- Past dates must exist in
- Performance:
- Far-future dates will run a day-by-day iterative loop (could be slow for very distant dates). Consider adding a max horizon.
- Production safety:
app.run(debug=True)is for development only.
- Provide charts: Generate
chartDataserver-side (last N days + predicted point) and render as a JSON script block in the template. - Validation: Enforce allowed date ranges in Flatpickr (min/max), and validate server-side.
- API endpoint: Add
/api/forecast?date=YYYY-MM-DD&crops=[...]that returns JSON for SPA usage. - Caching: Cache predictions per date to avoid repeated computation when iterating many days ahead.
- Columns flexibility: If you change
MODEL_COLUMNSin training, update both the scaler and app.
- Inputs:
future_dateasYYYY-MM-DD;cropsas JSON array of strings matchingMODEL_COLUMNS. - Output: For valid requests, a single-row DataFrame of selected crops with formatted prices; otherwise a string error.
- Error modes: Missing/invalid date, model/scaler/data not loaded, version incompatibilities.