codeblock / app.py
Aarzoo-Singh2206's picture
Update app.py
f533d30 verified
import gradio as gr
import tensorflow as tf
from tensorflow.keras.applications.efficientnet import preprocess_input
from tensorflow.keras.preprocessing import image
import numpy as np
# Load trained model
model = tf.keras.models.load_model("efficientnet_final_model.keras")
# Class labels
CLASS_NAMES = [
"Pomegranate__diseased", "mango_Sooty Mould", "mango_Powdery Mildew",
"mango_Healthy", "mango_Gall Midge", "mango_Die Back",
"mango_Cutting Weevil", "mango_Bacterial Canker", "mango_Anthracnose",
"guava_Healthy", "guava_Red Rust", "guava_Sooty Mould",
"guava_Algal Leaf Spot", "guava_Rust", "lime_Greening",
"lime_Canker", "lime_Healthy", "lime_Die Back",
"lime_Scab", "lime_Anthracnose", "lime_Sooty Mould"
]
# Predict function
def predict_disease(img):
print("๐Ÿ–ผ๏ธ Image received")
img = img.resize((160, 160))
img_array = image.img_to_array(img)
img_array = preprocess_input(img_array)
img_array = np.expand_dims(img_array, axis=0)
print(f"๐Ÿ“Š Model input shape: {img_array.shape}")
prediction = model.predict(img_array)[0]
print(f"๐Ÿ”ฎ Raw prediction: {prediction}")
top_idx = np.argmax(prediction)
confidence = prediction[top_idx] * 100
label = CLASS_NAMES[top_idx]
result = f"{label} ({confidence:.2f}%)"
print(f"โœ… Final Result: {result}")
return result
# Gradio interface
interface = gr.Interface(
fn=predict_disease,
inputs=gr.Image(type="pil"),
outputs="text",
title="๐ŸŒฟ Fruit Leaf Disease Classifier",
description="Upload a fruit or leaf image to predict its disease type.",
examples=[
["Phytopthora.jpg"],
["RedRust.jpg"]
],
cache_examples=False,
allow_flagging="never"
)
# Launch with share link
if __name__ == "__main__":
interface.launch(share=True)