# nahyan0077/secure-code-analyzer-model architecture

> Architecture diagram of the GitHub repository nahyan0077/secure-code-analyzer-model, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nahyan0077/secure-code-analyzer-model
- Repository: https://github.com/nahyan0077/secure-code-analyzer-model

## Overview

1. This repository is a single-purpose machine learning system for binary vulnerability detection in C/C++ source code. Its main architectural boundary is between offline model development and online inference/explanation. The core artifact is a fine-tuned BERT-base classifier with an optimized decision threshold.

2. The data pipeline lives under `src/data/`. `dataset_loader.py` is the ingestion boundary for code samples and labels, while `preprocessing.py` performs code-specific normalization before tokenization. The README indicates that preprocessing is tailored to source code, including camelCase splitting and operator normalization, which materially affects model quality.

3. Model lifecycle code is split across `src/model/`. `train.py` handles training, `evaluate.py` computes performance metrics, `predict.py` is the inference entry point, and `model_loader.py` abstracts loading the trained model and tokenizer artifacts. This separation makes the training pipeline, evaluation pipeline, and runtime prediction path distinct.

4. The classifier itself is not a vanilla transformer-only setup; the README describes an optimized BERT-base with an MLP classifier head and class-imbalance handling. That means the architecture is likely: tokenized code input → BERT encoder → task-specific head → logits/probability → calibrated threshold decision.

5. Decision calibration is its own step. `scripts/calibrate_threshold.py` suggests the raw model score is not used directly; instead, a threshold is tuned to trade off recall vs precision for security triage. This is an important architectural boundary because the final “vulnerable / not vulnerable” output depends on calibration, not just the base classifier score.

6. Serving is implemented as an API layer in `src/api/`. `main.py` is the service entry point and `schemas.py` defines request/response contracts. The API accepts code plus a threshold override, runs the inference pipeline, and returns a vulnerability report rather than only a class label.

7. Explainability is a separate subsystem under `src/explainability/`. `shap_explainer.py`, `lime_explainer.py`, and `global_explainer.py` generate local and global token-level explanations, while `visualizer.py` renders them. This means the system is designed to expose both prediction and rationale, not just classification results.

8. There is an additional visualization path for demos and analysis. `src/utils/visualizer.py`, `generate_plots.py`, and `src/model/predict.py` with `--visualize` or `--attention` produce saved artifacts such as SHAP heatmaps, attention maps, and training/evaluation plots. Outputs are written to `reports/plots/`, which makes reporting a first-class part of the architecture.

9. Reporting artifacts are kept in `reports/`, including `metrics.json` and `model_card.md`. These serve as the handoff point between evaluation and documentation, and they also feed plot generation and presentation workflows. The repository even includes a presentation deck, reinforcing that analysis outputs are meant for human review.

10. Configuration and platform concerns are centralized in `configs/config.py` and `src/utils/`. `device.py` likely handles CPU/GPU selection, `logger.py` standardizes logging, and `utils/visualizer.py` supports shared plotting code. The project is packaged with `pyproject.toml` and `uv.lock`, and operationalized through `Makefile` targets such as setup, training, and development serving.

Main entry points: [`src/model/predict.py`](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/predict.py) (Prediction), [`src/model/train.py`](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/train.py) (Training), [`src/data/preprocessing.py`](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/data/preprocessing.py) (Preprocess code), [`src/utils/device.py`](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/utils/device.py) (Runtime utils)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_data["Data pipeline"]
  node_n_dataset_loader["Dataset loader<br/>ingestion<br/>[dataset_loader.py]"]
  node_n_preprocessing["Preprocess code<br/>normalization<br/>[preprocessing.py]"]
end

subgraph group_g_model["Model lifecycle"]
  node_n_train["Training<br/>[train.py]"]
  node_n_evaluate["Evaluation<br/>metrics pipeline<br/>[evaluate.py]"]
  node_n_predict["Prediction<br/>inference entry<br/>[predict.py]"]
  node_n_model_loader["Model loader<br/>artifact loader<br/>[model_loader.py]"]
  node_n_bert_core{{"BERT classifier<br/>bert+mlp"}}
  node_n_threshold["Threshold tuning<br/>calibration"]
end

subgraph group_g_api["Serving API"]
  node_n_api_main["API service<br/>serving entry<br/>[main.py]"]
  node_n_api_schemas["API schemas<br/>contracts<br/>[schemas.py]"]
end

subgraph group_g_xai["Explainability"]
  node_n_shap["SHAP explainer<br/>local xai<br/>[shap_explainer.py]"]
  node_n_lime["LIME explainer<br/>local xai<br/>[lime_explainer.py]"]
  node_n_global_xai["Global explainer<br/>global xai"]
  node_n_xai_viz["XAI visualizer<br/>rendering<br/>[visualizer.py]"]
end

subgraph group_g_ops["Reporting and ops"]
  node_n_runtime_utils["Runtime utils<br/>shared support<br/>[device.py]"]
  node_n_plot_utils["Plot utils<br/>plot support<br/>[visualizer.py]"]
  node_n_plots["Plot generation<br/>analysis script<br/>[generate_plots.py]"]
  node_n_reports[("Reports<br/>artifacts<br/>[metrics.json]")]
end

node_n_dataset_loader -->|"raw samples"| node_n_preprocessing
node_n_preprocessing -->|"token-ready data"| node_n_train
node_n_preprocessing -->|"eval data"| node_n_evaluate
node_n_preprocessing -->|"input code"| node_n_predict
node_n_train -->|"fine-tunes"| node_n_bert_core
node_n_bert_core -->|"model artifact"| node_n_model_loader
node_n_model_loader -->|"infer"| node_n_predict
node_n_train -->|"validation scores"| node_n_threshold
node_n_threshold -->|"decision rule"| node_n_predict
node_n_evaluate -->|"metrics"| node_n_reports
node_n_train -->|"training outputs"| node_n_reports
node_n_api_schemas -->|"contracts"| node_n_api_main
node_n_api_main -->|"serve inference"| node_n_predict
node_n_predict -->|"optional xai"| node_n_shap
node_n_predict -->|"optional xai"| node_n_lime
node_n_predict -->|"analysis"| node_n_global_xai
node_n_shap -->|"render"| node_n_xai_viz
node_n_lime -->|"render"| node_n_xai_viz
node_n_global_xai -->|"render"| node_n_xai_viz
node_n_plot_utils -->|"shared plots"| node_n_plots
node_n_plots -->|"save artifacts"| node_n_reports
node_n_runtime_utils -->|"device/logging"| node_n_train
node_n_runtime_utils -->|"device/logging"| node_n_predict
node_n_runtime_utils -->|"service support"| node_n_api_main

click node_n_dataset_loader "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/data/dataset_loader.py"
click node_n_preprocessing "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/data/preprocessing.py"
click node_n_train "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/train.py"
click node_n_evaluate "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/evaluate.py"
click node_n_predict "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/predict.py"
click node_n_model_loader "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/model_loader.py"
click node_n_threshold "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/scripts/calibrate_threshold.py"
click node_n_api_main "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/api/main.py"
click node_n_api_schemas "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/api/schemas.py"
click node_n_shap "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/shap_explainer.py"
click node_n_lime "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/lime_explainer.py"
click node_n_global_xai "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/global_explainer.py"
click node_n_xai_viz "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/visualizer.py"
click node_n_runtime_utils "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/utils/device.py"
click node_n_plot_utils "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/utils/visualizer.py"
click node_n_plots "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/generate_plots.py"
click node_n_reports "https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/reports/metrics.json"

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_n_dataset_loader,node_n_preprocessing toneBlue
class node_n_train,node_n_evaluate,node_n_predict,node_n_model_loader,node_n_bert_core,node_n_threshold toneAmber
class node_n_api_main,node_n_api_schemas toneMint
class node_n_shap,node_n_lime,node_n_global_xai,node_n_xai_viz toneRose
class node_n_runtime_utils,node_n_plot_utils,node_n_plots,node_n_reports toneIndigo
```

## Components

### Data pipeline

Ingests and normalizes C/C++ code samples for model consumption.

- [Dataset loader](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/data/dataset_loader.py): ingestion · Loads code samples and labels for offline and evaluation workflows. · `src/data/dataset_loader.py`
- [Preprocess code](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/data/preprocessing.py): normalization · Code-aware normalization such as camelCase splitting and operator normalization. · `src/data/preprocessing.py`

### Model lifecycle

Training, evaluation, loading, prediction, and threshold calibration.

- [Training](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/train.py): training pipeline · Fine-tunes the vulnerability classifier on prepared code data. · `src/model/train.py`
- [Evaluation](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/evaluate.py): metrics pipeline · Computes validation/test metrics for the classifier. · `src/model/evaluate.py`
- [Prediction](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/predict.py): inference entry · Runs model inference and optional visualization on new code input. · `src/model/predict.py`
- [Model loader](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/model/model_loader.py): artifact loader · Loads trained model and tokenizer artifacts for runtime use. · `src/model/model_loader.py`
- **BERT classifier**: bert+mlp · Fine-tuned BERT-base encoder with task-specific MLP head and class-imbalance handling.
- [Threshold tuning](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/scripts/calibrate_threshold.py): calibration · Converts raw scores into the final vulnerable/not-vulnerable decision. · `scripts/calibrate_threshold.py`

### Serving API

Online request/response boundary for vulnerability inference.

- [API service](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/api/main.py): serving entry · Online service boundary for vulnerability analysis requests. · `src/api/main.py`
- [API schemas](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/api/schemas.py): contracts · Defines request and response payloads for inference and reports. · `src/api/schemas.py`

### Explainability

Token-level and global explanation generation plus rendering.

- [SHAP explainer](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/shap_explainer.py): local xai · Generates token-level SHAP explanations for predictions. · `src/explainability/shap_explainer.py`
- [LIME explainer](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/lime_explainer.py): local xai · Generates alternative local explanations for code inputs. · `src/explainability/lime_explainer.py`
- [Global explainer](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/global_explainer.py): global xai · Summarizes model behavior across datasets or sample sets. · `src/explainability/global_explainer.py`
- [XAI visualizer](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/explainability/visualizer.py): rendering · Renders explanation artifacts such as heatmaps and token highlights. · `src/explainability/visualizer.py`

### Reporting and ops

Shared runtime utilities, plotting, and persisted evaluation artifacts.

- [Runtime utils](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/utils/device.py): shared support · Shared device selection and logging helpers used across runtime paths. · `src/utils/device.py`
- [Plot utils](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/src/utils/visualizer.py): plot support · Shared plotting and saved-artifact helpers for reports and demos. · `src/utils/visualizer.py`
- [Plot generation](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/generate_plots.py): analysis script · Produces training, evaluation, and explanation plots. · `generate_plots.py`
- [Reports](https://github.com/nahyan0077/secure-code-analyzer-model/blob/main/reports/metrics.json): artifacts · Persisted metrics and model card handoff for human review. · `reports/metrics.json`

## Connections

- Dataset loader → Preprocess code: raw samples · Loads labeled code before normalization.
- Preprocess code → Training: token-ready data · Feeds normalized code into training.
- Preprocess code → Evaluation: eval data · Prepares code for metric computation.
- Preprocess code → Prediction: input code · Normalizes online inference input.
- Training → BERT classifier: fine-tunes · Produces the task-specific classifier artifact.
- BERT classifier → Model loader: model artifact · Loaded later for inference and serving.
- Model loader → Prediction: infer · Provides the trained model and tokenizer at runtime.
- Training → Threshold tuning: validation scores · Uses held-out performance to select a decision threshold.
- Threshold tuning → Prediction: decision rule · Maps probability output to final label.
- Evaluation → Reports: metrics · Persists evaluation results and model summary artifacts.
- Training → Reports: training outputs · Feeds documentation and reporting artifacts.
- API schemas → API service: contracts · Defines the service payload boundary.
- API service → Prediction: serve inference · Routes requests into the prediction pipeline.
- Prediction → SHAP explainer: optional xai · Can emit SHAP explanations for predictions.
- Prediction → LIME explainer: optional xai · Can emit LIME explanations for predictions.
- Prediction → Global explainer: analysis · Feeds explanation workflows beyond single-sample inference.
- SHAP explainer → XAI visualizer: render · Produces visual explanation artifacts.
- LIME explainer → XAI visualizer: render · Produces visual explanation artifacts.
- Global explainer → XAI visualizer: render · Visualizes aggregate explanation outputs.
- Plot utils → Plot generation: shared plots · Supports report and demo plot generation.
- Plot generation → Reports: save artifacts · Writes generated plots and analysis outputs.
- Runtime utils → Training: device/logging · Shared runtime support for training execution.
- Runtime utils → Prediction: device/logging · Shared runtime support for inference execution.
- Runtime utils → API service: service support · Shared runtime support for the API service.
