# astroshreyas1/lesioniq architecture

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

- Interactive diagram: https://gitdiagram.com/astroshreyas1/lesioniq
- Repository: https://github.com/astroshreyas1/lesioniq

## Overview

1. This is a split monorepo with two main runtime surfaces: a Python backend that performs dermoscopy inference/training, and a React + TypeScript frontend that acts as a clinical review workstation. The two communicate through one backend API contract centered on a typed `CaseRecord`.

2. The backend is the computational core. `backend/api.py` exposes a FastAPI endpoint (`POST /cases/analyze`) that receives an uploaded image plus patient metadata, then calls the inference pipeline. A standalone CLI wrapper also exists via `backend/inference.py`, so the same pipeline serves both API and command-line use.

3. The inference flow is sequential and artifact-producing: input image + metadata → preprocessing → hybrid classifier inference → post-processing/calibration → explainability generation → local SLM narration → `diagnosis.json` plus image artifacts. The output is stored as a per-case bundle under `backend/output/inference/<case_id>/`.

4. Image preprocessing is its own subsystem under `backend/preprocessing/`. It applies a fixed dermoscopy cleanup chain: hair removal (DullRazor), color normalization, CLAHE in LAB space, and circular border/vignette removal. These stages are saved as intermediate images and exposed to the frontend for provenance review.

5. The classifier subsystem under `backend/classifier/` contains the model definition, training, evaluation, explainability, and inference code. The core model is a dual-backbone hybrid: EfficientNet-B4 + Swin Transformer V2, with an optional metadata MLP fused into the final classifier head for the full mode.

6. Training is separate from inference and is driven by ISIC 2019 data manifests in `backend/data/`. It includes weighted sampling, augmentation, CutMix, SWA, and ablation scripts for the different model modes. This keeps the offline model-development path isolated from the runtime API path.

7. Post-training calibration is another distinct layer. Files in `backend/checkpoints/` hold the trained weights and learned runtime calibration parameters: threshold scales, temperature, and a MEL safety threshold. These are loaded during inference to adjust class scores and decision thresholds without retraining.

8. Explainability is first-class in the backend. The pipeline emits Grad-CAM++ overlays for the CNN branch, attention rollout for the transformer branch, and metadata attribution summaries. These are packaged into the case bundle and surfaced in the UI as separate review artifacts rather than being hidden behind one combined score.

9. There is also a local small-language-model explanation step. The backend passes `diagnosis.json` and visual evidence to an Ollama-hosted Gemma model inside Docker Compose, then validates or repairs the generated report before returning it. The architecture explicitly treats this as a downstream narrative layer, not the source of truth for diagnosis.

10. The frontend under `frontend/src/` is a stateful single-page app that consumes the same typed case record across multiple screens: Case Review, Explainability, Preprocessing, History, Compare, and Settings. It is organized into layout, primitive UI, and domain-specific components, with the API adapter isolated in `frontend/src/lib/lesioniqApi.ts`.

11. The frontend’s main data boundary is the live analysis response. Uploaded images only run inference once required metadata is complete, and the UI expects the backend to return both structured predictions and artifact URLs. Demo cases exist only as optional seed data behind an environment flag and do not change the live API path.

12. Deployment is also split: the frontend is built as a static Vite app, while the backend runs on a local GPU host. Docker Compose brings up the backend plus Ollama, and the README also documents a Vercel + ngrok pattern for exposing the frontend and backend separately. External dependencies that materially affect architecture are ISIC 2019 for training and Ollama/Gemma for narrative generation.

13. In diagram form, the key boundary is between “analysis bundle generation” in Python and “case review/rendering” in React. Everything else—preprocessing, classification, calibration, explainability, and SLM narration—feeds that bundle, while the UI is a consumer of it rather than a participant in model logic.

Main entry points: [`frontend/src/app/App.tsx`](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/app/App.tsx) (App shell), [`backend/classifier/models.py`](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py) (Classifier), [`backend/preprocessing`](https://github.com/astroshreyas1/lesioniq/tree/main/backend/preprocessing) (Preprocess), [`backend/api.py`](https://github.com/astroshreyas1/lesioniq/blob/main/backend/api.py) (API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Python backend"]
  node_api["API<br/>[api.py]"]
  node_cli["CLI<br/>inference wrapper<br/>[inference.py]"]
  node_preprocess["Preprocess<br/>image pipeline"]
  node_dullrazor["DullRazor<br/>cleanup stage<br/>[dull_razor.py]"]
  node_shade["Normalize<br/>color normalization<br/>[shades_of_grey.py]"]
  node_clahe["CLAHE<br/>contrast stage<br/>[apply_clahe.py]"]
  node_border["Border removal<br/>cleanup stage"]
  node_classifier["Classifier<br/>hybrid model<br/>[models.py]"]
  node_effnet["EfficientNet<br/>cnn backbone<br/>[models.py]"]
  node_swin["Swin V2<br/>transformer backbone<br/>[models.py]"]
  node_meta_mlp["Metadata MLP<br/>feature fusion<br/>[models.py]"]
  node_train["Training<br/>[train.py]"]
  node_eval["Evaluation<br/>metrics and ablation<br/>[evaluate.py]"]
  node_calibrate[("Calibration<br/>runtime tuning")]
  node_xai["Explainability<br/>artifact generation<br/>[explainability.py]"]
  node_bundle[("Case bundle<br/>analysis output")]
  node_slm["Narration<br/>LLM postprocess<br/>[api.py]"]
end

subgraph group_frontend["React frontend"]
  node_case["Case record<br/>typed contract<br/>[lesioniq.ts]"]
  node_api_client["API client<br/>frontend adapter<br/>[lesioniqApi.ts]"]
  node_app["App shell<br/>spa shell<br/>[App.tsx]"]
  node_review["Case review<br/>screen<br/>[CaseReview.tsx]"]
  node_explain["Explainability UI<br/>screen<br/>[Explainability.tsx]"]
  node_pre_ui["Preprocessing UI<br/>screen<br/>[Preprocessing.tsx]"]
  node_history["History<br/>screen<br/>[History.tsx]"]
  node_compare["Compare<br/>screen<br/>[Compare.tsx]"]
  node_settings["Settings<br/>screen<br/>[Settings.tsx]"]
end

subgraph group_ops["Deployment"]
  node_compose["Compose<br/>docker stack<br/>[docker-compose.yml]"]
  node_backend_image["Backend image<br/>container build"]
  node_vite["Frontend build<br/>static app build<br/>[vite.config.ts]"]
end

node_api -->|"preprocesses"| node_preprocess
node_api -->|"runs"| node_classifier
node_cli -->|"preprocesses"| node_preprocess
node_cli -->|"runs"| node_classifier
node_preprocess -->|"stage"| node_dullrazor
node_preprocess -->|"stage"| node_shade
node_preprocess -->|"stage"| node_clahe
node_preprocess -->|"stage"| node_border
node_classifier -->|"backbone"| node_effnet
node_classifier -->|"backbone"| node_swin
node_classifier -.->|"optional fusion"| node_meta_mlp
node_classifier -->|"applies"| node_calibrate
node_classifier -->|"emits"| node_xai
node_train -.->|"trains"| node_classifier
node_train -.->|"measured by"| node_eval
node_calibrate -->|"writes"| node_bundle
node_xai -->|"packages"| node_bundle
node_api -->|"narrates via"| node_slm
node_slm -->|"repairs"| node_bundle
node_bundle -->|"exposes"| node_case
node_case -->|"consumed by"| node_api_client
node_api_client -->|"feeds"| node_app
node_app -->|"navigates"| node_review
node_app -->|"navigates"| node_explain
node_app -->|"navigates"| node_pre_ui
node_app -->|"navigates"| node_history
node_app -->|"navigates"| node_compare
node_app -->|"navigates"| node_settings
node_compose -->|"runs"| node_api
node_compose -->|"runs"| node_slm
node_backend_image -.->|"builds"| node_api
node_vite -.->|"builds"| node_app

click node_api "https://github.com/astroshreyas1/lesioniq/blob/main/backend/api.py"
click node_cli "https://github.com/astroshreyas1/lesioniq/blob/main/backend/inference.py"
click node_preprocess "https://github.com/astroshreyas1/lesioniq/tree/main/backend/preprocessing"
click node_dullrazor "https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/dull_razor.py"
click node_shade "https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/shades_of_grey.py"
click node_clahe "https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/apply_clahe.py"
click node_border "https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/remove_circular_border.py"
click node_classifier "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py"
click node_effnet "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py"
click node_swin "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py"
click node_meta_mlp "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py"
click node_train "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/train.py"
click node_eval "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/evaluate.py"
click node_calibrate "https://github.com/astroshreyas1/lesioniq/tree/main/backend/checkpoints"
click node_xai "https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/explainability.py"
click node_slm "https://github.com/astroshreyas1/lesioniq/blob/main/backend/api.py"
click node_case "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/types/lesioniq.ts"
click node_api_client "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/lib/lesioniqApi.ts"
click node_app "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/app/App.tsx"
click node_review "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/CaseReview.tsx"
click node_explain "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Explainability.tsx"
click node_pre_ui "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Preprocessing.tsx"
click node_history "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/History.tsx"
click node_compare "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Compare.tsx"
click node_settings "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Settings.tsx"
click node_compose "https://github.com/astroshreyas1/lesioniq/blob/main/docker-compose.yml"
click node_backend_image "https://github.com/astroshreyas1/lesioniq/tree/main/backend/Dockerfile"
click node_vite "https://github.com/astroshreyas1/lesioniq/blob/main/frontend/vite.config.ts"

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_api,node_cli,node_preprocess,node_dullrazor,node_shade,node_clahe,node_border,node_classifier,node_effnet,node_swin,node_meta_mlp,node_train,node_eval,node_calibrate,node_xai,node_bundle,node_slm toneBlue
class node_case,node_api_client,node_app,node_review,node_explain,node_pre_ui,node_history,node_compare,node_settings toneAmber
class node_compose,node_backend_image,node_vite toneMint
```

## Components

### Python backend

Inference, preprocessing, calibration, and training pipeline.

- [API](https://github.com/astroshreyas1/lesioniq/blob/main/backend/api.py): FastAPI endpoint · Receives case analysis requests and returns bundled results. · `backend/api.py`
- [CLI](https://github.com/astroshreyas1/lesioniq/blob/main/backend/inference.py): inference wrapper · Command-line entrypoint for the same inference pipeline. · `backend/inference.py`
- [Preprocess](https://github.com/astroshreyas1/lesioniq/tree/main/backend/preprocessing): image pipeline · Dermoscopy cleanup chain: hair removal, normalization, contrast enhancement, and border cleanup. · `backend/preprocessing`
- [DullRazor](https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/dull_razor.py): cleanup stage · Hair removal stage. · `backend/preprocessing/dull_razor.py`
- [Normalize](https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/shades_of_grey.py): color normalization · Color normalization stage in the preprocessing chain. · `backend/preprocessing/shades_of_grey.py`
- [CLAHE](https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/apply_clahe.py): contrast stage · Contrast enhancement in LAB space. · `backend/preprocessing/apply_clahe.py`
- [Border removal](https://github.com/astroshreyas1/lesioniq/blob/main/backend/preprocessing/remove_circular_border.py): cleanup stage · Circular border and vignette removal stage. · `backend/preprocessing/remove_circular_border.py`
- [Classifier](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py): hybrid model · Dual-backbone dermoscopy model with optional metadata fusion. · `backend/classifier/models.py`
- [EfficientNet](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py): cnn backbone · CNN branch used for local lesion feature extraction. · `backend/classifier/models.py`
- [Swin V2](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py): transformer backbone · Transformer branch used for global image context. · `backend/classifier/models.py`
- [Metadata MLP](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/models.py): feature fusion · Optional patient metadata encoder fused into the classifier head. · `backend/classifier/models.py`
- [Training](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/train.py): offline training · Weighted sampling, augmentation, CutMix, SWA, and ablations. · `backend/classifier/train.py`
- [Evaluation](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/evaluate.py): metrics and ablation · Model evaluation and ablation workflows. · `backend/classifier/evaluate.py`
- [Calibration](https://github.com/astroshreyas1/lesioniq/tree/main/backend/checkpoints): runtime tuning · Loads threshold scales, temperature, and safety thresholds for inference. · `backend/checkpoints`
- [Explainability](https://github.com/astroshreyas1/lesioniq/blob/main/backend/classifier/explainability.py): artifact generation · Produces Grad-CAM++, attention rollout, and metadata attribution summaries. · `backend/classifier/explainability.py`
- **Case bundle**: analysis output · Per-case diagnosis JSON plus image artifacts produced by inference.
- [Narration](https://github.com/astroshreyas1/lesioniq/blob/main/backend/api.py): LLM postprocess · Generates and repairs natural-language report text from diagnosis and evidence. · `backend/api.py`

### React frontend

Clinical review workstation and UI state.

- [Case record](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/types/lesioniq.ts): typed contract · Shared typed case payload and artifact metadata consumed by the UI. · `frontend/src/types/lesioniq.ts`
- [API client](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/lib/lesioniqApi.ts): frontend adapter · Typed adapter for backend case analysis and artifact retrieval. · `frontend/src/lib/lesioniqApi.ts`
- [App shell](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/app/App.tsx): spa shell · Top-level navigation and screen coordination for the workstation. · `frontend/src/app/App.tsx`
- [Case review](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/CaseReview.tsx): screen · Primary review surface for predictions, metadata, and uploaded cases. · `frontend/src/screens/CaseReview.tsx`
- [Explainability UI](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Explainability.tsx): screen · Renders overlays, attribution summaries, and evidence panels. · `frontend/src/screens/Explainability.tsx`
- [Preprocessing UI](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Preprocessing.tsx): screen · Shows preprocessing provenance and intermediate image artifacts. · `frontend/src/screens/Preprocessing.tsx`
- [History](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/History.tsx): screen · Displays prior cases and review history. · `frontend/src/screens/History.tsx`
- [Compare](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Compare.tsx): screen · Side-by-side case comparison workspace. · `frontend/src/screens/Compare.tsx`
- [Settings](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/src/screens/Settings.tsx): screen · User preferences and application configuration. · `frontend/src/screens/Settings.tsx`

### Deployment

Containerization and local service orchestration.

- [Compose](https://github.com/astroshreyas1/lesioniq/blob/main/docker-compose.yml): docker stack · Runs the backend alongside local Ollama for narration generation. · `docker-compose.yml`
- [Backend image](https://github.com/astroshreyas1/lesioniq/tree/main/backend/Dockerfile): container build · Backend runtime container definition. · `backend/Dockerfile`
- [Frontend build](https://github.com/astroshreyas1/lesioniq/blob/main/frontend/vite.config.ts): static app build · Vite-based frontend build and deployment surface. · `frontend/vite.config.ts`

## Connections

- API → Preprocess: preprocesses · API calls the image cleanup chain before inference.
- API → Classifier: runs · API invokes the hybrid classifier for a case.
- CLI → Preprocess: preprocesses · CLI uses the same preprocessing pipeline.
- CLI → Classifier: runs · CLI shares the inference core.
- Preprocess → DullRazor: stage · Hair removal stage.
- Preprocess → Normalize: stage · Color normalization stage.
- Preprocess → CLAHE: stage · Contrast enhancement stage.
- Preprocess → Border removal: stage · Final artifact cleanup stage.
- Classifier → EfficientNet: backbone · CNN branch used by the hybrid model.
- Classifier → Swin V2: backbone · Transformer branch used by the hybrid model.
- Classifier → Metadata MLP: optional fusion · Metadata path used in full model mode.
- Classifier → Calibration: applies · Inference loads calibration parameters before final decisions.
- Classifier → Explainability: emits · Model inference produces explainability artifacts.
- Training → Classifier: trains · Offline training produces model weights for runtime use.
- Training → Evaluation: measured by · Training is assessed through evaluation and ablation workflows.
- Calibration → Case bundle: writes · Calibrated scores and thresholds contribute to diagnosis output.
- Explainability → Case bundle: packages · Explainability outputs are stored in the per-case bundle.
- API → Narration: narrates via · API sends diagnosis evidence to the local language model step.
- Narration → Case bundle: repairs · Narrative output is validated and stored alongside the case bundle.
- Case bundle → Case record: exposes · The backend bundle is mapped into the typed case record.
- Case record → API client: consumed by · Frontend uses the typed contract through its API adapter.
- API client → App shell: feeds · Fetched case data drives the SPA.
- App shell → Case review: navigates · Primary review screen.
- App shell → Explainability UI: navigates · Explainability review screen.
- App shell → Preprocessing UI: navigates · Preprocessing provenance screen.
- App shell → History: navigates · Historical case screen.
- App shell → Compare: navigates · Comparison screen.
- App shell → Settings: navigates · Configuration screen.
- Compose → API: runs · Compose launches the backend service.
- Compose → Narration: runs · Compose also launches local Ollama for narration.
- Backend image → API: builds · Container image packages backend runtime.
- Frontend build → App shell: builds · Vite builds the frontend application.
