Jupyter Notebook
0
17 commits
updated Jul 2, 2025
This repository implementeda fine-tuned LegalBERT model for multi-label posture classification of US judicial opinions.
This solution addresses the Thomson Reuters Data Science Challenge by:
The model fine-tunes nlpaueb/legal-bert-base-uncased using LoRA (Low-Rank Adaptation) for parameter-efficient training on the top 10 most frequent procedural postures plus an "Other" category.
# Using conda (recommended)
conda create -n tr-challenge python=3.11
conda activate tr-challenge
pip install -r requirements.txt
ThomsonReutersChallenge/
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ run.sh # Quick execution script
βββ .gitignore # Git ignore rules
βββ data/ # Data directory
β βββ TRDataChallenge2023.txt # Main dataset (JSONL format)
β
βββ config/ # Configuration files
β βββ postures.json # Top 10 postures with kappa values
β
βββ src/ # Source code
β βββ __init__.py # Package initialization
β βββ main.py # Main execution script
β βββ data_processor.py # Data loading and preprocessing
β βββ model.py # LegalBERT model with LoRA
β βββ trainer.py # Training and evaluation logic
β
βββ outputs/ # Generated outputs
β βββ *.png # Data exploration plots
β βββ legalbert_multi/ # Model artifacts
β βββ tokenizer/ # Tokenizer files
β βββ pytorch_model.bin # Trained model weights
β βββ thresholds.json # Optimized classification thresholds
β βββ metrics.json # Evaluation metrics
β βββ plots/ # Training and evaluation plots
β
βββ TR_Data_Challenge_Exploratory_Analysis.ipynb # Jupyter notebook with exploratory analysis on data (Question 1)
data/TRDataChallenge2023.txt: Main dataset in JSONL format
documentId, postures, and sectionsconfig/postures.json: Configuration file with top 10 postures and their kappa values
{
"documentId": "doc_12345",
"postures": ["On Appeal (Civil)", "Motion to Dismiss"],
"sections": [
{
"headtext": "Introduction",
"paragraphs": ["This case involves...", "The plaintiff argues..."]
}
]
}
# Start Jupyter
jupyter notebook
# Open TR_Data_Challenge_Exploratory_Analysis.ipynb
# Run all cells for complete analysis
# Make script executable
chmod +x run.sh
# Run the complete pipeline
./run.sh
# Train from scratch
python src/main.py \
--data_path ./data/TRDataChallenge2023.txt \
--postures_path ./config/postures.json \
--output_dir ./outputs/my_model
# Evaluate existing model
python src/main.py \
--do_eval \
--model_dir ./outputs/legalbert_multi \
--data_path ./data/TRDataChallenge2023.txt \
--postures_path ./config/postures.json
The model supports extensive customization through command-line arguments:
python src/main.py \
--do_train \
--data_path data/TRDataChallenge2023.txt \
--postures_path config/postures.json \
--output_dir outputs/custom_model \
--model_name nlpaueb/legal-bert-base-uncased \
--max_chunk_length 512 \
--stride 128 \
--lora_r 8 \
--lora_alpha 32 \
--batch_size 2 \
--grad_accum_steps 16 \
--learning_rate 3e-4 \
--num_epochs 10 \
--early_stop_patience 3 \
--random_seed 42
--max_chunk_length: Maximum tokens per chunk (default: 512)--stride: Overlap between chunks (default: 128)--lora_r: LoRA rank parameter (default: 8)--lora_alpha: LoRA alpha parameter (default: 32)--batch_size: Per-device batch size (default: 4 for Mac M4 and 32 for NVIDIA RTX)--grad_accum_steps: Gradient accumulation steps (default: 32 for Mac M4 and 4 for NVIDIA RTX)python src/main.py \
--do_train \
--continue_training \
--model_dir ./outputs/legalbert_multi \
--output_dir ./outputs/legalbert_multi_continued \
--num_epochs 3
nlpaueb/legal-bert-base-uncased[HEAD] {headtext} [SEP] {paragraph_text}outputs/legalbert_multi/pytorch_model.bin: Trained model weightsoutputs/legalbert_multi/tokenizer/: Tokenizer configurationoutputs/legalbert_multi/thresholds.json: Optimized classification thresholdsoutputs/legalbert_multi/metrics.json: Comprehensive evaluation metricsoutputs/legalbert_multi/plots/: Training curves and performance visualizationsoutputs/*.png: Exploratory data analysis plotsAll experiments use random_seed=42 for reproducibility across:
All dependencies are version-pinned in requirements.txt to ensure consistent results.
With the default configuration, you should achieve:
Jupyter Notebook
0
17 commits
updated Jul 2, 2025
This repository implementeda fine-tuned LegalBERT model for multi-label posture classification of US judicial opinions.
This solution addresses the Thomson Reuters Data Science Challenge by:
The model fine-tunes nlpaueb/legal-bert-base-uncased using LoRA (Low-Rank Adaptation) for parameter-efficient training on the top 10 most frequent procedural postures plus an "Other" category.
# Using conda (recommended)
conda create -n tr-challenge python=3.11
conda activate tr-challenge
pip install -r requirements.txt
ThomsonReutersChallenge/
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ run.sh # Quick execution script
βββ .gitignore # Git ignore rules
βββ data/ # Data directory
β βββ TRDataChallenge2023.txt # Main dataset (JSONL format)
β
βββ config/ # Configuration files
β βββ postures.json # Top 10 postures with kappa values
β
βββ src/ # Source code
β βββ __init__.py # Package initialization
β βββ main.py # Main execution script
β βββ data_processor.py # Data loading and preprocessing
β βββ model.py # LegalBERT model with LoRA
β βββ trainer.py # Training and evaluation logic
β
βββ outputs/ # Generated outputs
β βββ *.png # Data exploration plots
β βββ legalbert_multi/ # Model artifacts
β βββ tokenizer/ # Tokenizer files
β βββ pytorch_model.bin # Trained model weights
β βββ thresholds.json # Optimized classification thresholds
β βββ metrics.json # Evaluation metrics
β βββ plots/ # Training and evaluation plots
β
βββ TR_Data_Challenge_Exploratory_Analysis.ipynb # Jupyter notebook with exploratory analysis on data (Question 1)
data/TRDataChallenge2023.txt: Main dataset in JSONL format
documentId, postures, and sectionsconfig/postures.json: Configuration file with top 10 postures and their kappa values
{
"documentId": "doc_12345",
"postures": ["On Appeal (Civil)", "Motion to Dismiss"],
"sections": [
{
"headtext": "Introduction",
"paragraphs": ["This case involves...", "The plaintiff argues..."]
}
]
}
# Start Jupyter
jupyter notebook
# Open TR_Data_Challenge_Exploratory_Analysis.ipynb
# Run all cells for complete analysis
# Make script executable
chmod +x run.sh
# Run the complete pipeline
./run.sh
# Train from scratch
python src/main.py \
--data_path ./data/TRDataChallenge2023.txt \
--postures_path ./config/postures.json \
--output_dir ./outputs/my_model
# Evaluate existing model
python src/main.py \
--do_eval \
--model_dir ./outputs/legalbert_multi \
--data_path ./data/TRDataChallenge2023.txt \
--postures_path ./config/postures.json
The model supports extensive customization through command-line arguments:
python src/main.py \
--do_train \
--data_path data/TRDataChallenge2023.txt \
--postures_path config/postures.json \
--output_dir outputs/custom_model \
--model_name nlpaueb/legal-bert-base-uncased \
--max_chunk_length 512 \
--stride 128 \
--lora_r 8 \
--lora_alpha 32 \
--batch_size 2 \
--grad_accum_steps 16 \
--learning_rate 3e-4 \
--num_epochs 10 \
--early_stop_patience 3 \
--random_seed 42
--max_chunk_length: Maximum tokens per chunk (default: 512)--stride: Overlap between chunks (default: 128)--lora_r: LoRA rank parameter (default: 8)--lora_alpha: LoRA alpha parameter (default: 32)--batch_size: Per-device batch size (default: 4 for Mac M4 and 32 for NVIDIA RTX)--grad_accum_steps: Gradient accumulation steps (default: 32 for Mac M4 and 4 for NVIDIA RTX)python src/main.py \
--do_train \
--continue_training \
--model_dir ./outputs/legalbert_multi \
--output_dir ./outputs/legalbert_multi_continued \
--num_epochs 3
nlpaueb/legal-bert-base-uncased[HEAD] {headtext} [SEP] {paragraph_text}outputs/legalbert_multi/pytorch_model.bin: Trained model weightsoutputs/legalbert_multi/tokenizer/: Tokenizer configurationoutputs/legalbert_multi/thresholds.json: Optimized classification thresholdsoutputs/legalbert_multi/metrics.json: Comprehensive evaluation metricsoutputs/legalbert_multi/plots/: Training curves and performance visualizationsoutputs/*.png: Exploratory data analysis plotsAll experiments use random_seed=42 for reproducibility across:
All dependencies are version-pinned in requirements.txt to ensure consistent results.
With the default configuration, you should achieve: