How kafkascoop/blood-cancer-detection works
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: database.py Motor database layer · App.tsx Routes and guards · main.py FastAPI application · predictions.py Prediction endpoints
Components 22
Browser SPA
- React/Vite entry
frontend/src/main.tsx - Routes and guards
frontend/src/App.tsx - Session state
frontend/src/context/AuthContext.tsx - API client · Uses credentialed cookies for backend requests.
frontend/src/utils/api.ts - CBC and image workflows
frontend/src/pages/BloodTest.tsx
FastAPI backend
- FastAPI application · Assembles routers and lifecycle behavior.
backend/app/main.py - API router domains · Auth, prediction, results, dashboard, settings, and activity APIs.
- Prediction endpoints · Accepts validated CBC data and image uploads.
backend/app/routers/predictions.py - Authentication endpoints
backend/app/routers/auth.py - Token and password service
backend/app/services/auth.py - JWT cookie resolution · Resolves users for protected endpoints.
backend/app/dependencies.py - Motor database layer
backend/app/database.py - Request activity logger
backend/app/services/activity_logger.py
Inference and ML assets
- Prediction policy · Selects CNN, OpenCV/Gradient-Boosting, or rule-based fallback.
backend/app/services/prediction.py - Model loading and inference
backend/app/services/model_service.py - Detection schemas
backend/app/models/detection.py - Packaged models · Random Forest, image Gradient-Boosting, and optional CNN.
backend/app/ml/models/blood_test_model.pkl
Offline training
- Sklearn training · Trains persisted sklearn models from generated and CSV data.
backend/app/ml/train.py - Optional CNN training
backend/app/ml/train_cnn.py - Synthetic data and image features · Generates data and extracts OpenCV morphology features.
backend/app/ml/data.py - CSV and labeled images
Other components
- MongoDB · Stores users, detections, settings, and TTL-retained activity logs.
Connections 25
- 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