# kdhungel/ml-api-threat-detection architecture

> Architecture diagram of the GitHub repository kdhungel/ml-api-threat-detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kdhungel/ml-api-threat-detection
- Repository: https://github.com/kdhungel/ml-api-threat-detection
- Video tour (about a minute): https://gitdiagram.com/kdhungel/ml-api-threat-detection/video

## Overview

The product detects model-extraction behavior by observing prediction requests over time: callers submit patient measurements to the API, which authenticates them, predicts, and logs each patient; BigQuery-backed detection evaluates caller-level behavioral signals and creates alerts. Synthetic and live traffic, plus the Python prototype, support validation. The cloud workflow and major capabilities are documented in the README; some cloud implementation details are unsampled, so those relationships are cited to the README rather than inferred from source.

Main entry points: [`api/app/main.py`](https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/main.py) (Prediction endpoint), [`detection/function/main.py`](https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/function/main.py) (Scheduled detector), [`traffic/generate.py`](https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/generate.py) (Synthetic traffic), [`infra/bigquery/sql/requests_live.sql`](https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/requests_live.sql) (Request views)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prediction_api["Prediction API"]
  node_prediction_endpoint["Prediction endpoint<br/>[main.py]"]
  node_key_auth["Key authentication<br/>[auth.py]"]
  node_classifier["Breast cancer model<br/>[model.py]"]
  node_request_logging["Request log writers<br/>[logs.py]"]
end

subgraph group_log_pipeline["Log Pipeline"]
  node_cloud_logging[("Cloud Logging")]
  node_bigquery_logs[("BigQuery request logs<br/>[request_logs.sql]")]
  node_request_views["Request views<br/>[requests_live.sql]"]
end

subgraph group_detection_alerts["Detection and Alerts"]
  node_signal_sql["BigQuery signal queries"]
  node_detector_function["Scheduled detector<br/>[main.py]"]
  node_scheduler["Cloud Scheduler"]
  node_alert_store[("BigQuery alerts<br/>[alerts.sql]")]
  node_monitoring["Log alerting"]
  node_email(("Email notifications"))
  node_dashboard["Looker Studio dashboard<br/>[dashboard.md]"]
end

subgraph group_traffic_validation["Traffic and Validation"]
  node_generated_traffic["Synthetic traffic<br/>[generate.py]"]
  node_live_traffic["Live traffic callers<br/>[callers.py]"]
  node_csv_adapter["Local log adapter<br/>[logs_to_csv.py]"]
  node_signal_prototype["Signal calculations<br/>[signals.py]"]
  node_prototype_evaluator["Prototype evaluator<br/>[evaluate.py]"]
  node_threshold_study["Threshold study<br/>[thresholds.py]"]
end

node_callers(("Hospitals and callers"))

node_callers -->|"submit predictions"| node_prediction_endpoint
node_prediction_endpoint -->|"checks key"| node_key_auth
node_prediction_endpoint -->|"predicts"| node_classifier
node_prediction_endpoint -->|"writes rows"| node_request_logging
node_request_logging -->|"emits entries"| node_cloud_logging
node_cloud_logging -->|"sink writes"| node_bigquery_logs
node_bigquery_logs -->|"feeds views"| node_request_views
node_generated_traffic -->|"supplies generated data"| node_request_views
node_live_traffic -->|"calls API"| node_prediction_endpoint
node_live_traffic -->|"uses model answers"| node_classifier
node_csv_adapter -->|"imports features"| node_signal_prototype
node_generated_traffic -->|"provides test data"| node_prototype_evaluator
node_prototype_evaluator -->|"scores signals"| node_signal_prototype
node_threshold_study -->|"simulates callers"| node_generated_traffic
node_threshold_study -->|"tests thresholds"| node_signal_prototype
node_request_views -->|"provides requests"| node_signal_sql
node_scheduler -->|"invokes daily"| node_detector_function
node_detector_function -->|"computes signals"| node_signal_sql
node_detector_function -->|"merges alerts"| node_alert_store
node_detector_function -->|"logs alert events"| node_cloud_logging
node_cloud_logging -->|"drives metrics"| node_monitoring
node_monitoring -->|"notifies"| node_email
node_alert_store -->|"feeds dashboard"| node_dashboard

click node_prediction_endpoint "https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/main.py"
click node_key_auth "https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/auth.py"
click node_classifier "https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/model.py"
click node_request_logging "https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/logs.py"
click node_bigquery_logs "https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/request_logs.sql"
click node_request_views "https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/requests_live.sql"
click node_generated_traffic "https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/generate.py"
click node_live_traffic "https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/live/callers.py"
click node_csv_adapter "https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/live/logs_to_csv.py"
click node_signal_prototype "https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/signals.py"
click node_prototype_evaluator "https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/evaluate.py"
click node_threshold_study "https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/thresholds.py"
click node_signal_sql "https://github.com/kdhungel/ml-api-threat-detection/tree/main/detection/queries"
click node_detector_function "https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/function/main.py"
click node_alert_store "https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/alerts.sql"
click node_monitoring "https://github.com/kdhungel/ml-api-threat-detection/tree/main/infra/monitoring"
click node_dashboard "https://github.com/kdhungel/ml-api-threat-detection/blob/main/docs/dashboard.md"

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_prediction_endpoint,node_key_auth,node_classifier,node_request_logging toneBlue
class node_cloud_logging,node_bigquery_logs,node_request_views toneAmber
class node_signal_sql,node_detector_function,node_scheduler,node_alert_store,node_monitoring,node_email,node_dashboard toneMint
class node_generated_traffic,node_live_traffic,node_csv_adapter,node_signal_prototype,node_prototype_evaluator,node_threshold_study toneRose
class node_callers toneIndigo
```

## Components

### Prediction API

- [Prediction endpoint](https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/main.py): `api/app/main.py`
- [Key authentication](https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/auth.py): `api/app/auth.py`
- [Breast cancer model](https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/model.py): `api/app/model.py`
- [Request log writers](https://github.com/kdhungel/ml-api-threat-detection/blob/main/api/app/logs.py): `api/app/logs.py`

### Log Pipeline

- **Cloud Logging**
- [BigQuery request logs](https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/request_logs.sql): `infra/bigquery/sql/request_logs.sql`
- [Request views](https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/requests_live.sql): `infra/bigquery/sql/requests_live.sql`

### Detection and Alerts

- [BigQuery signal queries](https://github.com/kdhungel/ml-api-threat-detection/tree/main/detection/queries): `detection/queries`
- [Scheduled detector](https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/function/main.py): `detection/function/main.py`
- **Cloud Scheduler**
- [BigQuery alerts](https://github.com/kdhungel/ml-api-threat-detection/blob/main/infra/bigquery/sql/alerts.sql): `infra/bigquery/sql/alerts.sql`
- [Log alerting](https://github.com/kdhungel/ml-api-threat-detection/tree/main/infra/monitoring): `infra/monitoring`
- **Email notifications**
- [Looker Studio dashboard](https://github.com/kdhungel/ml-api-threat-detection/blob/main/docs/dashboard.md): `docs/dashboard.md`

### Traffic and Validation

- [Synthetic traffic](https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/generate.py): `traffic/generate.py`
- [Live traffic callers](https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/live/callers.py): `traffic/live/callers.py`
- [Local log adapter](https://github.com/kdhungel/ml-api-threat-detection/blob/main/traffic/live/logs_to_csv.py): `traffic/live/logs_to_csv.py`
- [Signal calculations](https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/signals.py): `detection/prototype/signals.py`
- [Prototype evaluator](https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/evaluate.py): `detection/prototype/evaluate.py`
- [Threshold study](https://github.com/kdhungel/ml-api-threat-detection/blob/main/detection/prototype/thresholds.py): `detection/prototype/thresholds.py`

### Other components

- **Hospitals and callers**

## Connections

- Hospitals and callers → Prediction endpoint: submit predictions (evidence: `README.md`)
- Prediction endpoint → Key authentication: checks key (evidence: `api/app/main.py`)
- Prediction endpoint → Breast cancer model: predicts (evidence: `api/app/main.py`)
- Prediction endpoint → Request log writers: writes rows (evidence: `api/app/main.py`)
- Request log writers → Cloud Logging: emits entries (evidence: `api/app/logs.py`)
- Cloud Logging → BigQuery request logs: sink writes (evidence: `README.md`)
- BigQuery request logs → Request views: feeds views (evidence: `README.md`)
- Synthetic traffic → Request views: supplies generated data (evidence: `README.md`)
- Live traffic callers → Prediction endpoint: calls API (evidence: `README.md`)
- Live traffic callers → Breast cancer model: uses model answers (evidence: `traffic/live/callers.py`)
- Local log adapter → Signal calculations: imports features (evidence: `traffic/live/logs_to_csv.py`)
- Synthetic traffic → Prototype evaluator: provides test data (evidence: `detection/prototype/evaluate.py`)
- Prototype evaluator → Signal calculations: scores signals (evidence: `detection/prototype/evaluate.py`)
- Threshold study → Synthetic traffic: simulates callers (evidence: `detection/prototype/thresholds.py`)
- Threshold study → Signal calculations: tests thresholds (evidence: `detection/prototype/thresholds.py`)
- Request views → BigQuery signal queries: provides requests (evidence: `README.md`)
- Cloud Scheduler → Scheduled detector: invokes daily (evidence: `README.md`)
- Scheduled detector → BigQuery signal queries: computes signals (evidence: `README.md`)
- Scheduled detector → BigQuery alerts: merges alerts (evidence: `README.md`)
- Scheduled detector → Cloud Logging: logs alert events (evidence: `README.md`)
- Cloud Logging → Log alerting: drives metrics (evidence: `README.md`)
- Log alerting → Email notifications: notifies (evidence: `README.md`)
- BigQuery alerts → Looker Studio dashboard: feeds dashboard (evidence: `README.md`)
