Spaces:
Sleeping
Sleeping
| """ | |
| Farm Disease Detection Tab | |
| =========================== | |
| UI component for analyzing entire crops or fields. | |
| """ | |
| import gradio as gr | |
| from src.utils.farm_classifier import analyze_farm_image | |
| def analyze_farm(image): | |
| """ | |
| Analyze a farm/crop image using Gemini Vision. | |
| Provides overall health assessment, disease detection, | |
| and recommendations for the entire crop/field. | |
| """ | |
| if image is None: | |
| return "β οΈ Please upload an image of your crop." | |
| # Call Gemini analyzer | |
| result = analyze_farm_image(image) | |
| # Handle error | |
| if not result["success"]: | |
| return f"β Error: {result['error']}" | |
| # Format result as Markdown | |
| health = result.get("health_status", "Unknown") | |
| health_emoji = { | |
| "Healthy": "β ", | |
| "Diseased": "π¦ ", | |
| "Stressed": "β οΈ", | |
| "Unknown": "β" | |
| }.get(health, "β") | |
| severity = result.get("overall_severity", "Unknown") | |
| severity_emoji = { | |
| "None": "π’", | |
| "Low": "π‘", | |
| "Medium": "π ", | |
| "High": "π΄", | |
| "Critical": "β" | |
| }.get(severity, "βͺ") | |
| output = f""" | |
| ## πΎ Farm Analysis Result | |
| ### Overview | |
| - **Crop Identified:** {result.get("crop_identified", "Unknown")} | |
| - **Health Status:** {health_emoji} {health} | |
| - **Confidence:** {result.get("confidence", "N/A")}% | |
| - **Overall Severity:** {severity_emoji} {severity} | |
| ### Issues Detected | |
| """ | |
| # Add issues | |
| issues = result.get("issues_detected", []) | |
| if issues: | |
| for issue in issues: | |
| sev = issue.get("severity", "Unknown") | |
| output += f"- **{issue.get('name', 'Unknown')}** - Severity: {sev}" | |
| if issue.get("affected_area"): | |
| output += f" - Affected: {issue['affected_area']}" | |
| output += "\n" | |
| else: | |
| output += "- No issues detected\n" | |
| output += "\n### π‘ Recommendations\n" | |
| # Add recommendations | |
| recommendations = result.get("recommendations", []) | |
| if recommendations: | |
| for i, rec in enumerate(recommendations, start=1): | |
| output += f"{i}. {rec}\n" | |
| else: | |
| output += "- No specific recommendations\n" | |
| # Add observations if present | |
| observations = result.get("observations", "") | |
| if observations: | |
| output += f"\n### π Additional Observations\n{observations}\n" | |
| return output | |
| def create_farm_tab(): | |
| """Create the farm disease detection tab.""" | |
| with gr.Tab("πΎ Farm Disease Detection"): | |
| gr.Markdown("Analyze your entire crop or field.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| farm_image = gr.Image( | |
| label="π· Upload a photo of the crop", | |
| type="pil", | |
| height=300 | |
| ) | |
| farm_btn = gr.Button("π Analyze", variant="primary") | |
| with gr.Column(): | |
| farm_output = gr.Markdown(label="Result") | |
| # Example images | |
| gr.Markdown("### πΈ Try with example images:") | |
| gr.Examples( | |
| examples=[ | |
| ["src/ui/pictures/farms/PED-Tudor-Borza-1068x580.png"], | |
| ["src/ui/pictures/farms/early-die-august-2020.png"] | |
| ], | |
| inputs=[farm_image], | |
| label="Example Farms" | |
| ) | |
| # Connect button | |
| farm_btn.click( | |
| fn=analyze_farm, | |
| inputs=[farm_image], | |
| outputs=[farm_output] | |
| ) | |