Downloads · 30 days
0
sudharshan001/crop-disease-ai
crop-disease-ai is a machine learning model from sudharshan001. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
IEEE Paper Implementation — End-to-end deep learning pipeline for automated crop disease diagnosis with LLM-powered treatment recommendations.
Downloads · 30 days
0
Access
Public
Updated Mar 26, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.json218 KB · 49%
From the Hugging Face model README
IEEE Paper Implementation — End-to-end deep learning pipeline for automated crop disease diagnosis with LLM-powered treatment recommendations.
┌──────────────────────────────────────────────────────────────────┐
│ FRONTEND │
│ Next.js 14 · TypeScript · TailwindCSS │
│ ┌──────────┐ ┌──────────┐ ┌────────────┐ ┌──────────────┐ │
│ │ Upload │→│ Loader │→│ Result │ │ History │ │
│ │ (Dropzone)│ │(Spinner) │ │(Tabs+Badge)│ │ (Pagination) │ │
│ └──────────┘ └──────────┘ └────────────┘ └──────────────┘ │
└──────────────────────┬───────────────────────────────────────────┘
│ HTTP / REST
┌──────────────────────▼───────────────────────────────────────────┐
│ BACKEND │
│ FastAPI · PyTorch · OpenCV · llama-cpp-python │
│ │
│ ┌────────────┐ ┌────────────┐ ┌───────────┐ ┌────────────┐ │
│ │ Preprocess │→│ ResNet18 │→│ Severity │→│ LLM / Dict │ │
│ │ (OpenCV) │ │ (CNN) │ │ Estimator │ │ (phi-2 GGUF)│ │
│ └────────────┘ └────────────┘ └───────────┘ └─────┬──────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────▼──────┐ │
│ │ SQLite (aiosqlite) │ │
│ └────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────┘
| Layer | Technology |
|---|---|
| Frontend | Next.js 14, React 18, TypeScript, TailwindCSS, Axios, react-dropzone |
| Backend | FastAPI, Uvicorn, Pydantic v2 |
| CNN Model | PyTorch ResNet18 (ImageNet pretrained), TorchScript export |
| Preprocessing | OpenCV (resize, blur, HSV segmentation, normalization) |
| LLM | phi-2 via llama-cpp-python (GGUF Q4_K_M) or bitsandbytes 4-bit |
| Database | SQLite via aiosqlite |
| Disease Classes | 16 (15 diseases + 1 Healthy) across 5 crops |
| # | Disease | Crop |
|---|---|---|
| 1 | Apple Scab | Apple |
| 2 | Apple Black Rot | Apple |
| 3 | Apple Cedar Rust | Apple |
| 4 | Corn Cercospora Leaf Spot | Corn |
| 5 | Corn Common Rust | Corn |
| 6 | Corn Northern Leaf Blight | Corn |
| 7 | Grape Black Rot | Grape |
| 8 | Grape Esca (Black Measles) | Grape |
| 9 | Grape Leaf Blight | Grape |
| 10 | Potato Early Blight | Potato |
| 11 | Potato Late Blight | Potato |
| 12 | Tomato Bacterial Spot | Tomato |
| 13 | Tomato Early Blight | Tomato |
| 14 | Tomato Late Blight | Tomato |
| 15 | Tomato Leaf Mold | Tomato |
| 16 | Healthy | — |
cd crop-disease-ai/backend
Windows (PowerShell):
python -m venv venv
.\venv\Scripts\Activate.ps1
Windows (CMD):
python -m venv venv
venv\Scripts\activate.bat
Linux / macOS:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Note:
llama-cpp-pythonrequires a prebuilt wheel on Windows. If it fails to build, install it separately:pip install llama-cpp-python --prefer-binary --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
Windows:
copy .env.example .env
Linux / macOS:
cp .env.example .env
pip install huggingface-hub
Windows (PowerShell):
cd models
python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='TheBloke/phi-2-GGUF', filename='phi-2.Q4_K_M.gguf', local_dir='.')"
cd ..
Linux / macOS:
cd models
python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='TheBloke/phi-2-GGUF', filename='phi-2.Q4_K_M.gguf', local_dir='.')"
cd ..
The file is ~1.8 GB. Without it, the system uses a built-in expert fallback dictionary and works fully without any LLM.
# Download PlantVillage dataset and extract to a folder
# Expected structure: dataset_root/ClassName/image.jpg
python train.py --data-dir /path/to/plantvillage --epochs 25 --batch-size 32
This saves:
models/disease_model_scripted.pt -- TorchScript modelmodels/disease_model_best.pth -- Checkpointmodels/class_mapping.json -- Class index mappingmodels/training_history.json -- Loss/accuracy curvesWithout training, the system runs in demo mode using an ImageNet-pretrained backbone. Predictions will vary per image but won't be accurate.
python run.py
You should see:
Host: 0.0.0.0
Port: 8000
Device: cpu
LLM: gguf (or fallback)
Open a new terminal (keep the backend running).
cd crop-disease-ai/frontend
npm install
npm run dev
You should see:
Local: http://localhost:3000
| Issue | Fix |
|---|---|
ModuleNotFoundError: No module named 'uvicorn' | Activate venv first: .\venv\Scripts\Activate.ps1 |
llama-cpp-python build fails | Install prebuilt wheel (see Step 3 note above) |
| Port 8000 already in use | Kill existing process: Get-NetTCPConnection -LocalPort 8000 | % { Stop-Process -Id $_.OwningProcess -Force } |
| LLM shows "fallback" | Download phi-2 model (Step 5) or set LLM_BACKEND=none in .env |
| Frontend can't reach backend | Ensure backend runs on port 8000 and check frontend/.env.local has NEXT_PUBLIC_API_URL=http://localhost:8000 |
| Method | Endpoint | Description |
|---|---|---|
GET | /api/health | Health check — model & LLM status |
POST | /api/predict | Upload image → disease prediction + recommendations |
GET | /api/history | Paginated prediction history |
Request: multipart/form-data
file (required): JPEG/PNG imageregion (optional): Geographic region for localized adviceResponse:
{
"disease": "Tomato Late Blight",
"confidence": 0.943,
"severity": "severe",
"recommendation": {
"description": "Late blight is caused by...",
"causes": "Phytophthora infestans...",
"treatment": "Apply copper-based fungicide...",
"prevention": "Use resistant varieties..."
},
"timestamp": "2024-01-15T10:30:00",
"filename": "leaf_sample.jpg"
}
crop-disease-ai/
├── backend/
│ ├── app/
│ │ ├── __init__.py
│ │ ├── config.py # Settings & disease classes
│ │ ├── preprocessing.py # OpenCV image pipeline
│ │ ├── model.py # ResNet18 classifier
│ │ ├── severity.py # Severity estimation
│ │ ├── llm.py # LLM engine + fallback dict
│ │ ├── database.py # Async SQLite operations
│ │ ├── schemas.py # Pydantic response models
│ │ └── main.py # FastAPI app & endpoints
│ ├── context/
│ │ ├── SYSTEM_ARCHITECTURE.md
│ │ ├── MODEL_DETAILS.md
│ │ ├── FUTURE_WORK.md
│ │ └── DEPLOYMENT_GUIDE.md
│ ├── models/ # Model weights directory
│ ├── train.py # Training script
│ ├── run.py # Uvicorn entry point
│ ├── requirements.txt
│ └── .env.example
├── frontend/
│ ├── app/
│ │ ├── components/
│ │ │ ├── Upload.tsx # Drag-and-drop upload
│ │ │ ├── Result.tsx # Prediction results + tabs
│ │ │ ├── Loader.tsx # Loading animation
│ │ │ └── SeverityBadge.tsx
│ │ ├── history/
│ │ │ └── page.tsx # Prediction history
│ │ ├── layout.tsx # Root layout
│ │ ├── page.tsx # Home / diagnosis page
│ │ └── globals.css
│ ├── lib/
│ │ └── api.ts # Axios API client & types
│ ├── package.json
│ ├── tailwind.config.ts
│ └── tsconfig.json
└── README.md
The system supports two LLM backends (configurable via LLM_BACKEND in .env):
| Backend | Best For | Requirements |
|---|---|---|
gguf (default) | CPU / Windows / Low RAM | llama-cpp-python |
bitsandbytes | NVIDIA GPU | transformers, bitsandbytes, accelerate, CUDA |
Set LLM_BACKEND=none to disable the LLM entirely and rely on the built-in expert dictionary.
.pt) for production inferenceSee backend/context/FUTURE_WORK.md for detailed plans.
MIT License — See LICENSE for details.