pedronettotrue/legal-nlp-benchmark-br
Viewer • Updated • 50.5k • 58
How to use pedronettotrue/legalbench-br-v2-bertimbau-base with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="pedronettotrue/legalbench-br-v2-bertimbau-base") # Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("pedronettotrue/legalbench-br-v2-bertimbau-base")
model = AutoModelForSequenceClassification.from_pretrained("pedronettotrue/legalbench-br-v2-bertimbau-base")Fine-tuned BERTimbau for Brazilian legal area classification across 5 legal domains and 5 courts.
Part of the LegalBench-BR benchmark.
| Property | Value |
|---|---|
| Base model | neuralmind/bert-base-portuguese-cased (BERTimbau) |
| Fine-tuning method | LoRA (r=16, alpha=32, dropout=0.1) |
| Target modules | query, value |
| Trainable parameters | 593,669 (0.54% of total) |
| Training data | 50,000 samples (oversampled from 17,462 labeled) |
| Max sequence length | 128 tokens |
| Training epochs | 5 (with early stopping, patience=2) |
| GPU | Google Colab T4 (~35 min) |
| Metric | Score |
|---|---|
| Accuracy | 99.60% |
| F1 Macro | 0.9960 |
| Inference latency | 86.4 ms (CPU) |
| Legal Area | F1 Score |
|---|---|
| Administrativo | 0.995 |
| Civel | 0.995 |
| Consumidor | 0.995 |
| Penal | 0.995 |
| Tributario | 0.995 |
| Model | Accuracy | F1 Macro | Latency |
|---|---|---|---|
| BERTimbau v2 (this model) | 99.60% | 0.9960 | 86 ms |
| Legal-BERTimbau v2 | 98.00% | 0.9800 | 85 ms |
| Claude 3.5 Haiku | 77.20% | 0.7534 | 1,192 ms |
| GPT-4o | 74.80% | 0.7095 | 1,048 ms |
| GPT-4o-mini | 55.60% | 0.5243 | 969 ms |
| Llama 3.1 8B | 47.20% | 0.3811 | 811 ms |
LABEL2ID = {
"civel": 0,
"consumidor": 1,
"tributario": 2,
"administrativo": 3,
"penal": 4
}
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_name = "pedronettotrue/bertimbau-legal-tjsc-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
ID2LABEL = {0: "civel", 1: "consumidor", 2: "tributario", 3: "administrativo", 4: "penal"}
text = "Classe: Apelacao Civel. Assuntos: Responsabilidade Civil, Danos Morais"
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
with torch.no_grad():
logits = model(**inputs).logits
predicted_class = ID2LABEL[logits.argmax().item()]
print(f"Predicted: {predicted_class}")
# Output: civel
merge_and_unload() -- loads without PEFT dependencyThe model was trained on metadata from Brazilian court decisions (procedural class + legal subjects), not full-text judicial opinions. This is an important distinction: the model classifies based on structured judicial metadata patterns.
Courts covered in training:
@misc{legalbenchbr2026,
title={LegalBench-BR v2: A Multi-Court Benchmark Demonstrating Fine-Tuned Encoder
Superiority over LLMs for Brazilian Legal Area Classification},
year={2026},
url={https://huggingface.co/pedronettotrue/bertimbau-legal-tjsc-v2}
}
Apache 2.0
Base model
neuralmind/bert-base-portuguese-cased