Using Transformer neural networks to predict local farm produce prices.
- Data Collection: Synthetic data generator / CSV upload / FAO data
- Data Preparation: Automated cleaning, normalization, and sequence creation
- Transformer Model: Multi-head self-attention for time series forecasting
- Evaluation: MAE, RMSE, MAPE, R² with interactive visualizations
- Web Dashboard: Streamlit app with data explorer, trends, training, and predictions
# Install dependencies
pip install -r requirements.txt
# Run the full pipeline (command line)
python main.py
# Run the web dashboard
streamlit run app.pyThis opens a browser with 5 pages:
- 📊 Data Explorer — View and analyze the price dataset
- 📈 Price Trends — Interactive historical price charts
- 🤖 Train Model — Configure and train the Transformer with live progress
- 🔮 Predictions — Generate future price forecasts with charts
- 📋 Evaluation — View MAE, RMSE, scatter plots, error distributions
localfarm/
├── data/ # Price data (CSV)
├── src/
│ ├── __init__.py # Package init
│ ├── data_collection.py # Data loading & generation
│ ├── data_preparation.py # Cleaning, normalization, sequences
│ ├── model.py # Transformer architecture
│ ├── train.py # Training loop
│ ├── evaluate.py # Metrics & visualization
│ └── predict.py # Inference & forecasting
├── app.py # Streamlit web application
├── main.py # CLI pipeline runner
├── requirements.txt
└── README.md
Input (batch, 30, 11 features)
→ Linear Projection → (batch, 30, 64)
→ Positional Encoding
→ 3× Transformer Encoder (4-head attention, d_ff=256)
→ Flatten → FC layers
→ Output (batch, 1) [next day price]
| Feature | Description |
|---|---|
| Price | Normalized daily price |
| day_of_week | Day of week (0-1) |
| day_of_month | Day of month (0-1) |
| month | Month of year (0-1) |
| week_of_year | Week of year (0-1) |
| price_lag_1 | Price 1 day ago |
| price_lag_7 | Price 7 days ago |
| price_lag_14 | Price 14 days ago |
| rolling_mean_7 | 7-day rolling average |
| rolling_std_7 | 7-day rolling std deviation |
| rolling_mean_30 | 30-day rolling average |
| Metric | Description |
|---|---|
| MAE | Mean Absolute Error |
| RMSE | Root Mean Square Error |
| MAPE | Mean Absolute Percentage Error |
| R² | Coefficient of Determination |
- PyTorch — Deep learning framework
- Streamlit — Web dashboard
- Plotly — Interactive charts
- scikit-learn — Data preprocessing
- pandas/numpy — Data manipulation
Transformer-Based Time Series Forecasting for Local Farm Produce Price Prediction Step 1 — Data Collection Use datasets such as: -FAO food price data -Kaggle agricultural commodity price datasets -Local market price records (if available) Data format example:
Date Produce Market Price Step 2 — Data Preparation Sort prices by date Handle missing values Normalize price values Convert data into sequences (e.g., last 30 days → next day price)
The above is the requirement for a Final Year University project, How do we implement this project?
Step 3 — Model Development Implement a Transformer-based forecasting model. Train the model on historical price sequences. The model learns relationships between past and future prices.
Step 4 — Evaluation Compare predicted price with real price using: MAE (Mean Absolute Error) RMSE (Root Mean Square Error)
Step 5 — System Development Build a system that: Stores price data Shows price trends Displays predicted future prices Visualizes prediction vs actual price