File size: 7,540 Bytes
400e6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252906f
400e6f3
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import os

# Define image dimensions
IMG_HEIGHT = 150
IMG_WIDTH = 150

# All 70 class names from the trained model
class_names = [
    'Algal Leaf Spot (Jackfruit)',
    'Anthracnose (Mango)',
    'Aphids (Cotton)',
    'Apple scab (Apple)',
    'Bacterial Blight (Cotton)',
    'Bacterial Canker (Mango)',
    'Bacterial Leaf Spot (Pumpkin)',
    'Bacterial spot (Peach)',
    'Bacterial spot (Pepper, bell)',
    'Bacterial spot (Tomato)',
    'BacterialBlights (Sugarcane)',
    'Black Rot (Cauliflower)',
    'Black Spot (Jackfruit)',
    'Black rot (Apple)',
    'Black rot (Grape)',
    'BrownSpot (Rice)',
    'Cedar apple rust (Apple)',
    'Cercospora leaf spot Gray leaf spot (Corn (maize))',
    'Common rust (Corn (maize))',
    'Cutting Weevil (Mango)',
    'Die Back (Mango)',
    'Downy Mildew (Pumpkin)',
    'Early blight (Potato)',
    'Early blight (Tomato)',
    'Esca (Black Measles) (Grape)',
    'Gall Midge (Mango)',
    'Haunglongbing (Citrus greening) (Orange)',
    'Healthy (Cauliflower)',
    'Healthy (Cotton)',
    'Healthy (Jackfruit)',
    'Healthy (Mango)',
    'Healthy (Rice)',
    'Healthy (Sugarcane)',
    'Healthy Leaf (Pumpkin)',
    'Hispa (Rice)',
    'Late blight (Potato)',
    'Late blight (Tomato)',
    'Leaf Mold (Tomato)',
    'Leaf blight (Isariopsis Leaf Spot) (Grape)',
    'Leaf scorch (Strawberry)',
    'LeafBlast (Rice)',
    'Mosaic (Sugarcane)',
    'Mosaic Disease (Pumpkin)',
    'Northern Leaf Blight (Corn (maize))',
    'Powdery Mildew (Cotton)',
    'Powdery Mildew (Mango)',
    'Powdery Mildew (Pumpkin)',
    'Powdery mildew (Cherry (including sour))',
    'RedRot (Sugarcane)',
    'Rust (Sugarcane)',
    'Septoria leaf spot (Tomato)',
    'Sooty Mould (Mango)',
    'Spider mites Two-spotted spider mite (Tomato)',
    'Target Spot (Tomato)',
    'Target spot (Cotton)',
    'Tomato Yellow Leaf Curl Virus (Tomato)',
    'Tomato mosaic virus (Tomato)',
    'Unknown Disease',
    'Yellow (Sugarcane)',
    'healthy (Apple)',
    'healthy (Blueberry)',
    'healthy (Cherry (including sour))',
    'healthy (Corn (maize))',
    'healthy (Grape)',
    'healthy (Peach)',
    'healthy (Pepper, bell)',
    'healthy (Potato)',
    'healthy (Raspberry)',
    'healthy (Soybean)',
    'healthy (Strawberry)',
    'healthy (Tomato)'
]

# Load the TensorFlow SavedModel
print("Loading model...")
print(f"Current directory: {os.getcwd()}")
print(f"Files in current directory: {os.listdir('.')}")

model = None
infer = None

try:
    # Try different possible model paths
    possible_paths = [
        './plant_disease_savemodel',
        './plant_disease_savedmodel',
        'plant_disease_savemodel',
        'plant_disease_savedmodel'
    ]
    
    model_path = None
    for path in possible_paths:
        if os.path.exists(path):
            model_path = path
            print(f"Found model at: {model_path}")
            break
    
    if model_path is None:
        raise FileNotFoundError("Model directory not found. Please ensure 'plant_disease_savemodel' folder is uploaded.")
    
    # Check if model files exist
    model_files = os.listdir(model_path)
    print(f"Files in model directory: {model_files}")
    
    # Load the model
    model = tf.saved_model.load(model_path)
    infer = model.signatures["serving_default"]
    print(f"✅ Model loaded successfully from {model_path}")
    
except Exception as e:
    print(f"❌ Error loading model: {e}")
    import traceback
    traceback.print_exc()
    model = None
    infer = None

def predict_disease(image):
    """
    Predict plant disease from an image
    
    Args:
        image: PIL Image or numpy array
        
    Returns:
        dict: Dictionary with class names as keys and confidence scores as values
        Format compatible with CropGuard mobile app
    """
    if model is None or infer is None:
        return {
            "Error": 1.0,
            "Message": "Model not loaded. Please check the model files."
        }
    
    try:
        # Convert to PIL Image if needed
        if isinstance(image, np.ndarray):
            img = Image.fromarray(image.astype('uint8'), 'RGB')
        else:
            img = image
        
        # Ensure RGB mode
        if img.mode != 'RGB':
            img = img.convert('RGB')
        
        # Resize to model input size (150x150 as per training)
        img = img.resize((IMG_WIDTH, IMG_HEIGHT))
        
        # Convert to array and normalize
        img_array = np.array(img, dtype=np.float32)
        img_array = img_array / 255.0  # Normalize to [0, 1]
        
        # Add batch dimension
        img_array = np.expand_dims(img_array, axis=0)
        
        # Make prediction
        predictions = infer(tf.constant(img_array))
        
        # Get the output tensor (try different possible keys)
        if 'output_0' in predictions:
            output = predictions['output_0'].numpy()
        elif 'dense_1' in predictions:
            output = predictions['dense_1'].numpy()
        elif 'dense' in predictions:
            output = predictions['dense'].numpy()
        else:
            # Use the first output
            output = list(predictions.values())[0].numpy()
        
        # Get predictions for all classes
        predictions_dict = {}
        for i, class_name in enumerate(class_names):
            if i < len(output[0]):
                predictions_dict[class_name] = float(output[0][i])
        
        # Log top prediction for debugging
        top_class = max(predictions_dict.items(), key=lambda x: x[1])
        print(f"Top prediction: {top_class[0]} ({top_class[1]*100:.2f}%)")
        
        # Return in format compatible with Gradio Label output
        # Gradio will automatically show top predictions
        # Mobile app expects: { "class_name": confidence, ... }
        return predictions_dict
        
    except Exception as e:
        print(f"Prediction error: {str(e)}")
        import traceback
        traceback.print_exc()
        return {
            "Error": 1.0,
            "Message": f"Prediction failed: {str(e)}"
        }

# Create Gradio interface
title = "🌱 CropGuard Tech - Plant Disease Detection"
description = """
Upload an image of a plant leaf to detect diseases using AI.

**Supported Crops:** Apple, Blueberry, Cauliflower, Cherry, Corn, Cotton, Grape, Jackfruit, Mango, Orange, Peach, Pepper, Potato, Pumpkin, Raspberry, Rice, Soybean, Strawberry, Sugarcane, Tomato

**Model Specs:**
- 70 disease classes
- 95%+ accuracy
- CNN architecture
- Trained on 10,000+ images
"""

article = """
### About CropGuard Tech
This AI model was trained on Google Colab using a comprehensive plant disease dataset from Kaggle. 
It can identify 70 different plant diseases across 19+ crop varieties.

**Model Repository:** [View on Hugging Face](https://huggingface.co/4lph4v3rs3/plant-disease-classification-model)
"""

examples = [
    # You can add example images here if you have them
]

# Create the interface
iface = gr.Interface(
    fn=predict_disease,
    inputs=gr.Image(label="Upload Plant Leaf Image"),
    outputs=gr.Label(num_top_classes=5, label="Disease Predictions"),
    title=title,
    description=description,
    article=article,
    examples=examples
)

# Launch the app
if __name__ == "__main__":
    iface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        ssr_mode=False  # Disable SSR to avoid hot reload errors
    )