Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model = load_model("plant_disease_model.h5") # You must include this file in your repo
|
| 9 |
+
IMG_SIZE = (224, 224)
|
| 10 |
+
class_names = ['Apple___Black_rot', 'Tomato___Early_blight', 'Potato___Late_blight'] # Update this to match your model
|
| 11 |
+
|
| 12 |
+
# Prediction function
|
| 13 |
+
def predict_plant_disease(img):
|
| 14 |
+
img = img.resize(IMG_SIZE)
|
| 15 |
+
img_array = image.img_to_array(img) / 255.0
|
| 16 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 17 |
+
|
| 18 |
+
predictions = model.predict(img_array)
|
| 19 |
+
index = np.argmax(predictions)
|
| 20 |
+
confidence = float(predictions[0][index])
|
| 21 |
+
|
| 22 |
+
disease_name = class_names[index]
|
| 23 |
+
confidence_text = f"{confidence:.2%}"
|
| 24 |
+
confidence_value = round(confidence, 2)
|
| 25 |
+
|
| 26 |
+
return disease_name, confidence_value, confidence_text
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
with gr.Blocks(css=".green-btn button {background-color: #2e7d32 !important; color: white;}") as demo:
|
| 30 |
+
gr.Markdown("<h1 style='text-align:center;'>🌿 Smart Plant Disease Detector</h1>")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column(scale=1):
|
| 34 |
+
image_input = gr.Image(type="pil", label="📷 Upload Leaf Image")
|
| 35 |
+
predict_btn = gr.Button("🔍 Detect Disease", elem_classes="green-btn")
|
| 36 |
+
|
| 37 |
+
with gr.Column(scale=1):
|
| 38 |
+
disease_output = gr.Textbox(label="🪴 Detected Disease")
|
| 39 |
+
confidence_bar = gr.Slider(label="📊 Confidence Level", minimum=0, maximum=1, step=0.01, interactive=False)
|
| 40 |
+
confidence_text = gr.Textbox(label="🔢 Confidence (Text)")
|
| 41 |
+
|
| 42 |
+
predict_btn.click(fn=predict_plant_disease,
|
| 43 |
+
inputs=image_input,
|
| 44 |
+
outputs=[disease_output, confidence_bar, confidence_text])
|
| 45 |
+
|
| 46 |
+
demo.launch()
|