This project examines emotion classification in African languages (Zulu and Nigerian Pidgin English) using transfer learning approaches with multilingual language models. We implement and evaluate various methods including zero-shot transfer, cross-lingual sequential fine-tuning, and adapter-based approaches to enhance emotion detection capabilities in low-resource African languages.
Create a virtual environment
python -m venv .venv
.\.venv\Scripts\Activate.ps1
Install dependencies
pip install -r requirements.txt
# For analysis components, install additional packages:
pip install bertviz shap seaborn matplotlib tabulate jupyter
# For adapter-based approaches:
pip install adapter-transformers peft
Create a .env file with your HuggingFace token
# Create .env file with the following content:
echo "HUGGING_FACE_HUB_TOKEN=your_token_here" > .env
You'll need a Hugging Face account and API token to access datasets:
If you encounter issues with sentencepiece installation, follow these steps:
First, you need to install the pwsh.exe if you don't have it
Install build prerequisites
pip install cmake
Manual build process
# Clone the repository
git clone https://github.com/google/sentencepiece.git
cd sentencepiece
# Create build directory and compile
mkdir build
cd build
# Configure with CMake
cmake .. -DSPM_ENABLE_SHARED=OFF -DCMAKE_INSTALL_PREFIX=./root
# Build and install
cmake --build . --config Release --target install
# Build Python wheel
cd ..\python
python setup.py bdist_wheel
# Install the wheel
pip install (Get-Item .\dist\sentencepiece*.whl).FullName
Return to project directory
cd ..\..\
Important: Model files are not included in the Git repository due to their large size. These files include:
saved-models directory.venv directoryIf you need the pre-trained models:
Option 1: Download from external storage Contact the project maintainers for access to the pre-trained models.
Option 2: Train models from scratch Use the training scripts in this repository to train the models:
# Example: Running adapter model training
python run_adapter_fine_tuning.py
# Example: Running cross-lingual training
python run_cross_lingual_fine_tuning.py
Option 3: Use Hugging Face Hub Some models may be available on the Hugging Face Hub. See the Models section below for details.
This repository uses Git Large File Storage (LFS) for managing large model files. To work with these files:
Install Git LFS
# Install Git LFS
git lfs install
Clone the repository with LFS support
# When cloning for the first time
git lfs clone https://github.com/your-username/COS760-group-29.git
# Or if you already cloned without LFS
git lfs pull
Track large files Large model files (*.safetensors, *.bin, *.pt) are automatically tracked by Git LFS.
Push changes
git add .
git commit -m "Your commit message"
git push
Note: The repository primarily uses Jupyter notebooks for adapter and cross-lingual approaches. Therefore, you will not find standalone
run_adapter_fine_tuning.pyorrun_cross_lingual_fine_tuning.pyscripts.
COS760-group-29/
├── data/ # Data loading and preprocessing
│ └── data_loader.py # Functions for loading and preprocessing datasets
├── results/ # Evaluation results and analysis utilities
│ ├── attention_visualization.py # Attention pattern visualization
│ ├── attention_visualization.ipynb # Interactive attention visualization
│ ├── baseline_error_analysis.py # Error analysis for baseline models
│ ├── baseline_shap_analysis.py # SHAP analysis for baseline models
│ ├── error_analysis.py # Error analysis for fine-tuned models
│ ├── evaluate_fine_tuned.py # Evaluation for fine-tuned models
│ ├── model_evaluation.py # General model evaluation functions
│ ├── shap_analysis.py # SHAP analysis for fine-tuned models
│ └── zero_shot_shap_analysis.py # SHAP analysis for zero-shot models
├── saved-models/ # Directory for storing model outputs
│ └── adapters/ # Subdirectory for storing adapter models
├── training/ # Training and fine-tuning scripts
│ ├── fine_tuning.py # Fine-tuning models on English data
│ ├── AdapterMethods.ipynb # Notebook for adapter-based fine-tuning
│ ├── Cross_lingual_transfer_learning.ipynb # Notebook for cross-lingual approaches
├── presentation_visuals/ # Visualizations for presentation and reporting
├── run_evaluation.py # Main script for model evaluation
├── run_fine_tuned_evaluation.py # Script for evaluating fine-tuned models
├── run_model_analysis.py # Script for comprehensive model analysis
├── README.md
├── requirements.txt
└── sentencepiece/ # SentencePiece tokenizer library
The project uses several datasets that need to be downloaded via the Hugging Face Hub:
BRIGHTER Emotion Dataset: Multi-language emotion classification dataset
brighter-dataset/BRIGHTER-emotion-categories/engbrighter-dataset/BRIGHTER-emotion-categories/zulbrighter-dataset/BRIGHTER-emotion-categories/pcmAfriHate Dataset: Used for pre-training adapters
afrihate/afrihate/zulafrihate/afrihate/pcmAfriSenti Dataset: Used for pre-training Nigerian Pidgin English adapters
shmuhammad/AfriSenti-twitter-sentiment/pcmThese datasets are downloaded automatically when running the scripts if you have set up your Hugging Face token correctly.
Setup the environment as described in the Installation section.
Standard Fine-tuning (Zero-shot approach):
python -m training.fine_tuning
This script fine-tunes mBERT and AfriBERTa on English emotion data from the BRIGHTER dataset.
Cross-lingual and Triple Sequential Fine-tuning:
Use the Jupyter notebook for these approaches:
jupyter notebook training/Cross_lingual_transfer_learning.ipynb
This notebook contains:
Adapter-based Fine-tuning:
Use the adapter methods notebook:
jupyter notebook training/AdapterMethods.ipynb
This notebook implements:
For both the adapter and cross-lingual approaches:
Start Jupyter:
jupyter notebook
Navigate to the respective notebook:
training/AdapterMethods.ipynbtraining/Cross_lingual_transfer_learning.ipynbExecute cells in sequence, following the instructions and comments within each notebook.
Save trained models to the saved-models directory as specified in the notebooks.
Evaluate fine-tuned models (Zero-shot transfer):
python run_fine_tuned_evaluation.py
This evaluates standard fine-tuned models on Zulu and Nigerian Pidgin test sets.
Run baseline model evaluation (non-fine-tuned models):
python run_evaluation.py
Evaluate notebook-trained models:
For cross-lingual and adapter models, use the evaluation cells in the respective notebooks:
AdapterMethods.ipynb contains evaluation sections for adapter-based modelsCross_lingual_transfer_learning.ipynb contains evaluation sections for cross-lingual modelsBaseline Model Analysis:
# Error analysis for baseline models
python -m results.baseline_error_analysis
# SHAP analysis for baseline models
python -m results.baseline_shap_analysis
Zero-shot Model Analysis:
# SHAP analysis for zero-shot models
python -m results.zero_shot_shap_analysis
Comprehensive Analysis:
# Run all analysis types (attention, error, SHAP)
python run_model_analysis.py --run-all
# Or run specific analyses
python run_model_analysis.py --shap-only # SHAP analysis only
python run_model_analysis.py --error-only # Error analysis only
python run_model_analysis.py --attention-only # Attention visualization only
Interactive Attention Visualization:
jupyter notebook results/attention_visualization.ipynb
This notebook provides interactive visualizations of attention patterns.
The analysis scripts generate various outputs in specific directories:
SHAP Analysis:
shap_analysis/baseline/zero_shot_shap_analysis/Error Analysis:
error_analysis/mBERT_Zulu/, error_analysis/mBERT_PCM/, etc.Attention Visualization:
visualizations/results/attention_visualization.ipynbModel Outputs:
saved-models/mbert_english/, saved-models/afriberta_english/saved-models/adapters/Metrics and Charts:
presentation_visuals/This project utilizes two main multilingual pre-trained models:
mBERT (bert-base-multilingual-cased)
AfriBERTa (castorini/afriberta_base)
We implement several fine-tuning approaches:
Standard Fine-tuning
Sequential Fine-tuning
Triple Sequential Fine-tuning
Adapter-based Fine-tuning
This project evaluates two multilingual models:
Evaluating the base pre-trained models directly on Zulu and Nigerian Pidgin emotion classification.
Fine-tune the models on English data from the BRIGHTER dataset, then test directly on Zulu and Nigerian Pidgin without any fine-tuning on these target languages.
This approach investigates how knowledge transfers from a high-resource language (English) to low-resource African languages, addressing the challenge of limited labeled data.
Fine-tune the models first on English data, then continue fine-tuning on Zulu and Nigerian Pidgin data from the BRIGHTER dataset.
Fine-tune the models in sequence across three languages: English → Nigerian Pidgin → Zulu, potentially improving cross-lingual transfer.
This approach uses lightweight adapters to efficiently fine-tune the models for specific languages:
Stage 1: Train language-specific adapters for Zulu and Nigerian Pidgin using:
Stage 2: Plug these language-specific adapters into the base models and fine-tune on BRIGHTER emotion data
The method follows this process:
Benefits of the adapter approach:
The results of our experiments are available in the following locations:
Performance Metrics and Comparison Tables
presentation_visuals/ for compiled charts and tablesVisualizations
shap_analysis/error_analysis/visualizations/Interactive Notebooks
training/AdapterMethods.ipynb (final cells)training/Cross_lingual_transfer_learning.ipynb (final cells)results/attention_visualization.ipynbDetailed Analysis
python run_model_analysis.py --run-all
5 commits
Jupyter Notebook
99.8%
This project examines emotion classification in African languages (Zulu and Nigerian Pidgin English) using transfer learning approaches with multilingual language models. We implement and evaluate various methods including zero-shot transfer, cross-lingual sequential fine-tuning, and adapter-based approaches to enhance emotion detection capabilities in low-resource African languages.
Create a virtual environment
python -m venv .venv
.\.venv\Scripts\Activate.ps1
Install dependencies
pip install -r requirements.txt
# For analysis components, install additional packages:
pip install bertviz shap seaborn matplotlib tabulate jupyter
# For adapter-based approaches:
pip install adapter-transformers peft
Create a .env file with your HuggingFace token
# Create .env file with the following content:
echo "HUGGING_FACE_HUB_TOKEN=your_token_here" > .env
You'll need a Hugging Face account and API token to access datasets:
If you encounter issues with sentencepiece installation, follow these steps:
First, you need to install the pwsh.exe if you don't have it
Install build prerequisites
pip install cmake
Manual build process
# Clone the repository
git clone https://github.com/google/sentencepiece.git
cd sentencepiece
# Create build directory and compile
mkdir build
cd build
# Configure with CMake
cmake .. -DSPM_ENABLE_SHARED=OFF -DCMAKE_INSTALL_PREFIX=./root
# Build and install
cmake --build . --config Release --target install
# Build Python wheel
cd ..\python
python setup.py bdist_wheel
# Install the wheel
pip install (Get-Item .\dist\sentencepiece*.whl).FullName
Return to project directory
cd ..\..\
Important: Model files are not included in the Git repository due to their large size. These files include:
saved-models directory.venv directoryIf you need the pre-trained models:
Option 1: Download from external storage Contact the project maintainers for access to the pre-trained models.
Option 2: Train models from scratch Use the training scripts in this repository to train the models:
# Example: Running adapter model training
python run_adapter_fine_tuning.py
# Example: Running cross-lingual training
python run_cross_lingual_fine_tuning.py
Option 3: Use Hugging Face Hub Some models may be available on the Hugging Face Hub. See the Models section below for details.
This repository uses Git Large File Storage (LFS) for managing large model files. To work with these files:
Install Git LFS
# Install Git LFS
git lfs install
Clone the repository with LFS support
# When cloning for the first time
git lfs clone https://github.com/your-username/COS760-group-29.git
# Or if you already cloned without LFS
git lfs pull
Track large files Large model files (*.safetensors, *.bin, *.pt) are automatically tracked by Git LFS.
Push changes
git add .
git commit -m "Your commit message"
git push
Note: The repository primarily uses Jupyter notebooks for adapter and cross-lingual approaches. Therefore, you will not find standalone
run_adapter_fine_tuning.pyorrun_cross_lingual_fine_tuning.pyscripts.
COS760-group-29/
├── data/ # Data loading and preprocessing
│ └── data_loader.py # Functions for loading and preprocessing datasets
├── results/ # Evaluation results and analysis utilities
│ ├── attention_visualization.py # Attention pattern visualization
│ ├── attention_visualization.ipynb # Interactive attention visualization
│ ├── baseline_error_analysis.py # Error analysis for baseline models
│ ├── baseline_shap_analysis.py # SHAP analysis for baseline models
│ ├── error_analysis.py # Error analysis for fine-tuned models
│ ├── evaluate_fine_tuned.py # Evaluation for fine-tuned models
│ ├── model_evaluation.py # General model evaluation functions
│ ├── shap_analysis.py # SHAP analysis for fine-tuned models
│ └── zero_shot_shap_analysis.py # SHAP analysis for zero-shot models
├── saved-models/ # Directory for storing model outputs
│ └── adapters/ # Subdirectory for storing adapter models
├── training/ # Training and fine-tuning scripts
│ ├── fine_tuning.py # Fine-tuning models on English data
│ ├── AdapterMethods.ipynb # Notebook for adapter-based fine-tuning
│ ├── Cross_lingual_transfer_learning.ipynb # Notebook for cross-lingual approaches
├── presentation_visuals/ # Visualizations for presentation and reporting
├── run_evaluation.py # Main script for model evaluation
├── run_fine_tuned_evaluation.py # Script for evaluating fine-tuned models
├── run_model_analysis.py # Script for comprehensive model analysis
├── README.md
├── requirements.txt
└── sentencepiece/ # SentencePiece tokenizer library
The project uses several datasets that need to be downloaded via the Hugging Face Hub:
BRIGHTER Emotion Dataset: Multi-language emotion classification dataset
brighter-dataset/BRIGHTER-emotion-categories/engbrighter-dataset/BRIGHTER-emotion-categories/zulbrighter-dataset/BRIGHTER-emotion-categories/pcmAfriHate Dataset: Used for pre-training adapters
afrihate/afrihate/zulafrihate/afrihate/pcmAfriSenti Dataset: Used for pre-training Nigerian Pidgin English adapters
shmuhammad/AfriSenti-twitter-sentiment/pcmThese datasets are downloaded automatically when running the scripts if you have set up your Hugging Face token correctly.
Setup the environment as described in the Installation section.
Standard Fine-tuning (Zero-shot approach):
python -m training.fine_tuning
This script fine-tunes mBERT and AfriBERTa on English emotion data from the BRIGHTER dataset.
Cross-lingual and Triple Sequential Fine-tuning:
Use the Jupyter notebook for these approaches:
jupyter notebook training/Cross_lingual_transfer_learning.ipynb
This notebook contains:
Adapter-based Fine-tuning:
Use the adapter methods notebook:
jupyter notebook training/AdapterMethods.ipynb
This notebook implements:
For both the adapter and cross-lingual approaches:
Start Jupyter:
jupyter notebook
Navigate to the respective notebook:
training/AdapterMethods.ipynbtraining/Cross_lingual_transfer_learning.ipynbExecute cells in sequence, following the instructions and comments within each notebook.
Save trained models to the saved-models directory as specified in the notebooks.
Evaluate fine-tuned models (Zero-shot transfer):
python run_fine_tuned_evaluation.py
This evaluates standard fine-tuned models on Zulu and Nigerian Pidgin test sets.
Run baseline model evaluation (non-fine-tuned models):
python run_evaluation.py
Evaluate notebook-trained models:
For cross-lingual and adapter models, use the evaluation cells in the respective notebooks:
AdapterMethods.ipynb contains evaluation sections for adapter-based modelsCross_lingual_transfer_learning.ipynb contains evaluation sections for cross-lingual modelsBaseline Model Analysis:
# Error analysis for baseline models
python -m results.baseline_error_analysis
# SHAP analysis for baseline models
python -m results.baseline_shap_analysis
Zero-shot Model Analysis:
# SHAP analysis for zero-shot models
python -m results.zero_shot_shap_analysis
Comprehensive Analysis:
# Run all analysis types (attention, error, SHAP)
python run_model_analysis.py --run-all
# Or run specific analyses
python run_model_analysis.py --shap-only # SHAP analysis only
python run_model_analysis.py --error-only # Error analysis only
python run_model_analysis.py --attention-only # Attention visualization only
Interactive Attention Visualization:
jupyter notebook results/attention_visualization.ipynb
This notebook provides interactive visualizations of attention patterns.
The analysis scripts generate various outputs in specific directories:
SHAP Analysis:
shap_analysis/baseline/zero_shot_shap_analysis/Error Analysis:
error_analysis/mBERT_Zulu/, error_analysis/mBERT_PCM/, etc.Attention Visualization:
visualizations/results/attention_visualization.ipynbModel Outputs:
saved-models/mbert_english/, saved-models/afriberta_english/saved-models/adapters/Metrics and Charts:
presentation_visuals/This project utilizes two main multilingual pre-trained models:
mBERT (bert-base-multilingual-cased)
AfriBERTa (castorini/afriberta_base)
We implement several fine-tuning approaches:
Standard Fine-tuning
Sequential Fine-tuning
Triple Sequential Fine-tuning
Adapter-based Fine-tuning
This project evaluates two multilingual models:
Evaluating the base pre-trained models directly on Zulu and Nigerian Pidgin emotion classification.
Fine-tune the models on English data from the BRIGHTER dataset, then test directly on Zulu and Nigerian Pidgin without any fine-tuning on these target languages.
This approach investigates how knowledge transfers from a high-resource language (English) to low-resource African languages, addressing the challenge of limited labeled data.
Fine-tune the models first on English data, then continue fine-tuning on Zulu and Nigerian Pidgin data from the BRIGHTER dataset.
Fine-tune the models in sequence across three languages: English → Nigerian Pidgin → Zulu, potentially improving cross-lingual transfer.
This approach uses lightweight adapters to efficiently fine-tune the models for specific languages:
Stage 1: Train language-specific adapters for Zulu and Nigerian Pidgin using:
Stage 2: Plug these language-specific adapters into the base models and fine-tune on BRIGHTER emotion data
The method follows this process:
Benefits of the adapter approach:
The results of our experiments are available in the following locations:
Performance Metrics and Comparison Tables
presentation_visuals/ for compiled charts and tablesVisualizations
shap_analysis/error_analysis/visualizations/Interactive Notebooks
training/AdapterMethods.ipynb (final cells)training/Cross_lingual_transfer_learning.ipynb (final cells)results/attention_visualization.ipynbDetailed Analysis
python run_model_analysis.py --run-all
5 commits
Jupyter Notebook
99.8%