Views
No views yet
law-ai/InLegalBERT designed to predict the outcome of legal appeals (Accepted vs. Rejected) based on the text of the legal judgment. It utilizes Low-Rank Adaptation (LoRA) for parameter-efficient fine-tuning (PEFT), making it highly efficient while retaining strong predictive capabilities on complex, domain-specific Indian legal texts.law-ai/InLegalBERT1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3from peft import PeftModel
4
5base_model_name = "law-ai/InLegalBERT"
6peft_model_id = "Beelzi/InLegalBERT-Appeal-Predictor-LoRA"
7
8tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
9base_model = AutoModelForSequenceClassification.from_pretrained(base_model_name, num_labels=2)
10model = PeftModel.from_pretrained(base_model, peft_model_id)
11
12def predict_long_text(text):
13 tokens = tokenizer(text, truncation=False, return_tensors="pt")
14 input_ids = tokens["input_ids"]
15 attention_mask = tokens["attention_mask"]
16
17 if input_ids.shape[1] > 512:
18 head_ids = input_ids[:, :128]
19 head_mask = attention_mask[:, :128]
20 tail_ids = input_ids[:, -383:]
21 tail_mask = attention_mask[:, -383:]
22
23 sep_id = torch.tensor([[tokenizer.sep_token_id]], device=input_ids.device)
24 sep_mask = torch.tensor([[1]], device=attention_mask.device)
25
26 input_ids = torch.cat([head_ids, tail_ids, sep_id], dim=1)
27 attention_mask = torch.cat([head_mask, tail_mask, sep_mask], dim=1)
28
29 input_ids = input_ids.to(model.device)
30 attention_mask = attention_mask.to(model.device)
31
32 model.eval()
33 with torch.no_grad():
34 outputs = model(input_ids=input_ids, attention_mask=attention_mask)
35 logits = outputs.logits
36 final_pred = torch.argmax(logits, dim=1).item()
37
38 return "Accepted" if final_pred == 1 else "Rejected"