import gradio as gr import tensorflow as tf import numpy as np from tensorflow.keras.models import load_model # Load your model with the new path model_path = 'best_model.keras' model = load_model(model_path) # Define the image size your model expects IMG_SIZE = (224, 224) # Define class names class_names = [ 'Corn___Common_Rust', 'Corn___Gray_Leaf_Spot', 'Corn___Healthy', 'Corn___Northern_Leaf_Blight', 'Corn___Northern_Leaf_Spot', 'Corn___Phaeosphaeria_Leaf_Spot' ] # Define prediction function def predict(image): img_array = tf.image.resize(image, IMG_SIZE) img_array = tf.expand_dims(img_array, axis=0) / 255.0 predictions = model.predict(img_array) predicted_class = np.argmax(predictions[0]) confidence = np.max(predictions[0]) if confidence <= 0.5: return "Unknown Object" else: return {class_names[predicted_class]: float(confidence)} # Create Gradio interface interface = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs=gr.Label(num_top_classes=6), title="Maize Leaf Disease Detection", description="Upload an image of a maize leaf to classify its disease." ) # Launch the interface if __name__ == "__main__": interface.launch()