Dises / app.py
slowzy17's picture
Upload 8 files
13eab3f verified
Raw History Blame Contribute Delete
1.87 kB
"""
EDEN Plant Disease Detection - Gradio Interface
"""
import gradio as gr
import torch
from PIL import Image
import json
from torchvision import transforms
from modeling import load_model
# Load model and labels
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model, labels_data = load_model(".", device)
idx_to_disease = labels_data["idx_to_disease"]
# Define preprocessing
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
def predict_disease(image):
"""Predict plant disease from image"""
if image is None:
return {}
# Preprocess
img_tensor = transform(image).unsqueeze(0).to(device)
# Inference
with torch.no_grad():
outputs = model(img_tensor)
probabilities = torch.softmax(outputs, dim=1)[0]
# Get top 5 predictions
top5_prob, top5_idx = torch.topk(probabilities, 5)
results = {}
for prob, idx in zip(top5_prob, top5_idx):
disease = idx_to_disease[str(idx.item())]
results[disease] = prob.item()
return results
# Create Gradio interface
title = " EDEN Plant Disease Detection"
description = """
Detect plant diseases using AI-powered image analysis.
Upload an image of a plant leaf to identify potential diseases.
**Model**: Mob-Res (MobileNetV2 + ResNet hybrid)
**Accuracy**: 99.4%
**Diseases**: Multiple common plant diseases
"""
demo = gr.Interface(
fn=predict_disease,
inputs=gr.Image(type="pil", label="Upload Plant Image"),
outputs=gr.Label(num_top_classes=5, label="Disease Predictions"),
title=title,
description=description,
examples=None
)
if __name__ == "__main__":
demo.launch()