Views
No views yet
nlpaueb/legal-bert-base-uncased
for multi-class contract clause classification across all 41 CUAD clause types.| Property | Value |
|---|---|
| Base model | nlpaueb/legal-bert-base-uncased |
| Adapter type | LoRA (PEFT) |
| Task | Multi-class sequence classification |
| Classes | 41 CUAD clause types |
| LoRA rank (r) | 16 |
| LoRA alpha | 32 |
| LoRA dropout | 0.1 |
| Target modules | query, value |
| Max sequence length | 256 tokens |
| Epochs | 5 |
| Learning rate | 2e-4 |
| Batch size | 16 |
| Weight decay | 0.01 |
| Warmup ratio | 0.1 |
| Optimizer | AdamW (default HF Trainer) |
| Hardware | Kaggle GPU (T4) |
| PEFT version | 0.18.1 |
| Epoch | Train Loss | Val Loss | Accuracy | Weighted F1 | Macro F1 |
|---|---|---|---|---|---|
| 1 | 5.992 | 4.285 | 43.22% | 0.316 | 0.158 |
| 2 | 2.881 | 2.485 | 65.81% | 0.601 | 0.382 |
| 3 | 2.203 | 2.124 | 69.79% | 0.651 | 0.448 |
| 4 | 1.958 | 2.005 | 71.05% | 0.668 | 0.488 |
| 5 | 1.852 | 1.944 | 71.46% | 0.677 | 0.502 |
| Metric | Baseline (untrained) | Fine-Tuned (this model) |
|---|---|---|
| Accuracy | 3.28% | 71.46% |
| Weighted F1 | 0.0082 | 0.6771 |
| Macro F1 | 0.0053 | 0.5016 |
The baseline was evaluated by running the untrainednlpaueb/legal-bert-base-uncasedmodel directly on the test set without any fine-tuning. The near-random performance (3.28%) confirms the base model has no prior knowledge of CUAD clause types.
| Metric | Base Model | Fine-Tuned |
|---|---|---|
| MMLU Abstract Algebra Accuracy | 19.00% | 24.00% |
| Class | Precision | Recall | F1 | Support |
|---|---|---|---|---|
| Non-Disparagement (13) | 1.00 | 1.00 | 1.00 | 89 |
| Termination for Convenience (14) | 0.96 | 0.95 | 0.96 | 109 |
| Expiration Date (4) | 0.92 | 0.97 | 0.95 | 127 |
| Irrevocable or Perpetual License (29) | 0.72 | 0.79 | 0.75 | 89 |
| Audit Rights (32) | 0.84 | 0.93 | 0.88 | 82 |
| Effective Date (3) | 0.88 | 0.90 | 0.89 | 125 |
| Renewal Term (5) | 0.72 | 0.97 | 0.83 | 133 |
| Insurance (37)* | 0.00 | 0.00 | 0.00 | 33 |
* Some rare classes (e.g. Insurance label index 37, classes 0, 1, 2) have very few training examples and score near zero — see Limitations section below.
| Clause | Predicted Type | Confidence |
|---|---|---|
| "Either party may terminate this Agreement upon 30 days written notice." | Termination for Convenience | 79.50% |
| "Licensee shall not transfer or sublicense any rights granted herein." | Anti-Assignment | 61.04% |
| "This Agreement shall be governed by the laws of California." | Governing Law | 96.87% |
| "The Company shall maintain insurance coverage of at least $1,000,000." | Insurance | 97.44% |
| "Neither party shall disclose confidential information to third parties." | Anti-Assignment | 41.98% |
peft library.pip install transformers peft scikit-learn1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3from peft import PeftModel
4
5base_model_id = "nlpaueb/legal-bert-base-uncased"
6adapter_id = "Mokshith31/legalbert-contract-clause-classification"
7
8# Load tokenizer and base model
9tokenizer = AutoTokenizer.from_pretrained(base_model_id)
10base_model = AutoModelForSequenceClassification.from_pretrained(
11 base_model_id,
12 num_labels=41
13)
14
15# Load LoRA adapter on top
16model = PeftModel.from_pretrained(base_model, adapter_id)
17model.eval()
18
19# Label mapping (ID → clause type name)
20id2label = {
21 0: "Document Name", 1: "Parties", 2: "Agreement Date",
22 3: "Effective Date", 4: "Expiration Date", 5: "Renewal Term",
23 6: "Notice Period to Terminate Renewal", 7: "Governing Law",
24 8: "Most Favored Nation", 9: "Non-Compete", 10: "Exclusivity",
25 11: "No-Solicit of Customers", 12: "No-Solicit of Employees",
26 13: "Non-Disparagement", 14: "Termination for Convenience",
27 15: "ROFR / ROFO / ROFN", 16: "Change of Control",
28 17: "Anti-Assignment", 18: "Revenue / Profit Sharing",
29 19: "Price Restriction", 20: "Minimum Commitment",
30 21: "Volume Restriction", 22: "IP Ownership Assignment",
31 23: "Joint IP Ownership", 24: "License Grant",
32 25: "Non-Transferable License", 26: "Affiliate License-Licensor",
33 27: "Affiliate License-Licensee",
34 28: "Unlimited / All-You-Can-Eat License",
35 29: "Irrevocable or Perpetual License", 30: "Source Code Escrow",
36 31: "Post-Termination Services", 32: "Audit Rights",
37 33: "Uncapped Liability", 34: "Cap on Liability",
38 35: "Liquidated Damages", 36: "Warranty Duration",
39 37: "Insurance", 38: "Covenant Not to Sue",
40 39: "Third Party Beneficiary", 40: "Other"
41}
42
43# Run inference
44clause = "This Agreement shall be governed by the laws of California."
45
46inputs = tokenizer(
47 clause,
48 return_tensors="pt",
49 truncation=True,
50 max_length=256
51)
52
53with torch.no_grad():
54 outputs = model(**inputs)
55
56probs = torch.softmax(outputs.logits, dim=1)
57pred_id = outputs.logits.argmax(dim=-1).item()
58confidence = probs.max().item()
59
60print(f"Predicted clause type: {id2label[pred_id]}")
61print(f"Confidence: {confidence:.2%}")1import torch
2from peft import PeftModel
3from transformers import (AutoModelForSequenceClassification,
4 AutoTokenizer, pipeline)
5
6base = AutoModelForSequenceClassification.from_pretrained(
7 "nlpaueb/legal-bert-base-uncased", num_labels=41
8)
9model = PeftModel.from_pretrained(
10 base,
11 "Mokshith31/legalbert-contract-clause-classification"
12)
13model = model.merge_and_unload() # fuse LoRA weights into base
14
15tokenizer = AutoTokenizer.from_pretrained(
16 "nlpaueb/legal-bert-base-uncased"
17)
18
19classifier = pipeline(
20 "text-classification",
21 model=model,
22 tokenizer=tokenizer
23)
24
25result = classifier(
26 "Either party may terminate upon 30 days written notice.",
27 truncation=True,
28 max_length=256
29)
30print(result)| ID | Clause Type |
|---|---|
| 0 | Document Name |
| 1 | Parties |
| 2 | Agreement Date |
| 3 | Effective Date |
| 4 | Expiration Date |
| 5 | Renewal Term |
| 6 | Notice Period to Terminate Renewal |
| 7 | Governing Law |
| 8 | Most Favored Nation |
| 9 | Non-Compete |
| 10 | Exclusivity |
| 11 | No-Solicit of Customers |
| 12 | No-Solicit of Employees |
| 13 | Non-Disparagement |
| 14 | Termination for Convenience |
| 15 | ROFR / ROFO / ROFN |
| 16 | Change of Control |
| 17 | Anti-Assignment |
| 18 | Revenue / Profit Sharing |
| 19 | Price Restriction |
| 20 | Minimum Commitment |
| 21 | Volume Restriction |
| 22 | IP Ownership Assignment |
| 23 | Joint IP Ownership |
| 24 | License Grant |
| 25 | Non-Transferable License |
| 26 | Affiliate License-Licensor |
| 27 | Affiliate License-Licensee |
| 28 | Unlimited / All-You-Can-Eat License |
| 29 | Irrevocable or Perpetual License |
| 30 | Source Code Escrow |
| 31 | Post-Termination Services |
| 32 | Audit Rights |
| 33 | Uncapped Liability |
| 34 | Cap on Liability |
| 35 | Liquidated Damages |
| 36 | Warranty Duration |
| 37 | Insurance |
| 38 | Covenant Not to Sue |
| 39 | Third Party Beneficiary |
| 40 | Other |
1@article{hendrycks2021cuad,
2 title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},
3 author={Hendrycks, Dan and Burns, Collin and Chen, Anya and Ball, Spencer},
4 journal={arXiv preprint arXiv:2103.06268},
5 year={2021}
6}1@inproceedings{chalkidis-etal-2020-legal,
2 title={LEGAL-BERT: The Muppets straight out of Law School},
3 author={Chalkidis, Ilias and Fergadiotis, Manos and Malakasiotis,
4 Prodromos and Aletras, Nikolaos and Androutsopoulos, Ion},
5 booktitle={Findings of EMNLP},
6 year={2020}
7}| Library | Version |
|---|---|
| Transformers | latest |
| PEFT | 0.18.1 |
| PyTorch | latest |
| Datasets | latest |
| scikit-learn | latest |
| Accelerate | latest |