# badie16/phytovigil architecture

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

- Interactive diagram: https://gitdiagram.com/badie16/phytovigil
- Repository: https://github.com/badie16/phytovigil

## Overview

##### Project type and purpose
- PhytoVigil is a full-stack plant-disease-detection solution comprising three main user-facing clients (mobile, web admin) and a backend API with an embedded AI service.
- Key use cases: farmers snap leaf photos on mobile, images are sent to the backend for CNN inference, results and natural-treatment recommendations are returned; administrators manage diseases, plants, users via a Next.js admin UI.

##### File structure
- Top level:
- backend/ – FastAPI monolith (app/core, crud, routes, services, ml, models, schemas)
- mobile/  – React Native (Expo) application (app screens, components, hooks, contexts, services for local/remote storage)
- (README.md points to a web/ folder for Next.js admin UI, not shown in file tree; assume similar structure under web/)
- backend/app/core: config, security → app bootstrap and JWT setup
- backend/app/crud + models + schemas → ORM layer (SQLAlchemy + Pydantic)
- backend/app/routes → API endpoints (auth, ml, plants, scans, diseases, users, files)
- backend/app/services → domain logic (file uploads, ML processing)
- backend/app/ml → model artifacts (H5 file), preprocessor, loader, prediction service
- mobile/app & mobile/components & mobile/services → UI screens, reusable UI primitives, offline (SQLite + AsyncStorage) and remote services for API calls

##### README for additional insights
- Confirms technology stack: FastAPI, PostgreSQL, TensorFlow, JWT on backend; React Native + AsyncStorage on mobile; Next.js + Tailwind CSS for admin web; Gemini API for extra info.
- Highlights feature set: secure auth with roles, local/cloud sync, AI-driven disease detection, admin management console.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Mobile Client
    subgraph "Mobile Client" 
        direction TB
        MC_UI["UI Screens & Components"]:::frontend
        MC_LocalDB["Local DB (SQLite & AsyncStorage)"]:::db
        MC_Sync["Sync Service"]:::frontend
    end

    %% Web Admin Client
    WA["Web Admin Client (Next.js)"]:::frontend

    %% Backend API
    subgraph "Backend API (FastAPI)" 
        direction TB
        API_Main["main.py"]:::backend
        subgraph "Core"
            direction TB
            Core_Config["config.py"]:::backend
            Core_Security["security.py"]:::backend
        end
        subgraph "Database Layer"
            direction TB
            DB_Layer["database.py & schema.sql"]:::backend
        end
        subgraph "ORM (CRUD)"
            direction TB
            CRUD_Disease["crud/disease.py"]:::backend
            CRUD_Plant["crud/plant.py"]:::backend
            CRUD_Scan["crud/scan.py"]:::backend
            CRUD_User["crud/user.py"]:::backend
        end
        subgraph "Models & Schemas"
            direction TB
            Models["models/*.py"]:::backend
            Schemas["schemas/*.py"]:::backend
        end
        subgraph "Routes"
            direction TB
            R_Auth["routes/auth.py"]:::backend
            R_ML["routes/ml.py"]:::backend
            R_Plants["routes/plants.py"]:::backend
            R_Diseases["routes/diseases.py"]:::backend
            R_Scans["routes/scans.py"]:::backend
            R_User["routes/user.py"]:::backend
            R_Files["routes/files.py"]:::backend
        end
        subgraph "Services"
            direction TB
            S_File["file_service.py"]:::backend
            S_ML["ml_service.py"]:::backend
        end
    end

    %% Embedded ML Service
    subgraph "ML Service" 
        direction TB
        ML_Preproc["image_preprocessor.py"]:::ml
        ML_Loader["model_loader.py"]:::ml
        ML_Predict["prediction_service.py"]:::ml
        ML_Model["plant_disease_model.h5"]:::ml
        ML_Classes["class_names.json"]:::ml
    end

    %% Databases
    PG["PostgreSQL"]:::db
    GI["Gemini API"]:::external

    %% Relationships
    MC_UI -->|"HTTP Requests"| API_Main
    MC_Sync -->|"API Sync"| API_Main
    MC_UI -->|"Reads/Writes"| MC_LocalDB
    MC_Sync -->|"Checks Network"| MC_LocalDB

    WA -->|"HTTP Requests"| API_Main

    API_Main -->|"Uses"| Core_Config
    API_Main -->|"Uses"| Core_Security
    API_Main -->|"Connects To"| DB_Layer
    API_Main -->|"CRUD Ops"| CRUD_Disease
    API_Main -->|"CRUD Ops"| CRUD_Plant
    API_Main -->|"CRUD Ops"| CRUD_Scan
    API_Main -->|"CRUD Ops"| CRUD_User
    API_Main -->|"Handles"| R_Auth
    API_Main -->|"Handles"| R_ML
    API_Main -->|"Handles"| R_Plants
    API_Main -->|"Handles"| R_Diseases
    API_Main -->|"Handles"| R_Scans
    API_Main -->|"Handles"| R_User
    API_Main -->|"Handles"| R_Files
    API_Main -->|"Calls"| S_File
    API_Main -->|"Calls"| S_ML
    S_ML -->|"Invokes"| ML_Preproc
    S_ML -->|"Loads Model"| ML_Loader
    S_ML -->|"Runs Prediction"| ML_Predict
    ML_Loader -->|"Loads Artifact"| ML_Model
    ML_Predict -->|"Uses Classes"| ML_Classes

    API_Main -->|"Query/Store"| PG
    API_Main -->|"Fetch Recommendations"| GI

    %% Click Events - Mobile
    click MC_UI "https://github.com/badie16/phytovigil/tree/master/mobile/app"
    click MC_LocalDB "https://github.com/badie16/phytovigil/blob/master/mobile/services/local/databaseService.native.ts"
    click MC_Sync "https://github.com/badie16/phytovigil/blob/master/mobile/hooks/useNetworkStatus.ts"
    %% More mobile mappings
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/app/tabs/index.tsx"
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/app/auth/login.tsx"
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/app/plants/[id].tsx"
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/components/camera-button.tsx"
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/hooks/use-camera.ts"
    click MC_UI "https://github.com/badie16/phytovigil/blob/master/mobile/contexts/auth-context.tsx"
    click MC_Sync "https://github.com/badie16/phytovigil/blob/master/mobile/services/remote/plantService.ts"

    %% Click Events - Backend
    click API_Main "https://github.com/badie16/phytovigil/blob/master/backend/app/main.py"
    click Core_Config "https://github.com/badie16/phytovigil/blob/master/backend/app/core/config.py"
    click Core_Security "https://github.com/badie16/phytovigil/blob/master/backend/app/core/security.py"
    click DB_Layer "https://github.com/badie16/phytovigil/blob/master/backend/app/database.py"
    click DB_Layer "https://github.com/badie16/phytovigil/blob/master/backend/app/database/schema.sql"
    click CRUD_Disease "https://github.com/badie16/phytovigil/blob/master/backend/app/crud/disease.py"
    click CRUD_Plant "https://github.com/badie16/phytovigil/blob/master/backend/app/crud/plant.py"
    click CRUD_Scan "https://github.com/badie16/phytovigil/blob/master/backend/app/crud/scan.py"
    click CRUD_User "https://github.com/badie16/phytovigil/blob/master/backend/app/crud/user.py"
    click Models "https://github.com/badie16/phytovigil/blob/master/backend/app/models/plant.py"
    click Schemas "https://github.com/badie16/phytovigil/blob/master/backend/app/schemas/plant.py"
    click R_Auth "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/auth.py"
    click R_ML "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/ml.py"
    click R_Plants "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/plants.py"
    click R_Diseases "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/diseases.py"
    click R_Scans "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/scans.py"
    click R_User "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/user.py"
    click R_Files "https://github.com/badie16/phytovigil/blob/master/backend/app/routes/files.py"
    click S_File "https://github.com/badie16/phytovigil/blob/master/backend/app/services/file_service.py"
    click S_ML "https://github.com/badie16/phytovigil/blob/master/backend/app/services/ml_service.py"

    %% Click Events - ML
    click ML_Preproc "https://github.com/badie16/phytovigil/blob/master/backend/app/ml/image_preprocessor.py"
    click ML_Loader "https://github.com/badie16/phytovigil/blob/master/backend/app/ml/model_loader.py"
    click ML_Predict "https://github.com/badie16/phytovigil/blob/master/backend/app/ml/prediction_service.py"

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#3498DB,color:#1B4F72
    classDef backend fill:#D5F5E3,stroke:#28B463,color:#145A32
    classDef db fill:#FCF3CF,stroke:#F1C40F,color:#9A7D0A
    classDef ml fill:#FADBD8,stroke:#E74C3C,color:#641E16
    classDef external fill:#E8E8E8,stroke:#7F8C8D,color:#2C3E50
```
