# wakassodev/brain_tumor_classification architecture

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

- Interactive diagram: https://gitdiagram.com/wakassodev/brain_tumor_classification
- Repository: https://github.com/wakassodev/brain_tumor_classification

## Overview

Step 1: Identify the project type and purpose
- This is a monolithic, full-stack Python web application built with Streamlit that serves a deep-learning pipeline for classifying brain MRI scans.
- Purpose: Allow users (clinicians, researchers) to upload MRI images, select a model (Xception or custom CNN), receive a tumor classification with confidence scores, view saliency maps, and get an AI-powered explanation of the prediction.

Step 2: Analyze the file structure
- Top-level files only, no subdirectories:
- app.py (or myApp.py in README) – entry point, handles UI and service logic
- cnn_model.h5 – custom CNN weights
- xception_model.weights.h5 – transfer-learning weights
- requirements.txt – Python dependencies (Streamlit, TensorFlow/Keras, OpenCV, Plotly, Google AI SDK)
- .env (not in repo but implied) – holds GOOGLE_API_KEY
- No database or message queue; all inference is in-process.
- Monolith pattern: UI, business logic, and model inference reside in one service.

Step 3: Extract additional insights from README
- Streams:
- Streamlit UI for image upload, model selection, and results display
- OpenCV for preprocessing (resizing, normalization)
- TensorFlow/Keras for loading and running models
- Saliency map generation module
- Plotly for bar-chart visualization
- Google Generative AI API for textual explanation via env var GOOGLE_API_KEY

1. User Browser (Client)
2. Streamlit Server (app.py)
– UI Layer
– Controller Layer
3. Preprocessing Module (OpenCV)
4. Model Inference Module
- Custom CNN (.h5)
- Xception Transfer-Learning (.weights.h5)
5. Saliency Map Generator
6. Explanation Service (Google Gen AI API)
7. Visualization Module (Plotly)
8. Environment & Config (env vars, .env file)

1. Client → Streamlit UI: upload image + select model
2. Streamlit UI → Preprocessing: raw image → processed tensor
3. Preprocessing → Model Inference: tensor → chosen model → class probabilities
4. Model Inference → Saliency Generator: tensor + model → saliency map
5. Model Inference & Saliency → Explanation Service: send data + GOOGLE_API_KEY → receive text explanation
6. Streamlit UI ← Visualization:
- Bar chart (Plotly) ← probabilities
- Saliency map overlay
- Text explanation
7. Streamlit UI → Client: rendered UI with results

c. Architectural patterns & principles:
- Layered architecture: Presentation (Streamlit UI), Application Logic (preprocessing & orchestration), ML (model inference), Integration (external API).
- Configuration externalized via env vars (.env).
- Monolith deploys as one container/process.

d. Key technologies/frameworks:
- Python 3.x, Streamlit
- TensorFlow & Keras
- OpenCV (cv2)
- Plotly
- Google Generative AI SDK
- Virtual environment (venv)

Step 5: Tailoring guidelines for a full-stack ML web app
- Split between Client (web browser) and Server (Streamlit)
- Model selection as a decision branch in the inference pipeline
- Flow from raw input → preprocessing → inference → postprocessing → UI
- External dependency (Google Gen AI) as an integration point

- Client: light blue rectangle

Step 7: Capture essential architectural elements
- Do not over-fragment: keep roughly 6–8 components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Components
    Client["User Browser"]:::client
    Streamlit["Streamlit Server (app.py)"]:::server
    Preproc["Preprocessing Module (OpenCV)"]:::process
    subgraph "Model Inference Layer"
        InferenceCustom["Custom CNN Model (.h5)"]:::model
        InferenceXcept["Xception Model (.weights.h5)"]:::model
    end
    Saliency["Saliency Map Generator"]:::process
    Explanation["Explanation Service (Google Gen AI API)"]:::external
    Viz["Visualization Module (Plotly)"]:::process
    Config["Config / Environment (.env)"]:::config

    %% Data Flows
    Client -->|"upload image + select model"| Streamlit
    Streamlit -->|"raw image"| Preproc
    Preproc -->|"processed tensor"| InferenceCustom
    Preproc -->|"processed tensor"| InferenceXcept
    InferenceCustom -->|"class probabilities"| Saliency
    InferenceXcept -->|"class probabilities"| Saliency
    InferenceCustom -->|"probabilities & tensor"| Explanation
    InferenceXcept -->|"probabilities & tensor"| Explanation
    Saliency -->|"saliency data"| Explanation
    Explanation -->|"text explanation"| Viz
    InferenceCustom -->|"probabilities"| Viz
    InferenceXcept -->|"probabilities"| Viz
    Saliency -->|"map overlay"| Viz
    Viz -->|"render results"| Streamlit
    Streamlit -->|"display UI"| Client
    Config --> Explanation
    Config --> Streamlit

    %% Click Events
    click Streamlit "https://github.com/wakassodev/brain_tumor_classification/blob/main/app.py"
    click Preproc "https://github.com/wakassodev/brain_tumor_classification/blob/main/app.py"
    click Saliency "https://github.com/wakassodev/brain_tumor_classification/blob/main/app.py"
    click Viz "https://github.com/wakassodev/brain_tumor_classification/blob/main/app.py"
    click Explanation "https://github.com/wakassodev/brain_tumor_classification/blob/main/app.py"
    click InferenceCustom "https://github.com/wakassodev/brain_tumor_classification/blob/main/cnn_model.h5"
    click InferenceXcept "https://github.com/wakassodev/brain_tumor_classification/blob/main/xception_model.weights.h5"
    click Config "https://github.com/wakassodev/brain_tumor_classification/blob/main/.env"

    %% Styles
    classDef client fill:#B3E5FC,stroke:#0288D1,stroke-width:2px
    classDef server fill:#1976D2,stroke:#0D47A1,stroke-width:2px
    classDef process fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px
    classDef model fill:#FFCC80,stroke:#EF6C00,stroke-width:2px
    classDef external fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px
    classDef config fill:#CFD8DC,stroke:#607D8B,stroke-width:2px
```
