This gives LED excellent performance on structural legal documents.
Legal LED supports long contexts (~16k tokens), but many legal bills exceed that.
To summarize documents up to 30k tokens, this pipeline was used:
This improves semantic cohesion and section preservation.
1from transformers import AutoTokenizer, LEDForConditionalGeneration
2import torch
3
4model_name = "Anurag33Gaikwad/legal-led-billsum-summarization"
5
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = LEDForConditionalGeneration.from_pretrained(model_name)
8
9text = """Your long legal or legislative document here..."""
10
11inputs = tokenizer(
12 text,
13 return_tensors="pt",
14 truncation=True,
15 max_length=4096,
16)
17
18# LED requires global attention on the first token
19global_attention_mask = torch.zeros_like(inputs["input_ids"])
20global_attention_mask[:, 0] = 1
21
22summary_ids = model.generate(
23 inputs["input_ids"],
24 global_attention_mask=global_attention_mask,
25 num_beams=5,
26 max_length=512,
27 early_stopping=True
28)
29
30print(tokenizer.decode(summary_ids[0], skip_special_tokens=True))