# ateeb09/leaf-crop architecture

> Architecture diagram of the GitHub repository ateeb09/leaf-crop, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ateeb09/leaf-crop
- Repository: https://github.com/ateeb09/leaf-crop

## Overview

1. **Overall shape**
   - This repository is a hybrid ML advisory system with two runtime entry points: a **Streamlit dashboard** for interactive local use and a **Flask API + static site** for a more production-like deployment.
   - The core business logic and model loading are centralized so both UIs can share the same predictions and advisory pipeline.

2. **User-facing surfaces**
   - `app.py` is the **Streamlit** entry point.
   - `server.py` is the **Flask** entry point, serving the API and the compiled frontend in `static/`.
   - `frontend/` appears to hold the frontend source, while `static/` contains the built assets actually served by Flask.

3. **Shared application core**
   - `core.py` is the main shared boundary for **configuration, model loading, and prediction/advisory logic**.
   - This is likely where the two UIs converge before calling the ML artifacts, which keeps the presentation layer separated from inference logic.

4. **Weather-driven disease risk pipeline**
   - The system fetches live weather from **OpenWeatherMap** or accepts demo weather values.
   - Those environmental features feed `risk_model.pkl`, which is a **Random Forest** model for predicting crop disease outbreak risk.
   - Supporting weather data and historical processing are stored in CSVs like `daily_weather_processed.csv`, `bengaluru.csv`, and `satellite_dataset.csv`.

5. **Leaf image diagnosis pipeline**
   - Leaf diagnosis uses deep learning models saved as `.h5` files.
   - There is a **generic gate model** (`leaf_classifier.h5`) that likely determines whether the image is actually a leaf.
   - If the gate passes, the system routes into **crop-specific classifiers** such as `leaf_classifier_tomato.h5`, `leaf_classifier_potato.h5`, etc., with matching label maps in `leaf_classes_*.json`.

6. **Multi-crop model family**
   - The repository supports multiple crop-specific disease classifiers: tomato, potato, apple, grape, peach, cherry, corn/maize, strawberry, bell pepper, and a generic model.
   - `leaf_model.py`, `leaf_model_generic.py`, and `leaf_model_multi_crop.py` suggest separate training/inference code paths for single-crop and multi-crop variants.

7. **Satellite/environment estimation**
   - `satellite_model.pkl` and `satellite_model_meta.json` provide a second prediction branch for estimating environmental or soil-related attributes such as soil type, soil moisture, and wind speed.
   - This branch complements the risk model by turning local weather into broader agronomic context.

8. **Fertilizer recommendation subsystem**
   - `fertilizer_model.pkl`, `fertilizer_model.py`, `fertilizer_dataset.csv`, and `fertilizer_model_meta.json` form the fertilizer recommendation pipeline.
   - Inputs likely combine crop identity and estimated soil/environment properties to produce a tailored fertilizer suggestion.

9. **Training and data-preparation boundary**
   - Training and data transformation are explicitly separated from runtime through scripts like `train_risk_model.py`, `build_real_datasets.py`, and `reorganize_plant_diseases_dataset.py`.
   - `leaf_model_dataset/` contains the image training corpus for at least potato and tomato disease classes plus a non-leaf class.

10. **Deployment artifacts vs source**
   - The `.pkl` and `.h5` files are pre-trained runtime artifacts, while the CSVs and dataset folders are inputs used during training or baseline estimation.
   - `requirements.txt`, `requirements-web.txt`, and `runtime.txt` define the Python/runtime environment for local or hosted execution.

11. **End-to-end data flow**
   - A typical request starts with a city and crop selection, optionally plus a leaf image.
   - The app fetches or accepts weather, runs disease-risk prediction, optionally runs image-based disease classification, estimates environment/soil properties, and then produces fertilizer and treatment guidance.
   - The final output is an advisory bundle rather than a single prediction.

12. **What to diagram as boundaries**
   - Draw clear separations between: **presentation layers** (`app.py`, `server.py`, `frontend/`, `static/`), **shared orchestration** (`core.py`), **ML inference artifacts** (`*.pkl`, `*.h5`, `leaf_classes_*.json`), and **training/data prep** scripts and datasets.
   - The main external dependency worth showing is **OpenWeatherMap**, since live weather is a first-class input to the advisory flow.

Main entry points: [`core.py`](https://github.com/ateeb09/leaf-crop/blob/main/core.py) (Core logic), [`leaf_model.py`](https://github.com/ateeb09/leaf-crop/blob/main/leaf_model.py) (Leaf training), [`leaf_classifier.h5`](https://github.com/ateeb09/leaf-crop/blob/main/leaf_classifier.h5) (Leaf gate), [`server.py`](https://github.com/ateeb09/leaf-crop/blob/main/server.py) (Flask server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["Presentation"]
  node_app_py["Streamlit app<br/>entry point<br/>[app.py]"]
  node_server_py["Flask server<br/>entry point<br/>[server.py]"]
  node_frontend_src["Frontend src<br/>client source"]
  node_static_site["Static site<br/>served assets"]
end

subgraph group_core["Core"]
  node_core_py["Core logic<br/>orchestration<br/>[core.py]"]
end

subgraph group_artifacts["ML Artifacts"]
  node_risk_model[("Risk model<br/>random forest<br/>[risk_model.pkl]")]
  node_satellite_model[("Satellite model<br/>context model")]
  node_satellite_meta["Satellite meta<br/>metadata"]
  node_fertilizer_model[("Fertilizer model<br/>recommendation model")]
  node_fertilizer_meta["Fertilizer meta<br/>metadata"]
  node_leaf_gate[("Leaf gate<br/>image classifier<br/>[leaf_classifier.h5]")]
  node_leaf_family[("Crop classifiers<br/>multi-crop models")]
  node_leaf_labels["Class maps<br/>label maps<br/>[leaf_classes.json]"]
end

subgraph group_prep["Training & Data"]
  node_leaf_dataset["Leaf dataset<br/>image corpus"]
  node_leaf_models_py["Leaf training<br/>training code<br/>[leaf_model.py]"]
  node_leaf_multi_py["Multi-crop code<br/>training code"]
  node_leaf_generic_py["Generic leaf code<br/>training code"]
  node_risk_train_py["Risk training<br/>training script"]
  node_fertilizer_py["Fertilizer code<br/>model code"]
  node_satellite_py["Satellite code<br/>model code<br/>[satellite_model.py]"]
  node_weather_csv["Weather data<br/>tabular data"]
  node_bengaluru_csv["Bengaluru data<br/>tabular data<br/>[bengaluru.csv]"]
  node_satellite_csv["Satellite data<br/>tabular data"]
  node_fertilizer_csv["Fertilizer data<br/>tabular data"]
end

subgraph group_external["External"]
  node_weather_api(("OpenWeatherMap<br/>external weather API"))
end

node_app_py -->|"shared logic"| node_core_py
node_server_py -->|"shared logic"| node_core_py
node_server_py -->|"serves"| node_static_site
node_frontend_src -->|"builds to"| node_static_site
node_core_py -->|"fetches weather"| node_weather_api
node_core_py -->|"predicts risk"| node_risk_model
node_core_py -->|"estimates context"| node_satellite_model
node_core_py -->|"reads metadata"| node_satellite_meta
node_core_py -->|"recommends fertilizer"| node_fertilizer_model
node_core_py -->|"reads metadata"| node_fertilizer_meta
node_core_py -->|"checks image"| node_leaf_gate
node_core_py -->|"routes to"| node_leaf_family
node_core_py -->|"maps classes"| node_leaf_labels
node_leaf_models_py -->|"trains"| node_leaf_gate
node_leaf_models_py -->|"trains"| node_leaf_family
node_leaf_multi_py -->|"variant"| node_leaf_family
node_leaf_generic_py -->|"variant"| node_leaf_gate
node_leaf_models_py -->|"uses data"| node_leaf_dataset
node_risk_train_py -->|"trains on"| node_weather_csv
node_risk_train_py -->|"uses data"| node_bengaluru_csv
node_satellite_py -->|"trains on"| node_satellite_csv
node_fertilizer_py -->|"trains on"| node_fertilizer_csv

click node_app_py "https://github.com/ateeb09/leaf-crop/blob/main/app.py"
click node_server_py "https://github.com/ateeb09/leaf-crop/blob/main/server.py"
click node_frontend_src "https://github.com/ateeb09/leaf-crop/tree/main/frontend"
click node_static_site "https://github.com/ateeb09/leaf-crop/tree/main/static"
click node_core_py "https://github.com/ateeb09/leaf-crop/blob/main/core.py"
click node_risk_model "https://github.com/ateeb09/leaf-crop/blob/main/risk_model.pkl"
click node_satellite_model "https://github.com/ateeb09/leaf-crop/blob/main/satellite_model.pkl"
click node_satellite_meta "https://github.com/ateeb09/leaf-crop/blob/main/satellite_model_meta.json"
click node_fertilizer_model "https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model.pkl"
click node_fertilizer_meta "https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model_meta.json"
click node_leaf_gate "https://github.com/ateeb09/leaf-crop/blob/main/leaf_classifier.h5"
click node_leaf_labels "https://github.com/ateeb09/leaf-crop/blob/main/leaf_classes.json"
click node_leaf_dataset "https://github.com/ateeb09/leaf-crop/tree/main/leaf_model_dataset"
click node_leaf_models_py "https://github.com/ateeb09/leaf-crop/blob/main/leaf_model.py"
click node_leaf_multi_py "https://github.com/ateeb09/leaf-crop/blob/main/leaf_model_multi_crop.py"
click node_leaf_generic_py "https://github.com/ateeb09/leaf-crop/blob/main/leaf_model_generic.py"
click node_risk_train_py "https://github.com/ateeb09/leaf-crop/blob/main/train_risk_model.py"
click node_fertilizer_py "https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model.py"
click node_satellite_py "https://github.com/ateeb09/leaf-crop/blob/main/satellite_model.py"
click node_weather_csv "https://github.com/ateeb09/leaf-crop/blob/main/daily_weather_processed.csv"
click node_bengaluru_csv "https://github.com/ateeb09/leaf-crop/blob/main/bengaluru.csv"
click node_satellite_csv "https://github.com/ateeb09/leaf-crop/blob/main/satellite_dataset.csv"
click node_fertilizer_csv "https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_dataset.csv"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_py,node_server_py,node_frontend_src,node_static_site toneBlue
class node_core_py toneAmber
class node_risk_model,node_satellite_model,node_satellite_meta,node_fertilizer_model,node_fertilizer_meta,node_leaf_gate,node_leaf_family,node_leaf_labels toneMint
class node_leaf_dataset,node_leaf_models_py,node_leaf_multi_py,node_leaf_generic_py,node_risk_train_py,node_fertilizer_py,node_satellite_py,node_weather_csv,node_bengaluru_csv,node_satellite_csv,node_fertilizer_csv toneRose
class node_weather_api toneIndigo
```

## Components

### Presentation

Interactive and deployed user-facing entry points

- [Streamlit app](https://github.com/ateeb09/leaf-crop/blob/main/app.py): entry point · Local interactive dashboard entry point · `app.py`
- [Flask server](https://github.com/ateeb09/leaf-crop/blob/main/server.py): entry point · API and static-site serving entry point · `server.py`
- [Frontend src](https://github.com/ateeb09/leaf-crop/tree/main/frontend): client source · Frontend application source bundled for deployment · `frontend`
- [Static site](https://github.com/ateeb09/leaf-crop/tree/main/static): served assets · Built frontend assets served by Flask · `static`

### Core

Shared orchestration and prediction logic

- [Core logic](https://github.com/ateeb09/leaf-crop/blob/main/core.py): orchestration · Shared configuration, model loading, and advisory pipeline · `core.py`

### ML Artifacts

Pretrained models and label maps used at runtime

- [Risk model](https://github.com/ateeb09/leaf-crop/blob/main/risk_model.pkl): random forest · Weather-driven disease risk predictor · `risk_model.pkl`
- [Satellite model](https://github.com/ateeb09/leaf-crop/blob/main/satellite_model.pkl): context model · Estimates soil and environmental attributes · `satellite_model.pkl`
- [Satellite meta](https://github.com/ateeb09/leaf-crop/blob/main/satellite_model_meta.json): metadata · Metadata for satellite/environment model outputs · `satellite_model_meta.json`
- [Fertilizer model](https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model.pkl): recommendation model · Produces fertilizer suggestions from crop and context inputs · `fertilizer_model.pkl`
- [Fertilizer meta](https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model_meta.json): metadata · Metadata for fertilizer recommendation outputs · `fertilizer_model_meta.json`
- [Leaf gate](https://github.com/ateeb09/leaf-crop/blob/main/leaf_classifier.h5): image classifier · Generic model that checks whether an image is a leaf · `leaf_classifier.h5`
- **Crop classifiers**: multi-crop models · Crop-specific disease classifiers for tomato, potato, apple, grape, peach, cherry, corn, strawberry, and bell pepper
- [Class maps](https://github.com/ateeb09/leaf-crop/blob/main/leaf_classes.json): label maps · JSON label dictionaries for leaf classifiers · `leaf_classes.json`

### Training & Data

Scripts and datasets for building models

- [Leaf dataset](https://github.com/ateeb09/leaf-crop/tree/main/leaf_model_dataset): image corpus · Training images for leaf and disease classes · `leaf_model_dataset`
- [Leaf training](https://github.com/ateeb09/leaf-crop/blob/main/leaf_model.py): training code · Training and inference code for leaf classifiers · `leaf_model.py`
- [Multi-crop code](https://github.com/ateeb09/leaf-crop/blob/main/leaf_model_multi_crop.py): training code · Multi-crop leaf model variant and routing logic · `leaf_model_multi_crop.py`
- [Generic leaf code](https://github.com/ateeb09/leaf-crop/blob/main/leaf_model_generic.py): training code · Generic leaf classifier training and inference code · `leaf_model_generic.py`
- [Risk training](https://github.com/ateeb09/leaf-crop/blob/main/train_risk_model.py): training script · Builds the crop disease risk model · `train_risk_model.py`
- [Fertilizer code](https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_model.py): model code · Fertilizer recommendation implementation · `fertilizer_model.py`
- [Satellite code](https://github.com/ateeb09/leaf-crop/blob/main/satellite_model.py): model code · Satellite/environment model implementation · `satellite_model.py`
- [Weather data](https://github.com/ateeb09/leaf-crop/blob/main/daily_weather_processed.csv): tabular data · Processed weather history used for modeling · `daily_weather_processed.csv`
- [Bengaluru data](https://github.com/ateeb09/leaf-crop/blob/main/bengaluru.csv): tabular data · City-level weather sample or baseline dataset · `bengaluru.csv`
- [Satellite data](https://github.com/ateeb09/leaf-crop/blob/main/satellite_dataset.csv): tabular data · Dataset for environmental estimation modeling · `satellite_dataset.csv`
- [Fertilizer data](https://github.com/ateeb09/leaf-crop/blob/main/fertilizer_dataset.csv): tabular data · Training dataset for fertilizer recommendations · `fertilizer_dataset.csv`

### External

Live third-party dependency

- **OpenWeatherMap**: external weather API · Live weather source used as a first-class input

## Connections

- Streamlit app → Core logic: shared logic · Streamlit dashboard delegates prediction and advice generation
- Flask server → Core logic: shared logic · Flask API uses the shared orchestration layer
- Flask server → Static site: serves · Flask serves the compiled frontend assets
- Frontend src → Static site: builds to · Frontend source is packaged into static assets
- Core logic → OpenWeatherMap: fetches weather · Retrieves live weather for advisory inputs
- Core logic → Risk model: predicts risk · Runs the weather-driven disease risk model
- Core logic → Satellite model: estimates context · Infers soil and environmental attributes
- Core logic → Satellite meta: reads metadata · Uses model metadata to interpret outputs
- Core logic → Fertilizer model: recommends fertilizer · Generates fertilizer guidance from crop and context
- Core logic → Fertilizer meta: reads metadata · Uses metadata for fertilizer output interpretation
- Core logic → Leaf gate: checks image · First-stage gate for image-based diagnosis
- Core logic → Crop classifiers: routes to · Dispatches to the appropriate crop-specific classifier
- Core logic → Class maps: maps classes · Converts classifier outputs into human-readable labels
- Leaf training → Leaf gate: trains · Training code for the generic gate model
- Leaf training → Crop classifiers: trains · Training code for crop-specific image classifiers
- Multi-crop code → Crop classifiers: variant · Multi-crop model path used for broader crop coverage
- Generic leaf code → Leaf gate: variant · Generic leaf classifier implementation
- Leaf training → Leaf dataset: uses data · Leaf training corpus for disease classification
- Risk training → Weather data: trains on · Builds the risk model from historical weather data
- Risk training → Bengaluru data: uses data · Supplemental weather dataset for training or baseline input
- Satellite code → Satellite data: trains on · Builds environmental estimation model from satellite data
- Fertilizer code → Fertilizer data: trains on · Builds fertilizer recommendation model from dataset
