File size: 11,254 Bytes
6935d81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6250100
6935d81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---

language:
- bn
- en
license: mit
tags:
- agriculture
- bangladesh
- crop-disease-detection
- yolov8
- gemma
- lora
- rag
- faiss
- bm25
- bengali-nlp
- computer-vision
pipeline_tag: text-generation
---


# KrishokChat: Agricultural Advisory & Disease Detection Model Suite

**KrishokChat** is an end-to-end provenance-traceable multi-task Bengali agricultural advisory and crop disease diagnosis system designed for Bangladeshi farming ecosystems.

This repository contains the complete suite of models, fine-tuned weights, knowledge indexes, and advisory maps used by the KrishokChat application.

---

## πŸ“‚ Repository Structure

```

RaiyanKhaan/KrishokChat-Advisory-System/

β”œβ”€β”€ README.md                                  # Main Model Card & Usage Guide

β”œβ”€β”€ vision/                                    # Ultralytics YOLO Crop & Disease Vision Models

β”‚   β”œβ”€β”€ crop_classifier/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # 9-Crop Root Classifier (11.96 MB)

β”‚   β”‚   └── class_names.json                   # Crop class mapping

β”‚   β”œβ”€β”€ brassica_disease/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # Brassica Disease Classifier (10.54 MB)

β”‚   β”‚   β”œβ”€β”€ class_names.json                   # Disease labels (Alternaria, Black Rot, Downy Mildew, etc.)

β”‚   β”‚   └── disease_details.json               # Symptoms & remedy metadata

β”‚   β”œβ”€β”€ corn_disease/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # Corn / Maize Disease Classifier (10.52 MB)

β”‚   β”‚   β”œβ”€β”€ class_names.json                   # Labels (Common Rust, Gray Leaf Spot, Northern Leaf Blight, Healthy)

β”‚   β”‚   └── disease_details.json

β”‚   β”œβ”€β”€ potato_disease/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # Potato Disease Classifier (10.52 MB)

β”‚   β”‚   β”œβ”€β”€ class_names.json                   # Labels (Early Blight, Late Blight, Healthy)

β”‚   β”‚   └── disease_details.json

β”‚   β”œβ”€β”€ rice_disease/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # Rice Disease Classifier (3.06 MB)

β”‚   β”‚   β”œβ”€β”€ class_names.json                   # Labels (Bacterial Blight, Brown Spot, Blast, Rice Hispa, Sheath Blight)

β”‚   β”‚   └── disease_details.json

β”‚   β”œβ”€β”€ wheat_disease/

β”‚   β”‚   β”œβ”€β”€ model.pt                           # Wheat Disease Classifier (9.00 MB)

β”‚   β”‚   β”œβ”€β”€ class_names.json                   # Labels (Wheat Blast, Leaf Rust, Stem Rust, Stripe Rust, etc.)

β”‚   β”‚   β”œβ”€β”€ disease_details.json

β”‚   β”‚   └── metadata.json

β”‚   β”œβ”€β”€ verification_report.json               # Benchmark accuracies & test validation outputs

β”‚   └── verification_report_live.md

β”œβ”€β”€ gemma_llm/                                 # Bengali Agricultural Domain Language Models

β”‚   β”œβ”€β”€ krishokchat.f16.gguf                   # 16-bit GGUF model for fast CPU/GPU inference via llama.cpp/Ollama (1.29 GB)

β”‚   └── checkpoint-4020/                       # Stage-1 SFT Fine-Tuned LoRA Adapter Weights & Tokenizer

β”‚       β”œβ”€β”€ adapter_model.safetensors          # LoRA weights (266.2 MB)

β”‚       β”œβ”€β”€ adapter_config.json                # LoRA hyperparameter configuration

β”‚       β”œβ”€β”€ tokenizer.json                     # Bengali-extended SentencePiece tokenizer (30.68 MB)

β”‚       β”œβ”€β”€ tokenizer_config.json

β”‚       β”œβ”€β”€ processor_config.json

β”‚       β”œβ”€β”€ chat_template.jinja                # Jinja2 chat template for conversation turns

β”‚       β”œβ”€β”€ trainer_state.json                 # Training history & loss curves

β”‚       └── training_args.bin

β”œβ”€β”€ rag_knowledge_index/                       # Hybrid Dense (FAISS) + Sparse (BM25) Knowledge System

β”‚   β”œβ”€β”€ indexes/

β”‚   β”‚   β”œβ”€β”€ nodes.faiss                        # FAISS FlatIP index (BGE-M3 1024-dim dense embeddings) (8.34 MB)

β”‚   β”‚   β”œβ”€β”€ embeddings.npy                     # Dense embedding matrix

β”‚   β”‚   β”œβ”€β”€ bm25_index.pkl                     # Sparse BM25 Okapi retrieval index (16.23 MB)

β”‚   β”‚   β”œβ”€β”€ bm25_corpus_tok.pkl                # Tokenized BM25 corpus (4.47 MB)

β”‚   β”‚   β”œβ”€β”€ node_ids.json                      # Node identifier lookup table

β”‚   β”‚   β”œβ”€β”€ term_map.json                      # Domain taxonomy, pesticide brand mapping & disease synonyms

β”‚   β”‚   └── phase4_dialect_map.json            # 6-Region Dialect normalization map (Barisal, Chittagong, Sylhet, etc.)

β”‚   β”œβ”€β”€ processed/

β”‚   β”‚   β”œβ”€β”€ knowledge_nodes_clean.jsonl        # 2,135 Clean Knowledge Nodes (BARC, BARI, BRRI, CABI, DAE) (11.99 MB)

β”‚   β”‚   └── knowledge_nodes_refined.jsonl      # Refined multi-turn grounded knowledge nodes (9.95 MB)

β”‚   β”œβ”€β”€ eval/

β”‚   β”‚   β”œβ”€β”€ farmer_benchmark_1000.jsonl        # 1,000 real-world Bengali farmer benchmark queries

β”‚   β”‚   β”œβ”€β”€ coverage_gaps_v1.json              # Coverage gap audit report

β”‚   β”‚   └── dialect_map_derivation_audit_v1.json

β”‚   β”œβ”€β”€ provenance/

β”‚   β”‚   β”œβ”€β”€ manifest_md_to_qa.json             # Literature-to-QA audit trail (9.98 MB)

β”‚   β”‚   └── manifest_node_to_qa.json           # Node-to-QA audit trail (2.78 MB)

β”‚   β”œβ”€β”€ manifest.json                          # Build manifest with version hashes

β”‚   └── README.md

└── advisory_engine/                           # Structured Advisory Mapping

    β”œβ”€β”€ disease_knowledge_map.json             # Disease-to-remedy & dosage structured dictionary

    β”œβ”€β”€ generated_knowledge_nodes.jsonl        # Multi-step expert verified advisory nodes

    β”œβ”€β”€ generation_tasks.json                  # Advisory evaluation tasks

    └── test_cases.md                          # Clinical test cases & expected responses

```

---

## 🌿 1. Vision Models (Crop & Disease Detection)

The vision pipeline implements a two-stage hierarchical classifier using **Ultralytics YOLO**:
1. **Stage 1 (Root Crop Classifier)**: Identifies the crop species (`Brassica`, `Corn`, `Potato`, `Rice`, `Wheat`, `Solanacea`, etc.).
2. **Stage 2 (Disease Specialist)**: Routes the image to the dedicated crop disease classifier to determine the specific pathogen / health state.

### Classes Supported:
- **Rice Diseases**: Bacterial Leaf Blight, Brown Spot, Healthy Leaf, Leaf Blast, Leaf Scald, Narrow Brown Leaf Spot, Rice Hispa, Sheath Blight.
- **Wheat Diseases**: Wheat Blast, Black Point, Fusarium Foot Rot, Leaf Rust, Leaf Blight, Powdery Mildew, Stem Rust, Stripe Rust, Healthy.
- **Potato Diseases**: Early Blight, Late Blight, Healthy Leaf.
- **Corn Diseases**: Common Rust, Gray Leaf Spot, Northern Leaf Blight, Healthy.
- **Brassica Diseases**: Cabbage Alternaria Spot, Cabbage Black Rot, Cabbage Downy Mildew, Cauliflower Downy Mildew, Cauliflower Bacterial Soft Rot, Cauliflower Black Spot, Healthy.

### Usage Example:
```python

from ultralytics import YOLO

from PIL import Image



# 1. Classify Crop

crop_model = YOLO("vision/crop_classifier/model.pt")

crop_result = crop_model("leaf.jpg")[0]

predicted_crop = crop_model.names[crop_result.probs.top1]

print(f"Detected Crop: {predicted_crop}")



# 2. Classify Disease (e.g., Rice)

if predicted_crop.lower() == "rice":

    disease_model = YOLO("vision/rice_disease/model.pt")

    disease_result = disease_model("leaf.jpg")[0]

    predicted_disease = disease_model.names[disease_result.probs.top1]

    print(f"Diagnosis: {predicted_disease}")

```

---

## πŸ€– 2. Language Models (Gemma Bengali Agriculture LLM)

- **Base Model**: Google Gemma 4B Instruction-Tuned
- **Fine-Tuning**: LoRA Stage 1 Supervised Fine-Tuning (SFT) on verified Bengali agricultural Q&A datasets.
- **GGUF Quantization**: 16-bit GGUF model optimized for CPU/GPU edge deployment.

### Usage with Hugging Face Transformers & PEFT:
```python

from transformers import AutoModelForCausalLM, AutoTokenizer

from peft import PeftModel

import torch



base_model_id = "google/gemma-2-2b-it" # or 4B base

tokenizer = AutoTokenizer.from_pretrained("gemma_llm/checkpoint-4020")

base_model = AutoModelForCausalLM.from_pretrained(

    base_model_id,

    torch_dtype=torch.float16,

    device_map="auto"

)

model = PeftModel.from_pretrained(base_model, "gemma_llm/checkpoint-4020")



prompt = "<start_of_turn>user\nধানের ব্লাস্ট রোগের লক্ষণ ও ΰ¦ͺ্রঀিকার কী?<end_of_turn>\n<start_of_turn>model\n"

inputs = tokenizer(prompt, return_tensors="pt").to("cuda")

outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.2)

print(tokenizer.decode(outputs[0], skip_special_tokens=True))

```

### Usage with llama-cpp (GGUF):
```python

from llama_cpp import Llama



llm = Llama(

    model_path="gemma_llm/krishokchat.f16.gguf",

    n_ctx=2048,

    n_threads=6

)

output = llm(

    "<start_of_turn>user\nআলুর ΰ¦²ΰ§‡ΰ¦Ÿ ΰ¦¬ΰ§ΰ¦²ΰ¦Ύΰ¦‡ΰ¦Ÿ কীভাবে ΰ¦ͺ্রঀিরোধ করব?<end_of_turn>\n<start_of_turn>model\n",

    max_tokens=256,

    temperature=0.2

)

print(output["choices"][0]["text"])

```

---

## πŸ“š 3. Hybrid RAG Knowledge & Retrieval System

The RAG index combines dense semantic search and sparse lexical matching:
- **Dense Index**: FAISS FlatIP index with 1024-dimensional `BAAI/bge-m3` multilingual embeddings across 2,135 curated agricultural knowledge nodes.
- **Sparse Index**: BM25 Okapi index tokenized for Bengali domain vocabulary.
- **Dialect Normalization**: 6 Regional Bangladeshi dialects (Rajshahi, Sylhet, Chittagong, Barisal, Rangpur, Noakhali) mapped to standard terms.
- **Sources**: Bangladesh Agricultural Research Council (BARC FRG 2024), BARI, BRRI, CABI, DAE, DLS, DoF.

### Usage Example:
```python

import faiss

import numpy as np

import pickle

import json



# 1. Load FAISS index and metadata

index = faiss.read_index("rag_knowledge_index/indexes/nodes.faiss")

with open("rag_knowledge_index/indexes/node_ids.json", "r", encoding="utf-8") as f:

    node_ids = json.load(f)



# 2. Load BM25 index

with open("rag_knowledge_index/indexes/bm25_index.pkl", "rb") as f:

    bm25 = pickle.load(f)



print(f"FAISS index loaded with {index.ntotal} vectors.")

```

---

## πŸ”— Integration with Frontend (`suggested-questions.tsx`)

In KrishokChat's user interface, `suggested-questions.tsx` connects directly to this model suite:
- Vision outputs (`predicted_crop`, `predicted_disease`) are mapped via `CROP_LABELS` and `DISEASE_LABELS` to dynamically generate targeted Bengali advisory prompts.
- `DialectSelector` triggers regional dialect queries normalized by `phase4_dialect_map.json` into the hybrid RAG retrieval pipeline.

---

## πŸ“„ License & Attribution

- **License**: MIT License
- **Developed by**: Raiyan Reza ([RaiyanKhaan](https://github.com/RaiyanKhaan)) and Shakil Ahmed ([MRSHAKILS](https://github.com/MRSHAKILS))
- **Project**: KrishokChat - A Provenance-Traceable Multi-Task Bengali Agricultural Advisory System