CoolFace
Modelpublic

writetoasik/indian-legal-assistant-aya

sourceHugging Faceapache-2.0updated 9mo agoView on Hugging Face
0likes12downloads
Model Card

🏛️ Indian Legal Assistant (Aya-23-8B LoRA)

A fine-tuned version of CohereForAI/aya-23-8B specialized in Indian law, covering the Indian Penal Code (IPC), Code of Criminal Procedure (CrPC), Indian Constitution, and legal procedures. Supports both English and Tamil.

Model Description

This model is designed to help people understand Indian law by providing detailed explanations of:

  • —Indian Penal Code (IPC) - Criminal offenses, punishments, sections 302, 420, 498A, etc.
  • —Bharatiya Nyaya Sanhita (BNS) - New criminal code replacing IPC (2024)
  • —Code of Criminal Procedure (CrPC) - FIR filing, bail, arrest procedures
  • —Indian Constitution - Fundamental rights, articles, legal provisions
  • —Legal Procedures - How to file complaints, get bail, rights during arrest

Key Features

FeatureDetails
LanguagesEnglish, Tamil (தமிழ்)
DomainIndian Legal System
Base ModelCohereForAI/aya-23-8B
Fine-tuning MethodQLoRA (4-bit quantization)
Parameters Trained~50M (0.6% of base model)

Model Details

  • —Developed by: writetoasik
  • —Model type: Causal Language Model (LoRA Adapter)
  • —Language(s): English, Tamil
  • —License: Apache 2.0
  • —Fine-tuned from: CohereForAI/aya-23-8B

How to Use

Installation

bash
pip install torch transformers peft bitsandbytes accelerate

Quick Start

python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel

# Load model with 4-bit quantization
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type='nf4',
    bnb_4bit_compute_dtype=torch.bfloat16
)

# Load base model and adapter
model = AutoModelForCausalLM.from_pretrained(
    "CohereForAI/aya-23-8B",
    quantization_config=bnb_config,
    device_map="auto"
)
model = PeftModel.from_pretrained(model, "writetoasik/indian-legal-assistant-aya")
tokenizer = AutoTokenizer.from_pretrained("CohereForAI/aya-23-8B")

# System prompt for best results
SYSTEM = """You are an expert Indian legal assistant. Provide DETAILED explanations including:
- What the section/law means
- Punishment or penalty
- Whether offense is bailable or not
- Practical guidance"""

# Ask a question
question = "What is Section 420 IPC?"

prompt = f'<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{SYSTEM}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{question}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>'

inputs = tokenizer(prompt, return_tensors='pt').to('cuda')
outputs = model.generate(
    **inputs,
    max_new_tokens=500,
    min_new_tokens=50,
    temperature=0.8,
    top_p=0.9,
    do_sample=True,
    repetition_penalty=1.15,
    pad_token_id=tokenizer.eos_token_id
)

response = tokenizer.decode(outputs[0], skip_special_tokens=False)
if '<|CHATBOT_TOKEN|>' in response:
    response = response.split('<|CHATBOT_TOKEN|>')[-1]
if '<|END_OF_TURN_TOKEN|>' in response:
    response = response.split('<|END_OF_TURN_TOKEN|>')[0]

print(response.strip())

Interactive Chat

python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel

print("🏛️ Loading Indian Legal Assistant...")

bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type='nf4', bnb_4bit_compute_dtype=torch.bfloat16)
model = AutoModelForCausalLM.from_pretrained("CohereForAI/aya-23-8B", quantization_config=bnb, device_map='auto')
model = PeftModel.from_pretrained(model, "writetoasik/indian-legal-assistant-aya")
tokenizer = AutoTokenizer.from_pretrained("CohereForAI/aya-23-8B")

SYSTEM = "You are an expert Indian legal assistant. Provide detailed explanations."

def ask(question):
    prompt = f'<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{SYSTEM}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{question}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>'
    inputs = tokenizer(prompt, return_tensors='pt').to('cuda')
    outputs = model.generate(**inputs, max_new_tokens=500, temperature=0.8, do_sample=True, pad_token_id=tokenizer.eos_token_id)
    response = tokenizer.decode(outputs[0], skip_special_tokens=False)
    if '<|CHATBOT_TOKEN|>' in response:
        response = response.split('<|CHATBOT_TOKEN|>')[-1]
    if '<|END_OF_TURN_TOKEN|>' in response:
        response = response.split('<|END_OF_TURN_TOKEN|>')[0]
    return response.strip()

# Interactive loop
while True:
    q = input("\n❓ You: ")
    if q.lower() in ['quit', 'exit']:
        break
    print(f"\n📜 {ask(q)}")

Example Outputs

English: Section 420 IPC

Question: What is Section 420 IPC?

Response:

Section 420 IPC deals with cheating and dishonestly inducing delivery of property. Offense: Cheating someone and thereby dishonestly inducing the deceived person to deliver property, or to make, alter or destroy a valuable security. Punishment: Imprisonment up to 7 years AND fine. If the thing delivered is a valuable security, punishment extends to 10 years. Nature: Cognizable, Non-bailable, Compoundable with court permission. Key elements: Deception, fraudulent inducement, actual delivery of property.

English: Anticipatory Bail

Question: What is anticipatory bail and how to apply for it?

Response:

Anticipatory bail, as per Section 438 of CrPC, allows a person to seek bail in anticipation of arrest. To obtain anticipatory bail, the accused must apply to the appropriate court (Sessions Court or High Court), providing reasons for the application. The decision lies within the discretion of the court, considering factors like the nature of allegations and likelihood of absconding.

Tamil: IPC Section 302

Question: IPC பிரிவு 302 பற்றி விரிவாக விளக்கவும்

Response:

Section 302 of the IPC addresses the punishment for 'murder'. It provides three distinct options: 1. Death sentence 2. Life Imprisonment 3. Rigorous Imprisonment up to ten years In addition, the offender is also liable to a fine. When murder occurs under circumstances outlined in Sections 300-304A, the court is obligated to impose a harsher sentence.

Training Details

Training Data

Combined dataset of 26,748 examples from:

DatasetExamplesContent
jft-ai-team/IndianPenalCode-Legal-Insights5,175IPC sections Q&A
ninadn/indian-legal7,030Case summaries
Techmaestro369/indian-legal-texts-finetuning14,543IPC, CrPC, Constitution Q&A

Training Procedure

Training Hyperparameters
ParameterValue
MethodQLoRA (4-bit quantization)
LoRA Rank (r)32
LoRA Alpha64
LoRA Dropout0.05
Target Modulesqproj, kproj, vproj, oproj, gateproj, upproj, down_proj
Epochs2
Batch Size2
Gradient Accumulation8
Effective Batch Size16
Learning Rate2e-4
Optimizerpagedadamw8bit
Precisionbf16
Max Sequence Length2048
Training Results
MetricValue
Initial Loss~2.1
Final Loss1.24
Training Time23 hours 12 minutes
Total Steps3,344

Compute Infrastructure

Hardware
  • —GPU: NVIDIA L40S (46GB VRAM)
  • —GPU Memory Used: ~35-40GB during training
Software
  • —Framework: PyTorch 2.x
  • —Libraries: transformers, peft, bitsandbytes, accelerate
  • —PEFT Version: 0.18.0

Intended Uses

Direct Use

  • —Understanding Indian Penal Code (IPC) sections
  • —Learning about legal procedures (FIR, bail, arrest rights)
  • —Getting explanations of Constitutional articles
  • —Legal education and awareness
  • —Research on Indian law

Out-of-Scope Use

This model should NOT be used for:

  • —Actual legal advice in real cases
  • —Court proceedings or legal documentation
  • —Replacing consultation with qualified advocates
  • —Making legal decisions without professional guidance

Bias, Risks, and Limitations

Limitations

  1. 1.Not Legal Advice: This model provides informational content only, not professional legal advice
  2. 2.May Be Outdated: Laws change; IPC is being replaced by BNS (2024)
  3. 3.Accuracy: May occasionally provide incorrect or incomplete information
  4. 4.Coverage: Primarily trained on IPC, CrPC, Constitution; may lack depth in specialized areas
  5. 5.Language: Tamil support is present but may be less comprehensive than English

Recommendations

  • —Always verify information with official legal sources
  • —Consult qualified advocates for actual legal matters
  • —Use as an educational tool, not a replacement for legal counsel
  • —Cross-reference with current legislation (BNS/BNSS for post-2024)

⚠️ Disclaimer

This AI assistant is for informational and educational purposes only. It does not constitute legal advice.

For any legal matters, please consult a qualified advocate or legal professional. Laws are subject to change, and this model may not reflect the most recent amendments.

நீங்கள் சட்ட விஷயங்களுக்கு தகுதிவாய்ந்த வழக்கறிஞரை அணுகவும். இது சட்ட ஆலோசனை அல்ல.

Citation

If you use this model, please cite:

bibtex
@misc{indian-legal-assistant-aya,
  author = {writetoasik},
  title = {Indian Legal Assistant - Aya-23-8B LoRA},
  year = {2024},
  publisher = {HuggingFace},
  url = {https://huggingface.co/writetoasik/indian-legal-assistant-aya}
}

Acknowledgments

  • —CohereForAI for the Aya-23-8B base model
  • —Dataset creators for Indian legal training data
  • —HuggingFace for hosting infrastructure

Model Card Contact


Built with ❤️ for making Indian law more accessible


---