from tensorflow.keras.models import load_model from PIL import Image import numpy as np # ✅ Define your class names in the exact training order class_names = [ "Pepper__bell___Bacterial_spot", "Pepper__bell___healthy", "Potato___healthy", "Potato___Early_blight" ] # ✅ Load your trained model # Make sure this file is in the same folder as model.py model = load_model("final_plantvillage_model.keras") def predict(image: Image.Image): """ Predict the class of a given PIL image. """ image = image.resize((224, 224)) img_array = np.array(image) / 255.0 img_array = np.expand_dims(img_array, axis=0) predictions = model.predict(img_array) predicted_class = class_names[np.argmax(predictions)] confidence = float(np.max(predictions)) return {predicted_class: confidence}