# posi2/crop-price-prediction architecture

> Architecture diagram of the GitHub repository posi2/crop-price-prediction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/posi2/crop-price-prediction
- Repository: https://github.com/posi2/crop-price-prediction

## Overview

##### Project Type and Purpose
- This is a full-stack machine learning application focused on predicting crop prices using historical price data combined with geographical and climatic inputs.
- The project uses IBM Watson Studio for data exploration and Jupyter Notebooks, leverages Scikit-Learn and other ML libraries for modeling, and employs Flask as the web framework to deliver predictions via a web interface.

##### Main Components
- Data Preprocessing & Exploration:
- Jupyter Notebook (Crop Price Prediction.ipynb) where initial data analysis, visualization (histograms, heatmaps, scatter plots), and preprocessing steps (normalization, one-hot encoding, etc.) are performed.
- Machine Learning Model:
- Model creation and serialization component. This is handled by cropmodel.py, with the trained ML model saved in external files (model.json for architecture and model.h5 for weights).
- Web Application Backend:
- Flask application (cropwebapp.py) which serves as the backend to expose endpoints and integrate the ML predictions.
- Frontend/UI Layer:
- Presentation layer built using templating (HTML files in the template directory) that works together with static resources.
- Importance of the static assets and styling configuration (SCSS files in the scss folder and CSS in the static/css directory) used to style the web interface.
- External Data Sources:
- Optionally, include a component for external data ingestion or links to the dataset sources (from government data portals and rainfall data mentioned in the README) to indicate where raw data comes from.

##### Relationships and Interactions
- Data flows from the raw data sources → Preprocessing in the Jupyter Notebook → ML model training and serialization.
- The serialized model files (model.json and model.h5) are then utilized by the Flask backend (through cropmodel.py and cropwebapp.py) to make live predictions.
- The Flask server interacts with the presentation layer by rendering HTML templates from the ‘template’ folder and serving static assets (CSS, fonts, images) from the ‘static’ directory.

##### Architectural Patterns or Design Principles
- Separation of Concerns: Clearly separate the data processing/ML component from the web serving component.
- MVC-style Structure (in simplified form): Though not strictly MVC, note that the template directory (views), cropwebapp.py (controller), and underlying model logic (model) together form this separation.
- Component Reusability: The ML model’s separation allows potential reuse in other platforms (like mobile apps) or services via an API.

c. Artifacts to the “Flask Web Application” component (from cropwebapp.py), which exposes prediction endpoints.
d. From the Flask component, illustrate connections to the “UI Layer” – this includes the templates (index.html, layout.html, result.html) and the static assets (stylesheets in scss and static/css, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Data Sources & Dependencies
    RawData["Raw Data & Data Sources"]:::external
    Watson["IBM Watson Studio"]:::external
    Scikit["Scikit-Learn & ML Libraries"]:::external

    %% Machine Learning Layer
    subgraph "Machine Learning Layer"
        Jupyter["Jupyter Notebook - Data Exploration & Preprocessing"]:::ml
        MLTraining["ML Model Training (cropmodel.py)"]:::ml
        ModelJSON["Serialized Model (model.json)"]:::ml
        ModelH5["Serialized Weights (model.h5)"]:::ml
    end

    %% Web Application Backend
    subgraph "Web Application Backend"
        Flask["Flask Web Application (cropwebapp.py)"]:::backend
    end

    %% Frontend/UI Layer
    subgraph "Frontend/UI Layer"
        Index["Template: index.html"]:::frontend
        Layout["Template: layout.html"]:::frontend
        Result["Template: result.html"]:::frontend
        StaticAssets["Static Assets (static/)"]:::frontend
        SCSS["SCSS Styles (scss/)"]:::frontend
    end

    %% Data Flow Connections
    RawData -->|"provides_raw_data"| Jupyter
    Watson -->|"assists_with_exploration"| Jupyter
    Jupyter -->|"outputs_preprocessed_data"| MLTraining
    Scikit -->|"supports_ML_functions"| MLTraining
    MLTraining -->|"produces_model_architecture"| ModelJSON
    MLTraining -->|"produces_model_weights"| ModelH5
    ModelJSON -->|"loaded_by"| Flask
    ModelH5 -->|"loaded_by"| Flask
    Flask -->|"renders"| Index
    Flask -->|"renders"| Layout
    Flask -->|"renders"| Result
    Flask -->|"serves"| StaticAssets
    Flask -->|"serves"| SCSS

    %% Click Events
    click Jupyter "https://github.com/posi2/crop-price-prediction/blob/master/Crop Price Prediction.ipynb"
    click MLTraining "https://github.com/posi2/crop-price-prediction/blob/master/cropmodel.py"
    click ModelJSON "https://github.com/posi2/crop-price-prediction/blob/master/model.json"
    click ModelH5 "https://github.com/posi2/crop-price-prediction/blob/master/model.h5"
    click Flask "https://github.com/posi2/crop-price-prediction/blob/master/cropwebapp.py"
    click Index "https://github.com/posi2/crop-price-prediction/blob/master/template/index.html"
    click Layout "https://github.com/posi2/crop-price-prediction/blob/master/template/layout.html"
    click Result "https://github.com/posi2/crop-price-prediction/blob/master/template/result.html"
    click StaticAssets "https://github.com/posi2/crop-price-prediction/tree/master/static/"
    click SCSS "https://github.com/posi2/crop-price-prediction/tree/master/scss/"

    %% Styles
    classDef external fill:#FFCCCB,stroke:#000,stroke-width:2px;
    classDef ml fill:#C1FFC1,stroke:#000,stroke-width:2px;
    classDef backend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef frontend fill:#FFE4B5,stroke:#000,stroke-width:2px;
```
