Samdutse commited on
Commit
562ce13
Β·
verified Β·
1 Parent(s): 0ed0b51

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import tensorflow as tf
4
+ import numpy as np
5
+ import json
6
+ from PIL import Image
7
+
8
+ # Load model and class mapping
9
+ model = tf.keras.models.load_model("cropguard_model.h5")
10
+
11
+ with open("class_indices.json", "r") as f:
12
+ class_indices = json.load(f)
13
+
14
+ idx_to_class = {v: k for k, v in class_indices.items()}
15
+
16
+ # Human-readable labels and treatment tips
17
+ disease_info = {
18
+ "Pepper__bell___Bacterial_spot": {
19
+ "name": "Bell Pepper β€” Bacterial Spot",
20
+ "tip": "Remove infected leaves, avoid overhead watering, apply copper-based bactericide."
21
+ },
22
+ "Pepper__bell___healthy": {
23
+ "name": "Bell Pepper β€” Healthy",
24
+ "tip": "No action needed. Maintain good watering and spacing practices."
25
+ },
26
+ "Potato___Early_blight": {
27
+ "name": "Potato β€” Early Blight",
28
+ "tip": "Remove affected foliage, rotate crops yearly, apply fungicide if severe."
29
+ },
30
+ "Potato___Late_blight": {
31
+ "name": "Potato β€” Late Blight",
32
+ "tip": "Highly destructive β€” remove infected plants immediately, apply fungicide, avoid wet foliage."
33
+ },
34
+ "Potato___healthy": {
35
+ "name": "Potato β€” Healthy",
36
+ "tip": "No action needed. Continue regular monitoring."
37
+ },
38
+ "Tomato_Bacterial_spot": {
39
+ "name": "Tomato β€” Bacterial Spot",
40
+ "tip": "Avoid overhead watering, remove infected leaves, apply copper-based spray."
41
+ },
42
+ "Tomato_Early_blight": {
43
+ "name": "Tomato β€” Early Blight",
44
+ "tip": "Remove lower infected leaves, mulch soil, apply fungicide preventatively."
45
+ },
46
+ "Tomato_Late_blight": {
47
+ "name": "Tomato β€” Late Blight",
48
+ "tip": "Act fast β€” highly contagious. Remove and destroy infected plants, apply fungicide."
49
+ },
50
+ "Tomato_Leaf_Mold": {
51
+ "name": "Tomato β€” Leaf Mold",
52
+ "tip": "Improve air circulation, reduce humidity, apply fungicide if persistent."
53
+ },
54
+ "Tomato_Septoria_leaf_spot": {
55
+ "name": "Tomato β€” Septoria Leaf Spot",
56
+ "tip": "Remove infected lower leaves, avoid wetting foliage, rotate crops."
57
+ },
58
+ "Tomato_Spider_mites_Two_spotted_spider_mite": {
59
+ "name": "Tomato β€” Spider Mites",
60
+ "tip": "Spray with insecticidal soap or neem oil, increase humidity around plants."
61
+ },
62
+ "Tomato__Target_Spot": {
63
+ "name": "Tomato β€” Target Spot",
64
+ "tip": "Remove infected debris, apply fungicide, improve air circulation."
65
+ },
66
+ "Tomato__Tomato_YellowLeaf__Curl_Virus": {
67
+ "name": "Tomato β€” Yellow Leaf Curl Virus",
68
+ "tip": "No cure β€” remove infected plants to prevent spread, control whiteflies (the vector)."
69
+ },
70
+ "Tomato__Tomato_mosaic_virus": {
71
+ "name": "Tomato β€” Mosaic Virus",
72
+ "tip": "No cure β€” remove and destroy infected plants, disinfect tools between use."
73
+ },
74
+ "Tomato_healthy": {
75
+ "name": "Tomato β€” Healthy",
76
+ "tip": "No action needed. Continue regular care and monitoring."
77
+ },
78
+ }
79
+
80
+ def predict_disease(img):
81
+ if img is None:
82
+ return "Please upload a leaf image.", ""
83
+
84
+ img = img.resize((224, 224))
85
+ img_array = np.array(img) / 255.0
86
+ img_array = np.expand_dims(img_array, axis=0)
87
+
88
+ preds = model.predict(img_array, verbose=0)[0]
89
+ pred_idx = np.argmax(preds)
90
+ confidence = round(float(preds[pred_idx]) * 100, 1)
91
+
92
+ raw_label = idx_to_class[pred_idx]
93
+ info = disease_info.get(raw_label, {"name": raw_label, "tip": "No info available."})
94
+
95
+ result = f"{info['name']} ({confidence}% confidence)"
96
+ tip = f"πŸ’‘ Recommendation: {info['tip']}"
97
+
98
+ # Top 3 predictions
99
+ top3_idx = np.argsort(preds)[::-1][:3]
100
+ breakdown = "Top 3 predictions:\n"
101
+ for idx in top3_idx:
102
+ lbl = disease_info.get(idx_to_class[idx], {"name": idx_to_class[idx]})["name"]
103
+ pct = round(float(preds[idx]) * 100, 1)
104
+ breakdown += f" β€’ {lbl}: {pct}%\n"
105
+
106
+ return result, tip, breakdown
107
+
108
+ with gr.Blocks(title="CropGuard") as demo:
109
+ gr.Markdown("""
110
+ # 🌱 CropGuard β€” Crop Disease Detector
111
+ ### Upload a photo of a Tomato, Potato, or Bell Pepper leaf to detect disease
112
+ *Built by Samuel Yaula Dutse | MobileNetV2 Transfer Learning | 93% Accuracy*
113
+ """)
114
+
115
+ with gr.Row():
116
+ with gr.Column():
117
+ image_input = gr.Image(type="pil", label="Upload Leaf Image")
118
+ submit_btn = gr.Button("Diagnose", variant="primary")
119
+
120
+ with gr.Column():
121
+ result_output = gr.Textbox(label="Diagnosis", interactive=False)
122
+ tip_output = gr.Textbox(label="Recommendation", interactive=False)
123
+ breakdown_output = gr.Textbox(label="Confidence Breakdown", lines=5, interactive=False)
124
+
125
+ submit_btn.click(
126
+ fn=predict_disease,
127
+ inputs=[image_input],
128
+ outputs=[result_output, tip_output, breakdown_output]
129
+ )
130
+
131
+ demo.launch()