Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdded the Treatment Code
app.py
CHANGED
|
@@ -31,18 +31,56 @@ CITRUS_CLASS_NAMES = [
|
|
| 31 |
'Yellow dragon', 'Yellow leaves'
|
| 32 |
]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# --- CORN CONFIGURATION ---
|
| 35 |
CORN_MODEL_PATH = "CornModel.tflite"
|
| 36 |
CORN_CLASS_NAMES = ['Blight', 'Common_Rust', 'Gray_Leaf_Spot', 'Healthy']
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# --- APPLE CONFIGURATION ---
|
| 39 |
APPLE_MODEL_PATH = "apple_modelH.tflite"
|
| 40 |
APPLE_CLASS_NAMES = ['Apple Scab', 'Black Rot', 'Cedar Apple Rust', 'Healthy']
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# --- POTATO CONFIGURATION ---
|
| 43 |
POTATO_MODEL_PATH = "potato_model.tflite"
|
| 44 |
POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# --- TOMATO CONFIGURATION ---
|
| 47 |
TOMATO_MODEL_PATH = "TomatoModelH.tflite"
|
| 48 |
|
|
@@ -54,10 +92,32 @@ TOMATO_CLASS_NAMES = [
|
|
| 54 |
'Tomato___healthy'
|
| 55 |
]
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# --- RICE CONFIGURATION ---
|
| 58 |
RICE_MODEL_PATH = "RiceModel.tflite"
|
| 59 |
RICE_CLASS_NAMES = ['Bacterialblight', 'Blast', 'Brownspot', 'Tungro']
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# --- 2. LOAD Orange TFLITE MODEL ---
|
| 62 |
citrus_interpreter = tf.lite.Interpreter(model_path=CITRUS_MODEL_PATH)
|
| 63 |
citrus_interpreter.allocate_tensors()
|
|
@@ -135,6 +195,15 @@ async def predict_citrus(file: UploadFile = File(...)):
|
|
| 135 |
"confidence": confidence
|
| 136 |
}
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# --- Corn's Code ---
|
| 140 |
@app.post("/predict_corn")
|
|
@@ -162,9 +231,13 @@ async def predict_corn(file: UploadFile = File(...)):
|
|
| 162 |
confidence = float(np.max(output_data[0]))
|
| 163 |
predicted_class = CORN_CLASS_NAMES[prediction]
|
| 164 |
|
|
|
|
|
|
|
|
|
|
| 165 |
return {
|
| 166 |
"class": predicted_class,
|
| 167 |
-
"confidence": confidence
|
|
|
|
| 168 |
}
|
| 169 |
|
| 170 |
# --- Apple's Code ---
|
|
@@ -193,9 +266,13 @@ async def predict_apple(file: UploadFile = File(...)):
|
|
| 193 |
confidence = float(np.max(output_data[0]))
|
| 194 |
predicted_class = APPLE_CLASS_NAMES[prediction]
|
| 195 |
|
|
|
|
|
|
|
|
|
|
| 196 |
return {
|
| 197 |
"class": predicted_class,
|
| 198 |
-
"confidence": confidence
|
|
|
|
| 199 |
}
|
| 200 |
|
| 201 |
# --- Potato's Code ---
|
|
@@ -224,10 +301,14 @@ async def predict_potato(file: UploadFile = File(...)):
|
|
| 224 |
confidence = float(np.max(output_data[0]))
|
| 225 |
predicted_class = POTATO_CLASS_NAMES[prediction]
|
| 226 |
|
|
|
|
|
|
|
|
|
|
| 227 |
return {
|
| 228 |
"class": predicted_class,
|
| 229 |
-
"confidence": confidence
|
| 230 |
-
|
|
|
|
| 231 |
|
| 232 |
# --- Tomato's Code ---
|
| 233 |
@app.post("/predict_tomato")
|
|
@@ -277,9 +358,16 @@ async def predict_tomato(file: UploadFile = File(...)):
|
|
| 277 |
# Clean up the class name for the app response (e.g., "Tomato___Early_blight" -> "Early blight")
|
| 278 |
display_name = predicted_class.replace('Tomato___', '').replace('_', ' ')
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
return {
|
| 281 |
"class": display_name,
|
| 282 |
-
"confidence": confidence
|
|
|
|
| 283 |
}
|
| 284 |
|
| 285 |
except Exception as e:
|
|
@@ -316,7 +404,14 @@ async def predict_rice(file: UploadFile = File(...)):
|
|
| 316 |
# Add spaces to the class names for a cleaner app display
|
| 317 |
display_name = predicted_class.replace('Bacterialblight', 'Bacterial Blight').replace('Brownspot', 'Brown Spot')
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
return {
|
| 320 |
"class": display_name,
|
| 321 |
-
"confidence": confidence
|
| 322 |
-
|
|
|
|
|
|
| 31 |
'Yellow dragon', 'Yellow leaves'
|
| 32 |
]
|
| 33 |
|
| 34 |
+
# Orange / Citrus Treatment Knowledge Base
|
| 35 |
+
CITRUS_TREATMENTS = {
|
| 36 |
+
'Citrus canker': 'Apply copper-based bactericides. Prune and destroy infected branches immediately.',
|
| 37 |
+
'Citrus greening': 'No known cure. Remove and destroy infected trees to prevent spread. Control psyllid insect populations.',
|
| 38 |
+
'Citrus mealybugs': 'Spray with insecticidal soap or neem oil. Introduce natural predators like ladybugs.',
|
| 39 |
+
'Die back': 'Prune affected branches well below the dying part. Ensure proper soil drainage and avoid overwatering.',
|
| 40 |
+
'Foliage damaged': 'Protect plants from extreme weather. Check for pests and ensure balanced fertilization.',
|
| 41 |
+
'Healthy leaf': 'Your citrus plant looks completely healthy! Maintain regular watering and nutrient care.',
|
| 42 |
+
'Powdery mildew': 'Apply sulfur-based fungicides or neem oil. Prune to improve air circulation around the leaves.',
|
| 43 |
+
'Shot hole': 'Apply copper-based fungicides during the dormant season. Avoid overhead watering to keep leaves dry.',
|
| 44 |
+
'Spiny whitefly': 'Use horticultural oils or insecticidal soaps. Clean the leaves to remove black sooty mold.',
|
| 45 |
+
'Yellow dragon': 'This is another name for Citrus Greening. There is no cure; remove the tree and control insect vectors.',
|
| 46 |
+
'Yellow leaves': 'Check for nutrient deficiencies (like Nitrogen or Iron). Do not overwater, and ensure good soil drainage.'
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
# --- CORN CONFIGURATION ---
|
| 50 |
CORN_MODEL_PATH = "CornModel.tflite"
|
| 51 |
CORN_CLASS_NAMES = ['Blight', 'Common_Rust', 'Gray_Leaf_Spot', 'Healthy']
|
| 52 |
|
| 53 |
+
# Corn Treatment Knowledge Base
|
| 54 |
+
CORN_TREATMENTS = {
|
| 55 |
+
'Blight': 'Apply appropriate fungicides. Rotate crops and manage crop residue to reduce fungal spores.',
|
| 56 |
+
'Common_Rust': 'Apply fungicides early when pustules first appear. Use rust-resistant corn hybrids for future planting.',
|
| 57 |
+
'Gray_Leaf_Spot': 'Use fungicides containing azoxystrobin or pyraclostrobin. Improve air circulation and practice crop rotation.',
|
| 58 |
+
'Healthy': 'Your corn plant looks healthy! Ensure consistent watering and balanced nitrogen fertilization.'
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
# --- APPLE CONFIGURATION ---
|
| 62 |
APPLE_MODEL_PATH = "apple_modelH.tflite"
|
| 63 |
APPLE_CLASS_NAMES = ['Apple Scab', 'Black Rot', 'Cedar Apple Rust', 'Healthy']
|
| 64 |
|
| 65 |
+
# Apple Treatment Knowledge Base
|
| 66 |
+
APPLE_TREATMENTS = {
|
| 67 |
+
'Apple Scab': 'Apply fungicides like captan or myclobutanil. Rake and destroy fallen leaves to prevent fungal spores from overwintering.',
|
| 68 |
+
'Black Rot': 'Prune out dead or diseased wood. Remove mummified fruit from the tree and ground. Apply fungicides early in the season.',
|
| 69 |
+
'Cedar Apple Rust': 'Remove nearby eastern red cedar hosts if possible. Apply protective fungicides in the spring before rain events.',
|
| 70 |
+
'Healthy': 'Your apple tree looks perfectly healthy! Maintain good pruning practices to ensure proper airflow and sunlight.'
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
# --- POTATO CONFIGURATION ---
|
| 74 |
POTATO_MODEL_PATH = "potato_model.tflite"
|
| 75 |
POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
|
| 76 |
|
| 77 |
+
# Potato Treatment Knowledge Base
|
| 78 |
+
POTATO_TREATMENTS = {
|
| 79 |
+
'Potato___Early_blight': 'Remove affected leaves. Apply fungicides like chlorothalonil or copper. Practice crop rotation next season.',
|
| 80 |
+
'Potato___Late_blight': 'Apply targeted fungicides immediately. Destroy heavily infected plants to prevent rapid spread to nearby crops.',
|
| 81 |
+
'Potato___healthy': 'Your potato plant looks healthy! Maintain consistent watering and hill up soil around the base of the plant.'
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
# --- TOMATO CONFIGURATION ---
|
| 85 |
TOMATO_MODEL_PATH = "TomatoModelH.tflite"
|
| 86 |
|
|
|
|
| 92 |
'Tomato___healthy'
|
| 93 |
]
|
| 94 |
|
| 95 |
+
# π₯ NEW: Tomato Treatment Knowledge Base
|
| 96 |
+
TOMATO_TREATMENTS = {
|
| 97 |
+
'Tomato___Bacterial_spot': 'Apply copper-based fungicides. Avoid overhead watering to keep leaves dry.',
|
| 98 |
+
'Tomato___Early_blight': 'Remove affected lower leaves. Apply a fungicide containing chlorothalonil or copper.',
|
| 99 |
+
'Tomato___Late_blight': 'Apply fungicides specifically labeled for late blight. Destroy deeply infected plants.',
|
| 100 |
+
'Tomato___Leaf_Mold': 'Improve air circulation by pruning. Apply preventative fungicides.',
|
| 101 |
+
'Tomato___Septoria_leaf_spot': 'Remove infected leaves. Mulch around the base of the plant and apply fungicide.',
|
| 102 |
+
'Tomato___Spider_mites Two-spotted_spider_mite': 'Spray with insecticidal soap or neem oil. Introduce ladybugs.',
|
| 103 |
+
'Tomato___Target_Spot': 'Improve airflow and reduce humidity. Apply targeted fungicides.',
|
| 104 |
+
'Tomato___Tomato_Yellow_Leaf_Curl_Virus': 'Remove and destroy infected plants. Control whitefly populations.',
|
| 105 |
+
'Tomato___Tomato_mosaic_virus': 'No cure. Remove infected plants immediately. Wash hands and tools thoroughly.',
|
| 106 |
+
'Tomato___healthy': 'Your plant looks completely healthy! Keep up the good work with regular care.'
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
# --- RICE CONFIGURATION ---
|
| 110 |
RICE_MODEL_PATH = "RiceModel.tflite"
|
| 111 |
RICE_CLASS_NAMES = ['Bacterialblight', 'Blast', 'Brownspot', 'Tungro']
|
| 112 |
|
| 113 |
+
# π₯ NEW: Rice Treatment Knowledge Base
|
| 114 |
+
RICE_TREATMENTS = {
|
| 115 |
+
'Bacterialblight': 'Apply copper-based bactericides. Avoid applying excessive nitrogen fertilizer and ensure good field drainage.',
|
| 116 |
+
'Blast': 'Apply fungicides like tricyclazole or azoxystrobin. Manage water levels carefully to avoid drought stress.',
|
| 117 |
+
'Brownspot': 'Improve soil fertility, especially with potassium and balanced nitrogen. Apply fungicides like propiconazole if severe.',
|
| 118 |
+
'Tungro': 'Control the green leafhopper insect vector using targeted insecticides. Immediately remove and destroy infected plants.'
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
# --- 2. LOAD Orange TFLITE MODEL ---
|
| 122 |
citrus_interpreter = tf.lite.Interpreter(model_path=CITRUS_MODEL_PATH)
|
| 123 |
citrus_interpreter.allocate_tensors()
|
|
|
|
| 195 |
"confidence": confidence
|
| 196 |
}
|
| 197 |
|
| 198 |
+
# π₯ NEW: Fetch the treatment advice
|
| 199 |
+
recommended_treatment = CITRUS_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 200 |
+
|
| 201 |
+
return {
|
| 202 |
+
"class": predicted_class,
|
| 203 |
+
"confidence": confidence,
|
| 204 |
+
"treatment": recommended_treatment
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
|
| 208 |
# --- Corn's Code ---
|
| 209 |
@app.post("/predict_corn")
|
|
|
|
| 231 |
confidence = float(np.max(output_data[0]))
|
| 232 |
predicted_class = CORN_CLASS_NAMES[prediction]
|
| 233 |
|
| 234 |
+
# π₯ NEW: Fetch the treatment advice
|
| 235 |
+
recommended_treatment = CORN_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 236 |
+
|
| 237 |
return {
|
| 238 |
"class": predicted_class,
|
| 239 |
+
"confidence": confidence,
|
| 240 |
+
"treatment": recommended_treatment
|
| 241 |
}
|
| 242 |
|
| 243 |
# --- Apple's Code ---
|
|
|
|
| 266 |
confidence = float(np.max(output_data[0]))
|
| 267 |
predicted_class = APPLE_CLASS_NAMES[prediction]
|
| 268 |
|
| 269 |
+
# π₯ NEW: Fetch the treatment advice
|
| 270 |
+
recommended_treatment = APPLE_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 271 |
+
|
| 272 |
return {
|
| 273 |
"class": predicted_class,
|
| 274 |
+
"confidence": confidence,
|
| 275 |
+
"treatment": recommended_treatment
|
| 276 |
}
|
| 277 |
|
| 278 |
# --- Potato's Code ---
|
|
|
|
| 301 |
confidence = float(np.max(output_data[0]))
|
| 302 |
predicted_class = POTATO_CLASS_NAMES[prediction]
|
| 303 |
|
| 304 |
+
# π₯ NEW: Fetch the treatment advice
|
| 305 |
+
recommended_treatment = POTATO_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 306 |
+
|
| 307 |
return {
|
| 308 |
"class": predicted_class,
|
| 309 |
+
"confidence": confidence,
|
| 310 |
+
"treatment": recommended_treatment
|
| 311 |
+
}
|
| 312 |
|
| 313 |
# --- Tomato's Code ---
|
| 314 |
@app.post("/predict_tomato")
|
|
|
|
| 358 |
# Clean up the class name for the app response (e.g., "Tomato___Early_blight" -> "Early blight")
|
| 359 |
display_name = predicted_class.replace('Tomato___', '').replace('_', ' ')
|
| 360 |
|
| 361 |
+
# Clean up the class name for the app response
|
| 362 |
+
display_name = predicted_class.replace('Tomato___', '').replace('_', ' ')
|
| 363 |
+
|
| 364 |
+
# π₯ NEW: Fetch the treatment advice
|
| 365 |
+
recommended_treatment = TOMATO_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 366 |
+
|
| 367 |
return {
|
| 368 |
"class": display_name,
|
| 369 |
+
"confidence": confidence,
|
| 370 |
+
"treatment": recommended_treatment
|
| 371 |
}
|
| 372 |
|
| 373 |
except Exception as e:
|
|
|
|
| 404 |
# Add spaces to the class names for a cleaner app display
|
| 405 |
display_name = predicted_class.replace('Bacterialblight', 'Bacterial Blight').replace('Brownspot', 'Brown Spot')
|
| 406 |
|
| 407 |
+
# Add spaces to the class names for a cleaner app display
|
| 408 |
+
display_name = predicted_class.replace('Bacterialblight', 'Bacterial Blight').replace('Brownspot', 'Brown Spot')
|
| 409 |
+
|
| 410 |
+
# π₯ NEW: Fetch the treatment advice
|
| 411 |
+
recommended_treatment = RICE_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
|
| 412 |
+
|
| 413 |
return {
|
| 414 |
"class": display_name,
|
| 415 |
+
"confidence": confidence,
|
| 416 |
+
"treatment": recommended_treatment
|
| 417 |
+
}
|