Leaf_Care / app.py
V8055's picture
Update app.py
3efdae4 verified
import gradio as gr
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
from PIL import Image
import torch
# Load model and feature extractor
model_name = "yusuf802/Leaf-Disease-Predictor"
extractor = AutoFeatureExtractor.from_pretrained(model_name)
model = AutoModelForImageClassification.from_pretrained(model_name)
# Class map (replace this with full mapping if needed)
class_map = {
"0": "Apple_Black_rot",
"1": "Apple_healthy",
"33": "Tomato_Late_blight",
"43": "Wheat_nitrogen_deficiency"
}
# Prediction function
def predict_leaf_disease(image):
image = image.convert("RGB")
inputs = extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=1)
pred_class = torch.argmax(probabilities, dim=1).item()
confidence = round(probabilities[0][pred_class].item() * 100, 2)
result = class_map.get(str(pred_class), "Unknown Class")
return f"{result} ({confidence}%)"
# Gradio Interface
demo = gr.Interface(
fn=predict_leaf_disease,
inputs=gr.Image(type="pil"),
outputs="text",
title="🌿 LeafCare - Plant Disease Predictor",
description="Upload a leaf image to detect plant diseases using AI/ML."
)
if __name__ == "__main__":
demo.launch()