How caitlincalder/detecting-african-mgt works
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: sadilar_classifier.py SADiLaR classifier · processed Processed data · phase2_finetuning_afrolxm.py AfroXLM-R fine-tune · dataset_prep.py Dataset prep
Components 16
Data layer
Raw corpora, lexica, and processed datasets used across the pipeline.
- Raw MGT · Machine-generated source corpora from multiple LLMs.
data/raw - Human text · Raw human text sourced from the Vukuzenzele corpus.
data/raw/vukuzenzele_raw.csv - SADiLaR lexica · Morphological dictionaries for isiZulu, isiXhosa, and Siswati.
data/sadilar - Processed data · Merged and split datasets produced by preprocessing.
data/processed - Dataset prep · Downloads, filters, normalizes, and merges human and MGT text.
code/dataset_prep.py
Phase 1
Classical baseline detector.
- Baseline detector · 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 · Fine-tunes Davlan/afro-xlmr-base with Transformers and PyTorch.
code/phase2_finetuning_afrolxm.py - Model package · 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 · Builds handcrafted lexical and morphological features from SADiLaR dictionaries.
code/sadilar_morph_features.py - SADiLaR classifier · Random Forest ensemble combining morphology features with transformer probabilities.
code/sadilar_classifier.py
Presentation
Interactive and scripted orchestration entry points.
- Streamlit app · Interactive interface that runs the detectors and shows confidence and SHAP explanations.
code/app.py - Experiment runner · Scripted end-to-end reproduction of the pipeline.
code/experiment.py - Notebooks · Exploration, reporting, and auxiliary preprocessing notebooks and utilities.
code/main.ipynb
Analysis outputs
Metrics, plots, and reporting artefacts.
- SHAP analysis · Generates feature attribution and comparison plots for the detector stack.
code/sadilar_plots_shap.py - Metrics · Evaluation summaries, curves, confusion matrices, and calibration-style outputs.
results/metrics - Plots · Rendered analysis figures and explanation outputs.
results/plots
Connections 22
- 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.