# caitlincalder/detecting-african-mgt architecture

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

- Interactive diagram: https://gitdiagram.com/caitlincalder/detecting-african-mgt
- Repository: https://github.com/caitlincalder/detecting-african-mgt

## Overview

1. **Repository purpose and overall shape**  
This is a research pipeline for machine-generated text detection in low-resource Bantu languages. The architecture is not a single app; it is a staged NLP system with shared data preparation, three detection phases, and several analysis/visualization entry points.

2. **Data layer and inputs**  
The raw inputs live in `data/raw/` as three machine-generated corpora, one per LLM source: ChatGPT-4o, Claude, and Gemini 2.5 Pro. Human text is pulled from the Hugging Face `dsfsi/vukuzenzele-monolingual` corpus and merged with the raw MGT data. SADiLaR morphological lexica for isiZulu, isiXhosa, and Siswati are stored separately in `data/sadilar/`.

3. **Preprocessing boundary**  
`code/dataset_prep.py` is the main ingestion-and-cleaning boundary. It downloads and filters the human corpus, cleans obvious artefacts and leakage-prone patterns, normalizes/truncates documents, and writes the main training file `data/processed/merged_dataset.csv` plus split outputs for human and MGT text. This is the primary data source for Phase 1 and Phase 2.

4. **Phase 1: classical baseline**  
`code/baseline.py` implements the lightweight detector: character n-gram TF-IDF feeding a Logistic Regression classifier. It is trained on isiZulu and isiXhosa, then evaluated on Siswati in a zero-shot setting. This phase is the simplest architectural branch and establishes a non-neural benchmark.

5. **Phase 2: transformer fine-tuning**  
`code/phase2_finetuning_afrolxm.py` and `code/finetuning_afrolxm.ipynb` fine-tune `Davlan/afro-xlmr-base` using Hugging Face Transformers and PyTorch. The saved artefacts in `code/best from phase 2/` represent the trained model package: config, tokenizer, and training state. This phase is the main neural detector and feeds later inference.

6. **Phase 2 model artefact boundary**  
The trained transformer is treated as a reusable inference service-like component, even though it is stored locally rather than deployed remotely. Other scripts expect its path via `AFROXLMR_MODEL_PATH`, which makes the model a configurable external dependency for Phase 3, the Streamlit app, and the experiment runner.

7. **Phase 3: morphology-aware ensemble**  
`code/sadilar_morph_features.py` derives handcrafted morphological and lexical features from the SADiLaR dictionaries and writes `data/processed/sadilar_morph_features.csv`. `code/sadilar_classifier.py` then combines those features with the AfroXLM-R output probabilities in a Random Forest ensemble. This is the key feature-fusion layer that augments neural predictions with language-specific morphology signals.

8. **Explainability and analysis outputs**  
`code/sadilar_plots_shap.py` and the artifacts under `results/plots/` provide SHAP-based interpretability and comparison plots. Phase 2 and Phase 3 also emit training curves, confusion matrices, calibration-style outputs, and summary metrics under `results/metrics/`. These outputs are analytical byproducts, not runtime dependencies.

9. **Interactive presentation layer**  
`code/app.py` is the user-facing Streamlit application. It orchestrates all three detectors on pasted text, surfaces confidence scores, and shows SHAP explanations. Architecturally, this is the presentation layer that depends on the preprocessing outputs and the Phase 2 model artefact, but does not retrain models.

10. **Experiment orchestration and notebooks**  
`code/experiment.py` is the scripted end-to-end runner for reproducing the pipeline from baseline through inference. `code/main.ipynb` is the main notebook for exploration and reporting, while `code/data pre-processing/` contains auxiliary topic-finding and preprocessing utilities that support analysis rather than the core detection pipeline.

11. **Main data flow across phases**  
The central flow is: raw human and machine text → cleaned/merged dataset → Phase 1 TF-IDF baseline and Phase 2 transformer fine-tuning → Phase 3 feature fusion using transformer probabilities plus SADiLaR-derived features → metrics/plots/SHAP outputs. Siswati is consistently held out from training to test cross-lingual transfer.

12. **Important architectural constraints**  
The project is designed around low-resource multilingual evaluation and leakage control. Several cleaning decisions, such as removing artefactual tokens and normalizing text length, are architectural safeguards so the detectors learn linguistic patterns rather than dataset quirks. Zero-shot Siswati evaluation is a defining boundary that shapes every phase.

Main entry points: [`code/sadilar_classifier.py`](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_classifier.py) (SADiLaR classifier), [`data/processed`](https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/processed) (Processed data), [`code/phase2_finetuning_afrolxm.py`](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/phase2_finetuning_afrolxm.py) (AfroXLM-R fine-tune), [`code/dataset_prep.py`](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/dataset_prep.py) (Dataset prep)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data layer"]
  node_raw_mgt[("Raw MGT<br/>corpora")]
  node_human_raw["Human text<br/>corpus"]
  node_sadilar_lexica[("SADiLaR lexica<br/>lexicons")]
  node_processed_data[("Processed data<br/>training data")]
  node_dataset_prep["Dataset prep<br/>ingest-clean<br/>[dataset_prep.py]"]
end

subgraph group_phase1["Phase 1"]
  node_baseline_detector["Baseline detector<br/>tfidf+lr<br/>[baseline.py]"]
end

subgraph group_phase2["Phase 2"]
  node_afrolxm_finetune["AfroXLM-R fine-tune<br/>transformer training"]
  node_afrolxm_artifact[("Model package<br/>trained model")]
end

subgraph group_phase3["Phase 3"]
  node_sadilar_features["Morph features<br/>feature extraction"]
  node_sadilar_ensemble["SADiLaR classifier<br/>ensemble"]
end

subgraph group_presentation["Presentation"]
  node_streamlit_app["Streamlit app<br/>presentation<br/>[app.py]"]
  node_experiment_runner["Experiment runner<br/>orchestration<br/>[experiment.py]"]
  node_analysis_notebooks["Notebooks<br/>[main.ipynb]"]
end

subgraph group_analysis["Analysis outputs"]
  node_shap_analysis["SHAP analysis<br/>interpretability"]
  node_metrics_out[("Metrics<br/>results")]
  node_plots_out[("Plots<br/>results")]
end

node_raw_mgt -->|"raw MGT"| node_dataset_prep
node_human_raw -->|"human corpus"| node_dataset_prep
node_dataset_prep -->|"writes"| node_processed_data
node_processed_data -->|"train/eval"| node_baseline_detector
node_processed_data -->|"fine-tune"| node_afrolxm_finetune
node_afrolxm_finetune -->|"save model"| node_afrolxm_artifact
node_afrolxm_artifact -->|"probabilities"| node_sadilar_ensemble
node_sadilar_lexica -->|"lexical source"| node_sadilar_features
node_sadilar_features -->|"feature fusion"| node_sadilar_ensemble
node_afrolxm_artifact -->|"inference model"| node_streamlit_app
node_processed_data -->|"inputs"| node_streamlit_app
node_baseline_detector -->|"reports"| node_metrics_out
node_afrolxm_finetune -->|"reports"| node_metrics_out
node_sadilar_ensemble -->|"reports"| node_metrics_out
node_sadilar_ensemble -->|"plots"| node_plots_out
node_shap_analysis -->|"visuals"| node_plots_out
node_experiment_runner -->|"run"| node_baseline_detector
node_experiment_runner -->|"run"| node_afrolxm_finetune
node_experiment_runner -->|"run"| node_sadilar_ensemble
node_analysis_notebooks -.->|"inspect"| node_dataset_prep
node_analysis_notebooks -.->|"reporting"| node_shap_analysis
node_streamlit_app -->|"explain"| node_shap_analysis

click node_raw_mgt "https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/raw"
click node_human_raw "https://github.com/caitlincalder/detecting-african-mgt/blob/main/data/raw/vukuzenzele_raw.csv"
click node_sadilar_lexica "https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/sadilar"
click node_processed_data "https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/processed"
click node_dataset_prep "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/dataset_prep.py"
click node_baseline_detector "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/baseline.py"
click node_afrolxm_finetune "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/phase2_finetuning_afrolxm.py"
click node_afrolxm_artifact "https://github.com/caitlincalder/detecting-african-mgt/tree/main/code/best from phase 2"
click node_sadilar_features "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_morph_features.py"
click node_sadilar_ensemble "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_classifier.py"
click node_shap_analysis "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_plots_shap.py"
click node_metrics_out "https://github.com/caitlincalder/detecting-african-mgt/tree/main/results/metrics"
click node_plots_out "https://github.com/caitlincalder/detecting-african-mgt/tree/main/results/plots"
click node_streamlit_app "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/app.py"
click node_experiment_runner "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/experiment.py"
click node_analysis_notebooks "https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/main.ipynb"

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_raw_mgt,node_human_raw,node_sadilar_lexica,node_processed_data,node_dataset_prep toneBlue
class node_baseline_detector toneAmber
class node_afrolxm_finetune,node_afrolxm_artifact toneMint
class node_sadilar_features,node_sadilar_ensemble toneRose
class node_streamlit_app,node_experiment_runner,node_analysis_notebooks toneIndigo
class node_shap_analysis,node_metrics_out,node_plots_out toneTeal
```

## Components

### Data layer

Raw corpora, lexica, and processed datasets used across the pipeline.

- [Raw MGT](https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/raw): corpora · Machine-generated source corpora from multiple LLMs. · `data/raw`
- [Human text](https://github.com/caitlincalder/detecting-african-mgt/blob/main/data/raw/vukuzenzele_raw.csv): corpus · Raw human text sourced from the Vukuzenzele corpus. · `data/raw/vukuzenzele_raw.csv`
- [SADiLaR lexica](https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/sadilar): lexicons · Morphological dictionaries for isiZulu, isiXhosa, and Siswati. · `data/sadilar`
- [Processed data](https://github.com/caitlincalder/detecting-african-mgt/tree/main/data/processed): training data · Merged and split datasets produced by preprocessing. · `data/processed`
- [Dataset prep](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/dataset_prep.py): ingest-clean · Downloads, filters, normalizes, and merges human and MGT text. · `code/dataset_prep.py`

### Phase 1

Classical baseline detector.

- [Baseline detector](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/baseline.py): tfidf+lr · Character n-gram TF-IDF with logistic regression for zero-shot evaluation. · `code/baseline.py`

### Phase 2

Transformer fine-tuning and reusable model artefact.

- [AfroXLM-R fine-tune](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/phase2_finetuning_afrolxm.py): transformer training · Fine-tunes Davlan/afro-xlmr-base with Transformers and PyTorch. · `code/phase2_finetuning_afrolxm.py`
- [Model package](https://github.com/caitlincalder/detecting-african-mgt/tree/HEAD/code/best%20from%20phase%202): trained model · Saved tokenizer, config, and training state for reuse in downstream inference. · `code/best from phase 2`

### Phase 3

Morphology-aware ensemble and explainability.

- [Morph features](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_morph_features.py): feature extraction · Builds handcrafted lexical and morphological features from SADiLaR dictionaries. · `code/sadilar_morph_features.py`
- [SADiLaR classifier](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_classifier.py): ensemble · Random Forest ensemble combining morphology features with transformer probabilities. · `code/sadilar_classifier.py`

### Presentation

Interactive and scripted orchestration entry points.

- [Streamlit app](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/app.py): presentation · Interactive interface that runs the detectors and shows confidence and SHAP explanations. · `code/app.py`
- [Experiment runner](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/experiment.py): orchestration · Scripted end-to-end reproduction of the pipeline. · `code/experiment.py`
- [Notebooks](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/main.ipynb): notebooks · Exploration, reporting, and auxiliary preprocessing notebooks and utilities. · `code/main.ipynb`

### Analysis outputs

Metrics, plots, and reporting artefacts.

- [SHAP analysis](https://github.com/caitlincalder/detecting-african-mgt/blob/main/code/sadilar_plots_shap.py): interpretability · Generates feature attribution and comparison plots for the detector stack. · `code/sadilar_plots_shap.py`
- [Metrics](https://github.com/caitlincalder/detecting-african-mgt/tree/main/results/metrics): results · Evaluation summaries, curves, confusion matrices, and calibration-style outputs. · `results/metrics`
- [Plots](https://github.com/caitlincalder/detecting-african-mgt/tree/main/results/plots): results · Rendered analysis figures and explanation outputs. · `results/plots`

## Connections

- Raw MGT → Dataset prep: raw MGT · Machine-generated corpora are cleaned and merged into training data.
- Human text → Dataset prep: human corpus · Raw human text is downloaded, filtered, and normalized.
- Dataset prep → Processed data: writes · Produces merged and split processed datasets.
- Processed data → Baseline detector: train/eval · Provides the dataset used for the classical baseline.
- Processed data → AfroXLM-R fine-tune: fine-tune · Feeds the transformer training pipeline.
- AfroXLM-R fine-tune → Model package: save model · Outputs the reusable local model package.
- Model package → SADiLaR classifier: probabilities · Supplies transformer predictions to the fusion model.
- SADiLaR lexica → Morph features: lexical source · Dictionary files drive morphology and lexical feature extraction.
- Morph features → SADiLaR classifier: feature fusion · Handcrafted features are combined with model probabilities.
- Model package → Streamlit app: inference model · The app loads the saved transformer artefact for predictions.
- Processed data → Streamlit app: inputs · The app relies on the same preprocessing boundary for consistent text handling.
- Baseline detector → Metrics: reports · Baseline evaluation contributes metrics and summaries.
- AfroXLM-R fine-tune → Metrics: reports · Transformer training emits evaluation metrics and curves.
- SADiLaR classifier → Metrics: reports · Ensemble evaluation writes summary metrics and comparisons.
- SADiLaR classifier → Plots: plots · Ensemble outputs feed comparison figures.
- SHAP analysis → Plots: visuals · Creates SHAP and related interpretability plots.
- Experiment runner → Baseline detector: run · Scripted orchestration can execute the baseline stage.
- Experiment runner → AfroXLM-R fine-tune: run · Scripted orchestration can trigger transformer fine-tuning.
- Experiment runner → SADiLaR classifier: run · Scripted orchestration can execute the fused detector.
- Notebooks → Dataset prep: inspect · Notebook workflows support preprocessing and exploration.
- Notebooks → SHAP analysis: reporting · Notebooks support interpretation and reporting workflows.
- Streamlit app → SHAP analysis: explain · The presentation layer surfaces SHAP explanations to users.
