AnkushRaheja commited on
Commit
99084e6
·
verified ·
1 Parent(s): 0804a4d

Upload 10 files

Browse files

Updated Version of Risc scorer and Clause Classifier models added

README.md ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: mit
4
+ tags:
5
+ - legal
6
+ - bert
7
+ - text-classification
8
+ - multi-task
9
+ datasets:
10
+ - coastalcph/lex_glue
11
+ ---
12
+
13
+ # Legal AI Risk Analyzer — V7
14
+
15
+ Multi-task Legal-BERT for clause classification and risk scoring.
16
+
17
+ ## Performance
18
+ | Metric | Score |
19
+ |--------|-------|
20
+ | Classification Accuracy | 0.8793 (87.93%) |
21
+ | F1 Score (weighted) | 0.8750 |
22
+ | R² Score (risk scoring) | 0.8692 |
23
+ | MAE (risk scoring) | 3.57 points |
24
+ | Risk Category Accuracy | 0.9336 (93.36%) |
25
+
26
+ ## Architecture
27
+ - **Base model**: `nlpaueb/legal-bert-base-uncased`
28
+ - **Task 1**: 100-class clause classification (LEDGAR)
29
+ - **Task 2**: Risk score regression (0–100)
30
+ - **Heads**: Linear(768→384→N) + ReLU + Dropout(0.25)
31
+
32
+ ## Usage
33
+ ```python
34
+ import torch, json
35
+ import torch.nn as nn
36
+ from transformers import AutoTokenizer, AutoModel
37
+
38
+ class MultiTaskLegalModel(nn.Module):
39
+ def __init__(self, model_name, num_labels,
40
+ hidden_size=768, dropout_rate=0.25):
41
+ super().__init__()
42
+ self.bert = AutoModel.from_pretrained(model_name)
43
+ self.dropout = nn.Dropout(dropout_rate)
44
+ self.classifier = nn.Sequential(
45
+ nn.Linear(hidden_size, hidden_size // 2),
46
+ nn.ReLU(), nn.Dropout(dropout_rate),
47
+ nn.Linear(hidden_size // 2, num_labels)
48
+ )
49
+ self.regressor = nn.Sequential(
50
+ nn.Linear(hidden_size, hidden_size // 2),
51
+ nn.ReLU(), nn.Dropout(dropout_rate),
52
+ nn.Linear(hidden_size // 2, 1),
53
+ nn.Sigmoid()
54
+ )
55
+ def forward(self, input_ids, attention_mask):
56
+ out = self.bert(input_ids=input_ids,
57
+ attention_mask=attention_mask,
58
+ return_dict=True)
59
+ cls = self.dropout(out.last_hidden_state[:, 0, :])
60
+ return self.classifier(cls), self.regressor(cls).squeeze(-1)
61
+
62
+
63
+ # ── Load ──────────────────────────────────────────────────────────────────
64
+ model_dir = "path/to/legal_ai_hf_upload" # or HF repo id after upload
65
+
66
+ with open(f"{model_dir}/metadata.json") as f:
67
+ meta = json.load(f)
68
+
69
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
70
+ model = MultiTaskLegalModel(model_dir, meta['num_labels'])
71
+ model.load_state_dict(
72
+ torch.load(f"{model_dir}/full_model.pt", map_location="cpu")
73
+ )
74
+ model.eval()
75
+
76
+ # ── Inference ─────────────────────────────────────────────────────────────
77
+ def analyse_clause(text: str) -> dict:
78
+ inputs = tokenizer(text, return_tensors="pt",
79
+ truncation=True, max_length=256, padding=True)
80
+ with torch.no_grad():
81
+ logits, risk = model(**inputs)
82
+ label = meta["label_names"][logits.argmax().item()]
83
+ score = round(float(risk.item()) * 100, 1)
84
+ category = "Low" if score < 40 else ("Medium" if score < 70 else "High")
85
+ confidence = round(float(torch.softmax(logits, dim=1).max()), 3)
86
+ return {
87
+ "clause_type": label,
88
+ "confidence": confidence,
89
+ "risk_score": score,
90
+ "category": category
91
+ }
92
+
93
+ # Test
94
+ result = analyse_clause(
95
+ "The Company shall indemnify and hold harmless from all claims "
96
+ "without limitation whatsoever."
97
+ )
98
+ print(result)
99
+ # {'clause_type': 'Indemnifications', 'confidence': 0.91,
100
+ # 'risk_score': 85.0, 'category': 'High'}
101
+ ```
102
+
103
+ ## Training details
104
+ - Dataset: LEDGAR — 60k train / 10k val / 10k test
105
+ - Hardware: 2× NVIDIA T4 (DataParallel)
106
+ - Batch size: 256 (128 per GPU) | FP16 mixed precision
107
+ - Optimizer: AdamW + Layer-wise LR Decay (γ=0.88)
108
+ - Scheduler: Cosine annealing + warmup (6%)
109
+ - Loss: CrossEntropyLoss (label_smoothing=0.1) + HuberLoss (δ=0.1)
110
+ - Epochs: 15
classifier_head.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:12af285bc91b425e73f27ab5883a41e8d594df89e43f612c1d5990de929d7e05
3
+ size 1337773
config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_cross_attention": false,
3
+ "architectures": [
4
+ "BertModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "dtype": "float32",
10
+ "eos_token_id": null,
11
+ "eos_token_ids": 0,
12
+ "finetuning_task": null,
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout_prob": 0.1,
15
+ "hidden_size": 768,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 3072,
18
+ "is_decoder": false,
19
+ "layer_norm_eps": 1e-12,
20
+ "max_position_embeddings": 512,
21
+ "model_type": "bert",
22
+ "num_attention_heads": 12,
23
+ "num_hidden_layers": 12,
24
+ "output_past": true,
25
+ "pad_token_id": 0,
26
+ "pruned_heads": {},
27
+ "tie_word_embeddings": true,
28
+ "torchscript": false,
29
+ "transformers_version": "5.2.0",
30
+ "type_vocab_size": 2,
31
+ "use_bfloat16": false,
32
+ "use_cache": true,
33
+ "vocab_size": 30522
34
+ }
full_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c30e9fd19d08ea0d840657146a4457349252f0505439f66c62ae9c49344013c
3
+ size 440531744
metadata.json ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_version": "V7",
3
+ "model_name": "nlpaueb/legal-bert-base-uncased",
4
+ "num_labels": 100,
5
+ "label_names": [
6
+ "Adjustments",
7
+ "Agreements",
8
+ "Amendments",
9
+ "Anti-Corruption Laws",
10
+ "Applicable Laws",
11
+ "Approvals",
12
+ "Arbitration",
13
+ "Assignments",
14
+ "Assigns",
15
+ "Authority",
16
+ "Authorizations",
17
+ "Base Salary",
18
+ "Benefits",
19
+ "Binding Effects",
20
+ "Books",
21
+ "Brokers",
22
+ "Capitalization",
23
+ "Change In Control",
24
+ "Closings",
25
+ "Compliance With Laws",
26
+ "Confidentiality",
27
+ "Consent To Jurisdiction",
28
+ "Consents",
29
+ "Construction",
30
+ "Cooperation",
31
+ "Costs",
32
+ "Counterparts",
33
+ "Death",
34
+ "Defined Terms",
35
+ "Definitions",
36
+ "Disability",
37
+ "Disclosures",
38
+ "Duties",
39
+ "Effective Dates",
40
+ "Effectiveness",
41
+ "Employment",
42
+ "Enforceability",
43
+ "Enforcements",
44
+ "Entire Agreements",
45
+ "Erisa",
46
+ "Existence",
47
+ "Expenses",
48
+ "Fees",
49
+ "Financial Statements",
50
+ "Forfeitures",
51
+ "Further Assurances",
52
+ "General",
53
+ "Governing Laws",
54
+ "Headings",
55
+ "Indemnifications",
56
+ "Indemnity",
57
+ "Insurances",
58
+ "Integration",
59
+ "Intellectual Property",
60
+ "Interests",
61
+ "Interpretations",
62
+ "Jurisdictions",
63
+ "Liens",
64
+ "Litigations",
65
+ "Miscellaneous",
66
+ "Modifications",
67
+ "No Conflicts",
68
+ "No Defaults",
69
+ "No Waivers",
70
+ "Non-Disparagement",
71
+ "Notices",
72
+ "Organizations",
73
+ "Participations",
74
+ "Payments",
75
+ "Positions",
76
+ "Powers",
77
+ "Publicity",
78
+ "Qualifications",
79
+ "Records",
80
+ "Releases",
81
+ "Remedies",
82
+ "Representations",
83
+ "Sales",
84
+ "Sanctions",
85
+ "Severability",
86
+ "Solvency",
87
+ "Specific Performance",
88
+ "Submission To Jurisdiction",
89
+ "Subsidiaries",
90
+ "Successors",
91
+ "Survival",
92
+ "Tax Withholdings",
93
+ "Taxes",
94
+ "Terminations",
95
+ "Terms",
96
+ "Titles",
97
+ "Transactions With Affiliates",
98
+ "Use Of Proceeds",
99
+ "Vacations",
100
+ "Venues",
101
+ "Vesting",
102
+ "Waiver Of Jury Trials",
103
+ "Waivers",
104
+ "Warranties",
105
+ "Withholdings"
106
+ ],
107
+ "max_sequence_length": 256,
108
+ "hidden_size": 768,
109
+ "dropout_rate": 0.25,
110
+ "training_gpus": 2,
111
+ "test_metrics": {
112
+ "classification": {
113
+ "accuracy": 0.8793,
114
+ "precision": 0.8757207303564448,
115
+ "recall": 0.8793,
116
+ "f1_score": 0.8749556050721212
117
+ },
118
+ "regression": {
119
+ "r2_score": 0.8692134972569636,
120
+ "mae": 3.5734300209999086,
121
+ "rmse": 5.846707991168016
122
+ },
123
+ "risk_categories": {
124
+ "accuracy": 0.9336
125
+ }
126
+ },
127
+ "training_config": {
128
+ "epochs": 15,
129
+ "batch_size": 256,
130
+ "batch_per_gpu": 128,
131
+ "base_learning_rate": 4e-05,
132
+ "warmup_ratio": 0.06,
133
+ "loss_functions": {
134
+ "classification": "CrossEntropyLoss (label_smoothing=0.1)",
135
+ "regression": "Huber Loss (delta=0.1)"
136
+ },
137
+ "loss_weights": {
138
+ "classification": 1.0,
139
+ "regression": 1.0
140
+ },
141
+ "optimizer": "AdamW with LLRD (gamma=0.88)",
142
+ "scheduler": "Cosine Annealing with Warmup",
143
+ "risk_label_coverage": "100/100 classes"
144
+ }
145
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ad1a5bca49725d153c49cef476c5a747f9a8a6f57c29d4bd89aca3bfdcc3df8
3
+ size 437951304
regressor_head.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a51bb8a7c3f8876b7bf5590c57a11a229733e06a341e846f55c7418fa768a57a
3
+ size 1185315
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": true,
5
+ "is_local": false,
6
+ "mask_token": "[MASK]",
7
+ "model_max_length": 512,
8
+ "pad_token": "[PAD]",
9
+ "sep_token": "[SEP]",
10
+ "strip_accents": null,
11
+ "tokenize_chinese_chars": true,
12
+ "tokenizer_class": "BertTokenizer",
13
+ "unk_token": "[UNK]"
14
+ }
training_history.csv ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ epoch,train_loss,train_loss_cls,train_loss_reg,train_acc,val_loss,val_accuracy,val_f1,val_precision,val_recall,val_r2,val_mae,val_rmse
2
+ 1,3.6710602258114107,3.664399197253775,0.0066610286676106936,0.33708333333333335,0.5214192204177379,0.7572,0.7074415922932084,0.7036618344786799,0.7572,0.7661761656092274,5.079775939941406,7.737287896207631
3
+ 2,1.5659432710485255,1.5635212751145058,0.0024219956506598504,0.7995333333333333,0.24467970952391624,0.833,0.8135713267477054,0.8135959772541416,0.833,0.8374696367492676,3.942265531730652,6.450770938813874
4
+ 3,1.2965205502002797,1.294595095451842,0.0019254570311688362,0.8434833333333334,0.21539378352463245,0.848,0.8345476464553864,0.8447049536660243,0.848,0.8555408712095498,3.7188382574081422,6.081586153227106
5
+ 4,1.2116904644255941,1.2100293194994014,0.0016611464165567243,0.8661,0.20681057162582875,0.8584,0.8485770548769197,0.8520636039272422,0.8584,0.8619396128537031,3.690098307609558,5.945370326210087
6
+ 5,1.1546311690452251,1.153167647250155,0.0014635232742875814,0.8831333333333333,0.196885877661407,0.8648,0.8553786209185408,0.8567124767369673,0.8648,0.8661651999474788,3.606699145126343,5.853678888960011
7
+ 6,1.1107255012431043,1.1093843274928155,0.0013411752805311946,0.898,0.19397251792252063,0.8702,0.862556230588007,0.8626834743488424,0.8702,0.868818861297368,3.5736558856964113,5.795355289996768
8
+ 7,1.0735298057819935,1.0722795833932592,0.0012502233292213938,0.911,0.19419998489320278,0.8729,0.8662488873408196,0.8668921113165466,0.8729,0.8677959834232898,3.5752074138641357,5.817905901660457
9
+ 8,1.0450228313182264,1.0438580972083071,0.0011647329100982306,0.9198666666666667,0.1971759855747223,0.8745,0.8682543336719728,0.868860550038201,0.8745,0.8690140446623786,3.5184344007492063,5.791042253720064
10
+ 9,1.0214721765924006,1.0203666260901918,0.001105548023305675,0.9289,0.19810124449431896,0.8759,0.8694744478579515,0.8721128694836355,0.8759,0.8700229420868484,3.55117027759552,5.768696875215731
11
+ 10,0.9992985306902135,0.9982370878787751,0.0010614459296351575,0.9360833333333334,0.19851093888282775,0.8787,0.8738019174469928,0.8775553207188174,0.8787,0.8699872963650783,3.574739803504944,5.769487842755515
12
+ 11,0.9855700401549644,0.9845429326625581,0.0010271075374862933,0.9424,0.19890217967331408,0.8797,0.875550782111539,0.8792660052234149,0.8797,0.8693493539105841,3.5854038719177246,5.78362529705738
13
+ 12,0.9731350028768498,0.9721321176975332,0.001002886459885284,0.9464833333333333,0.2014853686094284,0.8793,0.8748119788168973,0.8774139782397148,0.8793,0.8677888459116989,3.5980608905792235,5.818062949878145
14
+ 13,0.9651041398657129,0.9641242927693306,0.0009798480299341077,0.9500333333333333,0.20219865441322327,0.8797,0.8754897760360602,0.8785927062807161,0.8797,0.8688536145318553,3.566714022064209,5.794587570320839
15
+ 14,0.9606386002073897,0.959660526539417,0.0009780740605647418,0.95155,0.20259030126035213,0.8811,0.8768527318659198,0.8807566329654987,0.8811,0.869047870671788,3.5660733938217164,5.790294461724023
16
+ 15,0.959062233630647,0.9580984607655951,0.0009637737220668412,0.95235,0.20248149074614047,0.8813,0.8771001359798906,0.8810218945645605,0.8813,0.8691144230475137,3.565014412307739,5.78882290555858