predict / app.py
aniket-manhas's picture
Upload app.py
065a527 verified
import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras import models
from tensorflow.keras.applications.efficientnet import preprocess_input
# Step 1: Load the entire model (no need to manually load weights)
model = models.load_model("plant_disease_classifier.keras") # Load the saved model directly
# Step 2: Define class names in the correct order
class_names = ['Early Blight', 'Late Blight', 'Healthy'] # Ensure this matches the training order
# Step 3: Define prediction function
def predict(image: Image.Image):
image = image.convert("RGB")
image = image.resize((256, 256))
image_array = np.array(image, dtype=np.float32)
image_array = np.expand_dims(image_array, axis=0)
image_array = preprocess_input(image_array)
predictions = model.predict(image_array)
predicted_index = np.argmax(predictions)
confidence = float(predictions[0][predicted_index]) * 100
predicted_class = class_names[predicted_index]
return {predicted_class: confidence}
# Step 4: Define Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
title="Plant Disease Classifier",
description="Upload an image of a plant leaf to identify the disease."
)
# Step 5: Launch the app
if __name__ == "__main__":
interface.launch()