# kafkascoop/blood-cancer-detection architecture

> Architecture diagram of the GitHub repository kafkascoop/blood-cancer-detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kafkascoop/blood-cancer-detection
- Repository: https://github.com/kafkascoop/blood-cancer-detection

## Overview

#### Purpose and Runtime Shape
HematoScan is a research/educational full-stack blood-cancer screening system. It accepts CBC measurements or blood-smear images, classifies Normal/Leukemia/Lymphoma/Myeloma, persists results, and presents history and dashboard analytics. Runtime entry points are FastAPI at `backend/app/main.py` and the React/Vite SPA at `frontend/src/main.tsx`.

#### Frontend Application Boundary
The browser client is a React 19 + TypeScript SPA. `frontend/src/App.tsx` defines routing and route protection; `frontend/src/context/AuthContext.tsx` maintains session state; `frontend/src/utils/api.ts` is the Axios boundary to the backend, including cookie credentials. Diagnostic workflows live in `frontend/src/pages/BloodTest.tsx` and `frontend/src/pages/UploadImage.tsx`; result/history/dashboard views consume persisted API data.

#### API Composition and Access Control
FastAPI assembles API routers and application lifecycle behavior in `backend/app/main.py`. Authentication endpoints are implemented in `backend/app/routers/auth.py`; prediction, result, dashboard, settings, and activity APIs are separate router domains under `backend/app/routers/`. Protected endpoints rely on JWT-cookie user resolution in `backend/app/dependencies.py`, while password hashing and token issuance are encapsulated in `backend/app/services/auth.py`.

#### Prediction Workflows
`backend/app/routers/predictions.py` receives validated CBC data or uploaded images and delegates inference to the service layer. CBC inference uses a persisted scikit-learn Random Forest model. Image inference is mode-driven: auto attempts CNN then OpenCV-feature/Gradient-Boosting; explicit modes constrain that choice; unavailable models fall back to rule-based prediction. The routing/fallback policy belongs in `backend/app/services/prediction.py`; model loading and inference are in `backend/app/services/model_service.py`; request/result schemas are in `backend/app/models/detection.py`.

#### ML Assets and Offline Training
Packaged runtime models reside in `backend/app/ml/models/blood_test_model.pkl`, `backend/app/ml/models/image_model.pkl`, and `backend/app/ml/models/cnn_image_model.keras`. Training and dataset generation are offline processes, not request-path work: `backend/app/ml/train.py` trains sklearn models, `backend/app/ml/train_cnn.py` trains the optional TensorFlow/Keras CNN, and `backend/app/ml/data.py` generates synthetic data and extracts OpenCV morphology features. CSV data and labeled image directories under `backend/data/` support these pipelines.

#### Persistence, Settings, and Observability
MongoDB is the durable system of record, connected through Motor in `backend/app/database.py`; configuration is environment-based in `backend/app/config.py`. Core collections cover users, detections, settings, and activity logs. Prediction records enable results/history and dashboard aggregations through `backend/app/routers/results.py` and `backend/app/routers/dashboard.py`. Request activity is captured by `backend/app/services/activity_logger.py`, with activity query APIs in `backend/app/routers/activities.py`; retention is enforced by MongoDB TTL indexing.

#### External Dependencies and Operational Boundaries
Required infrastructure is MongoDB; backend deployment also requires a configured JWT signing secret, CORS origin, and upload directory. The API is intended to run under Uvicorn/Python 3.11; the SPA is built and served by Vite/Node 20+. Material ML runtimes are scikit-learn and OpenCV, with TensorFlow optional for CNN support. The system deliberately remains available during model failures through its rule-based prediction boundary.

Main entry points: [`backend/app/database.py`](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/database.py) (Motor database layer), [`frontend/src/App.tsx`](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/App.tsx) (Routes and guards), [`backend/app/main.py`](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/main.py) (FastAPI application), [`backend/app/routers/predictions.py`](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/routers/predictions.py) (Prediction endpoints)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser SPA"]
  node_spa_entry["React/Vite entry<br/>SPA entry<br/>[main.tsx]"]
  node_app_routes["Routes and guards<br/>React app shell<br/>[App.tsx]"]
  node_auth_context["Session state<br/>React context<br/>[AuthContext.tsx]"]
  node_api_client["API client<br/>Axios boundary<br/>[api.ts]"]
  node_diagnostic_ui["CBC and image workflows<br/>React pages<br/>[BloodTest.tsx]"]
end

subgraph group_api["FastAPI backend"]
  node_api_main["FastAPI application<br/>API composition<br/>[main.py]"]
  node_api_routers["API router domains<br/>FastAPI routers"]
  node_prediction_router["Prediction endpoints<br/>FastAPI router<br/>[predictions.py]"]
  node_auth_router["Authentication endpoints<br/>FastAPI router<br/>[auth.py]"]
  node_auth_service["Token and password service<br/>Auth service<br/>[auth.py]"]
  node_jwt_dependency["JWT cookie resolution<br/>Access control<br/>[dependencies.py]"]
  node_database_layer["Motor database layer<br/>Persistence adapter<br/>[database.py]"]
  node_activity_logger["Request activity logger<br/>Observability service<br/>[activity_logger.py]"]
end

subgraph group_ml["Inference and ML assets"]
  node_prediction_service{{"Prediction policy<br/>Inference orchestration<br/>[prediction.py]"}}
  node_model_service["Model loading and inference<br/>ML service<br/>[model_service.py]"]
  node_detection_schema["Detection schemas<br/>Request/result models<br/>[detection.py]"]
  node_runtime_models["Packaged models<br/>Model artifacts"]
end

subgraph group_offline["Offline training"]
  node_training["Sklearn training<br/>Offline pipeline<br/>[train.py]"]
  node_cnn_training["Optional CNN training<br/>Offline pipeline<br/>[train_cnn.py]"]
  node_data_pipeline["Synthetic data and image features<br/>Offline data pipeline<br/>[data.py]"]
  node_training_data["CSV and labeled images<br/>Training datasets"]
end

node_mongodb[("MongoDB<br/>System of record")]

node_spa_entry -->|"boots"| node_app_routes
node_app_routes -->|"uses"| node_auth_context
node_app_routes -->|"routes to"| node_diagnostic_ui
node_auth_context -->|"auth requests"| node_api_client
node_diagnostic_ui -->|"submits and reads results"| node_api_client
node_api_client -->|"credentialed HTTP"| node_api_main
node_api_main -->|"mounts"| node_api_routers
node_api_routers -->|"includes"| node_prediction_router
node_api_routers -->|"includes"| node_auth_router
node_api_routers -->|"protects endpoints"| node_jwt_dependency
node_auth_router -->|"delegates"| node_auth_service
node_prediction_router -->|"validates"| node_detection_schema
node_prediction_router -->|"delegates"| node_prediction_service
node_prediction_service -->|"selects inference"| node_model_service
node_model_service -->|"loads"| node_runtime_models
node_prediction_service -->|"persists detections"| node_database_layer
node_api_routers -->|"results and analytics queries"| node_database_layer
node_database_layer -->|"Motor"| node_mongodb
node_api_main -->|"captures requests"| node_activity_logger
node_activity_logger -->|"writes activity"| node_database_layer
node_training_data -->|"supplies inputs"| node_data_pipeline
node_data_pipeline -->|"prepares features"| node_training
node_data_pipeline -->|"prepares images"| node_cnn_training
node_training -.->|"produces sklearn artifacts"| node_runtime_models
node_cnn_training -.->|"produces optional CNN"| node_runtime_models

click node_spa_entry "https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/main.tsx"
click node_app_routes "https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/App.tsx"
click node_auth_context "https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/context/AuthContext.tsx"
click node_api_client "https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/utils/api.ts"
click node_diagnostic_ui "https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/pages/BloodTest.tsx"
click node_api_main "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/main.py"
click node_prediction_router "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/routers/predictions.py"
click node_auth_router "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/routers/auth.py"
click node_auth_service "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/auth.py"
click node_jwt_dependency "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/dependencies.py"
click node_prediction_service "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/prediction.py"
click node_model_service "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/model_service.py"
click node_detection_schema "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/models/detection.py"
click node_runtime_models "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/models/blood_test_model.pkl"
click node_database_layer "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/database.py"
click node_activity_logger "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/activity_logger.py"
click node_training "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/train.py"
click node_cnn_training "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/train_cnn.py"
click node_data_pipeline "https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/data.py"

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_spa_entry,node_app_routes,node_auth_context,node_api_client,node_diagnostic_ui toneBlue
class node_api_main,node_api_routers,node_prediction_router,node_auth_router,node_auth_service,node_jwt_dependency,node_database_layer,node_activity_logger toneAmber
class node_prediction_service,node_model_service,node_detection_schema,node_runtime_models toneMint
class node_training,node_cnn_training,node_data_pipeline,node_training_data toneRose
class node_mongodb toneNeutral
```

## Components

### Browser SPA

- [React/Vite entry](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/main.tsx): SPA entry · `frontend/src/main.tsx`
- [Routes and guards](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/App.tsx): React app shell · `frontend/src/App.tsx`
- [Session state](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/context/AuthContext.tsx): React context · `frontend/src/context/AuthContext.tsx`
- [API client](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/utils/api.ts): Axios boundary · Uses credentialed cookies for backend requests. · `frontend/src/utils/api.ts`
- [CBC and image workflows](https://github.com/kafkascoop/blood-cancer-detection/blob/main/frontend/src/pages/BloodTest.tsx): React pages · `frontend/src/pages/BloodTest.tsx`

### FastAPI backend

- [FastAPI application](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/main.py): API composition · Assembles routers and lifecycle behavior. · `backend/app/main.py`
- **API router domains**: FastAPI routers · Auth, prediction, results, dashboard, settings, and activity APIs.
- [Prediction endpoints](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/routers/predictions.py): FastAPI router · Accepts validated CBC data and image uploads. · `backend/app/routers/predictions.py`
- [Authentication endpoints](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/routers/auth.py): FastAPI router · `backend/app/routers/auth.py`
- [Token and password service](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/auth.py): Auth service · `backend/app/services/auth.py`
- [JWT cookie resolution](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/dependencies.py): Access control · Resolves users for protected endpoints. · `backend/app/dependencies.py`
- [Motor database layer](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/database.py): Persistence adapter · `backend/app/database.py`
- [Request activity logger](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/activity_logger.py): Observability service · `backend/app/services/activity_logger.py`

### Inference and ML assets

- [Prediction policy](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/prediction.py): Inference orchestration · Selects CNN, OpenCV/Gradient-Boosting, or rule-based fallback. · `backend/app/services/prediction.py`
- [Model loading and inference](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/services/model_service.py): ML service · `backend/app/services/model_service.py`
- [Detection schemas](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/models/detection.py): Request/result models · `backend/app/models/detection.py`
- [Packaged models](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/models/blood_test_model.pkl): Model artifacts · Random Forest, image Gradient-Boosting, and optional CNN. · `backend/app/ml/models/blood_test_model.pkl`

### Offline training

- [Sklearn training](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/train.py): Offline pipeline · Trains persisted sklearn models from generated and CSV data. · `backend/app/ml/train.py`
- [Optional CNN training](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/train_cnn.py): Offline pipeline · `backend/app/ml/train_cnn.py`
- [Synthetic data and image features](https://github.com/kafkascoop/blood-cancer-detection/blob/main/backend/app/ml/data.py): Offline data pipeline · Generates data and extracts OpenCV morphology features. · `backend/app/ml/data.py`
- **CSV and labeled images**: Training datasets

### Other components

- **MongoDB**: System of record · Stores users, detections, settings, and TTL-retained activity logs.

## Connections

- React/Vite entry → Routes and guards: boots
- Routes and guards → Session state: uses
- Routes and guards → CBC and image workflows: routes to
- Session state → API client: auth requests
- CBC and image workflows → API client: submits and reads results
- API client → FastAPI application: credentialed HTTP
- FastAPI application → API router domains: mounts
- API router domains → Prediction endpoints: includes
- API router domains → Authentication endpoints: includes
- API router domains → JWT cookie resolution: protects endpoints
- Authentication endpoints → Token and password service: delegates
- Prediction endpoints → Detection schemas: validates
- Prediction endpoints → Prediction policy: delegates
- Prediction policy → Model loading and inference: selects inference
- Model loading and inference → Packaged models: loads
- Prediction policy → Motor database layer: persists detections
- API router domains → Motor database layer: results and analytics queries
- Motor database layer → MongoDB: Motor
- FastAPI application → Request activity logger: captures requests
- Request activity logger → Motor database layer: writes activity
- CSV and labeled images → Synthetic data and image features: supplies inputs
- Synthetic data and image features → Sklearn training: prepares features
- Synthetic data and image features → Optional CNN training: prepares images
- Sklearn training → Packaged models: produces sklearn artifacts
- Optional CNN training → Packaged models: produces optional CNN
