PlantGuard

PlantGuard AI — AI Vision Model & Cloud Diagnostics

PlantGuard AI uses Google Gemini Vision AI hosted securely on the Express backend, replacing the legacy client-side SmolVLM model. This eliminates client-side memory exhaustion, mobile overheating, and heavy multi-hundred-megabyte model downloads while delivering enterprise-grade botanical diagnostics.


1. Architectural Motivation

Why We Migrated from Client-Side SmolVLM to Gemini

Feature Legacy Client-Side SmolVLM Server-Side Google Gemini Vision
Download Size ~180MB – 350MB (ONNX weights) 0 MB (no client model download)
Device Load High RAM (>1.5 GB), CPU/GPU throttling, battery drain Zero client inference overhead
Inference Latency 8 – 25 seconds on mobile devices 1.2 – 2.0 seconds over HTTPS
Diagnostic Accuracy General-purpose VLM (frequent hallucination of plant diseases) State-of-the-art vision reasoning with deep botanical taxonomy
Offline Fallback Required full model in browser cache Graceful offline queueing with Dexie IndexedDB sync

2. Gemini Vision Integration

2.1 Model Selection & Configuration

2.2 Security & Key Protection


3. Prompt Engineering & Structured Output

3.1 System Prompt

The model is instructed to act as an expert botanist and plant pathologist:

You are an expert plant pathologist and botanist AI assistant for PlantGuard AI.
Analyze the provided plant or leaf photograph. Identify the plant species and assess its health.
Provide structured, evidence-based botanical diagnostic observations.
If the image does not show a plant or leaf, indicate this clearly.
Always return your assessment strictly in valid JSON matching the requested schema.

3.2 Zod Validation Schema

Responses from Gemini are parsed and strictly validated using Zod (server/src/modules/ai/ai.schemas.ts):

export const PlantAnalysisResultSchema = z.object({
  plantName: z.string().min(1),
  identifiedSpecies: z.string().optional(),
  condition: z.enum(['healthy', 'attention', 'critical']),
  confidenceScore: z.number().min(0).max(1),
  summary: z.string().min(5),
  symptoms: z.array(z.string()).default([]),
  possibleCauses: z.array(z.string()).default([]),
  recommendations: z.array(z.string()).min(1),
  urgency: z.enum(['low', 'medium', 'high', 'immediate']).default('low'),
});

4. Rate Limiting & Resource Safeguards

To prevent API abuse and control Google Cloud costs:


5. Error Handling & Offline Strategy

5.1 Error Scenarios

5.2 Offline Resiliency

When the user’s device is disconnected from the internet:

  1. The client-side PWA detects offline state via navigator.onLine.
  2. The user can capture photographs and save notes locally in Dexie IndexedDB.
  3. Once internet connectivity is restored, the client outbox queues the observation for AI analysis and server synchronization.
  4. For automated testing and offline demos, the client includes DemoVisionProvider (client/src/services/ai/demoProvider.ts).