Views
No views yet
1# Transform a legal passive sentence to active voice
2passive_sentence = "The contract shall be executed by both parties within 30 days."
3# Model output: "Both parties shall execute the contract within 30 days."1# Simplify GDPR text
2passive_sentence = "Personal data may be processed by the controller for legitimate interests."
3# Model output: "The controller may process personal data for legitimate interests."1# Transform UN legal text
2passive_sentence = "All necessary measures shall be taken by Member States to ensure compliance."
3# Model output: "Member States shall take all necessary measures to ensure compliance."pip install transformers torch peft accelerate bitsandbytes1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5# Load base model with 4-bit quantization
6base_model = "mistralai/Mistral-7B-Instruct-v0.1"
7model = AutoModelForCausalLM.from_pretrained(
8 base_model,
9 load_in_4bit=True,
10 torch_dtype=torch.float16,
11 device_map="auto"
12)
13
14# Load LoRA adapter
15model = PeftModel.from_pretrained(model, "rafiaa/legal-passive-to-active-mistral-7b")
16tokenizer = AutoTokenizer.from_pretrained(base_model)
17
18# Set pad token
19if tokenizer.pad_token is None:
20 tokenizer.pad_token = tokenizer.eos_token1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5# Load base model (CPU compatible)
6base_model = "mistralai/Mistral-7B-Instruct-v0.1"
7model = AutoModelForCausalLM.from_pretrained(
8 base_model,
9 torch_dtype=torch.float32,
10 device_map="cpu"
11)
12
13# Load LoRA adapter
14model = PeftModel.from_pretrained(model, "rafiaa/legal-passive-to-active-mistral-7b")
15tokenizer = AutoTokenizer.from_pretrained(base_model)
16
17# Set pad token
18if tokenizer.pad_token is None:
19 tokenizer.pad_token = tokenizer.eos_token1def transform_passive_to_active(passive_sentence, max_length=512):
2 # Create instruction prompt
3 instruction = """You are a legal text transformation expert. Your task is to convert passive voice sentences to active voice while maintaining the exact legal meaning and terminology.
4
5Input: Transform the following legal sentence from passive to active voice.
6
7Legal Sentence: """
8
9 prompt = instruction + passive_sentence
10 inputs = tokenizer(prompt, return_tensors="pt")
11
12 with torch.no_grad():
13 outputs = model.generate(
14 **inputs,
15 max_length=max_length,
16 temperature=0.7,
17 do_sample=True,
18 pad_token_id=tokenizer.eos_token_id
19 )
20
21 return tokenizer.decode(outputs[0], skip_special_tokens=True)
22
23# Example usage
24passive = "The agreement shall be signed by the authorized representatives."
25active = transform_passive_to_active(passive)
26print(active)1# Batch processing multiple legal sentences
2legal_sentences = [
3 "The policy was established by the board of directors.",
4 "All documents must be reviewed by legal counsel.",
5 "The regulations were enacted by Parliament."
6]
7
8for sentence in legal_sentences:
9 transformed = transform_passive_to_active(sentence)
10 print(f"Passive: {sentence}")
11 print(f"Active: {transformed}\n")| Model | Human Eval Score | BERTScore F1 | Performance |
|---|---|---|---|
| Mistral-7B Base | Baseline | High | Good |
| legal-passive-to-active-mistral-7b | +15% | Higher | Excellent |
| legal-passive-to-active-llama2-7b | +6% | High | Good |
1@misc{legal-passive-active-mistral,
2 title={legal-passive-to-active-mistral-7b: An Enhanced LoRA Fine-tuned Model for Legal Voice Transformation},
3 author={Rafi Al Attrach},
4 year={2024},
5 url={https://huggingface.co/rafiaa/legal-passive-to-active-mistral-7b}
6}