| |
|
|
| import gradio as gr |
| from transformers import pipeline |
| from PIL import Image |
|
|
| |
| |
| |
| |
| classifier = pipeline( |
| "image-classification", |
| model="nateraw/vit-base-beans" |
| ) |
|
|
| |
| |
| |
| treatment_suggestions = { |
| "angular_leaf_spot": "Use fungicide sprays and avoid overhead watering.", |
| "bean_rust": "Apply sulfur-based fungicides and remove infected leaves.", |
| "healthy": "Your plant looks healthy. Maintain proper watering and sunlight.", |
| } |
|
|
| |
| |
| |
| def predict_disease(image): |
| if image is None: |
| return "Please upload an image.", "", "" |
|
|
| |
| image = Image.fromarray(image) |
|
|
| |
| results = classifier(image) |
|
|
| |
| top_result = results[0] |
| label = top_result["label"] |
| confidence = round(top_result["score"] * 100, 2) |
|
|
| |
| treatment = treatment_suggestions.get( |
| label.lower(), |
| "No specific treatment found. Consult an agricultural expert." |
| ) |
|
|
| return label, f"{confidence}%", treatment |
|
|
| |
| |
| |
| with gr.Blocks(title="πΏ Crop Disease Detection") as app: |
|
|
| gr.Markdown("# πΏ Crop Disease Detection from Leaf Images") |
| gr.Markdown("Upload a leaf image to detect disease and get treatment suggestions.") |
|
|
| with gr.Row(): |
| image_input = gr.Image(type="numpy", label="Upload Leaf Image") |
|
|
| with gr.Row(): |
| predict_button = gr.Button("π Predict") |
|
|
| with gr.Row(): |
| label_output = gr.Textbox(label="Predicted Disease") |
| confidence_output = gr.Textbox(label="Confidence Score") |
| treatment_output = gr.Textbox(label="Suggested Treatment") |
|
|
| |
| predict_button.click( |
| fn=predict_disease, |
| inputs=image_input, |
| outputs=[label_output, confidence_output, treatment_output] |
| ) |
|
|
| |
| |
| |
| if _name_ == "_main_": |
| app.launch() |