MHamzaShahid commited on
Commit
4034477
·
verified ·
1 Parent(s): 42c9b4b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+
6
+ # Load your pre-trained model (upload the .h5 file to the repo)
7
+ model = tf.keras.models.load_model('best_plant_model.h5')
8
+
9
+ # Full 38 class names from the dataset
10
+ CLASS_NAMES = [
11
+ "Apple___Apple_scab",
12
+ "Apple___Black_rot",
13
+ "Apple___Cedar_apple_rust",
14
+ "Apple___healthy",
15
+ "Blueberry___healthy",
16
+ "Cherry_(including_sour)___Powdery_mildew",
17
+ "Cherry_(including_sour)___healthy",
18
+ "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
19
+ "Corn_(maize)___Common_rust_",
20
+ "Corn_(maize)___Northern_Leaf_Blight",
21
+ "Corn_(maize)___healthy",
22
+ "Grape___Black_rot",
23
+ "Grape___Esca_(Black_Measles)",
24
+ "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)",
25
+ "Grape___healthy",
26
+ "Orange___Haunglongbing_(Citrus_greening)",
27
+ "Peach___Bacterial_spot",
28
+ "Peach___healthy",
29
+ "Pepper,_bell___Bacterial_spot",
30
+ "Pepper,_bell___healthy",
31
+ "Potato___Early_blight",
32
+ "Potato___Late_blight",
33
+ "Potato___healthy",
34
+ "Raspberry___healthy",
35
+ "Soybean___healthy",
36
+ "Squash___Powdery_mildew",
37
+ "Strawberry___Leaf_scorch",
38
+ "Strawberry___healthy",
39
+ "Tomato___Bacterial_spot",
40
+ "Tomato___Early_blight",
41
+ "Tomato___Late_blight",
42
+ "Tomato___Leaf_Mold",
43
+ "Tomato___Septoria_leaf_spot",
44
+ "Tomato___Spider_mites Two-spotted_spider_mite",
45
+ "Tomato___Target_Spot",
46
+ "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
47
+ "Tomato___Tomato_mosaic_virus",
48
+ "Tomato___healthy"
49
+ ]
50
+
51
+ # Prediction function
52
+ def predict_disease(image):
53
+ try:
54
+ # Preprocess: resize to 224x224, RGB, normalize to [0,1]
55
+ img = image.convert('RGB')
56
+ img = img.resize((224, 224))
57
+ img_array = np.array(img) / 255.0
58
+ img_array = np.expand_dims(img_array, axis=0)
59
+ # Predict
60
+ prediction = model.predict(img_array)
61
+ disease_class_idx = np.argmax(prediction[0])
62
+ confidence = prediction[0][disease_class_idx]
63
+ disease_class = CLASS_NAMES[disease_class_idx]
64
+ return {
65
+ "predicted_disease": disease_class,
66
+ "confidence": float(confidence)
67
+ }
68
+ except Exception as e:
69
+ return {"error": str(e)}
70
+
71
+ # Gradio interface
72
+ with gr.Blocks(title="Plant Disease Detector") as demo:
73
+ gr.Markdown("# 🌿 Plant Disease Detector")
74
+ gr.Markdown("Upload a plant leaf image to detect diseases across 14 crops (38 classes).")
75
+
76
+ with gr.Row():
77
+ with gr.Column():
78
+ image_input = gr.Image(type="pil", label="Upload Leaf Image")
79
+ predict_btn = gr.Button("Detect Disease", variant="primary")
80
+
81
+ with gr.Column():
82
+ output = gr.JSON(label="Prediction Result")
83
+
84
+ predict_btn.click(
85
+ fn=predict_disease,
86
+ inputs=image_input,
87
+ outputs=output
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch(server_name="0.0.0.0", server_port=7860)