# vsatyakiran/smart-harvest architecture

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

- Interactive diagram: https://gitdiagram.com/vsatyakiran/smart-harvest
- Repository: https://github.com/vsatyakiran/smart-harvest

## Overview

##### Project Type and Purpose
- Smart-Harvest is a full-stack web application that performs two main services:
a) Crop recommendation based on soil attributes (N, P, K, pH) and local weather (via a zipcode-based API).
b) Fertilizer recommendation for a chosen crop and soil profile.
- It uses a Python/Flask backend (`app.py`), pre-trained ML models (`Model.pkl`, `labels.pkl`), static front-end assets (HTML/CSS/JS under `static` and `templates`), and small data files (`fertilizer.csv`, `StatesSeason.json`, `allcrop.json`).

##### File Structure Analysis
- Top-level code:
- `app.py`: central Flask application handling HTTP routes and orchestrating ML calls.
- `model.py`, `fertilizer.py`: encapsulate prediction logic for crop and fertilizer modules.
- `app.ipynb`: Jupyter notebook used for model training (not used at runtime).
- Data/Models:
- `Model.pkl`, `labels.pkl`: serialized ML artifacts.
- `fertilizer.csv`, `StatesSeason.json`, `static/allcrop.json`: contain fertilizer rules, seasonal state data, crop details.
- Frontend:
- `templates/*.html`: Jinja2 templates for pages (homepage, crop info, fertilizer results, etc.).
- `static/`: SCSS/CSS styles, JavaScript modules (`homepage.js`, `crop_data.js`, etc.), images.
- No external persistent database; data is loaded from JSON/CSV files at runtime.

##### Key Components
a. User Interface (Browser)
b. Static Content Server (Flask `static/` endpoint serving CSS/JS/images)
c. Template Renderer (Flask `templates/` with Jinja2)
d. Application Server (Flask in `app.py`):
- HTTP endpoints: `/`, `/recommend-crop`, `/recommend-fertilizer`, `/crop-info`, etc.
- Controllers mapping requests to service modules.
e. Crop Recommendation Module (`model.py` + `Model.pkl`, `labels.pkl`)
f. Fertilizer Recommendation Module (`fertilizer.py` + `fertilizer.csv`)
g. Data Stores (flat files: JSON and CSV under project root/static)
h. External Weather API (zipcode → weather data)
i. (Optional) Model Training Pipeline (`app.ipynb`), separate from runtime.

##### Interactions and Data Flows
- Browser → HTTP GET `/` → Flask serves rendered `homepage.html` + static assets.
- User submits form (soil attributes + zipcode) → HTTP POST `/recommend-crop` →
- Flask calls weather API to fetch rainfall/temperature for zipcode.
- Combines soil + weather features → `model.py.predict()` loads `Model.pkl` → returns top 5 crops.
- Response rendered via `templates/Fertilizer_result.html` or JSON.
- User clicks a crop → HTTP GET `/crop-info?crop=X` → Flask loads `static/allcrop.json` → renders `Crop_info.html`.
- Fertilizer recommendation: form submission → HTTP POST `/recommend-fertilizer` → Flask calls `fertilizer.py.recommend()` with soil & crop → uses `fertilizer.csv` logic → renders `Fertilizer_result.html`.

##### Architectural Patterns and Design Principles
- MVC-like separation:
- Model modules (`model.py`, `fertilizer.py`) encapsulate business logic.
- Views (`templates/*.html`) handle markup.
- Controllers (Flask routes in `app.py`) orchestrate flow.
- Stateless HTTP services; no persistent DB, uses file-based data.
- ML artifacts are decoupled from code and loaded at runtime.

##### Technologies and Frameworks
- Backend: Python 3, Flask web framework.
- ML: scikit-learn (SVM, RandomForest, LogisticRegression), pickled models.
- Frontend: HTML5, SCSS/CSS, vanilla JavaScript.
- Data: JSON, CSV files.
- External Service: Weather API (e.g., OpenWeatherMap).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Side"
        Browser["User Interface (Browser)"]:::client
    end

    subgraph "Static Content Server"
        StaticServer["Static Server\n(CSS/JS/Images)"]:::static
    end

    subgraph "Template Renderer"
        TemplateRenderer["Template Renderer\n(Jinja2 Templates)"]:::templates
    end

    subgraph "Backend Services"
        FlaskApp["Flask App\n(app.py)"]:::backend
        CropModule["Crop Recommendation Module\n(model.py)"]:::backend
        FertModule["Fertilizer Recommendation Module\n(fertilizer.py)"]:::backend
    end

    subgraph "Data Stores"
        States["StatesSeason.json"]:::data
        Crops["allcrop.json"]:::data
        FertData["fertilizer.csv"]:::data
        ModelPkl["Model.pkl"]:::data
        Labels["labels.pkl"]:::data
    end

    subgraph "External Services"
        External["Weather API"]:::external
    end

    subgraph "Offline Training Pipeline"
        Notebook["Model Training Notebook\n(app.ipynb)"]:::data
    end

    %% Interactions
    Browser -->|"GET / and static assets"| StaticServer
    Browser -->|"GET /"| FlaskApp
    Browser -->|"POST /recommend-crop"| FlaskApp
    Browser -->|"POST /recommend-fertilizer"| FlaskApp
    FlaskApp -->|"renders HTML"| TemplateRenderer
    FlaskApp -->|"serves assets"| StaticServer
    FlaskApp -->|"fetch weather data"| External
    FlaskApp -->|"soil & weather features"| CropModule
    CropModule -->|"loads model"| ModelPkl
    CropModule -->|"loads labels"| Labels
    CropModule -->|"returns crop list"| FlaskApp
    FlaskApp -->|"serves crop info"| Crops
    FlaskApp -->|"crop selection"| TemplateRenderer
    FlaskApp -->|"soil & crop data"| FertModule
    FertModule -->|"loads fertilizer rules"| FertData
    FertModule -->|"returns fertilizer recommendation"| FlaskApp
    FlaskApp -->|"renders results"| TemplateRenderer
    Notebook -->|"produces artifacts"| ModelPkl
    Notebook -->|"produces artifacts"| Labels

    %% Click Events
    click Browser "https://github.com/vsatyakiran/smart-harvest/blob/main/templates/homepage.html"
    click StaticServer "https://github.com/vsatyakiran/smart-harvest/tree/main/static/"
    click TemplateRenderer "https://github.com/vsatyakiran/smart-harvest/tree/main/templates/"
    click FlaskApp "https://github.com/vsatyakiran/smart-harvest/blob/main/app.py"
    click CropModule "https://github.com/vsatyakiran/smart-harvest/blob/main/model.py"
    click ModelPkl "https://github.com/vsatyakiran/smart-harvest/blob/main/Model.pkl"
    click Labels "https://github.com/vsatyakiran/smart-harvest/blob/main/labels.pkl"
    click FertModule "https://github.com/vsatyakiran/smart-harvest/blob/main/fertilizer.py"
    click FertData "https://github.com/vsatyakiran/smart-harvest/blob/main/fertilizer.csv"
    click States "https://github.com/vsatyakiran/smart-harvest/blob/main/StatesSeason.json"
    click Crops "https://github.com/vsatyakiran/smart-harvest/blob/main/static/allcrop.json"
    click Notebook "https://github.com/vsatyakiran/smart-harvest/blob/main/app.ipynb"

    %% Styles
    classDef client fill:#CEF6F5,stroke:#333,stroke-width:1px;
    classDef static fill:#F2F5A9,stroke:#333,stroke-width:1px;
    classDef templates fill:#A9F5D0,stroke:#333,stroke-width:1px;
    classDef backend fill:#F5A9BC,stroke:#333,stroke-width:1px;
    classDef data fill:#F3E2A9,stroke:#333,stroke-width:1px;
    classDef external fill:#A9BCF5,stroke:#333,stroke-width:1px;
```
