CZerion's picture
Update app.py
eb63daf verified
raw
history blame
2.18 kB
import gradio as gr
from transformers import pipeline
from PIL import Image
# Initialize the plant disease classification pipeline
# You can replace the model with any fine-tuned plant disease model hosted on Hugging Face
plant_disease_classifier = pipeline(
task="image-classification",
model="wambugu71/crop_leaf_diseases_vit",
top_k=3
)
def diagnose_plant_health(image: Image.Image):
"""
Takes a PIL Image of a plant leaf and returns:
- Top predicted disease label
- Confidence score
- Care advice based on the label
"""
# Run the image through the classification pipeline
results = plant_disease_classifier(image)
# Format top-3 predictions
predictions = []
for res in results:
label = res['label']
score = res['score']
predictions.append(f"{label} ({score*100:.1f}%)")
# Determine advice based on the top prediction
top_label = results[0]['label'].lower()
if "healthy" in top_label:
advice = "Your plant looks healthy! Maintain regular watering and adequate sunlight."
else:
advice = (
f"Detected symptom: {results[0]['label']}. "
"Consider the following care steps:\n"
"1. Isolate the plant to prevent spread.\n"
"2. Prune affected areas with sterilized tools.\n"
"3. Apply an appropriate fungicide or treatment."
)
return "\n".join(predictions), advice
# Building the Gradio interface
iface = gr.Interface(
fn=diagnose_plant_health,
inputs=gr.Image(type="pil", label="Upload Plant Leaf Image"),
outputs=[
gr.Textbox(label="Predicted Diseases (Top 3)"),
gr.Textbox(label="Care Advice")
],
title="Home Plant Health Monitor",
description=(
"Upload a photo of your plant's leaf to diagnose diseases and receive care recommendations. "
"This app uses a fine-tuned image-classification model on common plant diseases."
),
examples=[
["Unhealthy_plant_1.jpg"],
["Healthy_plant_1.jpg"]
],
allow_flagging="never"
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)