Views
No views yet
BertForSequenceClassification6512830,5225120.14.48.31from transformers import BertTokenizer, BertForSequenceClassification
2
3model_name = "CoolCerebralTech/legalbert_finetuned"
4
5# Load tokenizer
6tokenizer = BertTokenizer.from_pretrained(model_name)
7
8# Load model
9model = BertForSequenceClassification.from_pretrained(model_name)
10
11⚖️ Inference: Legal Text Classification
12The model predicts legal categories based on input queries.
13
14🔹 Example: Classifying a Legal Question
15import torch
16
17def predict_legal_query(query):
18 """Runs inference on a legal query using LegalBERT."""
19 inputs = tokenizer(query, return_tensors="pt", truncation=True, padding=True, max_length=512)
20
21 with torch.no_grad():
22 outputs = model(**inputs)
23 logits = outputs.logits
24 predicted_class = torch.argmax(logits, dim=1).item() # Get highest scoring class
25
26 return predicted_class
27
28# 🔹 User Query
29query = "What are the fundamental rights under the Kenyan Constitution?"
30prediction = predict_legal_query(query)
31
32# 🔹 Label Mapping (Modify as needed)
33label_mapping = {0: "Civil Rights", 1: "Criminal Law", 2: "Property Law", 3: "Other"}
34predicted_label = label_mapping.get(prediction, "Unknown")
35
36print(f"📌 Query: {query}")
37print(f"🔹 Predicted Legal Category: {predicted_label}")
38
39✅ Sample Output
40📌 Query: What are the fundamental rights under the Kenyan Constitution?
41🔹 Predicted Legal Category: Civil Rights
42
43📌 Intended Use
44Legal Research & Analysis
45Kenyan Constitutional Law Classification
46Automated Legal Document Processing
47⚠️ Limitations & Warnings
48This model is fine-tuned on Kenyan constitutional law and may not generalize to other legal systems.
49Always consult a qualified legal expert before making legal conclusions.