Skip to content

Repository files navigation

🌾 Agent Kisaan — किसान का डिजिटल साथी

AI-powered platform for Indian farmers: crop disease detection, treatment advice, irrigation scheduling & market price alerts.

Architecture

6 Microservices:
  1. API Gateway     (FastAPI, port 8000) — Auth, routing, rate limiting
  2. Disease Detection (FastAPI + PyTorch, port 8001) — MobileNetV2
  3. Treatment Advisory (FastAPI + Kafka consumer) — RAG pipeline
  4. Irrigation Service (FastAPI, port 8003) — Weather + crop formulas
  5. Mandi Service   (FastAPI + Redis, port 8004) — Market prices
  6. Notification Service (Kafka consumer) — Central notification hub

Infrastructure:
  • Kafka + Zookeeper — async event streaming
  • PostgreSQL — persistent storage
  • Redis — rate limiting + price caching
  • Qdrant — vector database (for RAG)
  • Nginx — reverse proxy
  • Frontend — single-page HTML app (Node/Express)

🚀 Quick Start (Full Platform)

Prerequisites

  • Docker Desktop installed and running
  • 8GB RAM recommended (Kafka + multiple services)
  • Ports 80, 8000 free

1. Clone / Extract

# If downloaded as zip:
unzip agent-kisaan.zip
cd agent-kisaan

2. Configure (Optional)

cp .env .env.local
# Edit .env to add real API keys (weather, mandi, SMS)
# Platform works fully in demo mode without real keys

3. Start Everything

docker compose up -d

That's it! Docker Compose handles startup order automatically via health checks.

4. Access the Platform

5. First Login

  1. Open http://localhost
  2. Click "Get Started Free"
  3. Register with your phone number
  4. Upload a crop photo → get instant disease diagnosis!

🛠️ Startup Order (Automatic)

Docker Compose uses health-check-gated startup:

Zookeeper (healthy) 
  → Kafka (healthy)
    → PostgreSQL (healthy) + Redis (healthy) + Qdrant (healthy)
      → All 6 Application Services
        → Nginx

No service starts until its dependencies are fully healthy. First startup takes ~3-4 minutes as Kafka initializes.

📊 Services Status Check

# Check all services
docker compose ps

# View logs for a specific service
docker compose logs api-gateway -f
docker compose logs disease-detection -f
docker compose logs treatment-advisory -f

# Check individual health endpoints
curl http://localhost:8000/health
curl http://localhost:8000/api/mandi/prices

🔬 Disease Detection

The platform uses MobileNetV2 (PyTorch) trained on PlantVillage dataset:

  • 38 disease classes across 14 crop types
  • Simulation mode: If no trained model file is present, returns realistic demo results
  • Production mode: Download PlantVillage-trained weights and place at: docker volume inspect agent-kisaan_model_data

To use a real trained model:

# Copy model file into Docker volume
docker run --rm -v agent-kisaan_model_data:/models -v $(pwd):/src alpine \
  cp /src/mobilenetv2_plant_disease.pth /models/

🌾 Features

Disease Detection

  • Upload crop photo (JPEG/PNG/WebP, max 10MB)
  • Drag & drop supported
  • Binary classifier checks if image is actually a plant
  • Returns top-3 predictions with confidence scores
  • Async treatment advice via Kafka

Treatment Advisory (RAG)

  • Kafka consumer listens for disease.detected events
  • Knowledge base: ICAR guidelines, state agriculture department data
  • Deduplication: same farmer+crop+disease within 30 min → cached result
  • Fallback response when disease not in knowledge base

Irrigation Scheduling

  • Register fields with crop type, soil type, acreage
  • Weather-adjusted irrigation calculation (ET0 method)
  • Irrigation reminders published to Kafka

Mandi Prices

  • Live prices from Agmarknet API (or demo data if no API key)
  • Redis cache with 6-hour TTL
  • 3-tier fallback: Live API → Redis cache → PostgreSQL → demo data

Notifications

  • Centralized notification hub via Kafka
  • In-app notifications (read/unread tracking)
  • SMS ready (configure MSG91 key)

🔒 Security

  • JWT authentication (24-hour tokens, stateless)
  • Redis rate limiting: 10 requests/minute per user (fail-open)
  • File upload validation (type + size)
  • Nginx SSL termination ready

🧩 Edge Cases Handled

Edge Case Solution
Image not a crop Binary plant classifier, returns clear error
Kafka consumer crashes Manual offset commit (at-least-once delivery)
No relevant treatment in KB Similarity threshold + fallback message with KVK contact
Redis is down Fail-open: allow requests, log warning
Duplicate disease detections 30-minute dedup window per farmer+crop+disease
Model file missing Simulation mode + Docker healthcheck fails gracefully
Mandi API is down 3-tier fallback with staleness warning

🛑 Stopping

docker compose down          # Stop but keep data
docker compose down -v       # Stop and delete all data (fresh start)

🔧 Development

Run individual services:

# API Gateway only
cd api-gateway
pip install -r requirements.txt
uvicorn main:app --reload --port 8000

📁 Project Structure

agent-kisaan/
├── docker-compose.yml
├── .env
├── nginx/
│   └── nginx.conf
├── postgres/
│   └── init.sql
├── api-gateway/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
├── disease-detection/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
├── treatment-advisory/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
├── irrigation-service/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
├── mandi-service/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
├── notification-service/
│   ├── main.py
│   ├── requirements.txt
│   └── Dockerfile
└── frontend/
    ├── index.html
    ├── server.js
    ├── package.json
    └── Dockerfile

Kisan Call Center

For any agricultural queries: 1800-180-1551 (Toll free, 6AM–10PM)

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages