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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import networkx as nx
|
| 7 |
+
import os
|
| 8 |
+
from transformers import AutoModel, AutoConfig
|
| 9 |
+
from openai import OpenAI
|
| 10 |
+
import json # for black JSON
|
| 11 |
+
import gdown
|
| 12 |
+
|
| 13 |
+
# .....................................
|
| 14 |
+
MODEL_PATH = "best.pt"
|
| 15 |
+
if not os.path.exists(MODEL_PATH):
|
| 16 |
+
# Replace FILE_ID with Google Drive file ID
|
| 17 |
+
file_id = "1bGRLEC2_5GB53E-zEVH1Z4EQdKGA-YGI"
|
| 18 |
+
url = f"https://drive.google.com/uc?id={file_id}"
|
| 19 |
+
print("Downloading model from Google Drive...")
|
| 20 |
+
gdown.download(url, MODEL_PATH, quiet=False)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# -------------------------------
|
| 24 |
+
# Load Knowledge Graph + Symptom Map
|
| 25 |
+
# -------------------------------
|
| 26 |
+
# 1. Create Extended Knowledge Graph (KG)
|
| 27 |
+
# -------------------------------
|
| 28 |
+
G = nx.DiGraph()
|
| 29 |
+
|
| 30 |
+
# -------------------------------
|
| 31 |
+
# Symptoms β Diseases
|
| 32 |
+
# -------------------------------
|
| 33 |
+
# Apple
|
| 34 |
+
G.add_edge("Olive-brown velvety spots on leaves/fruits", "Apple___Apple_scab")
|
| 35 |
+
G.add_edge("Dark sunken lesions with concentric rings", "Apple___Black_rot")
|
| 36 |
+
G.add_edge("Orange/yellow spots with black centers", "Apple___Cedar_apple_rust")
|
| 37 |
+
G.add_edge("No visible disease", "Apple___healthy")
|
| 38 |
+
|
| 39 |
+
# Blueberry
|
| 40 |
+
G.add_edge("No visible disease", "Blueberry___healthy")
|
| 41 |
+
|
| 42 |
+
# Cherry
|
| 43 |
+
G.add_edge("White powdery coating on leaves", "Cherry_(including_sour)___Powdery_mildew")
|
| 44 |
+
G.add_edge("No visible disease", "Cherry_(including_sour)___healthy")
|
| 45 |
+
|
| 46 |
+
# Corn
|
| 47 |
+
G.add_edge("Gray/tan lesions with dark borders", "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot")
|
| 48 |
+
G.add_edge("Small reddish-brown pustules on leaves", "Corn_(maize)___Common_rust_")
|
| 49 |
+
G.add_edge("Cigar-shaped gray-green lesions on leaves", "Corn_(maize)___Northern_Leaf_Blight")
|
| 50 |
+
G.add_edge("No visible disease", "Corn_(maize)___healthy")
|
| 51 |
+
|
| 52 |
+
# Grape
|
| 53 |
+
G.add_edge("Circular black spots on leaves/fruits", "Grape___Black_rot")
|
| 54 |
+
G.add_edge("Interveinal chlorosis, necrosis (black measles)", "Grape___Esca_(Black_Measles)")
|
| 55 |
+
G.add_edge("Irregular brown spots with yellow halo", "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)")
|
| 56 |
+
G.add_edge("No visible disease", "Grape___healthy")
|
| 57 |
+
|
| 58 |
+
# Orange
|
| 59 |
+
G.add_edge("Yellow shoots, mottled leaves, misshapen fruits", "Orange___Haunglongbing_(Citrus_greening)")
|
| 60 |
+
|
| 61 |
+
# Peach
|
| 62 |
+
G.add_edge("Small dark water-soaked spots on leaves", "Peach___Bacterial_spot")
|
| 63 |
+
G.add_edge("No visible disease", "Peach___healthy")
|
| 64 |
+
|
| 65 |
+
# Pepper (typo fix: Pepper instead of 'pper,_bell')
|
| 66 |
+
G.add_edge("Brown lesions with yellow halo", "Pepper,_bell___Bacterial_spot")
|
| 67 |
+
G.add_edge("No visible disease", "Pepper,_bell___healthy")
|
| 68 |
+
|
| 69 |
+
# Potato
|
| 70 |
+
G.add_edge("Dark concentric spots on leaves", "Potato___Early_blight")
|
| 71 |
+
G.add_edge("Large irregular brown/black lesions", "Potato___Late_blight")
|
| 72 |
+
G.add_edge("No visible disease", "Potato___healthy")
|
| 73 |
+
|
| 74 |
+
# Raspberry
|
| 75 |
+
G.add_edge("No visible disease", "Raspberry___healthy")
|
| 76 |
+
|
| 77 |
+
# Soybean
|
| 78 |
+
G.add_edge("No visible disease", "Soybean___healthy")
|
| 79 |
+
|
| 80 |
+
# Squash
|
| 81 |
+
G.add_edge("White powdery patches on leaves", "Squash___Powdery_mildew")
|
| 82 |
+
|
| 83 |
+
# Strawberry
|
| 84 |
+
G.add_edge("Irregular brown leaf margins, scorching", "Strawberry___Leaf_scorch")
|
| 85 |
+
G.add_edge("No visible disease", "Strawberry___healthy")
|
| 86 |
+
|
| 87 |
+
# Tomato
|
| 88 |
+
G.add_edge("Water-soaked brown spots on leaves", "Tomato___Bacterial_spot")
|
| 89 |
+
G.add_edge("Concentric rings, target-like spots", "Tomato___Early_blight")
|
| 90 |
+
G.add_edge("Large dark blotches with fuzzy growth", "Tomato___Late_blight")
|
| 91 |
+
G.add_edge("Yellow patches on upper leaf, fuzzy underside", "Tomato___Leaf_Mold")
|
| 92 |
+
G.add_edge("Small circular dark spots with yellow halo", "Tomato___Septoria_leaf_spot")
|
| 93 |
+
G.add_edge("White/yellow stippling + webbing", "Tomato___Spider_mites Two-spotted_spider_mite")
|
| 94 |
+
G.add_edge("Brown/black target-like spots", "Tomato___Target_Spot")
|
| 95 |
+
G.add_edge("Leaf curling + yellow mosaic", "Tomato___Tomato_Yellow_Leaf_Curl_Virus")
|
| 96 |
+
G.add_edge("Mosaic mottling on leaves", "Tomato___Tomato_mosaic_virus")
|
| 97 |
+
G.add_edge("No visible disease", "Tomato___healthy")
|
| 98 |
+
|
| 99 |
+
# -------------------------------
|
| 100 |
+
# Diseases β Treatments
|
| 101 |
+
# -------------------------------
|
| 102 |
+
# Apple
|
| 103 |
+
G.add_edge("Apple___Apple_scab", "Fungicides (captan, myclobutanil), prune infected leaves")
|
| 104 |
+
G.add_edge("Apple___Black_rot", "Remove mummified fruits, use fungicides")
|
| 105 |
+
G.add_edge("Apple___Cedar_apple_rust", "Remove nearby junipers, apply fungicides")
|
| 106 |
+
G.add_edge("Apple___healthy", "No treatment needed")
|
| 107 |
+
|
| 108 |
+
# Blueberry
|
| 109 |
+
G.add_edge("Blueberry___healthy", "No treatment needed")
|
| 110 |
+
|
| 111 |
+
# Cherry
|
| 112 |
+
G.add_edge("Cherry_(including_sour)___Powdery_mildew", "Fungicides: sulfur, myclobutanil")
|
| 113 |
+
G.add_edge("Cherry_(including_sour)___healthy", "No treatment needed")
|
| 114 |
+
|
| 115 |
+
# Corn
|
| 116 |
+
G.add_edge("Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot", "Use resistant hybrids, fungicides")
|
| 117 |
+
G.add_edge("Corn_(maize)___Common_rust_", "Resistant hybrids, fungicide if severe")
|
| 118 |
+
G.add_edge("Corn_(maize)___Northern_Leaf_Blight", "Crop rotation, resistant varieties, fungicides")
|
| 119 |
+
G.add_edge("Corn_(maize)___healthy", "No treatment needed")
|
| 120 |
+
|
| 121 |
+
# Grape
|
| 122 |
+
G.add_edge("Grape___Black_rot", "Fungicides (mancozeb, myclobutanil), prune infected vines")
|
| 123 |
+
G.add_edge("Grape___Esca_(Black_Measles)", "Remove infected wood, fungicides not very effective")
|
| 124 |
+
G.add_edge("Grape___Leaf_blight_(Isariopsis_Leaf_Spot)", "Remove infected leaves, fungicides")
|
| 125 |
+
G.add_edge("Grape___healthy", "No treatment needed")
|
| 126 |
+
|
| 127 |
+
# Orange
|
| 128 |
+
G.add_edge("Orange___Haunglongbing_(Citrus_greening)", "No cure, control psyllid vector, use resistant rootstocks")
|
| 129 |
+
|
| 130 |
+
# Peach
|
| 131 |
+
G.add_edge("Peach___Bacterial_spot", "Copper fungicides, resistant varieties")
|
| 132 |
+
G.add_edge("Peach___healthy", "No treatment needed")
|
| 133 |
+
|
| 134 |
+
# Pepper
|
| 135 |
+
G.add_edge("Pepper,_bell___Bacterial_spot", "Use copper-based bactericides, resistant cultivars")
|
| 136 |
+
G.add_edge("Pepper,_bell___healthy", "No treatment needed")
|
| 137 |
+
|
| 138 |
+
# Potato
|
| 139 |
+
G.add_edge("Potato___Early_blight", "Use fungicides: Mancozeb, Chlorothalonil")
|
| 140 |
+
G.add_edge("Potato___Late_blight", "Copper-based fungicides, resistant varieties")
|
| 141 |
+
G.add_edge("Potato___healthy", "No treatment needed")
|
| 142 |
+
|
| 143 |
+
# Raspberry
|
| 144 |
+
G.add_edge("Raspberry___healthy", "No treatment needed")
|
| 145 |
+
|
| 146 |
+
# Soybean
|
| 147 |
+
G.add_edge("Soybean___healthy", "No treatment needed")
|
| 148 |
+
|
| 149 |
+
# Squash
|
| 150 |
+
G.add_edge("Squash___Powdery_mildew", "Sulfur-based fungicides, resistant varieties")
|
| 151 |
+
|
| 152 |
+
# Strawberry
|
| 153 |
+
G.add_edge("Strawberry___Leaf_scorch", "Remove infected leaves, apply fungicides")
|
| 154 |
+
G.add_edge("Strawberry___healthy", "No treatment needed")
|
| 155 |
+
|
| 156 |
+
# Tomato
|
| 157 |
+
G.add_edge("Tomato___Bacterial_spot", "Copper sprays, avoid overhead irrigation")
|
| 158 |
+
G.add_edge("Tomato___Early_blight", "Fungicides: Chlorothalonil, crop rotation")
|
| 159 |
+
G.add_edge("Tomato___Late_blight", "Copper fungicides, remove infected plants")
|
| 160 |
+
G.add_edge("Tomato___Leaf_Mold", "Fungicides: Chlorothalonil, improve ventilation")
|
| 161 |
+
G.add_edge("Tomato___Septoria_leaf_spot", "Apply fungicides, remove infected leaves")
|
| 162 |
+
G.add_edge("Tomato___Spider_mites Two-spotted_spider_mite", "Insecticidal soap, neem oil, predatory mites")
|
| 163 |
+
G.add_edge("Tomato___Target_Spot", "Fungicides: Chlorothalonil, Mancozeb")
|
| 164 |
+
G.add_edge("Tomato___Tomato_Yellow_Leaf_Curl_Virus", "No cure: use resistant varieties, control whiteflies")
|
| 165 |
+
G.add_edge("Tomato___Tomato_mosaic_virus", "Remove infected plants, disinfect tools")
|
| 166 |
+
G.add_edge("Tomato___healthy", "No treatment needed")
|
| 167 |
+
|
| 168 |
+
# -------------------------------
|
| 169 |
+
# 2. Map Model Predictions β Symptoms
|
| 170 |
+
# -------------------------------
|
| 171 |
+
symptom_map = {
|
| 172 |
+
"Apple___Apple_scab": "Olive-brown velvety spots on leaves/fruits",
|
| 173 |
+
"Apple___Black_rot": "Dark sunken lesions with concentric rings",
|
| 174 |
+
"Apple___Cedar_apple_rust": "Orange/yellow spots with black centers",
|
| 175 |
+
"Apple___healthy": "No visible disease",
|
| 176 |
+
|
| 177 |
+
"Blueberry___healthy": "No visible disease",
|
| 178 |
+
|
| 179 |
+
"Cherry_(including_sour)___Powdery_mildew": "White powdery coating on leaves",
|
| 180 |
+
"Cherry_(including_sour)___healthy": "No visible disease",
|
| 181 |
+
|
| 182 |
+
"Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot": "Gray/tan lesions with dark borders",
|
| 183 |
+
"Corn_(maize)___Common_rust_": "Small reddish-brown pustules on leaves",
|
| 184 |
+
"Corn_(maize)___Northern_Leaf_Blight": "Cigar-shaped gray-green lesions on leaves",
|
| 185 |
+
"Corn_(maize)___healthy": "No visible disease",
|
| 186 |
+
|
| 187 |
+
"Grape___Black_rot": "Circular black spots on leaves/fruits",
|
| 188 |
+
"Grape___Esca_(Black_Measles)": "Interveinal chlorosis, necrosis (black measles)",
|
| 189 |
+
"Grape___Leaf_blight_(Isariopsis_Leaf_Spot)": "Irregular brown spots with yellow halo",
|
| 190 |
+
"Grape___healthy": "No visible disease",
|
| 191 |
+
|
| 192 |
+
"Orange___Haunglongbing_(Citrus_greening)": "Yellow shoots, mottled leaves, misshapen fruits",
|
| 193 |
+
|
| 194 |
+
"Peach___Bacterial_spot": "Small dark water-soaked spots on leaves",
|
| 195 |
+
"Peach___healthy": "No visible disease",
|
| 196 |
+
|
| 197 |
+
"Pepper,_bell___Bacterial_spot": "Brown lesions with yellow halo",
|
| 198 |
+
"Pepper,_bell___healthy": "No visible disease",
|
| 199 |
+
|
| 200 |
+
"Potato___Early_blight": "Dark concentric spots on leaves",
|
| 201 |
+
"Potato___Late_blight": "Large irregular brown/black lesions",
|
| 202 |
+
"Potato___healthy": "No visible disease",
|
| 203 |
+
|
| 204 |
+
"Raspberry___healthy": "No visible disease",
|
| 205 |
+
"Soybean___healthy": "No visible disease",
|
| 206 |
+
|
| 207 |
+
"Squash___Powdery_mildew": "White powdery patches on leaves",
|
| 208 |
+
|
| 209 |
+
"Strawberry___Leaf_scorch": "Irregular brown leaf margins, scorching",
|
| 210 |
+
"Strawberry___healthy": "No visible disease",
|
| 211 |
+
|
| 212 |
+
"Tomato___Bacterial_spot": "Water-soaked brown spots on leaves",
|
| 213 |
+
"Tomato___Early_blight": "Concentric rings, target-like spots",
|
| 214 |
+
"Tomato___Late_blight": "Large dark blotches with fuzzy growth",
|
| 215 |
+
"Tomato___Leaf_Mold": "Yellow patches on upper leaf, fuzzy underside",
|
| 216 |
+
"Tomato___Septoria_leaf_spot": "Small circular dark spots with yellow halo",
|
| 217 |
+
"Tomato___Spider_mites Two-spotted_spider_mite": "White/yellow stippling + webbing",
|
| 218 |
+
"Tomato___Target_Spot": "Brown/black target-like spots",
|
| 219 |
+
"Tomato___Tomato_Yellow_Leaf_Curl_Virus": "Leaf curling + yellow mosaic",
|
| 220 |
+
"Tomato___Tomato_mosaic_virus": "Mosaic mottling on leaves",
|
| 221 |
+
"Tomato___healthy": "No visible disease"
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
# -------------------------------
|
| 225 |
+
# Model Setup
|
| 226 |
+
# -------------------------------
|
| 227 |
+
class DinoClassifier(nn.Module):
|
| 228 |
+
def __init__(self, base_model, num_classes, hidden_size):
|
| 229 |
+
super().__init__()
|
| 230 |
+
self.base = base_model
|
| 231 |
+
self.classifier = nn.Linear(hidden_size, num_classes)
|
| 232 |
+
|
| 233 |
+
def forward(self, x):
|
| 234 |
+
outputs = self.base(x)
|
| 235 |
+
pooled = outputs.last_hidden_state.mean(dim=1)
|
| 236 |
+
return self.classifier(pooled)
|
| 237 |
+
|
| 238 |
+
model_name = "facebook/dinov3-vits16-pretrain-lvd1689m"
|
| 239 |
+
hf_token = os.environ.get("HF_TOKEN") # Token from environment variable
|
| 240 |
+
|
| 241 |
+
config = AutoConfig.from_pretrained(model_name, use_auth_token=hf_token)
|
| 242 |
+
base_model = AutoModel.from_pretrained(model_name, config=config, use_auth_token=hf_token)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
num_classes = 38
|
| 246 |
+
model = DinoClassifier(base_model, num_classes, config.hidden_size)
|
| 247 |
+
checkpoint = torch.load(MODEL_PATH, map_location="cpu")
|
| 248 |
+
model.load_state_dict(checkpoint["model_state_dict"])
|
| 249 |
+
class_names = checkpoint["classes"]
|
| 250 |
+
model.eval()
|
| 251 |
+
|
| 252 |
+
# -------------------------------
|
| 253 |
+
# Image Preprocessing
|
| 254 |
+
# -------------------------------
|
| 255 |
+
val_test_tfms = transforms.Compose([
|
| 256 |
+
transforms.Resize((224, 224)),
|
| 257 |
+
transforms.ToTensor(),
|
| 258 |
+
transforms.Normalize(mean=(0.5, 0.5, 0.5),
|
| 259 |
+
std=(0.5, 0.5, 0.5)),
|
| 260 |
+
])
|
| 261 |
+
|
| 262 |
+
# -------------------------------
|
| 263 |
+
# OpenAI API Setup
|
| 264 |
+
# -------------------------------
|
| 265 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 266 |
+
|
| 267 |
+
# -------------------------------
|
| 268 |
+
# Prediction Pipeline (fast)
|
| 269 |
+
# -------------------------------
|
| 270 |
+
def predict_pipeline(image: Image.Image, get_explanation: bool = True):
|
| 271 |
+
# Preprocess
|
| 272 |
+
x = val_test_tfms(image).unsqueeze(0)
|
| 273 |
+
|
| 274 |
+
# Prediction
|
| 275 |
+
with torch.no_grad():
|
| 276 |
+
logits = model(x)
|
| 277 |
+
pred_idx = logits.argmax(1).item()
|
| 278 |
+
pred_class = class_names[pred_idx]
|
| 279 |
+
|
| 280 |
+
# KG Lookup
|
| 281 |
+
symptom = symptom_map.get(pred_class, "Unknown")
|
| 282 |
+
treatment = list(G.neighbors(pred_class)) if pred_class in G else ["No treatment found"]
|
| 283 |
+
|
| 284 |
+
# JSON summary (black text)
|
| 285 |
+
result_json = {
|
| 286 |
+
"Predicted Disease": pred_class,
|
| 287 |
+
"Symptom": symptom,
|
| 288 |
+
"Treatment": ", ".join(treatment),
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
# GPT explanation (optional, can be skipped to speed up)
|
| 292 |
+
if get_explanation:
|
| 293 |
+
prompt = f"""
|
| 294 |
+
You are an agriculture expert.
|
| 295 |
+
Disease: {pred_class}
|
| 296 |
+
Symptom: {symptom}
|
| 297 |
+
Treatment: {', '.join(treatment)}
|
| 298 |
+
|
| 299 |
+
Provide a **detailed explanation** in Markdown with sections:
|
| 300 |
+
1. What causes it
|
| 301 |
+
2. How to control it
|
| 302 |
+
3. Prevention methods
|
| 303 |
+
"""
|
| 304 |
+
try:
|
| 305 |
+
response = client.chat.completions.create(
|
| 306 |
+
model="gpt-4o-mini",
|
| 307 |
+
messages=[{"role": "user", "content": prompt}],
|
| 308 |
+
temperature=0.7
|
| 309 |
+
)
|
| 310 |
+
llm_text = response.choices[0].message.content
|
| 311 |
+
except Exception as e:
|
| 312 |
+
llm_text = f"**LLM Error:** {str(e)}"
|
| 313 |
+
else:
|
| 314 |
+
llm_text = "GPT explanation skipped for speed."
|
| 315 |
+
|
| 316 |
+
return json.dumps(result_json, indent=2), llm_text
|
| 317 |
+
|
| 318 |
+
# -------------------------------
|
| 319 |
+
# Gradio App (fast, black JSON)
|
| 320 |
+
# -------------------------------
|
| 321 |
+
demo = gr.Interface(
|
| 322 |
+
fn=predict_pipeline,
|
| 323 |
+
inputs=[gr.Image(type="pil")],
|
| 324 |
+
outputs=[
|
| 325 |
+
gr.Textbox(label="π Prediction Summary", lines=10, interactive=False),
|
| 326 |
+
gr.Markdown(label="π Detailed Explanation")
|
| 327 |
+
],
|
| 328 |
+
title="π± Plant Disease Detection with KG + GPT-4o-mini",
|
| 329 |
+
description="Upload a plant leaf image to detect disease, get symptoms, treatment, and optionally an expert explanation."
|
| 330 |
+
)
|
| 331 |
+
|
| 332 |
+
if __name__ == "__main__":
|
| 333 |
+
demo.launch()
|
| 334 |
+
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ed0ce22248f5f197a0b9b69a17b912a60dae47322003f017abb0386c4a4faf85
|
| 3 |
+
size 259545297
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
timm
|
| 5 |
+
gradio
|
| 6 |
+
networkx
|
| 7 |
+
Pillow
|
| 8 |
+
openai
|
| 9 |
+
gdown
|