Skip to content

Agri-Guard/AgriGuard

Repository files navigation

🌾 AgriGuard

Agricultural Intelligence System for Uganda β€” crop price forecasting, counterfeit input detection, and market intelligence delivered to farmers via web dashboard and USSD.

Built by Keith Ndiema Kissa (2025/BCS/101/PS) Β· Mbarara University of Science and Technology
Submitted: Ministry of ICT Government Systems Prototype Showcase 2026 Β·

Problem

Ugandan farmers face three compounding challenges:

  • Price blindness β€” no reliable way to know if today is a good day to sell
  • Counterfeit inputs β€” fake seeds and pesticides cost farmers yield and money
  • Market fragmentation β€” price gaps between markets go unexploited because farmers lack data

Solution

AgriGuard is a three-module intelligence layer:

Module What it does How
Price Forecasting Predicts crop prices 4 weeks ahead XGBoost + Prophet on WFP price history
Input Validator Flags suspicious agro-input reports Isolation Forest anomaly detection + Claude Vision label scanner
Market Intelligence Cross-market comparisons, arbitrage signals FastAPI serving WFP data with trend analytics

Accessible via a Streamlit web dashboard and a USSD interface (*183*7#) for farmers without smartphones.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Streamlit Frontend  (port 8501)                    β”‚
β”‚  Home Β· Dashboard Β· USSD Simulator                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚ HTTP / REST
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  FastAPI Backend  (port 8000)                       β”‚
β”‚  /forecasts  /markets  /prices  /api/v1/predict     β”‚
β”‚  /api/v1/validate  /health                          β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚             β”‚              β”‚
  XGBoost        Prophet        MySQL DB
  + IsoForest    fallback       (price history)
  .pkl models    (no model)
       β”‚
  WFP Uganda CSV  ←  scripts/download_wfp_data.py

Quick Start

1. Clone and set up environment

git clone https://github.com/Agri-Guard/AgriGuard.git
cd AgriGuard
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

2. Configure environment

cp config/env.example config/.env
# Edit config/.env β€” set DB credentials and ANTHROPIC_API_KEY

3. Download data and train models

python scripts/download_wfp_data.py      # ~2 MB WFP Uganda CSV
python scripts/train_models.py           # trains XGBoost + Isolation Forest

4a. Run with Docker Compose (recommended)

docker-compose up --build

4b. Run manually

# Terminal 1 β€” backend
uvicorn backend.app.main:app --reload --port 8000

# Terminal 2 β€” frontend
cd frontend
streamlit run Home.py --server.port 8501

Project Structure

AgriGuard/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── app/
β”‚       β”œβ”€β”€ main.py           # FastAPI entry point + router wiring
β”‚       β”œβ”€β”€ config.py         # Pydantic settings (env-var driven)
β”‚       β”œβ”€β”€ database.py       # SQLAlchemy engine + session
β”‚       β”œβ”€β”€ model.py          # ML model loader + inference
β”‚       β”œβ”€β”€ validator.py      # Input validation
β”‚       β”œβ”€β”€ schemas.py        # Core Pydantic schemas
β”‚       β”œβ”€β”€ models/
β”‚       β”‚   └── price.py      # SQLAlchemy ORM models
β”‚       β”œβ”€β”€ schemas/
β”‚       β”‚   └── price.py      # Price-domain Pydantic schemas
β”‚       β”œβ”€β”€ routers/
β”‚       β”‚   β”œβ”€β”€ forecasts.py  # Prophet/XGBoost forecast endpoints
β”‚       β”‚   β”œβ”€β”€ markets.py    # Market intelligence endpoints
β”‚       β”‚   └── prices.py     # CRUD price observation endpoints
β”‚       └── services/
β”‚           └── price_service.py
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ Home.py               # Landing page
β”‚   └── pages/
β”‚       β”œβ”€β”€ dashboard.py      # Main 5-tab dashboard
β”‚       └── ussd_simulator.py # Feature-phone USSD demo
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ download_wfp_data.py  # Fetch WFP Uganda CSV from HDX
β”‚   └── train_models.py       # Train XGBoost + Isolation Forest
β”œβ”€β”€ ml/
β”‚   └── models/               # Saved .pkl files (gitignored)
β”‚       └── metrics.json      # Evaluation metrics (committed)
β”œβ”€β”€ data/
β”‚   └── raw/                  # WFP CSV (gitignored)
β”œβ”€β”€ config/
β”‚   └── env.example           # Copy to .env and fill in
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
└── README.md

ML Methodology

Price Forecasting (XGBoost)

Features: temporal (year, month, week, day-of-year, cyclic month encoding), lag prices (1m, 3m, 6m), 3-month rolling average, label-encoded crop and market.

Training split: 80/20 time-ordered (no data leakage).

Metrics (see ml/models/metrics.json after training):

Metric Value
MAE ~120 UGX/kg
MAPE ~8%
RΒ² ~0.91

Prophet is used as an alternative for single-series forecasting and as a confidence-interval reference.

Counterfeit Detection (Isolation Forest)

Trained on the price feature space. Inputs that deviate significantly from the training distribution are flagged as potentially anomalous. contamination=0.05 (tunable based on field error rate estimates from MAAIF).

API Reference

Full interactive docs at /docs when the backend is running.

Method Endpoint Description
GET /health System health check
POST /api/v1/predict Quick price prediction
POST /api/v1/validate Fake input detector
GET /forecasts/{commodity} ML price forecast
GET /forecasts/commodities List available crops/markets
GET /markets/summary/{commodity} Best/worst market
GET /markets/movers Biggest price gainers/losers
GET /markets/national-summary All commodities snapshot
GET /prices Paginated price history
GET /prices/alerts Food security spike alerts

Data Sources

  • WFP VAM Food Prices β€” Uganda (HDX, open license) β€” historical crop prices across Ugandan markets
  • Open-Meteo β€” free 7-day weather forecasts (no API key required)
  • Anthropic Claude Vision API β€” counterfeit label scanning

Roadmap

  • SMS push alerts via Africa's Talking
  • Satellite crop health integration (Sentinel-2)
  • District-level food security index
  • Mobile app (React Native)
  • Multi-country expansion (Kenya, Tanzania)

License

AGPL-3.0 β€” see LICENSE

Author

Keith Ndiema Kissa
BSc Computer Science Β· Mbarara University of Science and Technology
veritasndiema@gmail.com Β· GitHub: Ve-stora

About

AgriGuard is an agricultural intelligence system designed to improve food security in Uganda by forecasting crop prices, detecting counterfeit seeds and agro-inputs, and providing market risk insights. It supports farmers and government with data-driven decisions to increase productivity, income stability, and market transparency.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors