omunaman's picture
Update app.py
3566f12 verified
# -*- coding: utf-8 -*-
"""gradio.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1B520JUHmyofueyUqotN2yj6Gad69uavo
"""
import gradio as gr
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import models, layers
input_shape = (256, 256, 3)
def create_model():
model = models.Sequential([
layers.Input(shape=(256, 256, 3)), # Define input shape here
layers.Resizing(256, 256), # Resize images
layers.Rescaling(1.0/255), # Rescale pixel values
layers.RandomFlip('horizontal_and_vertical'),
layers.RandomRotation(0.2),
layers.Conv2D(32, (3, 3), activation='relu'), # Remove input_shape here
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(3, activation='softmax')
])
return model
model = create_model()
model.load_weights('my_model_weights1.weights.h5')
def predict_disease(image):
"""
Preprocesses the image, performs prediction, and returns the predicted class with confidence score.
"""
img = tf.image.resize(image, [256, 256])
img = tf.expand_dims(img, axis=0) # Add batch dimension
# Predict probabilities
prediction = model.predict(img)[0] # Extract first element (batch of size 1)
labels = ['Early Blight', 'Late Blight', 'Healthy']
# Create a dictionary of class probabilities
class_probs = {label: float(prob) for label, prob in zip(labels, prediction)}
return class_probs
# ... (Rest of your code) ...
iface = gr.Interface(
fn=predict_disease,
inputs=gr.Image(), # Allow image editing
outputs=gr.Label(num_top_classes=3), # Display all three probabilities
title="Potato Disease Classifier 🍟πŸ₯”",
description=(
"Upload an image of a potato leaf, and I'll classify it as one of the following:\n"
"- **Early Blight** 🌱\n"
"- **Late Blight** 🌧️\n"
"- **Healthy** πŸ’š\n\n"
"Check out the probability bars for more details!"
),
)
iface.launch(share=True)