Spaces:
Running
Running
File size: 11,413 Bytes
fc10439 504405e fc10439 504405e fc10439 504405e a858de9 d4536af 504405e fc10439 |
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 |
from tensorflow.keras.models import load_model as keras_load_model
from tensorflow.keras.applications.xception import preprocess_input
from PIL import Image
import numpy as np
from huggingface_hub import hf_hub_download
from io import BytesIO
import os
from pathlib import Path
# Base directory: two levels up from this file
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Map model names to class labels
CLASS_NAMES = {
"xception_maize": ['fall armyworm', 'grasshoper', 'healthy', 'leaf beetle', 'leaf blight', 'leaf spot', 'streak virus'],
"xception_cassava": ["bacterial blight", "brown spot", "green mite", "healthy", "mosaic"],
"xception_cashew": ["anthracnose", "gumosis", "healthy", "leaf miner", "red rust"],
"xception_tomato": ["healthy", "leaf blight", "leaf curl", "septoria leaf spot", "verticulium wilt"]
}
HF_MODEL_REPO = {
"xception_maize": "SaiStack/xception_maize",
"xception_cassava": "SaiStack/xception_cassava",
"xception_cashew": "SaiStack/xception_cashew",
"xception_tomato": "SaiStack/xception_tomato"
}
DISEASE_DATA = {
"xception_maize": {
"fall armyworm": {
"description": "A destructive caterpillar that feeds on maize leaves and cobs.",
"symptoms": ["Holes in leaves", "Frass (insect poop) near whorls", "Stunted growth"],
"treatment": "Apply pesticides such as Spinosad or Bacillus thuringiensis. Early detection helps.",
"prevention": "Plant early, use pest-resistant varieties, rotate crops.",
"message": "Your maize might be under attack by fall armyworms. It's best to spray a safe pesticide like Spinosad and monitor regularly. Early action saves your yield!"
},
"grasshoper": {
"description": "Insects that chew on maize leaves, reducing photosynthesis.",
"symptoms": ["Jagged leaf edges", "Visible insects on leaves"],
"treatment": "Use neem-based sprays or mechanical control.",
"prevention": "Encourage natural predators like birds; avoid overuse of fertilizers.",
"message": "Looks like grasshoppers are chewing on your maize. Spray neem oil and try attracting birds to your farm—they’re great helpers!"
},
"healthy": {
"description": "Your crop shows no signs of disease or pest infestation.",
"symptoms": [],
"treatment": "No treatment needed.",
"prevention": "Keep monitoring and maintain good farming practices.",
"message": "Great job! Your maize is looking healthy. Keep up the good work and keep checking regularly."
},
"leaf beetle": {
"description": "Beetles that skeletonize maize leaves, reducing growth.",
"symptoms": ["Holes and transparent patches on leaves"],
"treatment": "Use insecticidal soap or approved beetle pesticides.",
"prevention": "Practice crop rotation and destroy old crop debris.",
"message": "Leaf beetles may be feeding on your maize. Spray with safe insecticides and clean up leftover plant waste after harvest."
},
"leaf blight": {
"description": "Fungal disease that causes dead streaks on leaves.",
"symptoms": ["Long, greyish lesions", "Yellowing and dying leaves"],
"treatment": "Apply fungicides like Mancozeb. Ensure good air circulation.",
"prevention": "Avoid overhead watering; plant in well-spaced rows.",
"message": "Maize leaf blight detected. Spray fungicide and avoid wetting leaves during irrigation."
},
"leaf spot": {
"description": "Spots caused by fungus or bacteria, reducing photosynthesis.",
"symptoms": ["Brown or black spots with yellow halos"],
"treatment": "Spray with copper-based fungicide.",
"prevention": "Use disease-free seeds and rotate maize with legumes.",
"message": "Spots on your maize leaves suggest leaf spot. A copper fungicide should do the trick. Rotate crops to prevent re-infection."
},
"streak virus": {
"description": "A viral disease transmitted by leafhoppers.",
"symptoms": ["Yellow streaks", "Stunted plants", "Chlorotic leaves"],
"treatment": "No cure, but remove and burn infected plants.",
"prevention": "Control leafhoppers; use virus-free seeds.",
"message": "Your maize may have streak virus. Uproot and burn infected plants quickly, and spray to control leafhoppers."
}
},
"xception_cassava": {
"bacterial blight": {
"description": "A bacterial infection causing leaf wilting and stem dieback.",
"symptoms": ["Angular leaf spots", "Wilted leaves", "Stem rot"],
"treatment": "No direct cure, but pruning infected areas helps.",
"prevention": "Use disease-free cuttings; plant early.",
"message": "Cassava bacterial blight found. Cut off infected stems and plant clean cuttings next time."
},
"brown spot": {
"description": "Fungal disease causing brown lesions on leaves.",
"symptoms": ["Brown dry patches", "Defoliation in severe cases"],
"treatment": "Apply Mancozeb or other fungicides.",
"prevention": "Avoid overhead watering and weed regularly.",
"message": "Your cassava shows brown spot symptoms. Apply fungicide and keep the field well-weeded."
},
"green mite": {
"description": "Microscopic pests that feed on cassava leaves.",
"symptoms": ["Leaf curling", "Yellowing", "Stunted growth"],
"treatment": "Use neem oil or biological controls like predatory mites.",
"prevention": "Use tolerant varieties and natural predators.",
"message": "Green mites might be on your cassava. Spray neem oil and look into mite-resistant varieties."
},
"healthy": {
"description": "Your cassava is in excellent condition.",
"symptoms": [],
"treatment": "None required.",
"prevention": "Keep monitoring and weed regularly.",
"message": "Well done! Your cassava looks healthy. Just maintain regular care and check for early signs of trouble."
},
"mosaic": {
"description": "A viral disease causing leaf distortion.",
"symptoms": ["Mottled leaves", "Distorted growth"],
"treatment": "Remove and destroy infected plants.",
"prevention": "Use resistant varieties and clean planting materials.",
"message": "Cassava mosaic virus detected. Uproot affected plants and use resistant varieties for next season."
}
},
"xception_cashew": {
"anthracnose": {
"description": "Fungal disease that attacks young shoots and fruits.",
"symptoms": ["Black lesions", "Fruit drop", "Leaf spots"],
"treatment": "Use copper-based fungicides like Copper Oxychloride.",
"prevention": "Prune to increase airflow and avoid overhead irrigation.",
"message": "Your cashew trees may have anthracnose. Apply copper fungicide and prune crowded branches."
},
"gumosis": {
"description": "A physiological disorder where gum oozes from the bark.",
"symptoms": ["Gum exudation", "Cracked bark", "Yellowing leaves"],
"treatment": "Apply Bordeaux paste to the wounds.",
"prevention": "Avoid waterlogging and mechanical injuries.",
"message": "Gumosis spotted. Apply Bordeaux paste and ensure the soil drains well."
},
"healthy": {
"description": "No signs of disease or pest issues on your cashew.",
"symptoms": [],
"treatment": "None needed.",
"prevention": "Maintain clean pruning and proper watering.",
"message": "Your cashew looks healthy! Keep following good care practices and you'll be rewarded with a good harvest."
},
"leaf miner": {
"description": "Insects that burrow inside cashew leaves.",
"symptoms": ["Winding trails on leaves", "Leaf curling"],
"treatment": "Spray with Imidacloprid or neem-based pesticide.",
"prevention": "Remove affected leaves; plant resistant varieties.",
"message": "Leaf miners are feeding inside your cashew leaves. Spray with neem oil and remove damaged leaves."
},
"red rust": {
"description": "Algae-caused disease that forms reddish growth on leaves.",
"symptoms": ["Reddish-orange spots", "Reduced vigor"],
"treatment": "Spray copper fungicide or lime-sulfur solution.",
"prevention": "Avoid overcrowding and increase air circulation.",
"message": "Red rust detected on your cashew. Treat with copper fungicide and give your plants some breathing room."
}
},
"xception_tomato": {
"healthy": {
"description": "Tomatoes are growing well without any visible issues.",
"symptoms": [],
"treatment": "None needed.",
"prevention": "Maintain good practices like mulching and spacing.",
"message": "Your tomatoes are doing great! Keep watering regularly and monitor for any signs of pests or disease."
},
"leaf blight": {
"description": "Fungal disease causing rapid leaf death.",
"symptoms": ["Brown lesions", "Leaf drop", "Stem cankers"],
"treatment": "Apply chlorothalonil or copper-based fungicide.",
"prevention": "Avoid overhead watering and plant spacing.",
"message": "Leaf blight may be affecting your tomatoes. Spray fungicide and avoid splashing water on leaves."
},
"leaf curl": {
"description": "Viral disease spread by whiteflies.",
"symptoms": ["Upward curling leaves", "Stunted growth"],
"treatment": "Remove infected plants and control whiteflies.",
"prevention": "Use yellow sticky traps and virus-resistant varieties.",
"message": "Tomato leaf curl detected. Remove infected plants and trap whiteflies with yellow sticky traps."
},
"septoria leaf spot": {
"description": "Fungal disease causing circular spots on tomato leaves.",
"symptoms": ["Small brown circular spots", "Yellowing lower leaves"],
"treatment": "Use Mancozeb or chlorothalonil spray.",
"prevention": "Avoid wetting leaves; improve air circulation.",
"message": "Spots on tomato leaves? That’s septoria. Spray fungicide and ensure your plants aren’t too crowded."
},
"verticulium wilt": {
"description": "Soil-borne fungus that blocks water flow in tomatoes.",
"symptoms": ["Wilting leaves", "Yellowing", "Reduced fruit size"],
"treatment": "Remove affected plants and solarize soil.",
"prevention": "Rotate crops and avoid planting tomatoes in the same spot every year.",
"message": "Tomato wilt detected. Uproot infected plants and rotate your crops next season."
}
}
}
MODEL_CACHE = {}
def load_model(model_key):
if model_key in MODEL_CACHE:
return MODEL_CACHE[model_key]
hf_repo_id = HF_MODEL_REPO[model_key]
model_path = hf_hub_download(
repo_id=hf_repo_id,
filename=f"{model_key}.keras",
repo_type="model",
cache_dir="/tmp/hf_model_cache"
)
model = keras_load_model(model_path)
MODEL_CACHE[model_key] = model
return model
# Unified prediction logic
def predict_image(file_bytes, model, model_name):
img = Image.open(BytesIO(file_bytes)).convert("RGB")
img = img.resize((299, 299)) # Xception size
arr = np.array(img, dtype=np.float32)
arr = preprocess_input(arr)
arr = np.expand_dims(arr, axis=0)
preds = model.predict(arr)
idx = int(np.argmax(preds))
label = CLASS_NAMES[model_name][idx]
conf = float(np.max(preds))
return label, conf, DISEASE_DATA[model_name][label]
|