File size: 2,183 Bytes
dc9f5c9
 
 
 
 
 
 
eb63daf
 
 
dc9f5c9
 
eb63daf
dc9f5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0191a19
 
dc9f5c9
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)