# swordboom/supplai architecture

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

- Interactive diagram: https://gitdiagram.com/swordboom/supplai
- Repository: https://github.com/swordboom/supplai

## Overview

1. **Overall shape**
   - SupplAI is a **Streamlit-based decision support app** centered on a single inference pipeline in `app.py`.
   - It combines **rule-based parsing, graph simulation, ML scoring, explainability, anomaly detection, and LLM-generated briefs/agent actions** into one dashboard.

2. **Primary runtime boundary**
   - `app.py` is the main orchestration layer and UI entry point.
   - The README’s core flow is: **parse disruption → run cascade → score nodes → find alternates → compute SHAP → generate brief → score anomalies → run agent**.
   - This means the app is not just a viewer; it is the orchestration boundary for all analytical subsystems.

3. **Supply network and cascade analytics**
   - `src/graph_builder.py`, `src/cascade_model.py`, and `src/material_flow.py` appear to form the network-simulation core.
   - These modules likely build the supply-chain graph, propagate disruption effects downstream, and model material movement/exposure across nodes and routes.
   - This subsystem is the main “system dynamics” layer feeding risk and rerouting.

4. **Risk scoring and delay prediction**
   - `src/risk_scoring.py` and `src/delay_model.py` provide node-level risk estimation.
   - The README says the delay model is a major component of composite risk, so risk scoring is a blend of **graph-derived signals + ML delay probability**.
   - Trained model artifacts live in `models/` and are loaded by the inference app rather than retrained at runtime.

5. **Rerouting and exposure validation**
   - `src/reroute.py` is the decision-support layer for alternate path finding.
   - It likely consumes the graph/cascade outputs and checks whether a candidate route reduces exposure to the active disruption.
   - This is a distinct operational boundary from risk scoring: risk identifies impacted nodes; rerouting proposes mitigation.

6. **Disruption ingestion and normalization**
   - `src/disruption_input.py` is the entry point for turning heterogeneous events into a common disruption format.
   - Supporting feeds are split across `src/news_fetcher.py`, `src/weather_monitor.py`, and `src/ofac_fetcher.py`.
   - The README also mentions earthquakes from USGS and news extraction from RSS, so this subsystem normalizes manual input, live feeds, and keyword fallbacks into one event schema.

7. **External data services**
   - The app integrates with several external sources when configured:
     - **Gemini** and **Groq** for LLM-driven parsing, briefs, and agent reasoning.
     - **OpenWeather/Open-Meteo** for weather disruption signals.
     - **USGS** for earthquakes.
     - **RSS/news sources** for live disruption headlines.
     - **GCS / Vertex AI** via `src/gcs_utils.py` and cloud-related env vars for managed artifact loading/deployment.
   - These are optional by design; the README notes deterministic fallbacks when API keys are absent.

8. **Explainability and anomaly detection**
   - `src/shap_explain.py` provides model explainability for the delay/risk model using SHAP.
   - `src/anomaly_detector.py` and the persisted `models/anomaly_model.pkl` support shipment anomaly scoring, likely at a city or shipment-feature level.
   - In the dashboard, these are separate user-facing analyses: one explains **why risk is high**, the other flags **unusual shipment behavior**.

9. **LLM-generated summaries and agent behavior**
   - `src/llm_brief.py` generates executive summaries/ops briefs.
   - `src/supply_chain_agent.py` implements an autonomous or semi-autonomous action planner, likely using tool-calling and the outputs of the other subsystems.
   - `src/historical_rag.py` suggests retrieval over prior incidents or internal knowledge to ground LLM outputs in historical context.

10. **Data and artifact layout**
   - The repository separates **training data**, **reference data**, and **serialized artifacts**:
     - `datasets/` contains source datasets used for training and evaluation.
     - `data/` contains supporting reference files like sanctions, tariffs, and supply-chain data.
     - `models/` stores persisted pickle models used by the app.
   - This separation makes the runtime app mostly read-only and artifact-driven.

11. **Training and optimization pipeline**
   - `train_models.py` is the main model-building workflow and writes sanity/evaluation outputs.
   - `optimize_models.py` experiments with optimized model variants.
   - `generate_dataset.py` and `generate_tariffs.py` are data-prep utilities for synthetic/global inputs.
   - This training boundary is separate from the dashboard runtime and is designed to refresh artifacts consumed by `app.py`.

12. **Deployment and operational packaging**
   - `Dockerfile` packages the app for containerized Streamlit execution.
   - `cloudbuild.yaml` wires the repo for Google Cloud Build, Artifact Registry, and Cloud Run deployment.
   - The presence of `.devcontainer` and `.env.example` shows the repo is built for reproducible local development and cloud deployment with optional secret injection.

13. **How to draw the architecture**
   - A clean diagram would show:
     - **User/UI layer**: Streamlit dashboard in `app.py`
     - **Analytical services**: graph/cascade, risk scoring, rerouting, SHAP, anomaly detection, LLM brief/agent
     - **Ingestion layer**: manual disruption input plus news/weather/earthquake/OFAC feeds
     - **Artifact/data layer**: `datasets/`, `data/`, `models/`
     - **External services**: Gemini, Groq, OpenWeather, USGS, RSS, GCS/Vertex
   - The key directional flow is from **event ingestion → normalization → graph impact simulation → risk/mitigation/explanation outputs**, with LLMs and anomaly detection as parallel enrichments.

Main entry points: [`src/risk_scoring.py`](https://github.com/swordboom/supplai/blob/main/src/risk_scoring.py) (Risk), [`app.py`](https://github.com/swordboom/supplai/blob/main/app.py) (App), [`models`](https://github.com/swordboom/supplai/tree/main/models) (Models), [`src/graph_builder.py`](https://github.com/swordboom/supplai/blob/main/src/graph_builder.py) (Graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI Runtime"]
  node_app["App<br/>streamlit entrypoint<br/>[app.py]"]
end

subgraph group_ingest["Ingestion"]
  node_disruption_input["Normalize<br/>event parser"]
  node_news_fetcher["News<br/>feed ingest<br/>[news_fetcher.py]"]
  node_weather_monitor["Weather<br/>external signal<br/>[weather_monitor.py]"]
  node_ofac_fetcher["Sanctions<br/>external feed<br/>[ofac_fetcher.py]"]
end

subgraph group_analytics["Analytics"]
  node_graph_builder{{"Graph<br/>network builder<br/>[graph_builder.py]"}}
  node_cascade_model["Cascade<br/>impact simulator<br/>[cascade_model.py]"]
  node_material_flow["Flow<br/>movement model<br/>[material_flow.py]"]
  node_risk_scoring["Risk<br/>composite scorer<br/>[risk_scoring.py]"]
  node_delay_model["Delay ML<br/>prediction model<br/>[delay_model.py]"]
  node_reroute["Reroute<br/>mitigation planner<br/>[reroute.py]"]
  node_shap_explain["Explain<br/>[shap_explain.py]"]
  node_anomaly_detector["Anomaly"]
  node_llm_brief["Brief<br/>llm summarizer<br/>[llm_brief.py]"]
  node_supply_chain_agent["Agent<br/>action planner"]
  node_historical_rag["RAG<br/>retrieval layer<br/>[historical_rag.py]"]
  node_gcs_utils["Cloud IO<br/>artifact loader<br/>[gcs_utils.py]"]
end

subgraph group_data["Data & Artifacts"]
  node_models[("Models<br/>serialized artifacts")]
  node_datasets[("Training Data<br/>tabular datasets")]
  node_reference_data[("Reference Data<br/>domain data")]
end

subgraph group_ops["Training & Deploy"]
  node_train_models["Train<br/>[train_models.py]"]
  node_optimize_models["Optimize<br/>model tuning<br/>[optimize_models.py]"]
  node_generate_dataset["Gen Data<br/>data prep"]
  node_generate_tariffs["Tariffs<br/>data prep"]
  node_dockerfile["Docker<br/>container build"]
  node_cloudbuild["Cloud Build<br/>deployment pipeline<br/>[cloudbuild.yaml]"]
end

node_app -->|"parse event"| node_disruption_input
node_app -->|"ingest news"| node_news_fetcher
node_app -->|"ingest weather"| node_weather_monitor
node_app -->|"ingest sanctions"| node_ofac_fetcher
node_disruption_input -->|"build graph"| node_graph_builder
node_graph_builder -->|"simulate impact"| node_cascade_model
node_graph_builder -->|"route flow"| node_material_flow
node_cascade_model -->|"impact features"| node_risk_scoring
node_material_flow -->|"exposure features"| node_risk_scoring
node_delay_model -->|"delay score"| node_risk_scoring
node_risk_scoring -->|"mitigate"| node_reroute
node_risk_scoring -->|"explain"| node_shap_explain
node_risk_scoring -->|"analyze anomaly"| node_anomaly_detector
node_risk_scoring -->|"summarize"| node_llm_brief
node_historical_rag -->|"ground brief"| node_llm_brief
node_llm_brief -->|"inform action"| node_supply_chain_agent
node_risk_scoring -->|"decision inputs"| node_supply_chain_agent
node_gcs_utils -->|"load artifacts"| node_models
node_models -->|"serve model"| node_delay_model
node_models -->|"serve model"| node_anomaly_detector
node_datasets -->|"train on"| node_train_models
node_reference_data -->|"enrich training"| node_train_models
node_train_models -->|"write artifacts"| node_models
node_optimize_models -->|"write optimized"| node_models
node_generate_dataset -->|"produce data"| node_datasets
node_generate_tariffs -->|"produce tariffs"| node_reference_data
node_dockerfile -->|"package app"| node_app
node_cloudbuild -->|"build image"| node_dockerfile

click node_app "https://github.com/swordboom/supplai/blob/main/app.py"
click node_disruption_input "https://github.com/swordboom/supplai/blob/main/src/disruption_input.py"
click node_news_fetcher "https://github.com/swordboom/supplai/blob/main/src/news_fetcher.py"
click node_weather_monitor "https://github.com/swordboom/supplai/blob/main/src/weather_monitor.py"
click node_ofac_fetcher "https://github.com/swordboom/supplai/blob/main/src/ofac_fetcher.py"
click node_graph_builder "https://github.com/swordboom/supplai/blob/main/src/graph_builder.py"
click node_cascade_model "https://github.com/swordboom/supplai/blob/main/src/cascade_model.py"
click node_material_flow "https://github.com/swordboom/supplai/blob/main/src/material_flow.py"
click node_risk_scoring "https://github.com/swordboom/supplai/blob/main/src/risk_scoring.py"
click node_delay_model "https://github.com/swordboom/supplai/blob/main/src/delay_model.py"
click node_reroute "https://github.com/swordboom/supplai/blob/main/src/reroute.py"
click node_shap_explain "https://github.com/swordboom/supplai/blob/main/src/shap_explain.py"
click node_anomaly_detector "https://github.com/swordboom/supplai/blob/main/src/anomaly_detector.py"
click node_llm_brief "https://github.com/swordboom/supplai/blob/main/src/llm_brief.py"
click node_supply_chain_agent "https://github.com/swordboom/supplai/blob/main/src/supply_chain_agent.py"
click node_historical_rag "https://github.com/swordboom/supplai/blob/main/src/historical_rag.py"
click node_gcs_utils "https://github.com/swordboom/supplai/blob/main/src/gcs_utils.py"
click node_models "https://github.com/swordboom/supplai/tree/main/models"
click node_datasets "https://github.com/swordboom/supplai/tree/main/datasets"
click node_reference_data "https://github.com/swordboom/supplai/tree/main/data"
click node_train_models "https://github.com/swordboom/supplai/blob/main/train_models.py"
click node_optimize_models "https://github.com/swordboom/supplai/blob/main/optimize_models.py"
click node_generate_dataset "https://github.com/swordboom/supplai/blob/main/generate_dataset.py"
click node_generate_tariffs "https://github.com/swordboom/supplai/blob/main/generate_tariffs.py"
click node_dockerfile "https://github.com/swordboom/supplai/tree/main/Dockerfile"
click node_cloudbuild "https://github.com/swordboom/supplai/blob/main/cloudbuild.yaml"

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_app toneBlue
class node_disruption_input,node_news_fetcher,node_weather_monitor,node_ofac_fetcher toneAmber
class node_graph_builder,node_cascade_model,node_material_flow,node_risk_scoring,node_delay_model,node_reroute,node_shap_explain,node_anomaly_detector,node_llm_brief,node_supply_chain_agent,node_historical_rag,node_gcs_utils toneMint
class node_models,node_datasets,node_reference_data toneRose
class node_train_models,node_optimize_models,node_generate_dataset,node_generate_tariffs,node_dockerfile,node_cloudbuild toneIndigo
```

## Components

### UI Runtime

Streamlit app entrypoint and orchestration boundary

- [App](https://github.com/swordboom/supplai/blob/main/app.py): streamlit entrypoint · Main dashboard and orchestration layer for the end-to-end inference flow · `app.py`

### Ingestion

Disruption normalization and external signal feeds

- [Normalize](https://github.com/swordboom/supplai/blob/main/src/disruption_input.py): event parser · Converts manual and live disruption inputs into a common event format · `src/disruption_input.py`
- [News](https://github.com/swordboom/supplai/blob/main/src/news_fetcher.py): feed ingest · Fetches disruption signals from news and RSS-style sources · `src/news_fetcher.py`
- [Weather](https://github.com/swordboom/supplai/blob/main/src/weather_monitor.py): external signal · Pulls weather-based disruption indicators from weather services · `src/weather_monitor.py`
- [Sanctions](https://github.com/swordboom/supplai/blob/main/src/ofac_fetcher.py): external feed · Loads OFAC and sanctions-related disruption data · `src/ofac_fetcher.py`

### Analytics

Graph simulation, scoring, explanations, and mitigation

- [Graph](https://github.com/swordboom/supplai/blob/main/src/graph_builder.py): network builder · Builds the supply network used by downstream simulation and scoring · `src/graph_builder.py`
- [Cascade](https://github.com/swordboom/supplai/blob/main/src/cascade_model.py): impact simulator · Propagates disruption effects through the supply network · `src/cascade_model.py`
- [Flow](https://github.com/swordboom/supplai/blob/main/src/material_flow.py): movement model · Models material movement and exposure across nodes and routes · `src/material_flow.py`
- [Risk](https://github.com/swordboom/supplai/blob/main/src/risk_scoring.py): composite scorer · Combines graph signals and model outputs into node-level risk · `src/risk_scoring.py`
- [Delay ML](https://github.com/swordboom/supplai/blob/main/src/delay_model.py): prediction model · Predicts shipment delay probability used by composite risk · `src/delay_model.py`
- [Reroute](https://github.com/swordboom/supplai/blob/main/src/reroute.py): mitigation planner · Finds alternate paths and checks exposure reduction for candidate routes · `src/reroute.py`
- [Explain](https://github.com/swordboom/supplai/blob/main/src/shap_explain.py): explainability · Produces SHAP-based explanations for model and risk outcomes · `src/shap_explain.py`
- [Anomaly](https://github.com/swordboom/supplai/blob/main/src/anomaly_detector.py): anomaly scorer · Flags unusual shipment behavior using persisted anomaly models · `src/anomaly_detector.py`
- [Brief](https://github.com/swordboom/supplai/blob/main/src/llm_brief.py): llm summarizer · Generates executive and operations summaries from analysis outputs · `src/llm_brief.py`
- [Agent](https://github.com/swordboom/supplai/blob/main/src/supply_chain_agent.py): action planner · Uses tool-like reasoning to recommend operational actions · `src/supply_chain_agent.py`
- [RAG](https://github.com/swordboom/supplai/blob/main/src/historical_rag.py): retrieval layer · Retrieves prior incidents and context to ground LLM outputs · `src/historical_rag.py`
- [Cloud IO](https://github.com/swordboom/supplai/blob/main/src/gcs_utils.py): artifact loader · Handles cloud storage and managed artifact access · `src/gcs_utils.py`

### Data & Artifacts

Training data, reference data, and serialized models

- [Models](https://github.com/swordboom/supplai/tree/main/models): serialized artifacts · Persisted delay and anomaly models loaded at runtime · `models`
- [Training Data](https://github.com/swordboom/supplai/tree/main/datasets): tabular datasets · Source datasets used to train and evaluate models · `datasets`
- [Reference Data](https://github.com/swordboom/supplai/tree/main/data): domain data · Supply-chain, sanctions, and tariff reference files · `data`

### Training & Deploy

Model refresh, containerization, and cloud packaging

- [Train](https://github.com/swordboom/supplai/blob/main/train_models.py): model training · Primary workflow for building and evaluating models · `train_models.py`
- [Optimize](https://github.com/swordboom/supplai/blob/main/optimize_models.py): model tuning · Builds optimized model variants for deployment · `optimize_models.py`
- [Gen Data](https://github.com/swordboom/supplai/blob/main/generate_dataset.py): data prep · Creates synthetic or derived datasets for training and analysis · `generate_dataset.py`
- [Tariffs](https://github.com/swordboom/supplai/blob/main/generate_tariffs.py): data prep · Generates tariff reference inputs for the dataset layer · `generate_tariffs.py`
- [Docker](https://github.com/swordboom/supplai/tree/main/Dockerfile): container build · Containerizes the Streamlit application for runtime deployment · `Dockerfile`
- [Cloud Build](https://github.com/swordboom/supplai/blob/main/cloudbuild.yaml): deployment pipeline · Defines cloud build and deployment packaging for the repo · `cloudbuild.yaml`

## Connections

- App → Normalize: parse event · Normalizes disruption input before analysis
- App → News: ingest news · Pulls external news signals into the app flow
- App → Weather: ingest weather · Adds weather disruption signals
- App → Sanctions: ingest sanctions · Adds sanctions-related disruption context
- Normalize → Graph: build graph · Creates the network representation used downstream
- Graph → Cascade: simulate impact · Feeds the disruption cascade model
- Graph → Flow: route flow · Provides topology for movement and exposure modeling
- Cascade → Risk: impact features · Supplies propagated disruption signals for scoring
- Flow → Risk: exposure features · Contributes route and node exposure inputs
- Delay ML → Risk: delay score · Provides ML delay probability as a risk component
- Risk → Reroute: mitigate · Uses scored risk to evaluate alternate paths
- Risk → Explain: explain · Explains why the risk score is high
- Risk → Anomaly: analyze anomaly · Pairs risk analysis with shipment anomaly scoring
- Risk → Brief: summarize · Feeds summary generation for executive briefing
- RAG → Brief: ground brief · Adds historical context to generated briefs
- Brief → Agent: inform action · Provides narrative context for agent recommendations
- Risk → Agent: decision inputs · Supplies operational state for action planning
- Cloud IO → Models: load artifacts · Reads persisted models from managed storage when configured
- Models → Delay ML: serve model · Runtime inference artifact for delay prediction
- Models → Anomaly: serve model · Runtime inference artifact for anomaly scoring
- Training Data → Train: train on · Primary training inputs
- Reference Data → Train: enrich training · Adds domain reference context during model building
- Train → Models: write artifacts · Persists trained models for inference
- Optimize → Models: write optimized · Stores tuned model variants
- Gen Data → Training Data: produce data · Generates training and evaluation datasets
- Tariffs → Reference Data: produce tariffs · Creates tariff reference inputs
- Docker → App: package app · Builds the runtime container around the dashboard
- Cloud Build → Docker: build image · Cloud build pipeline packages the container
