# coder360-crypto/btp architecture

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

- Interactive diagram: https://gitdiagram.com/coder360-crypto/btp
- Repository: https://github.com/coder360-crypto/btp

## Overview

##### Project type and purpose
- This is a command-line Medical Visual Question Answering (VQA) pipeline. It is **not** a web app or microservice suite; rather, it is a stand-alone Python framework that performs a sequence of image-and-question processing phases to produce medically-grounded answers without any model training.
- The README describes a 7-stage pipeline: ROI detection, focused image creation, concept extraction (via NVIDIA’s Gemma API), mechanistic grounding checks, knowledge-graph building, graph traversal, and answer synthesis.

##### File structure
- Top-level scripts:
- `main.py` – CLI entry point for single or batch processing.
- `test_pipeline.py` – Runs the same pipeline in a test or mock mode and writes evaluation metrics.
- Core code lives under `src/`:
- `src/core/` – Implements pipeline phases (`roi_detector.py`, `vqa_pipeline.py`, etc.)
- `src/graphs/knowledge_graph.py` – Builds and traverses the medical knowledge graph.
- `src/utils/` – Helper functions (`image_utils.py`, `nlp_utils.py`).
- `src/visualization/` – Renders visual checks or metrics plots.
- `src/data/` – Data ingestion or CSV helpers (if any).
- Config and requirements:
- `.env` / `.env.example` – Environment variables for API keys and thresholds.
- `requirements.txt` – Python dependencies.

##### README for additional insights
- Confirms the seven pipeline phases, describes configuration parameters, and shows the directory structure.
- Mentions that external service (NVIDIA Gemma 3-27B-IT) is invoked for concept extraction.
- Shows outputs: JSON answers, aggregated metrics, and visualizations.

a. Main components
1. **CLI Interface** (`main.py` / `test_pipeline.py`)
2. **Configuration Loader** (`src/config.py` + `.env`)
3. **ROI Detector** (`src/core/roi_detector.py`, uses `image_utils.py`)
4. **Image Focusing** (Gaussian blur module, part of `roi_detector.py` or utils)
5. **Concept Extractor** (`src/core/concept_extractor.py`) – calls NVIDIA Gemma VLM API
6. **Grounding Checker** (`src/core/vqa_pipeline.py` or within extractor)
7. **Knowledge Graph Builder** (`src/graphs/knowledge_graph.py`)
8. **Graph Traversal & Answer Synthesizer** (`src/core/answer_synthesizer.py`)
9. **Visualization & Reporting** (`src/visualization/…` + metric plots in `outputs/`)
10. **Data Sink** – writes JSON, CSV, visualizations to `outputs/` or `test_outputs/`

b. Relationships & interactions
- **CLI** → loads **Config** → loops over each (image, question)
- → passes to **ROI Detector** → returns masked image + ROI metadata
- → **Concept Extractor** calls external VLM → returns raw concepts
- → **Grounding Checker** filters concepts → verified concepts
- → **Graph Builder** constructs a graph data structure
- → **Graph Traverser** queries graph using question semantics
- → **Answer Synthesizer** formats final answer
- → **Reporter** writes result JSON and, in test mode, passes answers to **Metrics Evaluator** → writes CSV/graphs.

c. Architectural patterns & principles
- **Pipeline pattern**: Each stage accepts input, processes, and emits to the next.
- **Separation of concerns**: Detection, extraction, grounding, graphing, synthesis are decoupled modules.
- **Config­driven**: `.env` + CLI flags control thresholds, model choice, and mode (mock/real).
- **External integration**: Clear boundary between internal logic and the NVIDIA Gemma API.
- **Testable**: `test_pipeline.py` allows mocking ROI and grounding.

d. Relevant technologies
- Python 3.8+
- NVIDIA Gemma 3-27B-IT (external VLM API)
- Spacy (for NLP preprocessing)
- NLTK (setup via `setup_nltk.py`)
- matplotlib/seaborn or similar (for visualizations)
- CSV / JSON file I/O

##### Elements
- **Labels**: Name each component exactly as in code (e.g., ROI Detector, Concept Extractor).
- Processing stages (rectangles in blue)
- Visualization / metrics (rectangles in purple)

##### Detailed capture of architectural elements
- Configuration injection at the top of pipeline.
- Utility libraries (`image_utils`, `nlp_utils`) as side dependencies feeding into ROI and extraction modules.
- Output directories (`outputs/visualizations`, `test_outputs/graphs`) as sinks.

1. Input Layer:
- CLI / Config
- Image & Question Source
2. Preprocessing & ROI:
- ROI Detector → Focused Image
3. Core Pipeline:
- Concept Extraction ↔ External VLM API
- Grounding Checker
- KnowledgeGraph Builder
- Graph Traversal & Answer Synthesizer
4. Output Layer:
- Answer JSON (outputs/)
- Visualizations (outputs/visualizations/)
- Test Metrics (test_outputs/)
5. Utility Modules (off to the side):
- image_utils, nlp_utils, spacy/NLTK setups
- config loader
- logging

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    subgraph "Input Layer"
        CLI["CLI Interface"]:::process
        Config["Configuration Loader"]:::process
        CLI -->|loads config| Config
    end

    %% Preprocessing & ROI
    subgraph "Preprocessing & ROI"
        ROIDetector["ROI Detector"]:::process
        FocusedImage["Focused Image"]:::process
        CLI -->|image+question| ROIDetector
        Config --> ROIDetector
        ROIDetector -->|mask image| FocusedImage
    end

    %% Core Pipeline
    subgraph "Core Pipeline"
        ConceptExtractor["Concept Extractor"]:::process
        ExternalVLM["NVIDIA Gemma VLM API"]:::external
        GroundingChecker["Grounding Checker"]:::process
        KGBuilder["Knowledge Graph Builder"]:::process
        GraphSynth["Graph Traversal & Answer Synthesizer"]:::process

        FocusedImage --> ConceptExtractor
        ConceptExtractor -->|calls API| ExternalVLM
        ExternalVLM --> ConceptExtractor
        ConceptExtractor --> GroundingChecker
        GroundingChecker --> KGBuilder
        KGBuilder --> GraphSynth
    end

    %% Test Mode Metrics
    subgraph "Test Mode"
        Metrics["Metrics Evaluator"]:::visual
        GraphSynth -->|in test mode| Metrics
    end

    %% Output Layer
    subgraph "Output Layer"
        AnswerJSON["Answer JSON"]:::datastore
        Visuals["Visualizations"]:::visual
        TestMetrics["Test Metrics CSV/Graphs"]:::visual
        GraphSynth -->|writes| AnswerJSON
        GraphSynth -->|writes| Visuals
        Metrics -->|writes| TestMetrics
    end

    %% Utility Modules
    subgraph "Utility Modules"
        ImageUtils["image_utils"]:::util
        NLPUtils["nlp_utils"]:::util
        ConfigLoader["config.py"]:::util
        ImageUtils -.-> ROIDetector
        NLPUtils -.-> ConceptExtractor
        ConfigLoader -.-> Config
    end

    %% Click Events
    click CLI "https://github.com/coder360-crypto/btp/blob/main/main.py"
    click CLI "https://github.com/coder360-crypto/btp/blob/main/test_pipeline.py"
    click Config "https://github.com/coder360-crypto/btp/blob/main/src/config.py"
    click Config "https://github.com/coder360-crypto/btp/blob/main/.env"
    click ROIDetector "https://github.com/coder360-crypto/btp/blob/main/src/core/roi_detector.py"
    click ImageUtils "https://github.com/coder360-crypto/btp/blob/main/src/utils/image_utils.py"
    click ConceptExtractor "https://github.com/coder360-crypto/btp/blob/main/src/core/concept_extractor.py"
    click GroundingChecker "https://github.com/coder360-crypto/btp/blob/main/src/core/vqa_pipeline.py"
    click KGBuilder "https://github.com/coder360-crypto/btp/blob/main/src/graphs/knowledge_graph.py"
    click GraphSynth "https://github.com/coder360-crypto/btp/blob/main/src/core/answer_synthesizer.py"
    click Visuals "https://github.com/coder360-crypto/btp/tree/main/src/visualization/"
    click Visuals "https://github.com/coder360-crypto/btp/tree/main/outputs/visualizations/"
    click AnswerJSON "https://github.com/coder360-crypto/btp/tree/main/outputs/"
    click TestMetrics "https://github.com/coder360-crypto/btp/tree/main/test_outputs/"

    %% Styles
    classDef process fill:#D0E8FF,stroke:#024873,stroke-width:2px
    classDef external fill:#FFE5B4,stroke:#CC6600,stroke-dasharray: 5 5,stroke-width:2px
    classDef datastore fill:#D5F5E3,stroke:#1D8348,stroke-width:2px,shape:cylinder
    classDef visual fill:#F2D7D5,stroke:#C0392B,stroke-width:2px
    classDef util fill:#E8E8E8,stroke:#7F8C8D,stroke-width:2px,stroke-dasharray: 2 2
```
