File size: 14,789 Bytes
5fd7795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
"""
CropAI Agronomic Knowledge Base - Master Integration Hub
Contains curated pathology details, symptoms, exact CIBRC chemical prescriptions (calibrated per 16L/20L knapsack tank),
organic bio-controls, seasonal triggers, and historical outbreak contexts for 127+ crop diseases, pests,
insects, and physiological disorders across Chhattisgarh (Surguja, Bilaspur, Raipur, Durg, Bastar, etc.)
and Pan-India field, pulse, oilseed, cash, vegetable, and fruit crops.
"""

from typing import Dict, Any, Optional

# Import domain modules
from .data_cereals import CEREALS_KNOWLEDGE_BASE
from .data_pulses import PULSES_KNOWLEDGE_BASE
from .data_oilseeds import OILSEEDS_KNOWLEDGE_BASE
from .data_cashcrops import CASH_CROPS_KNOWLEDGE_BASE
from .data_vegetables import VEGETABLES_KNOWLEDGE_BASE
from .data_fruits import FRUITS_KNOWLEDGE_BASE
from .data_benchmark import BENCHMARK_KNOWLEDGE_BASE
from .hf_dataset_sync import get_all_hf_aliases

# Assemble unified agronomic knowledge base
DISEASE_KNOWLEDGE_BASE: Dict[str, Dict[str, Any]] = {}
DISEASE_KNOWLEDGE_BASE.update(CEREALS_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(PULSES_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(OILSEEDS_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(CASH_CROPS_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(VEGETABLES_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(FRUITS_KNOWLEDGE_BASE)
DISEASE_KNOWLEDGE_BASE.update(BENCHMARK_KNOWLEDGE_BASE)


# =========================================================================
# Hugging Face / Swin / MobileNet / ResNet / YOLO Label Aliases
# =========================================================================
HF_CLASS_MAP: Dict[str, str] = {
    # Rice / Paddy Models
    'Bacterialblight': 'Rice___Bacterial_Leaf_Blight',
    'Bacterial Leaf Blight': 'Rice___Bacterial_Leaf_Blight',
    'Blast': 'Rice___Blast',
    'Leaf Blast': 'Rice___Blast',
    'Neck Blast': 'Rice___Blast',
    'Brownspot': 'Rice___Brown_Spot',
    'Brown Spot': 'Rice___Brown_Spot',
    'Healthy': 'Rice___healthy',
    'Healthy Rice Leaf': 'Rice___healthy',
    'Rice healthy': 'Rice___healthy',
    'Tungro': 'Rice___Tungro',
    'Leaf Scald': 'Rice___Sheath_Blight',
    'Leaf scald': 'Rice___Sheath_Blight',
    'Sheath Blight': 'Rice___Sheath_Blight',
    'False Smut': 'Rice___False_Smut',
    'Khaira Disease': 'Rice___Khaira_Disease',
    'Yellow Stem Borer': 'Rice___Yellow_Stem_Borer',
    'Gall Midge': 'Rice___Gall_Midge',
    'Brown Plant Hopper': 'Rice___Brown_Plant_Hopper',
    'BPH': 'Rice___Brown_Plant_Hopper',
    'Leaf Folder': 'Rice___Leaf_Folder',
    'Rice Hispa': 'Rice___Hispa',
    'Gundhi Bug': 'Rice___Gundhi_Bug',
    'Bakanae': 'Rice___Bakanae',

    # Wheat
    'Wheat Leaf Rust': 'Wheat___Brown_Rust',
    'Wheat Brown Rust': 'Wheat___Brown_Rust',
    'Wheat Yellow Rust': 'Wheat___Yellow_Rust',
    'Wheat Stripe Rust': 'Wheat___Yellow_Rust',
    'Wheat Black Rust': 'Wheat___Black_Rust',
    'Wheat Stem Rust': 'Wheat___Black_Rust',
    'Wheat Loose Smut': 'Wheat___Loose_Smut',
    'Wheat Karnal Bunt': 'Wheat___Karnal_Bunt',
    'Wheat Powdery Mildew': 'Wheat___Powdery_Mildew',
    'Wheat Termite': 'Wheat___Termites',
    'Wheat healthy': 'Wheat___healthy',

    # Maize / Corn
    'Corn (Maize) with Cercospora and Gray Leaf Spot': 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
    'Corn (Maize) with Common Rust': 'Corn_(maize)___Common_rust_',
    'Corn (Maize) with Northern Leaf Blight': 'Corn_(maize)___Northern_Leaf_Blight',
    'Healthy Corn (Maize) Plant': 'Corn_(maize)___healthy',
    'Corn Fall Armyworm': 'Maize___Fall_Armyworm',
    'Fall Armyworm': 'Maize___Fall_Armyworm',
    'Corn Stem Borer': 'Maize___Stem_Borer',

    # Pulses (Chickpea, Pigeonpea, Urad, Moong, Tiwda)
    'Chickpea Pod Borer': 'Chickpea___Pod_Borer',
    'Gram Pod Borer': 'Chickpea___Pod_Borer',
    'Chickpea Fusarium Wilt': 'Chickpea___Fusarium_Wilt',
    'Chickpea Ascochyta Blight': 'Chickpea___Ascochyta_Blight',
    'Chickpea Dry Root Rot': 'Chickpea___Dry_Root_Rot',
    'Chickpea Collar Rot': 'Chickpea___Collar_Rot',
    'Chickpea healthy': 'Chickpea___healthy',
    'Pigeonpea Wilt': 'Pigeonpea___Fusarium_Wilt',
    'Arhar Wilt': 'Pigeonpea___Fusarium_Wilt',
    'Pigeonpea Sterility Mosaic': 'Pigeonpea___Sterility_Mosaic_Disease',
    'Pigeonpea Pod Borer': 'Pigeonpea___Pod_Borer',
    'Pigeonpea Pod Fly': 'Pigeonpea___Pod_Fly',
    'Pigeonpea Phytophthora Blight': 'Pigeonpea___Phytophthora_Blight',
    'Pigeonpea healthy': 'Pigeonpea___healthy',
    'Urad Yellow Mosaic': 'Urad_Moong___Yellow_Mosaic_Virus',
    'Moong Yellow Mosaic': 'Urad_Moong___Yellow_Mosaic_Virus',
    'Urad Cercospora Leaf Spot': 'Urad_Moong___Cercospora_Leaf_Spot',
    'Urad Powdery Mildew': 'Urad_Moong___Powdery_Mildew',
    'Urad healthy': 'Urad_Moong___healthy',
    'Tiwda Downy Mildew': 'Lathyrus___Downy_Mildew',
    'Tiwda Rust': 'Lathyrus___Rust',
    'Tiwda healthy': 'Lathyrus___healthy',

    # Oilseeds (Soybean, Mustard, Groundnut, Niger)
    'Soybean Girdle Beetle': 'Soybean___Girdle_Beetle',
    'Soybean Yellow Mosaic': 'Soybean___Yellow_Mosaic_Virus',
    'Soybean Rust': 'Soybean___Rust',
    'Soybean Anthracnose': 'Soybean___Anthracnose',
    'Soybean Stem Fly': 'Soybean___Stem_Fly',
    'Soybean Defoliator': 'Soybean___Spodoptera_Litura',
    'Healthy Soybean Plant': 'Soybean___healthy',
    'Soybean healthy': 'Soybean___healthy',
    'Mustard Aphid': 'Mustard___Aphid',
    'Mustard White Rust': 'Mustard___White_Rust',
    'Mustard Alternaria Blight': 'Mustard___Alternaria_Blight',
    'Mustard Powdery Mildew': 'Mustard___Powdery_Mildew',
    'Mustard Sclerotinia Stem Rot': 'Mustard___Sclerotinia_Stem_Rot',
    'Mustard healthy': 'Mustard___healthy',
    'Groundnut Tikka Leaf Spot': 'Groundnut___Tikka_Leaf_Spot',
    'Groundnut Collar Rot': 'Groundnut___Collar_Rot',
    'Groundnut Rust': 'Groundnut___Rust',
    'Groundnut healthy': 'Groundnut___healthy',

    # Cash Crops (Cotton, Sugarcane)
    'Cotton Pink Bollworm': 'Cotton___Pink_Bollworm',
    'Cotton Whitefly': 'Cotton___Whitefly',
    'Cotton Bacterial Blight': 'Cotton___Bacterial_Blight',
    'Cotton Leaf Curl Virus': 'Cotton___Leaf_Curl_Virus',
    'Cotton Boll Rot': 'Cotton___Boll_Rot_Complex',
    'Cotton healthy': 'Cotton___healthy',
    'Sugarcane Red Rot': 'Sugarcane___Red_Rot',
    'Sugarcane Early Shoot Borer': 'Sugarcane___Early_Shoot_Borer',
    'Sugarcane Pyrilla': 'Sugarcane___Pyrilla',
    'Sugarcane Smut': 'Sugarcane___Smut',
    'Sugarcane healthy': 'Sugarcane___healthy',

    # Vegetables (Tomato, Potato, Chilli, Brinjal, Okra)
    'Tomato with Bacterial Spot': 'Tomato___Bacterial_spot',
    'Tomato_Bacterial_spot': 'Tomato___Bacterial_spot',
    'Tomato Bacterial Spot': 'Tomato___Bacterial_spot',
    'Tomato with Early Blight': 'Tomato___Early_blight',
    'Tomato_Early_blight': 'Tomato___Early_blight',
    'Tomato Early Blight': 'Tomato___Early_blight',
    'Tomato with Late Blight': 'Tomato___Late_blight',
    'Tomato_Late_blight': 'Tomato___Late_blight',
    'Tomato Late Blight': 'Tomato___Late_blight',
    'Tomato with Leaf Mold': 'Tomato___Leaf_Mold',
    'Tomato_Leaf_Mold': 'Tomato___Leaf_Mold',
    'Tomato with Septoria Leaf Spot': 'Tomato___Septoria_leaf_spot',
    'Tomato_Septoria_leaf_spot': 'Tomato___Septoria_leaf_spot',
    'Tomato with Spider Mites or Two-spotted Spider Mite': 'Tomato___Spider_mites Two-spotted_spider_mite',
    'Tomato_Spider_mites_Two_spotted_spider_mite': 'Tomato___Spider_mites Two-spotted_spider_mite',
    'Tomato with Target Spot': 'Tomato___Target_Spot',
    'Tomato__Target_Spot': 'Tomato___Target_Spot',
    'Tomato Yellow Leaf Curl Virus': 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
    'Tomato__Tomato_YellowLeaf__Curl_Virus': 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
    'Tomato Mosaic Virus': 'Tomato___Tomato_mosaic_virus',
    'Tomato__Tomato_mosaic_virus': 'Tomato___Tomato_mosaic_virus',
    'Tomato Fruit Borer': 'Tomato___Fruit_Borer',
    'Healthy Tomato Plant': 'Tomato___healthy',
    'Tomato_healthy': 'Tomato___healthy',
    'Tomato healthy': 'Tomato___healthy',

    'Potato with Early Blight': 'Potato___Early_blight',
    'Potato_Early_blight': 'Potato___Early_blight',
    'Potato Early Blight': 'Potato___Early_blight',
    'Potato with Late Blight': 'Potato___Late_blight',
    'Potato_Late_blight': 'Potato___Late_blight',
    'Potato Late Blight': 'Potato___Late_blight',
    'Potato Bacterial Wilt': 'Potato___Bacterial_Wilt',
    'Healthy Potato Plant': 'Potato___healthy',
    'Potato_healthy': 'Potato___healthy',
    'Potato healthy': 'Potato___healthy',

    'Chilli Leaf Curl Virus': 'Chilli___Leaf_Curl_Virus',
    'Chilli Anthracnose Fruit Rot': 'Chilli___Anthracnose_Dieback',
    'Chilli Thrips': 'Chilli___Thrips',
    'Chilli healthy': 'Chilli___healthy',

    'Brinjal Shoot and Fruit Borer': 'Brinjal___Shoot_and_Fruit_Borer',
    'Brinjal Phomopsis Blight': 'Brinjal___Phomopsis_Blight_Fruit_Rot',
    'Brinjal Little Leaf': 'Brinjal___Little_Leaf_Disease',
    'Brinjal healthy': 'Brinjal___healthy',

    'Okra Yellow Vein Mosaic Virus': 'Okra___Yellow_Vein_Mosaic_Virus',
    'Okra Shoot and Fruit Borer': 'Okra___Shoot_and_Fruit_Borer',
    'Okra Powdery Mildew': 'Okra___Powdery_Mildew',
    'Okra healthy': 'Okra___healthy',

    # Fruits & Alliums (Mango, Papaya, Citrus, Onion, Garlic, Cucurbits)
    'Mango Powdery Mildew': 'Mango___Powdery_Mildew',
    'Mango Anthracnose': 'Mango___Anthracnose',
    'Mango Hopper': 'Mango___Hopper',
    'Mango Malformation': 'Mango___Malformation',
    'Papaya Ringspot Virus': 'Papaya___Ringspot_Virus',
    'Papaya Mealybug': 'Papaya___Mealybug',
    'Citrus Canker': 'Citrus___Canker',
    'Citrus Greening': 'Citrus___Greening_Huanglongbing',
    'Citrus Leaf Miner': 'Citrus___Leaf_Miner',
    'Orange with Citrus Greening': 'Orange___Haunglongbing_(Citrus_greening)',
    'Onion Purple Blotch': 'Onion_Garlic___Purple_Blotch',
    'Onion Thrips': 'Onion_Garlic___Thrips',
    'Garlic Stemphylium Blight': 'Onion_Garlic___Stemphylium_Blight',
    'Cucurbit Downy Mildew': 'Cucurbits___Downy_Mildew',
    'Cucurbit Powdery Mildew': 'Cucurbits___Powdery_Mildew',
    'Cucurbit Fruit Fly': 'Cucurbits___Fruit_Fly',

    # Benchmark PlantVillage
    'Apple Scab': 'Apple___Apple_scab',
    'Apple with Black Rot': 'Apple___Black_rot',
    'Cedar Apple Rust': 'Apple___Cedar_apple_rust',
    'Healthy Apple': 'Apple___healthy',
    'Healthy Blueberry Plant': 'Blueberry___healthy',
    'Cherry with Powdery Mildew': 'Cherry___Powdery_mildew',
    'Healthy Cherry Plant': 'Cherry___healthy',
    'Grape with Black Rot': 'Grape___Black_rot',
    'Grape with Esca (Black Measles)': 'Grape___Esca_(Black_Measles)',
    'Grape with Isariopsis Leaf Spot': 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
    'Healthy Grape Plant': 'Grape___healthy',
    'Peach with Bacterial Spot': 'Peach___Bacterial_spot',
    'Healthy Peach Plant': 'Peach___healthy',
    'Bell Pepper with Bacterial Spot': 'Pepper,_bell___Bacterial_spot',
    'Pepper__bell___Bacterial_spot': 'Pepper,_bell___Bacterial_spot',
    'Healthy Bell Pepper Plant': 'Pepper,_bell___healthy',
    'Pepper__bell___healthy': 'Pepper,_bell___healthy',
    'Healthy Raspberry Plant': 'Raspberry___healthy',
    'Squash with Powdery Mildew': 'Squash___Powdery_mildew',
    'Strawberry with Leaf Scorch': 'Strawberry___Leaf_scorch',
    'Healthy Strawberry Plant': 'Strawberry___healthy',
}
HF_CLASS_MAP.update(get_all_hf_aliases())

DEFAULT_UNKNOWN_DISEASE: Dict[str, Any] = {
    "name": "General Foliar Condition / Mild Anomaly",
    "scientific_name": "Pathology Under Observation",
    "crop": "General Crop",
    "category": "Anomaly",
    "severity": "Moderate",
    "is_healthy": False,
    "season_and_weather": "Variable conditions; monitor field microclimate closely",
    "economic_injury_level": "Inspect 20 random plants across field diagonals",
    "symptoms": [
        "Visual anomaly detected on the leaf surface",
        "Possible localized chlorosis, pigmentation irregularity, or mechanical injury",
        "Requires further inspection in bright natural daylight"
    ],
    "treatment": {
        "immediate_action": [
            "Isolate the affected branch or plant to prevent potential contagion",
            "Ensure leaf is clean and retake photo in bright natural daylight"
        ],
        "organic_control": [
            "Spray neem oil solution (0.5% - 80ml per 16L tank) as a broad-spectrum preventive bio-agent",
            "Ensure balanced compost and micro-nutrient foliar spray"
        ],
        "chemical_control": [
            "Consult with an agricultural extension officer (KVK / Krishi Mitr) before applying systemic chemical sprays"
        ]
    },
    "prevention": [
        "Maintain good farm sanitation and remove weed hosts",
        "Monitor crop weekly and avoid prolonged leaf wetness"
    ],
    "expert_consultation_recommended": True
}


def get_disease_info(class_id: str) -> Dict[str, Any]:
    """
    Look up disease metadata from canonical class ID, HuggingFace label, or normalized query.
    Returns comprehensive agronomic details including tank dosages, organic controls, and symptoms.
    """
    # 1. Direct dictionary match
    if class_id in DISEASE_KNOWLEDGE_BASE:
        info = DISEASE_KNOWLEDGE_BASE[class_id].copy()
        info["id"] = class_id
        return info

    # 2. Check Hugging Face explicit alias map
    if class_id in HF_CLASS_MAP:
        mapped_key = HF_CLASS_MAP[class_id]
        if mapped_key in DISEASE_KNOWLEDGE_BASE:
            info = DISEASE_KNOWLEDGE_BASE[mapped_key].copy()
            info["id"] = mapped_key
            return info

    # 3. Normalized fuzzy search
    clean_id = class_id.replace(" ", "_").replace("-", "_").replace("__", "_").lower()
    for key, data in DISEASE_KNOWLEDGE_BASE.items():
        clean_key = key.replace(" ", "_").replace("-", "_").replace("__", "_").lower()
        if clean_key == clean_id:
            info = data.copy()
            info["id"] = key
            return info

    # 4. Partial substring match in knowledge base keys
    for key, data in DISEASE_KNOWLEDGE_BASE.items():
        if class_id.lower() in key.lower() or key.lower() in class_id.lower():
            info = data.copy()
            info["id"] = key
            return info

    # 5. Fallback representation
    info = DEFAULT_UNKNOWN_DISEASE.copy()
    parts = class_id.split("___")
    if len(parts) == 2:
        info["crop"] = parts[0].replace("_", " ").title()
        info["name"] = f"{info['crop']} {parts[1].replace('_', ' ').title()}"
    else:
        info["crop"] = "Unknown Crop"
        info["name"] = class_id.replace("_", " ").title()
    info["id"] = class_id
    return info