# securdrgorp/cancercare-ai architecture

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

- Interactive diagram: https://gitdiagram.com/securdrgorp/cancercare-ai
- Repository: https://github.com/securdrgorp/cancercare-ai

## Overview

##### Project type and purpose
- This is a monolithic, full-stack web application built on Flask that exposes a REST/UI layer for AI-powered question answering in the cancer‐treatment domain.
- It combines a web frontend (HTML/CSS/JS/Chart.js) with a Python backend integrating several NLP/ML modules (BioBERT QA, sentence transformers, spaCy pipeline, data handlers) on local data files.

##### File structure
Top-level files & directories reveal an MVC-style separation:
- app.py: Flask app and route definitions (controllers).
- biobert_qa.py, context_provider.py, nlp_pipeline.py, data_handler.py: core service modules.
- data/: local CSV/JSON datasets (knowledge base).
- model/: locally stored BioBERT model.
- templates/: Jinja2 HTML views (index, chat, statistics, errors).
- static/: CSS/JS assets; Chart.js visualizations and custom script.
- scripts/saveModel.py: setup utility to download the BioBERT model.
- tests/: pytest suite covering each module and the Flask app.

##### README for insights
- Backend stack: Flask + Jinja2, BioBERT (PyTorch), Sentence Transformers, spaCy, Pandas/NumPy.
- Frontend stack: Bootstrap 5, Chart.js, custom JS for chat.
- Data flow: User → /api/chat or web pages → Flask routes → NLP pipeline + context provider → BioBERT QA engine → JSON/html response → user.
- No external DB: data lives in flat files.

##### Main components & interactions
a. Frontend (Browser)
- UI Views: index, chat, statistics (templates + static/js + static/css)
- Chart.js renders stats via GET /api/statistics
- AJAX POSTs to /api/chat
b. Web Server (Flask Application)
- Route controller layer (app.py)
- Error handling/logging
- Jinja2 template renderer
c. Service Modules (Python packages)
1. NLP Pipeline (nlp_pipeline.py)
– spaCy model loader, language detection, text normalization
2. Context Provider (context_provider.py)
– Loads knowledge base embeddings, performs semantic search
3. Data Handler (data_handler.py)
– Loads CSV/JSON, tabular data processing, statistics aggregation
4. BioBERT QA Engine (biobert_qa.py)
– Loads local BioBERT model files, runs extractive QA
d. Data Stores
- Local model directory (model/…)
- Flat files (data/*.csv, data/*.json)
e. External Dependencies (libraries)
– PyTorch/Transformers, SentenceTransformers, spaCy, pandas, NumPy

##### Relationships & data flows
- User → HTTP request → Flask route
- Flask route invokes:
- data_handler for any dashboard queries
- if chat: pass user text to nlp_pipeline → context_provider → biobert_qa → back up to Flask → JSON/html
- For statistics page: Flask → data_handler → Chart.js data payload
- All modules read from the same flat files; only BioBERT touches model directory.

##### Architectural patterns & design principles
- Layered architecture: UI layer, controller layer, service layer, data layer.
- Separation of concerns: each module has a single responsibility.
- No external database: file-based data storage.
- Synchronous request-response flow.
- Modular design to enable extensibility (e.g., swap in a new model).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend Layer
    subgraph "Frontend (Browser)"
        direction TB
        User["User (Browser)"]:::frontend
        Templates["Jinja2 Templates"]:::frontend
        CSS["static/css/style.css"]:::frontend
        JS["static/js/main.js"]:::frontend
    end

    %% Backend / Controllers
    subgraph "Backend (Flask Application)"
        direction TB
        FlaskApp["Flask App<br/>(app.py)"]:::backend
    end

    %% Service Modules
    subgraph "Service Modules"
        direction TB
        Nlp["NLP Pipeline<br/>(nlp_pipeline.py)"]:::service
        Context["Context Provider<br/>(context_provider.py)"]:::service
        BioBERT["BioBERT QA Engine<br/>(biobert_qa.py)"]:::service
        DataH["Data Handler<br/>(data_handler.py)"]:::service
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        Flat["Flat Files<br/>(data/)"]:::datastore
        ModelDir["Model Directory<br/>(model/)"]:::datastore
    end

    %% Setup Script
    SaveMod["Model Download Script<br/>(scripts/saveModel.py)"]:::script

    %% User Interactions
    User -->|Access pages| Templates
    User -->|Load CSS/JS| CSS
    User -->|Load CSS/JS| JS
    User -->|GET /, /chat, /statistics| FlaskApp
    User -->|AJAX POST /api/chat| FlaskApp
    User -->|GET /api/statistics| FlaskApp

    %% Backend to Services
    FlaskApp -->|calls Data Handler| DataH
    FlaskApp -->|calls NLP Pipeline| Nlp

    %% Chat Pipeline
    Nlp -->|normalized text| Context
    Context -->|semantic context| BioBERT
    BioBERT -->|answer JSON| FlaskApp

    %% Statistics Pipeline
    FlaskApp -->|requests stats| DataH
    DataH -->|reads data files| Flat
    FlaskApp -->|JSON payload| JS

    %% Data flows
    DataH -->|reads CSV/JSON| Flat
    BioBERT -->|loads model files| ModelDir

    %% Setup
    SaveMod -.->|downloads model| ModelDir

    %% Click Events
    click Templates "https://github.com/securdrgorp/cancercare-ai/tree/main/templates/"
    click CSS "https://github.com/securdrgorp/cancercare-ai/blob/main/static/css/style.css"
    click JS "https://github.com/securdrgorp/cancercare-ai/blob/main/static/js/main.js"
    click FlaskApp "https://github.com/securdrgorp/cancercare-ai/blob/main/app.py"
    click Nlp "https://github.com/securdrgorp/cancercare-ai/blob/main/nlp_pipeline.py"
    click Context "https://github.com/securdrgorp/cancercare-ai/blob/main/context_provider.py"
    click BioBERT "https://github.com/securdrgorp/cancercare-ai/blob/main/biobert_qa.py"
    click DataH "https://github.com/securdrgorp/cancercare-ai/blob/main/data_handler.py"
    click Flat "https://github.com/securdrgorp/cancercare-ai/tree/main/data/"
    click ModelDir "https://github.com/securdrgorp/cancercare-ai/tree/main/model/"
    click SaveMod "https://github.com/securdrgorp/cancercare-ai/blob/main/scripts/saveModel.py"

    %% Styles
    classDef frontend fill:#AEDFF7,stroke:#333,stroke-width:1px
    classDef backend fill:#A8E6A3,stroke:#333,stroke-width:1px
    classDef service fill:#FFD59E,stroke:#333,stroke-width:1px
    classDef datastore fill:#E2E2E2,stroke:#333,stroke-width:1px
    classDef script fill:#D0B0FF,stroke:#333,stroke-width:1px
```
