# nandhu-10/brain-tumour-detection-system architecture

> Architecture diagram of the GitHub repository nandhu-10/brain-tumour-detection-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nandhu-10/brain-tumour-detection-system
- Repository: https://github.com/nandhu-10/brain-tumour-detection-system

## Overview

##### What this project is (type + purpose)
This is a **single-service Flask web application** that serves an **AI inference workflow** for **brain tumor segmentation** on MRI images. Users upload an image via a browser UI; the backend runs a **PyTorch/MONAI UNet segmentation model** (`monai/model.pt`), generates an **overlay visualization**, and returns a results page.

So the “system” is basically:
- A **web UI** (Jinja2 templates)
- A **Flask backend** (request handling + orchestration)
- An **inference pipeline** (MONAI/PyTorch model + preprocessing/postprocessing)
- A **file-based persistence layer** (uploads/results under `static/`)

##### Read the file tree as architecture hints
From the tree:

- `app.py`
Likely the entire Flask server: routes, file upload handling, model loading, inference call, saving images, returning templates.

- `monai/model.pt`

- `templates/index.html`, `templates/result.html`
Server-rendered pages. Suggests **SSR** via Flask/Jinja rather than a SPA.

- `static/uploads`, `static/results`
Persistent storage directories (local filesystem). These are important “components” because the app writes and reads from them.

- `requirements.txt`

##### What
For maximum accuracy/detail, split into “logical components” even if they run in one process:

##### Web Application (Flask)
- **Flask App (`app.py`)**
- Route: `/` (renders `index.html`)
- Route: `/predict` or `/upload` (name unknown; show “Upload/Infer endpoint”)
- Route: `/result` (renders `result.html`)
- **Request handling**: multipart/form-data upload, validation, error paths

##### Inference / ML Pipeline (inside backend)
Model inference is a subsystem and should be broken down:
- **Pre-processing**
- Load image (Pillow)
- Convert to tensor/array (NumPy)
- Resize/normalize (often MONAI transforms)
- **Model Runner**
- Load UNet weights from `monai/model.pt` (PyTorch `torch.load`)
- Run forward pass (GPU/CPU)
- **Post-processing**
- Convert mask to image
- Thresholding / argmax
- Generate overlay visualization (Matplotlib / Pillow compositing)
- **Output artifact generation**
- Save result images to `static/results`

##### Storage (local filesystem)
- **Uploads folder**: `static/uploads`
- **Results folder**: `static/results`
These are effectively your “datastore” in this project (not a DB).

##### Browser → Flask: HTTP GET `/`
- Flask renders `templates/index.html`

##### Browser → Flask: HTTP POST (image upload)
- multipart/form-data containing MRI image

##### Flask → File Storage (uploads): save uploaded file
- `static/uploads/<file>`

##### Flask → Inference Pipeline: run preprocessing + model inference + postprocessing
- Inference pipeline reads:
- uploaded image
- model weights (`monai/model.pt`)

##### Inference Pipeline → File Storage (results): save mask/overlay
- `static/results/<overlay>.png` etc.

##### Flask → Browser: HTTP response rendering `templates/result.html`
- HTML includes `<img src="/static/results/...">` and maybe the original upload image.

##### Browser → Flask static: GET `/static/...` for images
- Browser fetches the original and overlay images from static endpoints.

- invalid file type/size → render error message on index
- model load failure → 500 error

##### Architectural patterns to highlight (specific to this repo)
- **Monolith**: one Python process (Flask + inference) rather than separate services.
- **Server-side rendering**: Jinja2 templates.
- **File-based persistence**: results stored on disk under `static/`.
- **Offline-trained model artifact**: `model.pt` is packaged with the app (no training pipeline in the repo tree).
- **Synchronous inference** (likely): request blocks until prediction is done; call this out (latency, scale implications).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% =========================
    %% Diagram 1: System Context
    %% =========================
    subgraph "System Context (High-level)"
        direction TB

        U["User Browser"]:::client

        subgraph "Single-service Monolith (Python Process)"
            direction TB
            APP["Flask Web App"]:::backend
        end

        FS[("Local File System\n(file-based persistence)")]:::storage
        UP[("Uploads Store\n/static/uploads")]:::storage
        RS[("Results Store\n/static/results")]:::storage

        MPT["Model Artifact\nUNet weights (model.pt)"]:::model
        LIBS["Runtime Dependencies\n(Flask, torch, MONAI, Pillow, NumPy, Matplotlib)"]:::deps

        FS --- UP
        FS --- RS
    end

    U -->|"HTTP GET /"| APP
    APP -->|"SSR HTML"| U

    U -->|"HTTP POST upload (multipart/form-data)"| APP
    APP -->|"write image file"| UP
    APP -->|"load weights"| MPT

    APP -->|"write overlay/mask images"| RS
    APP -->|"SSR HTML (includes /static/... img src)"| U

    U -->|"HTTP GET /static/uploads/*"| UP
    U -->|"HTTP GET /static/results/*"| RS

    APP -.->|"uses"| LIBS

    %% ==========================================
    %% Diagram 2: Container/Component Decomposition
    %% ==========================================
    subgraph "Component View (inside Flask Web App)"
        direction TB

        subgraph "Presentation (Server-side rendered)"
            direction TB
            TIDX["Jinja2 Template: index.html"]:::ui
            TRES["Jinja2 Template: result.html"]:::ui
            STATIC["Static File Serving (/static)"]:::ui
        end

        subgraph "Routes / Controllers (app.py)"
            direction TB
            R1["Route: / (Index)"]:::backend
            R2["Route: Upload & Predict (POST)"]:::backend
            R3["Route: Result Page (SSR)"]:::backend

            V["Request Validation\n(file type/size, error paths)"]:::backend
        end

        subgraph "Inference Pipeline (synchronous)"
            direction TB
            PRE["Pre-processing\n(Pillow load → NumPy/Tensor → resize/normalize)"]:::ml
            RUN["Model Runner\n(MONAI/PyTorch UNet forward pass)"]:::ml
            POST["Post-processing\n(argmax/threshold → mask)"]:::ml
            VIS["Overlay/Visualization\n(Matplotlib/Pillow compositing)"]:::ml
            OUT["Output Artifact Generation\n(save PNGs)"]:::ml
        end

        W["Model Weights Loader\n(torch.load model.pt)"]:::model
        CPU["Compute\nCPU/GPU"]:::note
    end

    %% Route interactions
    U -->|"GET /"| R1
    R1 -->|"render"| TIDX
    TIDX -->|"HTML response"| U

    U -->|"POST image"| R2
    R2 -->|"validate"| V
    V -->|"on success"| PRE
    V -->|"on failure (error message)"| TIDX

    R2 -->|"save upload"| UP
    PRE -->|"read image file"| UP

    PRE -->|"tensor"| RUN
    RUN -->|"load weights"| W
    W -->|"reads"| MPT
    RUN -.->|"runs on"| CPU
    RUN -->|"logits/mask"| POST
    POST -->|"mask"| VIS
    VIS -->|"overlay"| OUT
    OUT -->|"write results"| RS

    R2 -->|"render results"| R3
    R3 -->|"render"| TRES
    TRES -->|"HTML includes /static/uploads & /static/results"| U

    U -->|"GET /static/*"| STATIC
    STATIC -->|"serves files from"| UP
    STATIC -->|"serves files from"| RS

    %% Dependency notes
    R1 -.->|"uses Flask/Jinja2"| LIBS
    R2 -.->|"uses Flask upload handling"| LIBS
    PRE -.->|"uses Pillow/NumPy/MONAI transforms"| LIBS
    RUN -.->|"uses torch + MONAI"| LIBS
    VIS -.->|"uses Matplotlib/Pillow"| LIBS

    %% Click events (from component_mapping)
    click APP "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/app.py"
    click R1 "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/app.py"
    click R2 "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/app.py"
    click R3 "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/app.py"
    click V "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/app.py"

    click TIDX "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/templates/index.html"
    click TRES "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/templates/result.html"

    click MPT "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/monai/model.pt"
    click W "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/monai/model.pt"

    click UP "https://github.com/nandhu-10/brain-tumour-detection-system/tree/main/static/uploads/"
    click RS "https://github.com/nandhu-10/brain-tumour-detection-system/tree/main/static/results/"
    click STATIC "https://github.com/nandhu-10/brain-tumour-detection-system/tree/main/static/"
    click FS "https://github.com/nandhu-10/brain-tumour-detection-system/tree/main/static/"

    click LIBS "https://github.com/nandhu-10/brain-tumour-detection-system/blob/main/requirements.txt"

    %% Styles
    classDef client fill:#1e88e5,stroke:#0d47a1,color:#ffffff,stroke-width:1px
    classDef ui fill:#42a5f5,stroke:#1565c0,color:#ffffff,stroke-width:1px
    classDef backend fill:#2e7d32,stroke:#1b5e20,color:#ffffff,stroke-width:1px
    classDef ml fill:#43a047,stroke:#1b5e20,color:#ffffff,stroke-width:1px
    classDef storage fill:#fb8c00,stroke:#e65100,color:#1b1b1b,stroke-width:1px
    classDef model fill:#7e57c2,stroke:#4527a0,color:#ffffff,stroke-width:1px
    classDef deps fill:#6d6d6d,stroke:#424242,color:#ffffff,stroke-width:1px
    classDef note fill:#cfd8dc,stroke:#607d8b,color:#1b1b1b,stroke-width:1px
```
