Spaces:
Running
Running
| """ | |
| Leaf Disease Detection Tab | |
| =========================== | |
| UI component for analyzing individual leaves. | |
| """ | |
| import gradio as gr | |
| from src.utils.leaf_classifier import predict as classify_image | |
| def analyze_leaf(image): | |
| """ | |
| Analyze a leaf image to detect diseases. | |
| Args: | |
| image: PIL.Image from gr.Image component | |
| Returns: | |
| str: Result formatted as Markdown | |
| """ | |
| if image is None: | |
| return "β οΈ Please upload an image of a leaf." | |
| # Call classifier | |
| result = classify_image(image) | |
| # Handle error | |
| if not result["success"]: | |
| return f"β Error: {result['error']}" | |
| # Format result as Markdown | |
| emoji = "β " if result["is_healthy"] else "β οΈ" | |
| status = "πΏ Healthy Plant" if result["is_healthy"] else "π¦ Disease Detected" | |
| output = f""" | |
| ## π¬ Analysis Result | |
| ### Main Diagnosis | |
| - **Prediction:** {emoji} {result["prediction"]} | |
| - **Confidence:** {result["confidence"]}% | |
| - **Status:** {status} | |
| ### Details | |
| - **Plant:** {result["plant"]} | |
| - **Condition:** {result["disease"]} | |
| ### Other Possibilities | |
| """ | |
| # Add top-k alternatives (skip first one, it's the main prediction) | |
| for i, alt in enumerate(result["top_k"][1:], start=2): | |
| output += f"{i}. {alt['plant']} - {alt['disease']} ({alt['confidence']}%)\n" | |
| return output | |
| def create_leaf_tab(): | |
| """Create the leaf disease detection tab.""" | |
| with gr.Tab("π Leaves Disease Detection"): | |
| gr.Markdown("Analyze an individual leaf to detect diseases.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| leaf_image = gr.Image( | |
| label="π· Upload a photo of the leaf", | |
| type="pil", | |
| height=300 | |
| ) | |
| leaf_btn = gr.Button("π Analyze", variant="primary") | |
| with gr.Column(): | |
| leaf_output = gr.Markdown(label="Result") | |
| # Example images | |
| gr.Markdown("### πΈ Try with example images:") | |
| gr.Examples( | |
| examples=[ | |
| ["src/ui/pictures/leaves/185161-004-EAF28842.jpg"], | |
| ["src/ui/pictures/leaves/healthy.jpg"] | |
| ], | |
| inputs=[leaf_image], | |
| label="Example Leaves" | |
| ) | |
| # Connect button | |
| leaf_btn.click( | |
| fn=analyze_leaf, | |
| inputs=[leaf_image], | |
| outputs=[leaf_output] | |
| ) | |