# amarnathss/crop-and-fertilizer-recomendation-project architecture

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

- Interactive diagram: https://gitdiagram.com/amarnathss/crop-and-fertilizer-recomendation-project
- Repository: https://github.com/amarnathss/crop-and-fertilizer-recomendation-project

## Overview

##### Project Type & Purpose
- This is a full-stack web application (“Krisirava”) built with Flask (Python) that provides AI-driven crop and fertilizer recommendations.
- Purpose: Farmers input soil nutrients (N, P, K), climate parameters (temperature, humidity, pH, rainfall) and receive data-backed crop advisories and fertilizer schedules.

##### File Structure & Architectural Insights
Top-level:
- app.py            – Flask application entry point (routes, request handling)
- config.py         – Configuration (weather API key, settings)
- requirements.txt  – Python dependencies (Flask, scikit-learn, pandas, etc.)
- Procfile & Runtime.txt – Deployment descriptors (Gunicorn, Heroku)
Directories:
- models/           – Persisted AI models: RandomForest.pkl (crop advisory), plant_disease_model.pth (unused in README?)
- utils/            – Core logic for fertilizer recommendation (fertilizer.py)
- Data/             – fertilizer.csv dataset (nutrient requirements)
- templates/        – Jinja2 HTML templates (layout, crop, fertilizer, results pages)
- static/           – Front-end assets (CSS, JS, images)

##### System Components
- User Interface Layer
– Browser clients
– HTML pages (templates/)
– CSS/JS assets (static/)
- Web Server Layer
– Flask app (app.py)
– Route handlers for “/crop”, “/fertilizer”, etc.
– Configuration loader (config.py)
- Business Logic & Model-Serving Layer
– Utils module (fertilizer.py)
– Model loader: scikit-learn RandomForest.pkl, PyTorch .pth
– Data processing: NumPy, Pandas
- External Service
– OpenWeatherMap API (optional real-time climate data)
- Data Storage & Assets
– fertilizer.csv (local CSV)
– Trained model files
- Deployment & Runtime
– Gunicorn as WSGI server
– Heroku cloud (Procfile, env vars like WEATHER_API_KEY)

##### Relationships & Data Flow
1. User → Browser UI
2. UI calls Flask endpoints (POST/GET)
3. Flask reads inputs and:
a. Loads fertilizer.csv or calls utils for recommendation
b. Loads ML models for crop advisory
c. Optionally fetches weather data via external API
4. Business logic processes data, returns results
5. Flask renders result templates → Browser
6. Deployment layer sits in front (Gunicorn → Heroku)

##### Architectural Patterns & Design Principles
- MVC-like separation:
– View: templates + static
– Controller: Flask route handlers
– Model: ML models + CSV data + utility functions
- Single codebase with layered responsibilities
- External configuration (config.py, env vars)
- Stateless web layer, heavy lifting in memory (no DB)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client / Browser"
        direction TB
        Browser["Browser"]:::client
    end

    %% User Interface Layer
    subgraph "User Interface"
        direction TB
        TEMPL["HTML Templates"]:::frontend
        CSS["CSS Assets"]:::frontend
        JS["JavaScript Assets"]:::frontend
    end

    Browser -->|renders HTML| TEMPL
    Browser -->|loads CSS/JS| CSS
    Browser -->|loads CSS/JS| JS

    %% Web Server Layer
    subgraph "Web Server Layer"
        direction TB
        APP["app.py"]:::backend
        CFG["config.py"]:::backend
    end

    TEMPL -->|POST soil data| APP
    CSS -->|static serve| APP
    JS -->|AJAX calls| APP
    APP -->|loads config| CFG

    %% Business Logic & ML Layer
    subgraph "Business Logic & Model Serving"
        direction TB
        UTIL["utils/fertilizer.py"]:::logic
        DP["Data Processing\n(NumPy/Pandas)"]:::logic
        MODEL1["RandomForest.pkl"]:::model
        MODEL2["plant_disease_model.pth"]:::model
        DATASET["fertilizer.csv"]:::data
    end

    APP -->|calls recommendation| UTIL
    UTIL -->|reads| DATASET
    APP -->|loads| DP
    APP -->|loads model| MODEL1
    APP -->|loads model| MODEL2
    UTIL --> DP

    UTIL -->|returns recommendations| APP
    MODEL1 -->|predict crop| APP

    %% External Service
    subgraph "External Services"
        direction TB
        WEATHER["OpenWeatherMap API"]:::external
    end

    APP -->|GET weather JSON| WEATHER
    WEATHER -->|returns JSON| APP

    %% Deployment Infrastructure
    subgraph "Deployment & Runtime"
        direction TB
        HEROKU["Heroku"]:::infra
        GUNICORN["Gunicorn WSGI"]:::infra
        PROC["Procfile"]:::infra
        RUNTIME["Runtime.txt"]:::infra
        REQ["requirements.txt"]:::infra
    end

    HEROKU --> GUNICORN
    GUNICORN --> APP

    %% Click Events
    click TEMPL "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/tree/main/templates/"
    click CSS "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/tree/main/static/css/"
    click JS "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/tree/main/static/scripts/"
    click APP "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/app.py"
    click CFG "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/config.py"
    click UTIL "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/utils/fertilizer.py"
    click MODEL1 "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/models/RandomForest.pkl"
    click MODEL2 "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/models/plant_disease_model.pth"
    click DATASET "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/Data/fertilizer.csv"
    click PROC "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/tree/main/Procfile"
    click RUNTIME "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/Runtime.txt"
    click REQ "https://github.com/amarnathss/crop-and-fertilizer-recomendation-project/blob/main/requirements.txt"

    %% Styles
    classDef client fill:#FFFAE6,stroke:#E6B800,color:#333;
    classDef frontend fill:#E8F4FD,stroke:#1C77C3,color:#000;
    classDef backend fill:#E2F7E2,stroke:#28A745,color:#000;
    classDef logic fill:#FFF4E5,stroke:#FF9900,color:#000;
    classDef model fill:#F0E6FF,stroke:#8A2BE2,color:#000;
    classDef data fill:#E6F7FF,stroke:#00AACC,color:#000;
    classDef external fill:#FFE6E6,stroke:#CC0000,color:#000;
    classDef infra fill:#F2F2F2,stroke:#666666,color:#000;
```
