Skip to content

AnonymousV73X/SHAMBA-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌿 Shamba AI β€” Crop Disease Detection

Detect crop diseases instantly. Save your harvest. Save your income.

Shamba AI is a full-stack AI-powered crop disease detection platform built for Kenyan smallholder farmers. Point your phone at a sick leaf, get a diagnosis in seconds β€” treatment steps, product recommendations, and prevention tips included. Works on 2G/3G. No login required.


πŸ“Έ Screenshots

Home Screen
Home β€” Crop Scanner
Dark Mode
Dark Mode
Diagnosis Result
Diagnosis Result Card
AI Chat
AI Chat Assistant
Admin Dashboard
Admin Dashboard
AI Chat
OVERALL step Analysis

✨ Features

Feature Details
πŸ” Instant AI Diagnosis Upload a leaf photo β†’ disease name, confidence score & severity in seconds
🧠 Dual Inference Engine Local ONNX (MobileNetV2, PlantVillage) with Gemini Vision API fallback
πŸ’¬ AI Chat Assistant Ask follow-up questions about your diagnosis in context
πŸ“„ PDF Report Export Save and share your diagnosis report
🌍 Bilingual β€” EN / SW Full English & Swahili UI, switchable mid-session
πŸŒ™ Dark Mode Deep black-green theme, preference persisted
πŸ“± Mobile-First Optimised for low-end Android devices on 2G/3G
🚫 No Login Required Anonymous scanning β€” account optional for scan history
πŸ‘¨β€πŸ’Ό Admin Dashboard Manage users, diseases, and view scan analytics
πŸ“ž Expert Connect KALRO hotline modal for escalating to human agronomists

🦠 Supported Crops & Diseases

Crop Diseases Detected
🌽 Maize Lethal Necrosis (MLND), Gray Leaf Spot, Rust, Fall Armyworm
πŸ… Tomato Early Blight, Late Blight, Bacterial Spot, Yellow Leaf Curl Virus
πŸ₯¬ Kale (Sukuma Wiki) Black Rot, Downy Mildew, Aphids, Cabbage Worm
πŸ₯” Potato Late Blight
πŸ§… Onion Purple Blotch
🫘 Beans Gemini Vision open-ended diagnosis

The Gemini Vision fallback can diagnose any visible disease, disorder, or pest damage β€” including physiological disorders (nutrient deficiency, sunscald, blossom end rot) not in the local model.


πŸ—οΈ Architecture

shamba-ai/
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ index.html              ← Scan page (EN/SW, dark mode, crop picker, upload)
β”‚   β”œβ”€β”€ result.html             ← Diagnosis result card + AI chat panel
β”‚   β”œβ”€β”€ history.html            ← User scan history
β”‚   β”œβ”€β”€ login.html              ← Login / register
β”‚   └── admin_dashboard.html   ← Admin panel (users, diseases, analytics)
β”‚
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                  ← FastAPI entry point, router mounting, lifespan
β”‚   β”œβ”€β”€ config.py               ← Env config (DB, JWT, Gemini, ONNX paths)
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ detect.py           ← POST /api/detect β€” core detection endpoint
β”‚   β”‚   β”œβ”€β”€ chat.py             ← POST /api/chat β€” Gemini-powered Q&A
β”‚   β”‚   β”œβ”€β”€ auth.py             ← Register, login, JWT
β”‚   β”‚   β”œβ”€β”€ history.py          ← Scan history (authenticated)
β”‚   β”‚   └── admin.py            ← Admin CRUD endpoints
β”‚   β”œβ”€β”€ ai/
β”‚   β”‚   β”œβ”€β”€ model.py            ← ONNX MobileNetV2 wrapper + PlantVillage label mapping
β”‚   β”‚   β”œβ”€β”€ predict.py          ← Inference pipeline: ONNX β†’ Gemini fallback
β”‚   β”‚   β”œβ”€β”€ gemini_vision.py    ← Gemini Vision API integration
β”‚   β”‚   └── advice.py           ← Disease advice JSON lookup
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── database.py         ← SQLite (WAL), schema init, demo seed
β”‚   └── utils/
β”‚       β”œβ”€β”€ image_utils.py      ← Image validation, resize, format check
β”‚       └── auth_utils.py       ← JWT sign/verify, PBKDF2 password hashing
β”‚
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ plantvillage_mobilenetv2.onnx   ← Local CNN model (38 classes)
β”‚   β”œβ”€β”€ plantvillage_labels.json        ← PlantVillage class labels
β”‚   β”œβ”€β”€ labels.json                     ← App disease labels (mapped)
β”‚   └── disease_advice.json             ← Treatment, products, prevention per disease
β”‚
β”œβ”€β”€ uploads/                    ← Saved scan images
β”œβ”€β”€ requirements.txt
└── README.md

🧠 AI Inference Pipeline

User uploads image
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  ONNX MobileNetV2   β”‚  ← Local, fast, offline-capable
β”‚  (PlantVillage 38)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ mapped result?
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
   YES        NO / unmapped
    β”‚          β”‚
    β–Ό          β–Ό
Return     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
result     β”‚  Gemini Vision   β”‚  ← Open-ended diagnosis, any disease
           β”‚  API (fallback)  β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
          Map to known DB entry
          or return raw diagnosis
                    β”‚
                    β–Ό
         Advice lookup (treatment
         steps, products, prevention)

πŸš€ Getting Started

Prerequisites

  • Python 3.9+
  • pip
  • Gemini API key (for fallback + chat)

Installation

# 1. Clone the repo
git clone https://github.com/yourname/shamba-ai.git
cd shamba-ai

# 2. Install Python dependencies
pip install -r requirements.txt

# 3. Set environment variables (or edit config.py)
export GEMINI_API_KEY="your-gemini-api-key"
export JWT_SECRET="your-secret-key"

# 4. Start the server
cd backend
python app.py
# or
uvicorn app:app --reload --host 0.0.0.0 --port 8001

App runs at http://localhost:8001

Default Admin Credentials

Email:    admin@shamba.ai
Password: admin123

⚠️ Change these before deploying to production.


πŸ“‘ API Reference

Detection

Method Endpoint Auth Description
POST /api/detect Optional Detect disease from image. Multipart: image (file) + crop_type (string)

Response:

{
  "disease": "Tomato_Early_Blight",
  "display": "Early Blight",
  "confidence": 91.4,
  "severity": "Moderate",
  "symptoms": ["..."],
  "treatment_steps": [{ "step": 1, "text": "..." }],
  "products": [{ "name": "Mancozeb", "type": "Fungicide", "availability": "Agrovets" }],
  "prevention": ["..."],
  "source": "local_onnx"
}

Chat

Method Endpoint Auth Description
POST /api/chat Optional Ask follow-up about diagnosis. Body: { message, context, history, lang }

Auth

Method Endpoint Description
POST /api/auth/register Create account
POST /api/auth/login Login β†’ JWT token
GET /api/auth/me Current user info

History

Method Endpoint Auth Description
GET /api/history Required List user's past scans
GET /api/history/{id} Required Single scan result

Admin

Method Endpoint Description
GET /api/admin/stats Dashboard stats
GET/POST /api/admin/diseases List / add diseases
PUT/DELETE /api/admin/diseases/{id} Update / delete disease
GET /api/admin/users List all users
PUT /api/admin/users/{id}/ban Ban / unban user

Health

Method Endpoint Description
GET /api/health Server health check

πŸ› οΈ Tech Stack

Backend

  • FastAPI + Uvicorn
  • SQLite with WAL mode (via SQLAlchemy)
  • JWT auth (PyJWT) + PBKDF2 password hashing (passlib)
  • ONNX Runtime β€” MobileNetV2 local inference
  • Google Gemini Vision API β€” open-ended fallback diagnosis + chat
  • Pillow β€” image validation & preprocessing

Frontend

  • Vanilla HTML / CSS / JS β€” zero frameworks, zero build step
  • Outfit font (Google Fonts)
  • Bilingual i18n system (EN / SW) with localStorage persistence
  • Dark mode via CSS custom properties + data-theme attribute
  • PDF export via browser print API

🌍 Localisation

The UI is fully bilingual. Language is saved to localStorage under key shamba_lang and shared across all pages.

Key English Swahili
Crops Maize, Tomato, Kale... Mahindi, Nyanya, Sukuma Wiki...
Actions Analyse Crop Chunguza Zao
Upload Tap to upload or drag & drop Gonga ili kupakia au buruta & acha
Chat Ask Expert Uliza Mtaalamu

πŸ”’ Environment Variables

Variable Default Description
DATABASE_URL sqlite:///./shamba.db Database connection string
JWT_SECRET (insecure default) Change in production
GEMINI_API_KEY β€” Required for fallback diagnosis + chat

πŸ“¦ Deployment Notes

  • The SQLite DB auto-creates and seeds on first startup
  • ONNX model file (plantvillage_mobilenetv2.onnx) must be present in /model/ for local inference β€” server starts without it and falls back to Gemini
  • Set CORS origins explicitly in app.py before going to production (currently allow_origins=["*"])
  • Serve behind Nginx or Caddy in production; FastAPI serves the frontend as static files in development

πŸ“„ License

MIT License β€” free for agricultural, educational, and commercial use.


🌿 Shamba AI β€” Kuwawezesha wakulima wa Kenya kwa AI
Empowering Kenyan farmers with artificial intelligence

About

🌿 Shamba AI β€” AI-powered crop disease detection for Kenyan farmers. Upload a photo, get instant diagnosis, treatment steps & product recommendations. Works on 2G/3G.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages