# ravikant-diwakar/agrisens architecture

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

- Interactive diagram: https://gitdiagram.com/ravikant-diwakar/agrisens
- Repository: https://github.com/ravikant-diwakar/agrisens

## Overview

##### Project Type and Purpose
- This is a full‐stack, AI‐powered Smart Farming Assistant ("AgriSens") that provides farmers with (a) Smart Crop Recommendations, (b) Plant Disease Identification, (c) Fertilizer Recommendations, (d) Real-time Weather Forecasts, and (e) a Smart Farming Guide via a user-friendly web interface.

##### High‐Level File‐Structure Analysis
- Frontend (AgriSens-web-app): static HTML/CSS/JS pages for Home, Explore, Guide, Weather Forecast, and Developing-Phase.
- Crop Recommendation Service (CROP-RECOMMENDATION): Python `webapp.py`, several pre-trained `.pkl` model files, dataset CSV, `requirements.txt`.
- Plant Disease Identification Service (PLANT-DISEASE-IDENTIFICATION): Python `main.py`, trained Keras model `.keras`, settings JSON, Jupyter notebooks, `requirements.txt`.
- Datasets: raw CSVs for crop & fertilizer recommendations; image dataset referenced but not served live.
- Miscellaneous: `SystemArchitecture.md`, research paper PDF, license, project‐level `README.md`.

##### README Insights
- Confirms a static SPA-style frontend hosted (e.g., Netlify), and backend AI services (likely served via Flask/Streamlit).
- External integrations: real-time weather API.
- No persistent database—data and models load from the file system at runtime.

##### Main Architectural Components
A. Frontend/UI Layer
- Static pages (HTML/CSS/JS) served from a CDN/Netlify.
- Routes for Crop Rec form, Disease Identification upload, Fertilizer Rec form, Weather page, Guide.
B. API Gateway / Web Server
- One or two Python apps (`webapp.py` and `main.py`), each exposing REST endpoints (e.g., `/predict_crop`, `/predict_disease`).
- Runs on a server (e.g., AWS EC2, Heroku, Streamlit Cloud).
C. ML Inference Services
1. Crop Recommendation Service
– Loads multiple scikit-learn models (`RandomForest.pkl`, etc.).
– Accepts JSON payload of soil/climate features → returns best‐fit crop label.
2. Disease Identification Service
– Loads CNN Keras model (`trained_plant_disease_model.keras`).
– Accepts image upload → returns disease class/confidence.
3. Fertilizer Recommendation
– Could be integrated in `webapp.py` using CSV‐based lookup or a simple rule engine.
D. External Services
- Weather Forecast Module: HTTP calls to a third‐party Weather API (e.g., OpenWeatherMap) to fetch real‐time temperature, humidity, rainfall.
E. Data & Models Storage
- Model artifact directory on the server.
- CSV files for fertilizer rules and crop rec dataset (used offline for retraining).
F. Optional Logging/Monitoring
- (Not present in code, but recommended) logging service to capture API usage, errors, model latencies.

##### Component Interactions
1. User in Browser → (HTTP GET) → Static UI
2. User submits soil data → (HTTP POST /predict_crop) → Crop Rec Service → loads model → returns JSON → UI renders crop recommendation.
3. User uploads leaf image → (HTTP POST /predict_disease) → Disease Service → loads CNN → returns JSON → UI displays disease and guide link.
4. UI page for Weather → triggers JS fetch to backend or directly to external Weather API → displays real-time forecast.
5. Fertilizer page → similar flow to crop rec but with fertilizer logic.
6. Smart Farming Guide → static content.

##### Key Architectural Patterns
- Separation of Concerns: distinct UI, API, and ML layers.
- Microservices‐style: each ML feature (crop rec, disease id, fertilizer) runs in its own module/endpoint.
- Stateless Services: inference endpoints load models once on startup, handle requests independently (no session state).
- Client‐Server: browser UI vs. backend API.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    User["User Browser"]:::frontblue
    User -->|"HTTP GET"| UI

    subgraph "Frontend/UI Layer"
        UI["Static UI (Netlify/CDN)\n(HTML/CSS/JS)"]:::frontblue
    end

    %% Server Layer
    subgraph "API Gateway / Web Server"
        API["API Gateway / Web Server\n(port 5000)"]:::serverpurple
    end

    UI -->|"POST /predict_crop\n(JSON)"| API
    UI -->|"POST /predict_disease\n(image)"| API
    UI -->|"GET /predict_fertilizer\n(JSON)"| API

    %% ML Services
    subgraph "ML Services"
        Crop["Crop Recommendation Service"]:::mlgreen
        Disease["Plant Disease Identification Service"]:::mlgreen
        Fertilizer["Fertilizer Recommendation Logic"]:::mlgreen
    end

    API -->|"in-process call"| Crop
    API -->|"in-process call"| Disease
    API -->|"in-process call"| Fertilizer

    Crop -->|"loads .pkl files"| Storage
    Disease -->|"loads .keras model"| Storage
    Fertilizer -->|"reads CSV rules"| Storage

    Crop -->|"JSON response"| API
    Disease -->|"JSON response"| API
    Fertilizer -->|"JSON response"| API
    API -->|"JSON response"| UI

    %% External Services
    External["External Weather API"]:::externalorange
    UI -->|"JS fetch\n(REST JSON)"| External

    %% Storage
    Storage["Model & Data Storage\n(models & CSVs)"]:::storageyellow

    %% Click Events
    click UI "https://github.com/ravikant-diwakar/agrisens/blob/master/AgriSens-web-app/index.html"
    click Crop "https://github.com/ravikant-diwakar/agrisens/blob/master/CROP-RECOMMENDATION/webapp.py"
    click Disease "https://github.com/ravikant-diwakar/agrisens/blob/master/PLANT-DISEASE-IDENTIFICATION/main.py"
    click Storage "https://github.com/ravikant-diwakar/agrisens/blob/master/Datasets/Crop_recommendation.csv"

    %% Styles
    classDef frontblue fill:#D0E8FF,stroke:#0066CC,color:#003366;
    classDef serverpurple fill:#E6DAF8,stroke:#6A1B9A,color:#4A1457;
    classDef mlgreen fill:#E0F8E0,stroke:#2E7D32,color:#1B5E20;
    classDef storageyellow fill:#FFF8E1,stroke:#F9A825,color:#E65100;
    classDef externalorange fill:#FFE0B2,stroke:#FB8C00,color:#E65100;
```
