# wills17/brain-cancer-prediction architecture

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

- Interactive diagram: https://gitdiagram.com/wills17/brain-cancer-prediction
- Repository: https://github.com/wills17/brain-cancer-prediction

## Overview

##### Project Type and Purpose
- This is a full-stack ML web application:
- Front-end: HTML/CSS/JS (templates + static assets)
- Back-end: Flask server (app.py / app_r.py)
- ML inference: Keras & TFLite models under Models/
- Optional training pipeline scripts (brain_cancer_code.py)
- Cloud deployment on Render (render.yaml)

##### File Structure
- Top-level files:
- app.py        → main Flask routes using .h5 model
- app_r.py      → Flask routes using .tflite
- brain_cancer_predict.py → standalone inference test
- brain_cancer_code.py    → training script
- render.yaml   → Render.com deployment config
- requirements.txt
- Directories:
- Models/       → stored ML artifacts (H5, TFLite)
- templates/    → Jinja2 HTML views (home, about, predict, results)
- static/       → JS, CSS, preview images
- Config & Dependencies:
- render.yaml (deployment)
- requirements.txt (Flask, tflite-runtime, numpy, pillow, OpenCV-headless)

##### Main Components
a. Client (Browser)
- UI pages (templates/*.html)
- Static assets (styles.css, scripts.js)
- Drag & drop upload widget
b. Web Server (Flask)
- Entrypoints: app.py / app_r.py
- Route handlers:
- GET /         → home.html
- GET /about    → about.html
- GET /predict  → predict.html
- POST /predict → process upload, call inference
- GET /results  → results.html
- File upload handling, temporary storage
- Report generation (downloadable diagnosis)
c. ML Inference Layer
- Keras model loader (.h5) or TFLite runtime (.tflite)
- Prediction function (preprocess image → model.predict → JSON result)
d. Optional Training Pipeline
- Script brain_cancer_code.py (EfficientNetB0 base, training on Dataset.zip)
- Outputs new .h5 to Models/
e. Deployment Environment
- Render.com (render.yaml)
- Python environment (requirements.txt)

##### Relationships & Data Flow
1. User opens browser → requests home page
2. Browser downloads HTML/CSS/JS from Flask static/templates
3. User selects/uploads MRI image → JS sends POST to /predict
4. Flask route receives image → saves to server → calls inference function
5. Inference function loads model (H5 or TFLite) → returns class label + confidence
6. Flask returns results page + optional downloadable report
7. Browser displays prediction

##### Architectural Patterns & Principles
- MVC-like separation:
- Model → ML artifacts under Models/
- View  → Jinja2 templates + static
- Controller → Flask route handlers
- Single monolith service with clearly delineated layers
- Separation of inference (app_r.py for TFLite) vs training script

- Front-end: browser & static/templates
- Back-end: Flask
- ML: Models + inference code
- Deployment: Render

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Layer
    USER["User/Browser"]:::front

    %% Client Assets
    subgraph "Client (Browser Resources)"
        direction TB
        TEMPLATES["UI Pages (templates/)"]:::front
        STATIC["Static Assets (static/)"]:::front
    end

    %% Backend Layer
    subgraph "Web Server (Flask)"
        direction TB
        FLASK_H5["Flask App (H5) - app.py"]:::backend
        FLASK_TFLITE["Flask App (TFLite) - app_r.py"]:::backend
        REQS["Dependencies - requirements.txt"]:::backend
    end

    %% ML Inference Layer
    subgraph "ML Inference"
        direction TB
        TEST_SCRIPT["Standalone Test - brain_cancer_predict.py"]:::ml
        MODEL_H5["Model: Brain_cancer_model.h5"]:::ml
        MODEL_TFLITE["Model: Brain_cancer_model.tflite"]:::ml
    end

    %% Training Pipeline
    subgraph "Training Pipeline (Optional)"
        direction TB
        TRAIN_SCRIPT["Training Script - brain_cancer_code.py"]:::training
    end

    %% Deployment Layer
    subgraph "Deployment"
        direction TB
        RENDER["Render Config - render.yaml"]:::deployment
    end

    %% Data Flow
    USER -->|"GET /"| FLASK_H5
    USER -->|"GET /"| FLASK_TFLITE
    FLASK_H5 -->|"serves"| TEMPLATES
    FLASK_H5 -->|"serves"| STATIC
    FLASK_TFLITE -->|"serves"| TEMPLATES
    FLASK_TFLITE -->|"serves"| STATIC
    USER -->|"POST /predict"| FLASK_H5
    USER -->|"POST /predict"| FLASK_TFLITE
    FLASK_H5 -->|"loads"| MODEL_H5
    FLASK_TFLITE -->|"loads"| MODEL_TFLITE
    MODEL_H5 -->|"returns result"| FLASK_H5
    MODEL_TFLITE -->|"returns result"| FLASK_TFLITE
    FLASK_H5 -->|"renders results"| USER
    FLASK_TFLITE -->|"renders results"| USER

    %% Standalone Test Script Flow
    TEST_SCRIPT -->|"loads"| MODEL_H5
    TEST_SCRIPT -->|"loads"| MODEL_TFLITE

    %% Training Flow
    TRAIN_SCRIPT -->|"outputs new .h5 to"| MODEL_H5

    %% Deployment Flow
    RENDER -->|"deploys"| FLASK_H5
    RENDER -->|"deploys"| FLASK_TFLITE
    REQS -->|"used by"| FLASK_H5
    REQS -->|"used by"| FLASK_TFLITE
    REQS -->|"used by"| TEST_SCRIPT
    REQS -->|"used by"| TRAIN_SCRIPT

    %% Click Events
    click TEMPLATES "https://github.com/wills17/brain-cancer-prediction/tree/main/templates/"
    click STATIC "https://github.com/wills17/brain-cancer-prediction/tree/main/static/"
    click FLASK_H5 "https://github.com/wills17/brain-cancer-prediction/blob/main/app.py"
    click FLASK_TFLITE "https://github.com/wills17/brain-cancer-prediction/blob/main/app_r.py"
    click TEST_SCRIPT "https://github.com/wills17/brain-cancer-prediction/blob/main/brain_cancer_predict.py"
    click MODEL_H5 "https://github.com/wills17/brain-cancer-prediction/blob/main/Models/Brain_cancer_model.h5"
    click MODEL_TFLITE "https://github.com/wills17/brain-cancer-prediction/blob/main/Models/Brain_cancer_model.tflite"
    click TRAIN_SCRIPT "https://github.com/wills17/brain-cancer-prediction/blob/main/brain_cancer_code.py"
    click RENDER "https://github.com/wills17/brain-cancer-prediction/blob/main/render.yaml"
    click REQS "https://github.com/wills17/brain-cancer-prediction/blob/main/requirements.txt"

    %% Styles
    classDef front fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
    classDef backend fill:#E6F4EA,stroke:#28A745,color:#28A745;
    classDef ml fill:#FFF4E6,stroke:#D97706,color:#D97706;
    classDef training fill:#F3E8FF,stroke:#8B5CF6,color:#8B5CF6;
    classDef deployment fill:#F0F0F0,stroke:#6A737D,color:#6A737D;
```
