# med-el-ghazoui/plantcare-ai architecture

> Architecture diagram of the GitHub repository med-el-ghazoui/plantcare-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/med-el-ghazoui/plantcare-ai
- Repository: https://github.com/med-el-ghazoui/plantcare-ai

## Overview

AgriScan AI is a preliminary plant-disease screening product: an image reaches the API, is validated and classified, and the caller receives a prediction or uncertainty result. Authenticated users may have analysis metadata saved; a separate endpoint returns an annotated image. The repository also contains model training and evaluation, including external field-validation tooling. The README documents the intended mobile flow, but the sampled mobile files are platform shells; field evaluation is supported by the source index and excerpts, not a full inspection of its subsystem.

Main entry points: [`backend/app/main.py`](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/main.py) (FastAPI routes), [`backend/app/auth.py`](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/auth.py) (Account auth), [`backend/app/exports.py`](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/exports.py) (Annotated export), [`ml/agriscan_ml/field.py`](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/field.py) (Field evaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["API and inference"]
  node_api["FastAPI routes<br/>[main.py]"]
  node_validation["Image validation"]
  node_inference["Model inference<br/>[inference.py]"]
  node_decision["Confidence decision<br/>[decision.py]"]
  node_export["Annotated export<br/>[exports.py]"]
  node_watermark["Image watermark<br/>[watermark.py]"]
end

subgraph group_accounts["Accounts and history"]
  node_auth["Account auth<br/>[auth.py]"]
  node_security["Credential security<br/>[security.py]"]
  node_throttle["Login throttling<br/>[throttle.py]"]
  node_history["Analysis history<br/>[history.py]"]
  node_db["Database access<br/>[db.py]"]
  node_records[("User and analysis records<br/>[models.py]")]
end

subgraph group_ml["Model development"]
  node_data["Dataset pipeline<br/>[data.py]"]
  node_model_build["CNN architectures<br/>[models.py]"]
  node_train["Model training<br/>[train_mobilenet.py]"]
  node_metrics["Classification metrics<br/>[metrics.py]"]
  node_selective["Selective prediction<br/>[selective.py]"]
  node_evaluate["Model evaluation<br/>[evaluate.py]"]
end

subgraph group_field["Field validation"]
  node_field_manifest["Field manifest checks"]
  node_field_eval["Field evaluation<br/>[field.py]"]
  node_external_eval["External evaluation"]
end

node_user(("Image user"))
node_mobile["Mobile shell<br/>[MainActivity.kt]"]

node_user -->|"uploads image"| node_api
node_user -.->|"uses client"| node_mobile
node_mobile -.->|"sends image"| node_api
node_api -->|"decodes image"| node_validation
node_api -->|"runs prediction"| node_inference
node_api -->|"applies threshold"| node_decision
node_api -->|"checks credentials"| node_auth
node_api -->|"stores analysis"| node_db
node_api -->|"registers routes"| node_export
node_export -->|"decodes image"| node_validation
node_export -->|"runs prediction"| node_inference
node_export -->|"applies threshold"| node_decision
node_export -->|"composes output"| node_watermark
node_auth -->|"verifies credentials"| node_security
node_auth -->|"checks login limits"| node_throttle
node_auth -->|"loads user"| node_db
node_auth -->|"creates user"| node_records
node_history -->|"queries analyses"| node_records
node_history -->|"requires identity"| node_auth
node_train -->|"loads batches"| node_data
node_train -->|"builds classifier"| node_model_build
node_evaluate -->|"loads split"| node_data
node_evaluate -->|"reports scores"| node_metrics
node_external_eval -->|"validates manifest"| node_field_manifest
node_external_eval -->|"evaluates predictions"| node_field_eval
node_field_manifest -->|"checks protocol"| node_field_eval
node_field_eval -->|"computes metrics"| node_metrics
node_field_eval -->|"uses threshold metrics"| node_selective

click node_mobile "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/mobile/android/app/src/main/kotlin/ma/agriscan/agriscan_mobile/MainActivity.kt"
click node_api "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/main.py"
click node_validation "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/image_validation.py"
click node_inference "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/inference.py"
click node_decision "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/decision.py"
click node_export "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/exports.py"
click node_watermark "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/watermark.py"
click node_auth "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/auth.py"
click node_security "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/security.py"
click node_throttle "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/throttle.py"
click node_history "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/history.py"
click node_db "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/db.py"
click node_records "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/models.py"
click node_data "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/data.py"
click node_model_build "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/models.py"
click node_train "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/train_mobilenet.py"
click node_metrics "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/metrics.py"
click node_selective "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/selective.py"
click node_evaluate "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/evaluate.py"
click node_field_manifest "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/validate_field_manifest.py"
click node_field_eval "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/field.py"
click node_external_eval "https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/evaluate_external.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_validation,node_inference,node_decision,node_export,node_watermark,node_user toneBlue
class node_auth,node_security,node_throttle,node_history,node_db,node_records toneAmber
class node_data,node_model_build,node_train,node_metrics,node_selective,node_evaluate toneMint
class node_field_manifest,node_field_eval,node_external_eval toneRose
class node_mobile toneTeal
```

## Components

### API and inference

- [FastAPI routes](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/main.py): `backend/app/main.py`
- [Image validation](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/image_validation.py): `backend/app/image_validation.py`
- [Model inference](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/inference.py): `backend/app/inference.py`
- [Confidence decision](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/decision.py): `backend/app/decision.py`
- [Annotated export](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/exports.py): `backend/app/exports.py`
- [Image watermark](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/watermark.py): `backend/app/watermark.py`

### Accounts and history

- [Account auth](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/auth.py): `backend/app/auth.py`
- [Credential security](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/security.py): `backend/app/security.py`
- [Login throttling](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/throttle.py): `backend/app/throttle.py`
- [Analysis history](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/history.py): `backend/app/history.py`
- [Database access](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/db.py): `backend/app/db.py`
- [User and analysis records](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/backend/app/models.py): `backend/app/models.py`

### Model development

- [Dataset pipeline](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/data.py): `ml/agriscan_ml/data.py`
- [CNN architectures](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/models.py): `ml/agriscan_ml/models.py`
- [Model training](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/train_mobilenet.py): `ml/scripts/train_mobilenet.py`
- [Classification metrics](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/metrics.py): `ml/agriscan_ml/metrics.py`
- [Selective prediction](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/selective.py): `ml/agriscan_ml/selective.py`
- [Model evaluation](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/evaluate.py): `ml/scripts/evaluate.py`

### Field validation

- [Field manifest checks](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/validate_field_manifest.py): `ml/scripts/validate_field_manifest.py`
- [Field evaluation](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/agriscan_ml/field.py): `ml/agriscan_ml/field.py`
- [External evaluation](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/ml/scripts/evaluate_external.py): `ml/scripts/evaluate_external.py`

### Other components

- **Image user**
- [Mobile shell](https://github.com/med-el-ghazoui/plantcare-ai/blob/main/mobile/android/app/src/main/kotlin/ma/agriscan/agriscan_mobile/MainActivity.kt): `mobile/android/app/src/main/kotlin/ma/agriscan/agriscan_mobile/MainActivity.kt`

## Connections

- Image user → FastAPI routes: uploads image (evidence: `README.md`)
- Image user → Mobile shell: uses client (evidence: `README.md`)
- Mobile shell → FastAPI routes: sends image (evidence: `README.md`)
- FastAPI routes → Image validation: decodes image (evidence: `backend/app/main.py`)
- FastAPI routes → Model inference: runs prediction (evidence: `backend/app/main.py`)
- FastAPI routes → Confidence decision: applies threshold (evidence: `backend/app/main.py`)
- FastAPI routes → Account auth: checks credentials (evidence: `backend/app/main.py`)
- FastAPI routes → Database access: stores analysis (evidence: `backend/app/main.py`)
- FastAPI routes → Annotated export: registers routes (evidence: `backend/app/main.py`)
- Annotated export → Image validation: decodes image (evidence: `backend/app/exports.py`)
- Annotated export → Model inference: runs prediction (evidence: `backend/app/exports.py`)
- Annotated export → Confidence decision: applies threshold (evidence: `backend/app/exports.py`)
- Annotated export → Image watermark: composes output (evidence: `backend/app/exports.py`)
- Account auth → Credential security: verifies credentials (evidence: `backend/app/auth.py`)
- Account auth → Login throttling: checks login limits (evidence: `backend/app/auth.py`)
- Account auth → Database access: loads user (evidence: `backend/app/auth.py`)
- Account auth → User and analysis records: creates user (evidence: `backend/app/auth.py`)
- Analysis history → User and analysis records: queries analyses (evidence: `backend/app/history.py`)
- Analysis history → Account auth: requires identity (evidence: `backend/app/history.py`)
- Model training → Dataset pipeline: loads batches (evidence: `ml/scripts/train_mobilenet.py`)
- Model training → CNN architectures: builds classifier (evidence: `ml/scripts/train_mobilenet.py`)
- Model evaluation → Dataset pipeline: loads split (evidence: `ml/scripts/evaluate.py`)
- Model evaluation → Classification metrics: reports scores (evidence: `ml/scripts/evaluate.py`)
- External evaluation → Field manifest checks: validates manifest (evidence: `ml/scripts/evaluate_external.py`)
- External evaluation → Field evaluation: evaluates predictions (evidence: `ml/scripts/evaluate_external.py`)
- Field manifest checks → Field evaluation: checks protocol (evidence: `ml/scripts/validate_field_manifest.py`)
- Field evaluation → Classification metrics: computes metrics (evidence: `ml/agriscan_ml/field.py`)
- Field evaluation → Selective prediction: uses threshold metrics (evidence: `ml/agriscan_ml/field.py`)
