File size: 2,728 Bytes
9b6aeb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np

# Load the saved model
model = tf.keras.models.load_model('banana_disease_densenet121.keras')

# Load class names
class_names = np.load("class_names.npy", allow_pickle=True)

# Preprocess the input image
def preprocess_image(img):
    if img is None:
        return None
    img = img.resize((256, 256))  # Resize to match training
    img = np.array(img) / 255.0   # Normalize
    img = np.expand_dims(img, axis=0)  # Add batch dimension
    return img

# Prediction function
def predict_disease(img):
    img = preprocess_image(img)
    if img is None:
        return "⚠️ No image provided", None
    predictions = model.predict(img)[0]
    predicted_class = np.argmax(predictions)
    confidence_scores = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
    return f"Predicted: {class_names[predicted_class]}", confidence_scores


# 🌿 Custom theme
custom_theme = gr.themes.Soft()

# UI
with gr.Blocks(theme=custom_theme, css="""

#title {

  font-size: 32px;

  font-weight: bold;

  text-align: center;

  color: white;

  animation: fadeIn 2s ease-in-out;

}

#subtitle {

  text-align: center;

  font-size: 16px;

  color: #ccc;

  animation: fadeInUp 2s ease-in-out;

}

#prediction_box {

  font-size: 18px;

  padding: 12px;

  border-radius: 8px;

  min-height: 60px;   /* ensures readable area */

  word-wrap: break-word;

  white-space: normal;

}

.gr-button {

  transition: 0.3s;

}

.gr-button:hover {

  transform: scale(1.05);

  box-shadow: 0px 4px 20px rgba(0, 128, 0, 0.4);

}

@keyframes fadeIn {

  from {opacity: 0;}

  to {opacity: 1;}

}

@keyframes fadeInUp {

  from {opacity: 0; transform: translateY(10px);}

  to {opacity: 1; transform: translateY(0);}

}

""") as demo:

    gr.Markdown("<div id='title'>Banana Leaf Disease Classifier</div>")
    gr.Markdown("<div id='subtitle'>Upload a banana leaf image, and our AI will diagnose the disease</div>")

    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(type="pil", image_mode="RGB", label="Upload Banana Leaf")
            predict_btn = gr.Button("🔍 Predict Disease", variant="primary", elem_id="predict_btn")
        
        with gr.Column(scale=1):
            # ✅ Changed from Textbox to Text
            output_label = gr.Text(label="Prediction", interactive=False, elem_id="prediction_box")
            with gr.Accordion("Confidence Scores", open=True):
                output_conf = gr.Label(label="Scores")

    predict_btn.click(fn=predict_disease, inputs=image_input, outputs=[output_label, output_conf])

# Launch app
demo.launch(share=True)