This repository contains the code used to build, fine-tune, run, validate, and analyse large language models for immigration-related tweet classification. The project is organised as a research workflow rather than a packaged software library: data are prepared for instruction tuning, models are fine-tuned and merged, quantized models are run on an HPC cluster for large-scale inference, and the resulting outputs are analysed in R.
The core task is four-class classification of tweets into:
pro-immigrationanti-immigrationneutralunrelated
Across the repository, the code supports five linked stages:
- Create train/test datasets for instruction tuning.
- Fine-tune Llama-based models on one or more language datasets.
- Merge LoRA adapters back into a base model and export deployable weights.
- Run batch classification and validation jobs on HPC using GGUF models.
- Compare model performance across languages, translations, labels, and train/test splits.
Python scripts for dataset construction and supervised fine-tuning.
generate_training_test_dataset.pyBuilds Hugging Face train/test datasets from labelled tweet data, formats prompts in instruction style, and pushes datasets to the Hugging Face Hub.finetune-L32-3B-en-es.pyFine-tunesmeta-llama/Llama-3.2-3B-Instructwith LoRA/QLoRA-style settings on combined language datasets from Hugging Face.finetune-L32-3B-en-es_tech_demo.pyVariant of the fine-tuning workflow used for technical demos or alternate runs.merge_weights.pyLoads a base model plus LoRA adapters and merges them into a standalone model directory for downstream inference.
Cluster-oriented scripts used to run inference and validation jobs on Harvard FASRC-style infrastructure.
Large-scale tweet classification over compressed tweet archives.
gpu_tweet_jobs.shIterates over source.csv.gztweet files and submits one SLURM job per file.process_tweets_gpu_gguf.sbatchSLURM wrapper used to launch the classifier on the cluster.classify_tweets_gguf.pyLoads a GGUF model throughllama_cpp, classifies tweets one by one, saves checkpoints, and writes per-file outputs.
US-specific batch inference scripts.
gpu_tweet_usa.shJob launcher for USA datasets with explicit control over year, month, number of jobs, and wall time.process_tweets_gpu_gguf_usa.sbatchSLURM wrapper for the USA pipeline.classify_tweets_gguf_usa.pyClassifier script for the USA workflow.combine_usa_tweets_test.pyUtility for combining or post-processing USA output files.
Validation jobs run on cluster infrastructure.
validation_all.pyEvaluates one or more GGUF models against Hugging Face datasets, computes accuracy and weighted F1, and saves per-model outputs.validation.sbatchSLURM entry point for validation jobs.
R-based comparative performance analysis for the paper/notebook workflow.
modelling_llm_performances.RmdMain analysis notebook. It builds a unified modelling dataset from raw and translated outputs, estimates several logistic models, produces descriptive summaries, creates coefficient plots and heatmaps, and exports performance tables.helpers.RHelper functions for loading model output files, standardising metadata, computing accuracy intervals, generating confusion matrices, and building derived quantities used in the notebook.data/Input files for the modelling notebook, including raw train/test classifications and translated evaluation files.output/Figures and tables generated by the notebook.README.mdFolder-specific notes for the performance analysis workflow.
Older standalone R scripts for validation summaries and visual diagnostics.
model_confusion_matrices.RComputes and saves confusion matrices and heatmaps from classified outputs.model_accuracy_ci.RComputes and plots accuracy confidence intervals by model and dataset.
These scripts overlap conceptually with the newer model_performance/ workflow, but are kept as separate analysis utilities.
Paper-related bibliography and reference material.
ref.bibBibliography used in writing.doi_10.7910_DVN_3NCMB6.risReference export file.
General R helpers for a separate multimodel inference pipeline. This file is not the main helper file for the immigration classification notebook; that logic lives in model_performance/helpers.R.
Use fine-tuning/generate_training_test_dataset.py to:
- load manually labelled tweet data
- map labels to numeric class codes
- format instruction-tuning prompts
- split into train/test sets
- publish the datasets to Hugging Face
Use one of the scripts in fine-tuning/ to:
- load
meta-llama/Llama-3.2-3B-Instruct - combine one or more language datasets
- train LoRA adapters
- save and optionally push the resulting model artifacts
Use fine-tuning/merge_weights.py to merge adapter weights back into the base model so the result can be exported for local or cluster inference.
Use the shell and SLURM scripts in hpc/classification/ or hpc/classification_usa/ to:
- find tweet archives to process
- submit jobs in bulk
- run GGUF models with
llama_cpp - save predictions and checkpoints
Use:
hpc/validation/validation_all.pyfor automated evaluation runs on held-out datasetsvalidation/for standalone R summariesmodel_performance/modelling_llm_performances.Rmdfor the main comparative analysis across models and languages
- The repository contains research scripts, not a packaged application.
- Many scripts use hard-coded absolute paths for local machines, Harvard FASRC paths, and Hugging Face dataset/model names.
- Several scripts assume access to GPUs, SLURM,
llama_cpp, and locally stored GGUF or adapter files. - The most complete performance-comparison workflow is currently under
model_performance/.
If you are trying to understand the repository quickly, start with:
model_performance/modelling_llm_performances.Rmdmodel_performance/helpers.Rfine-tuning/generate_training_test_dataset.pyfine-tuning/finetune-L32-3B-en-es.pyhpc/classification/classify_tweets_gguf.py
These files cover the main analysis, helper logic, dataset generation, model training, and production-style inference workflow.