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.
Home β Crop Scanner |
Dark Mode |
Diagnosis Result Card |
AI Chat Assistant |
Admin Dashboard |
OVERALL step Analysis |
| 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 |
| 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.
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
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)
- Python 3.9+
pip- Gemini API key (for fallback + chat)
# 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 8001App runs at http://localhost:8001
Email: admin@shamba.ai
Password: admin123
β οΈ Change these before deploying to production.
| 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"
}| Method | Endpoint | Auth | Description |
|---|---|---|---|
POST |
/api/chat |
Optional | Ask follow-up about diagnosis. Body: { message, context, history, lang } |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/auth/register |
Create account |
POST |
/api/auth/login |
Login β JWT token |
GET |
/api/auth/me |
Current user info |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
GET |
/api/history |
Required | List user's past scans |
GET |
/api/history/{id} |
Required | Single scan result |
| 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 |
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/health |
Server health check |
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
localStoragepersistence - Dark mode via CSS custom properties +
data-themeattribute - PDF export via browser print API
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 |
| 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 |
- 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
CORSorigins explicitly inapp.pybefore going to production (currentlyallow_origins=["*"]) - Serve behind Nginx or Caddy in production; FastAPI serves the frontend as static files in development
MIT License β free for agricultural, educational, and commercial use.
πΏ Shamba AI β Kuwawezesha wakulima wa Kenya kwa AI
Empowering Kenyan farmers with artificial intelligence





