# nishanthkj/agriculture architecture

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

- Interactive diagram: https://gitdiagram.com/nishanthkj/agriculture
- Repository: https://github.com/nishanthkj/agriculture

## Overview

##### Project type and purpose
- This is a full-stack web application focused on agriculture leveraging AI, IoT, and generative AI.
- Frontend is built with Next.js 15 (React) to deliver UI, pages, and client-side API routes.
- Backend is a Django REST API handling data models (crop prediction, soil fertility, yield, pest, monitoring, management) and serving ML models (RF, Naive Bayes, DL).

##### File structure
- Top level:
- agri_frontend/ → Next.js app with pages under app/, API routes under app/api/, UI components, Prisma ORM for user data, LLM integrations.
- agri_backend/ → Django project with multiple apps: Agri (core), CropRF, CropYield, SoilFertility, predictor (GPT engine), plus static ML model files, Dockerfile, manage.py.
- agri_backend/agri_backend/ → Django settings, URLs, WSGI/ASGI.
- Models, serializers, views, URLs in each app follow DRF (Django REST Framework) conventions.
- Staticfiles directory holds prebuilt static assets for deployment.
- Patterns:
- Frontend: MVC-like with pages (V), components (V), API routes (C).
- Backend: Modular “app per domain” micro-app pattern; REST API controllers (views) with serializers.
- Codegen/infra: Dockerfile, environment files for local vs containerized deployment.

##### Key insights from README
- Technologies: Next.js 15, Tailwind, Prisma, Django + DRF, PostgreSQL, Docker, jupyter notebooks for training, ML models serialized (.pkl, .h5).
- Features: Crop Prediction, Soil Health Monitoring, Pest Prediction, Real-Time Monitoring, Generative AI chat, Camera and inventory management.
- Deployment: Frontend on Vercel/Netlify, Backend on Docker/Render/AWS, PostgreSQL in cloud.

##### Main components
a. User / Web Browser (farmers, stakeholders)
b. Next.js Frontend
- Static/SSR pages
- Client-side API routes (e.g., /api/chat, /api/soil)
- Prisma ORM → PostgreSQL for user/auth data
c. Django Backend (REST API)
- Apps: CropRF, CropYield, SoilFertility, Predictor (GPT), Agri core, etc.
- ML Model Loader modules (load .pkl, .h5 files)
- Database layer (PostgreSQL)
d. ML Models Storage
- Static model files in each Django app
- Training notebooks (outside runtime)
e. External Services
- IoT sensor network → feeds telemetry to soil-health endpoints
- Generative AI (GPT) engine for chat (internal microservice or third-party API)
- External climate & price data providers
f. Infrastructure
- Vercel/Netlify for Frontend
- Docker / AWS / Render for Backend
- PostgreSQL hosted on RDS/Postgres cloud
- CDN / Static file hosting

##### Relationships & interactions
- Browser ↔ Next.js Frontend: HTTP(S) user requests; SSR or client-side navigation.
- Frontend fetch() → Next.js API routes OR directly to Django REST endpoints at NEXT_PUBLIC_API_BASE_URL.
- Next.js API route (/api/chat) → GPT Engine (internal microservice or OpenAI API).
- Django REST endpoints ← IoT devices push sensor data.
- Django retrieves/stores in PostgreSQL; loads ML models from staticfiles to compute predictions.
- Django → returns JSON responses to Frontend.
- Frontend displays charts, maps, alerts.

##### Architectural patterns & principles
- Separation of concerns: UI vs business logic vs data storage vs ML.
- Modular apps in Django for each domain.
- API-driven design: front and back decoupled, RESTful endpoints.
- Microservices / internal services for LLM and IoT ingestion.
- Infrastructure as code: Dockerfiles, env configs.

##### Final advice
- Be as granular as needed: annotate which Django app handles which endpoint.
- ML inference steps separately from CRUD operations.
- This will provide stakeholders and engineers a clear picture of UI, API, ML, DB, and external integrations.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients and External Services
    Browser["User Browser"]:::ext
    IoT["IoT Sensors"]:::ext
    GPTCloud["Generative AI Service (GPT)"]:::ext

    %% Frontend Deployment Zone
    subgraph "Frontend Deployment (Vercel/Netlify)"
        direction TB
        NextJS["Next.js Frontend"]:::frontend
        Pages["Next.js Pages (app/)"]:::frontend
        UIComp["UI Components Library"]:::frontend
        ChatWidget["ChatBot Widget"]:::frontend
        MapComp["Map Component"]:::frontend
        APIRoutes["Next.js API Routes"]:::frontend
        LLMMid["LLM Integration Service"]:::frontend
        PrismaDB["Prisma ORM & Auth DB"]:::db
    end

    %% Backend Deployment Zone
    subgraph "Backend Deployment (Docker/AWS/Render)"
        direction TB
        DjangoAPI["Django REST API"]:::backend
        subgraph "Django Apps"
            direction TB
            CoreApp["Core Agri App"]:::backend
            CropRFApp["CropRF App"]:::backend
            CropYieldApp["CropYield App"]:::backend
            SoilApp["SoilFertility App"]:::backend
            PredictorApp["Predictor (GPT) App"]:::backend
        end
        MLStorage["ML Models Storage"]:::db
        PostgresDB["PostgreSQL Database"]:::db
    end

    %% Infrastructure Legend
    subgraph Legend
        direction TB
        style Legend fill:none,stroke:none
        FrontendColor["Frontend/UI"]:::frontend
        BackendColor["Backend Services"]:::backend
        DatabaseColor["Databases/Storage"]:::db
        ExternalColor["External Services"]:::ext
    end

    %% Connections: Client to Frontend
    Browser -->|"HTTP(S) requests"| NextJS
    NextJS -->|"renders Pages"| Pages
    NextJS -->|"uses UI Components"| UIComp
    NextJS -->|"embeds ChatBot"| ChatWidget
    NextJS -->|"embeds Map"| MapComp

    %% Frontend Data Flows
    NextJS -->|"fetch /api/chat"| APIRoutes
    NextJS -->|"fetch /api/soil"| APIRoutes
    NextJS -->|"fetch Auth APIs"| APIRoutes
    NextJS -->|"calls LLM Middleware"| LLMMid
    NextJS -->|"connects to"| PrismaDB

    APIRoutes -->|"proxy to Django REST"| DjangoAPI
    LLMMid -->|"calls GPT Cloud"| GPTCloud
    IoT -->|"POST /api/soil-health"| DjangoAPI

    %% Backend Internal Flows
    DjangoAPI -->|"routes to apps"| CoreApp
    DjangoAPI -->|"routes to apps"| CropRFApp
    DjangoAPI -->|"routes to apps"| CropYieldApp
    DjangoAPI -->|"routes to apps"| SoilApp
    DjangoAPI -->|"routes to apps"| PredictorApp

    CropRFApp -->|"loads .pkl models"| MLStorage
    CropYieldApp -->|"loads .h5 models"| MLStorage
    SoilApp -->|"loads DL model"| MLStorage
    PredictorApp -->|"handles chat inference"| MLStorage

    DjangoAPI -->|"CRUD & auth"| PostgresDB

    %% External Data Providers (climate, price) could be added as ext

    %% Click Events for Frontend
    click NextJS "https://github.com/nishanthkj/agriculture/tree/master/agri_frontend/"
    click Pages "https://github.com/nishanthkj/agriculture/tree/master/agri_frontend/app/"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/chat/route.ts"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/soil/route.ts"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/auth/login/route.ts"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/auth/logout/route.ts"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/auth/profile/route.ts"
    click APIRoutes "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/app/api/auth/signup/route.ts"
    click UIComp "https://github.com/nishanthkj/agriculture/tree/master/agri_frontend/components/ui/"
    click ChatWidget "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/components/ChatBotWidget/ChatBotWidget.tsx"
    click MapComp "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/components/Map/Map.jsx"
    click LLMMid "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/lib/llm-engine.ts"
    click PrismaDB "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/lib/prisma.ts"
    click PrismaDB "https://github.com/nishanthkj/agriculture/blob/master/agri_frontend/prisma/schema.prisma"

    %% Click Events for Backend
    click DjangoAPI "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/agri_backend/settings.py"
    click DjangoAPI "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/agri_backend/urls.py"
    click CoreApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/Agri/models.py"
    click CoreApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/Agri/serializers.py"
    click CoreApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/Agri/api/urls.py"
    click CoreApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/Agri/api/views.py"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/models.py"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/static/models/random_forest_model.pkl"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/static/models/naive_bayes_model.pkl"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/api/serializers.py"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/api/urls.py"
    click CropRFApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropRF/api/views.py"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/serializers.py"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/api/views.py"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/static/CropYield/models/model.h5"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/static/CropYield/models/encoders.pkl"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/static/CropYield/models/scaler.pkl"
    click CropYieldApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/CropYield/api/urls.py"
    click SoilApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/SoilFertility/api/serializers.py"
    click SoilApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/SoilFertility/api/urls.py"
    click SoilApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/SoilFertility/api/views.py"
    click SoilApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/SoilFertility/static/Models/soil_fertility_dl_model.h5"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/api/gpt_engine.py"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/api/run.py"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/api/urls.py"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/gpt_engine.py"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/api/views.py"
    click PredictorApp "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/predictor/views.py"
    click MLStorage "https://github.com/nishanthkj/agriculture/tree/master/agri_backend/staticfiles/"
    click PostgresDB "https://github.com/nishanthkj/agriculture/blob/master/agri_backend/.env"

    %% Styles
    classDef frontend fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef backend fill:#c8e6c9,stroke:#43a047,color:#1b5e20
    classDef db fill:#ffe0b2,stroke:#fb8c00,color:#e65100
    classDef ext fill:#f0f4c3,stroke:#afb42b,color:#827717
    classDef infra fill:#e1bee7,stroke:#8e24aa,color:#4a148c
```
