Fine-tuned
Legal-BERT classifier that detects whether a user in a multi-turn conversation is
seeking legal guidance.
Joint pipeline test (with
legalbert-primary_topic): legal
84.1%, topic
72.0%, joint
72.0%.
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4def serialize(messages, input_mode="user"):
5 lines = []
6 for msg in messages:
7 role = msg["role"]
8 if input_mode == "user" and role != "user":
9 continue
10 lines.append(f"{role.capitalize()}: {msg['content']}")
11 return "\n".join(lines)
12
13model_id = "AmirMohseni/legalbert-seeks_guidance"
14tokenizer = AutoTokenizer.from_pretrained(model_id)
15model = AutoModelForSequenceClassification.from_pretrained(model_id)
16input_mode = getattr(model.config, "legal_cls_input_mode", "user")
17max_length = getattr(model.config, "legal_cls_max_length", 512)
18
19text = serialize(conversation, input_mode=input_mode)
20enc = tokenizer(text, truncation=True, max_length=max_length, return_tensors="pt")
21with torch.no_grad():
22 pred_id = model(**enc).logits.argmax(dim=-1).item()
23print(model.config.id2label[str(pred_id)])
Selected from a 24-trial hyperparameter sweep (LR × input mode × effective batch). See
legalbert_classifier_sweep_colab.ipynb.