# khnychenkoav/plantdiseasedetector architecture

> Architecture diagram of the GitHub repository khnychenkoav/plantdiseasedetector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/khnychenkoav/plantdiseasedetector
- Repository: https://github.com/khnychenkoav/plantdiseasedetector

## Overview

##### Project Type and Purpose
- This is a full-stack web application with an integrated machine-learning inference component.
- Purpose: allow users to upload plant images, detect diseases via ML, render results and garden maps, and store data.

##### File Structure
- Top-level directories:
- frontend/ – React app (UI components, webpack).
- backend/ – FastAPI (or Django) service (routes, models, schemas).
- ml_model/ – Training and inference code; ONNX model.
- docker/ – Dockerfiles and Compose orchestration.
- docs/ – API and architecture specs.
- Notice MVC-style separation in backend (models, schemas, routes).
- Docker Compose defines at least three containers: frontend, backend, and likely a Postgres DB (plus MinIO/S3).

##### README for Stack and Runtime Insights
- Frontend runs on Node.js at port 3000.
- Backend (FastAPI) runs on Python at port 8000.
- ML inference uses the ONNX model (inference/predict.py).
- Data persists in PostgreSQL.
- Images stored in S3/MinIO.
- All services orchestrated via Docker Compose.

##### Main Components
a. Frontend UI (React)
b. API Layer (Backend FastAPI)
c. ML Inference Module
d. Database (PostgreSQL)
e. Object Storage (S3 or MinIO)
f. (Optional) External Camera or Cron‐job for Automated Uploads
g. Docker Compose / Container Boundary

##### Relationships & Data Flow
- User → Frontend: uploads image via UploadForm.js
- Frontend → Backend (/image_analysis endpoint): HTTP POST
- Backend → ML Inference Module: function call or internal HTTP/gRPC to predict.py
- Backend → Database: read/write users, gardens, disease records
- Backend → Object Storage: save raw images, fetch URLs
- Backend → Frontend: JSON response containing disease predictions & recommendations
- Frontend → Backend (/recommendations & /garden endpoints) for additional data
- (Optional) External Camera → Backend: via a scheduled job or API endpoint

##### Architectural Patterns
- Containerized microservices via Docker Compose.
- Separation of concerns: UI, API, ML, Data stores.
- Model‐View‐Controller style in backend (models/schemas/routes).
- Asynchronous tasks can be added (e.g., Celery) for heavy ML jobs.

##### Relevant Technologies & Frameworks
- React (frontend), Webpack
- FastAPI (Python backend), Pydantic schemas
- ONNX Runtime (inference)
- PostgreSQL
- S3/MinIO for object storage
- Docker & docker-compose

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Actors
    User["User"]:::external
    Camera["External Camera/Cron Job"]:::external

    subgraph "Docker Compose"
        direction TB

        subgraph "Frontend Container (port 3000)"
            direction TB
            App["App.js"]:::ui
            UploadForm["UploadForm.js"]:::ui
            ResultView["ResultView.js"]:::ui
            GardenMap["GardenMap.js"]:::ui
            Package["package.json"]:::ui
            Webpack["webpack.config.js"]:::ui
        end

        subgraph "Backend Container (port 8000)"
            direction TB
            Main["main.py"]:::api
            Config["config.py"]:::api
            DBConn["database.py"]:::api

            subgraph "Routes"
                direction TB
                ImageAnalysis["image_analysis.py (POST /image_analysis)"]:::api
                Recommendations["recommendations.py (GET /recommendations)"]:::api
                GardenRoute["garden.py (GET/POST /garden)"]:::api
            end

            subgraph "Models"
                direction TB
                DiseaseModel["disease_model.py"]:::api
                GardenModel["garden.py"]:::api
                UserModel["user.py"]:::api
            end

            subgraph "Schemas"
                direction TB
                DiseaseSchema["disease.py"]:::api
                GardenSchema["garden.py"]:::api
                UserSchema["user.py"]:::api
            end

            subgraph "ML Inference Module"
                direction TB
                Predict["predict.py"]:::ml
                ModelExport["model_export.py"]:::ml
            end
        end

        subgraph "Database Container"
            direction TB
            Postgres[(PostgreSQL)]:::data
        end

        subgraph "Storage Container"
            direction TB
            MinIO[(MinIO/S3)]:::data
        end
    end

    %% Flows
    User -->|uses UI| App
    Camera -->|scheduled upload| ImageAnalysis

    App -->|"POST /image_analysis"| ImageAnalysis
    App -->|"GET /recommendations"| Recommendations
    App -->|"GET /garden"| GardenRoute

    ImageAnalysis -->|calls inference| Predict
    Predict -->|uses ONNX model| ModelExport

    ImageAnalysis -->|read/write| Postgres
    ImageAnalysis -->|store/fetch images| MinIO
    ImageAnalysis -->|responds with JSON| App

    Recommendations -->|read data| Postgres
    Recommendations -->|responds| App

    GardenRoute -->|read/write| Postgres
    GardenRoute -->|responds| App

    %% Click Events
    click App "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/src/App.js"
    click UploadForm "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/src/components/UploadForm.js"
    click ResultView "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/src/components/ResultView.js"
    click GardenMap "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/src/components/GardenMap.js"
    click Package "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/package.json"
    click Webpack "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/frontend/webpack.config.js"

    click Main "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/main.py"
    click Config "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/config.py"
    click DBConn "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/database.py"

    click ImageAnalysis "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/routes/image_analysis.py"
    click Recommendations "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/routes/recommendations.py"
    click GardenRoute "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/routes/garden.py"

    click DiseaseModel "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/models/disease_model.py"
    click GardenModel "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/models/garden.py"
    click UserModel "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/models/user.py"

    click DiseaseSchema "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/schemas/disease.py"
    click GardenSchema "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/schemas/garden.py"
    click UserSchema "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/backend/app/schemas/user.py"

    click Predict "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/ml_model/inference/predict.py"
    click ModelExport "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/ml_model/inference/model_export.py"

    click dockerCompose "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docker/docker-compose.yml"
    click DockerfileFrontend "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docker/Dockerfile.frontend"
    click DockerfileBackend "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docker/Dockerfile.backend"

    click APISpec "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docs/api_specification.md"
    click ArchDoc "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docs/architecture.md"
    click DocsReadme "https://github.com/khnychenkoav/plantdiseasedetector/blob/main/docs/README.md"

    %% Styles
    classDef ui fill:#D6EAF8,stroke:#21618C,color:#1B4F72
    classDef api fill:#D5F5E3,stroke:#196F3D,color:#145A32
    classDef data fill:#FAD7A0,stroke:#B9770E,color:#7E5109
    classDef ml fill:#E8DAEF,stroke:#6C3483,color:#512E5F
    classDef external fill:#E5E7E9,stroke:#566573,color:#2C3E50
```
