usama1355 commited on
Commit
773069b
·
verified ·
1 Parent(s): 69c19ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -84
app.py CHANGED
@@ -41,56 +41,19 @@ CITRUS_CLASS_NAMES = [
41
  'Yellow dragon', 'Yellow leaves'
42
  ]
43
 
44
- # Orange / Citrus Treatment Knowledge Base
45
- CITRUS_TREATMENTS = {
46
- 'Citrus canker': 'Apply copper-based bactericides. Prune and destroy infected branches immediately.',
47
- 'Citrus greening': 'No known cure. Remove and destroy infected trees to prevent spread. Control psyllid insect populations.',
48
- 'Citrus mealybugs': 'Spray with insecticidal soap or neem oil. Introduce natural predators like ladybugs.',
49
- 'Die back': 'Prune affected branches well below the dying part. Ensure proper soil drainage and avoid overwatering.',
50
- 'Foliage damaged': 'Protect plants from extreme weather. Check for pests and ensure balanced fertilization.',
51
- 'Healthy leaf': 'Your citrus plant looks completely healthy! Maintain regular watering and nutrient care.',
52
- 'Powdery mildew': 'Apply sulfur-based fungicides or neem oil. Prune to improve air circulation around the leaves.',
53
- 'Shot hole': 'Apply copper-based fungicides during the dormant season. Avoid overhead watering to keep leaves dry.',
54
- 'Spiny whitefly': 'Use horticultural oils or insecticidal soaps. Clean the leaves to remove black sooty mold.',
55
- 'Yellow dragon': 'This is another name for Citrus Greening. There is no cure; remove the tree and control insect vectors.',
56
- 'Yellow leaves': 'Check for nutrient deficiencies (like Nitrogen or Iron). Do not overwater, and ensure good soil drainage.'
57
- }
58
 
59
  # --- CORN CONFIGURATION ---
60
  CORN_MODEL_PATH = "CoRn_model.tflite"
61
  CORN_CLASS_NAMES = ['Common Rust', 'Corn Leaf Blight', 'Gray Leaf Spot', 'Healthy', 'Insects damage']
62
 
63
- # Corn Treatment Knowledge Base
64
- CORN_TREATMENTS = {
65
- 'Common Rust': 'Apply fungicides early when pustules first appear. Use rust-resistant corn hybrids for future planting.',
66
- 'Corn Leaf Blight': 'Apply appropriate fungicides. Rotate crops and manage crop residue to reduce fungal spores.',
67
- 'Gray Leaf Spot': 'Use fungicides containing azoxystrobin or pyraclostrobin. Improve air circulation and practice crop rotation.',
68
- 'Healthy': 'Your corn plant looks healthy! Ensure consistent watering and balanced nitrogen fertilization.',
69
- 'Insects damage': 'Inspect leaves for insect activity. Apply appropriate insecticides or neem oil. Remove heavily damaged leaves.'
70
- }
71
-
72
  # --- APPLE CONFIGURATION ---
73
  APPLE_MODEL_PATH = "apple_modelH.tflite"
74
  APPLE_CLASS_NAMES = ['Apple Scab', 'Black Rot', 'Cedar Apple Rust', 'Healthy']
75
 
76
- # Apple Treatment Knowledge Base
77
- APPLE_TREATMENTS = {
78
- 'Apple Scab': 'Apply fungicides like captan or myclobutanil. Rake and destroy fallen leaves to prevent fungal spores from overwintering.',
79
- 'Black Rot': 'Prune out dead or diseased wood. Remove mummified fruit from the tree and ground. Apply fungicides early in the season.',
80
- 'Cedar Apple Rust': 'Remove nearby eastern red cedar hosts if possible. Apply protective fungicides in the spring before rain events.',
81
- 'Healthy': 'Your apple tree looks perfectly healthy! Maintain good pruning practices to ensure proper airflow and sunlight.'
82
- }
83
-
84
  # --- POTATO CONFIGURATION ---
85
  POTATO_MODEL_PATH = "pot_ato_model.tflite"
86
  POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
87
 
88
- # Potato Treatment Knowledge Base
89
- POTATO_TREATMENTS = {
90
- 'Potato___Early_blight': 'Remove affected leaves. Apply fungicides like chlorothalonil or copper. Practice crop rotation next season.',
91
- 'Potato___Late_blight': 'Apply targeted fungicides immediately. Destroy heavily infected plants to prevent rapid spread to nearby crops.',
92
- 'Potato___healthy': 'Your potato plant looks healthy! Maintain consistent watering and hill up soil around the base of the plant.'
93
- }
94
 
95
  # --- TOMATO CONFIGURATION ---
96
  TOMATO_MODEL_PATH = "TOMAto_model.tflite"
@@ -108,31 +71,11 @@ TOMATO_CLASS_NAMES = [
108
  'Tomato___healthy'
109
  ]
110
 
111
- # 🏥 NEW: Tomato Treatment Knowledge Base
112
- TOMATO_TREATMENTS = {
113
- 'Tomato___Bacterial_spot': 'Apply copper-based fungicides. Avoid overhead watering to keep leaves dry.',
114
- 'Tomato___Early_blight': 'Remove affected lower leaves. Apply a fungicide containing chlorothalonil or copper.',
115
- 'Tomato___Late_blight': 'Apply fungicides specifically labeled for late blight. Destroy deeply infected plants.',
116
- 'Tomato___Leaf_Mold': 'Improve air circulation by pruning. Apply preventative fungicides.',
117
- 'Tomato___Septoria_leaf_spot': 'Remove infected leaves. Mulch around the base of the plant and apply fungicide.',
118
- 'Tomato___Spider_mites Two-spotted_spider_mite': 'Spray with insecticidal soap or neem oil. Introduce ladybugs.',
119
- 'Tomato___Target_Spot': 'Improve airflow and reduce humidity. Apply targeted fungicides.',
120
- 'Tomato___Tomato_Yellow_Leaf_Curl_Virus': 'Remove and destroy infected plants. Control whitefly populations.',
121
- 'Tomato___Tomato_mosaic_virus': 'No cure. Remove infected plants immediately. Wash hands and tools thoroughly.',
122
- 'Tomato___healthy': 'Your plant looks completely healthy! Keep up the good work with regular care.'
123
- }
124
 
125
  # --- RICE CONFIGURATION ---
126
  RICE_MODEL_PATH = "New_Rice_Model.tflite"
127
  RICE_CLASS_NAMES = ['Bacterial Leaf Blight', 'Brown Spot', 'Healthy Rice Leaf', 'Leaf Blast', 'Leaf scald', 'Narrow Brown Leaf Spot', 'Rice Hispa', 'Sheath Blight']
128
 
129
- # 🏥 NEW: Rice Treatment Knowledge Base
130
- RICE_TREATMENTS = {
131
- 'Bacterialblight': 'Apply copper-based bactericides. Avoid applying excessive nitrogen fertilizer and ensure good field drainage.',
132
- 'Blast': 'Apply fungicides like tricyclazole or azoxystrobin. Manage water levels carefully to avoid drought stress.',
133
- 'Brownspot': 'Improve soil fertility, especially with potassium and balanced nitrogen. Apply fungicides like propiconazole if severe.',
134
- 'Tungro': 'Control the green leafhopper insect vector using targeted insecticides. Immediately remove and destroy infected plants.'
135
- }
136
 
137
  # --- 2. LOAD Orange TFLITE MODEL ---
138
  citrus_interpreter = tf.lite.Interpreter(model_path=CITRUS_MODEL_PATH)
@@ -205,13 +148,11 @@ async def predict_citrus(file: UploadFile = File(...)):
205
  confidence = float(np.max(output_data[0]))
206
  predicted_class = CITRUS_CLASS_NAMES[prediction]
207
 
208
- # 🏥 NEW: Fetch the treatment advice
209
- recommended_treatment = CITRUS_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
210
 
211
  return {
212
  "class": predicted_class,
213
- "confidence": confidence,
214
- "treatment": recommended_treatment
215
  }
216
 
217
 
@@ -241,13 +182,10 @@ async def predict_corn(file: UploadFile = File(...)):
241
  confidence = float(np.max(output_data[0]))
242
  predicted_class = CORN_CLASS_NAMES[prediction]
243
 
244
- # 🏥 NEW: Fetch the treatment advice
245
- recommended_treatment = CORN_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
246
-
247
  return {
248
  "class": predicted_class,
249
- "confidence": confidence,
250
- "treatment": recommended_treatment
251
  }
252
 
253
  # --- Apple's Code ---
@@ -276,13 +214,11 @@ async def predict_apple(file: UploadFile = File(...)):
276
  confidence = float(np.max(output_data[0]))
277
  predicted_class = APPLE_CLASS_NAMES[prediction]
278
 
279
- # 🏥 NEW: Fetch the treatment advice
280
- recommended_treatment = APPLE_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
281
 
282
  return {
283
  "class": predicted_class,
284
- "confidence": confidence,
285
- "treatment": recommended_treatment
286
  }
287
 
288
  # --- Potato's Code ---
@@ -311,13 +247,11 @@ async def predict_potato(file: UploadFile = File(...)):
311
  confidence = float(np.max(output_data[0]))
312
  predicted_class = POTATO_CLASS_NAMES[prediction]
313
 
314
- # 🏥 NEW: Fetch the treatment advice
315
- recommended_treatment = POTATO_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
316
 
317
  return {
318
  "class": predicted_class,
319
- "confidence": confidence,
320
- "treatment": recommended_treatment
321
  }
322
 
323
  # --- Tomato's Code ---
@@ -337,7 +271,7 @@ async def predict_tomato(file: UploadFile = File(...)):
337
  image = image.resize((224, 224))
338
 
339
  # 2. Custom Normalization: Converting [0, 255] to [-1.0, 1.0]
340
- input_data = np.array(image).astype(np.float32)/255.0
341
  input_data = np.expand_dims(input_data, axis=0)
342
 
343
  # --- RUN INFERENCE ---
@@ -354,13 +288,10 @@ async def predict_tomato(file: UploadFile = File(...)):
354
  display_name = predicted_class.replace('Tomato___', '').replace('_', ' ')
355
 
356
 
357
- # 🏥 NEW: Fetch the treatment advice
358
- recommended_treatment = TOMATO_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
359
 
360
  return {
361
  "class": display_name,
362
- "confidence": confidence,
363
- "treatment": recommended_treatment
364
  }
365
 
366
  except Exception as e:
@@ -397,11 +328,7 @@ async def predict_rice(file: UploadFile = File(...)):
397
  # Add spaces to the class names for a cleaner app display
398
  display_name = predicted_class.replace('Bacterialblight', 'Bacterial Blight').replace('Brownspot', 'Brown Spot')
399
 
400
- # 🏥 NEW: Fetch the treatment advice
401
- recommended_treatment = RICE_TREATMENTS.get(predicted_class, "Consult a local agricultural expert for treatment.")
402
-
403
  return {
404
  "class": display_name,
405
- "confidence": confidence,
406
- "treatment": recommended_treatment
407
  }
 
41
  'Yellow dragon', 'Yellow leaves'
42
  ]
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # --- CORN CONFIGURATION ---
46
  CORN_MODEL_PATH = "CoRn_model.tflite"
47
  CORN_CLASS_NAMES = ['Common Rust', 'Corn Leaf Blight', 'Gray Leaf Spot', 'Healthy', 'Insects damage']
48
 
 
 
 
 
 
 
 
 
 
49
  # --- APPLE CONFIGURATION ---
50
  APPLE_MODEL_PATH = "apple_modelH.tflite"
51
  APPLE_CLASS_NAMES = ['Apple Scab', 'Black Rot', 'Cedar Apple Rust', 'Healthy']
52
 
 
 
 
 
 
 
 
 
53
  # --- POTATO CONFIGURATION ---
54
  POTATO_MODEL_PATH = "pot_ato_model.tflite"
55
  POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
56
 
 
 
 
 
 
 
57
 
58
  # --- TOMATO CONFIGURATION ---
59
  TOMATO_MODEL_PATH = "TOMAto_model.tflite"
 
71
  'Tomato___healthy'
72
  ]
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  # --- RICE CONFIGURATION ---
76
  RICE_MODEL_PATH = "New_Rice_Model.tflite"
77
  RICE_CLASS_NAMES = ['Bacterial Leaf Blight', 'Brown Spot', 'Healthy Rice Leaf', 'Leaf Blast', 'Leaf scald', 'Narrow Brown Leaf Spot', 'Rice Hispa', 'Sheath Blight']
78
 
 
 
 
 
 
 
 
79
 
80
  # --- 2. LOAD Orange TFLITE MODEL ---
81
  citrus_interpreter = tf.lite.Interpreter(model_path=CITRUS_MODEL_PATH)
 
148
  confidence = float(np.max(output_data[0]))
149
  predicted_class = CITRUS_CLASS_NAMES[prediction]
150
 
151
+
 
152
 
153
  return {
154
  "class": predicted_class,
155
+ "confidence": confidence
 
156
  }
157
 
158
 
 
182
  confidence = float(np.max(output_data[0]))
183
  predicted_class = CORN_CLASS_NAMES[prediction]
184
 
185
+
 
 
186
  return {
187
  "class": predicted_class,
188
+ "confidence": confidence
 
189
  }
190
 
191
  # --- Apple's Code ---
 
214
  confidence = float(np.max(output_data[0]))
215
  predicted_class = APPLE_CLASS_NAMES[prediction]
216
 
217
+
 
218
 
219
  return {
220
  "class": predicted_class,
221
+ "confidence": confidence
 
222
  }
223
 
224
  # --- Potato's Code ---
 
247
  confidence = float(np.max(output_data[0]))
248
  predicted_class = POTATO_CLASS_NAMES[prediction]
249
 
250
+
 
251
 
252
  return {
253
  "class": predicted_class,
254
+ "confidence": confidence
 
255
  }
256
 
257
  # --- Tomato's Code ---
 
271
  image = image.resize((224, 224))
272
 
273
  # 2. Custom Normalization: Converting [0, 255] to [-1.0, 1.0]
274
+ input_data = np.array(image).astype(np.float32)
275
  input_data = np.expand_dims(input_data, axis=0)
276
 
277
  # --- RUN INFERENCE ---
 
288
  display_name = predicted_class.replace('Tomato___', '').replace('_', ' ')
289
 
290
 
 
 
291
 
292
  return {
293
  "class": display_name,
294
+ "confidence": confidence
 
295
  }
296
 
297
  except Exception as e:
 
328
  # Add spaces to the class names for a cleaner app display
329
  display_name = predicted_class.replace('Bacterialblight', 'Bacterial Blight').replace('Brownspot', 'Brown Spot')
330
 
 
 
 
331
  return {
332
  "class": display_name,
333
+ "confidence": confidence
 
334
  }