Instructions to use nafis8766/Efficient_legal_model_distillation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use nafis8766/Efficient_legal_model_distillation with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b-it") model = PeftModel.from_pretrained(base_model, "nafis8766/Efficient_legal_model_distillation") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use nafis8766/Efficient_legal_model_distillation with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf nafis8766/Efficient_legal_model_distillation # Run inference directly in the terminal: llama cli -hf nafis8766/Efficient_legal_model_distillation
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf nafis8766/Efficient_legal_model_distillation # Run inference directly in the terminal: llama cli -hf nafis8766/Efficient_legal_model_distillation
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf nafis8766/Efficient_legal_model_distillation # Run inference directly in the terminal: ./llama-cli -hf nafis8766/Efficient_legal_model_distillation
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf nafis8766/Efficient_legal_model_distillation # Run inference directly in the terminal: ./build/bin/llama-cli -hf nafis8766/Efficient_legal_model_distillation
Use Docker
docker model run hf.co/nafis8766/Efficient_legal_model_distillation
- LM Studio
- Jan
- vLLM
How to use nafis8766/Efficient_legal_model_distillation with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "nafis8766/Efficient_legal_model_distillation" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nafis8766/Efficient_legal_model_distillation", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/nafis8766/Efficient_legal_model_distillation
- Ollama
How to use nafis8766/Efficient_legal_model_distillation with Ollama:
ollama run hf.co/nafis8766/Efficient_legal_model_distillation
- Unsloth Studio
How to use nafis8766/Efficient_legal_model_distillation with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for nafis8766/Efficient_legal_model_distillation to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for nafis8766/Efficient_legal_model_distillation to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for nafis8766/Efficient_legal_model_distillation to start chatting
- Docker Model Runner
How to use nafis8766/Efficient_legal_model_distillation with Docker Model Runner:
docker model run hf.co/nafis8766/Efficient_legal_model_distillation
- Lemonade
How to use nafis8766/Efficient_legal_model_distillation with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull nafis8766/Efficient_legal_model_distillation
Run and chat with the model
lemonade run user.Efficient_legal_model_distillation-{{QUANT_TAG}}List all available models
lemonade list
- Atomic Chat
ποΈ Gemma-2 2B Distilled β Bangladesh Legal AI Assistant
This repository provides fine-tuned LoRA adapter weights and a 4-bit quantized GGUF model for domain-specific legal reasoning and question answering over Bangladesh Law.
The model was developed through Progressive Knowledge Distillation, transferring the structured legal reasoning and answer generation capabilities of a 9-Billion parameter teacher (Gemma-2 9B) into a lightweight 2-Billion parameter student (Gemma-2 2B). It is designed to work in tandem with a Hybrid RAG engine (BM25 + intfloat/multilingual-e5-small) for cited, faithful legal assistance.
π Model Summary
- Student Architecture:
google/gemma-2-2b-it(2.61B parameters) - Teacher Model:
google/gemma-2-9b-it(via Ollama & Hugging Face) - Distillation Method:
- Supervised Fine-Tuning (SFT) with 3-Step Chain-of-Thought (
<thought> ... </thought>) structured legal reasoning. - Sequence-Level Knowledge Distillation (SeqKD) with Top-50 Sparse Teacher Logits ($\sim 95%$ probability mass) and KL Divergence at temperature $T = 4.0$.
- Supervised Fine-Tuning (SFT) with 3-Step Chain-of-Thought (
- Target Domain: Bangladesh Jurisprudence (The Constitution of Bangladesh + Comprehensive Statutory Legislation & Acts).
- Hardware Footprint: Runs comfortably in 4-bit quantization on consumer GPUs, laptops, and edge devices ($\sim 1.63\text{ GB}$ GGUF).
π Benchmark Evaluation ($N=50$)
Evaluated against teacher reference answers across $N=50$ legal benchmark queries using automated lexical/semantic metrics and LLM judge scoring (gemma3:27b):
| Model / Evaluation Pipeline | ROUGE-L | BLEU | BERTScore F1 | Faithfulness (1β5) | Relevance (1β5) | Avg Tokens/sec |
|---|---|---|---|---|---|---|
Teacher Reference (gemma2:9b) |
1.000 | 1.000 | 1.000 | 4.80 | 4.90 | $\sim 28.5$ |
Base Student (gemma-2-2b-it unfinetuned) |
0.284 | 0.091 | 0.382 | 2.10 | 2.65 | $\sim 45.2$ |
| Distilled Student (No RAG) | 0.412 | 0.198 | 0.521 | 2.30 | 3.10 | $\sim 54.0$ |
| Distilled Student + Hybrid RAG (Stage 7) | 0.472 | 0.255 | 0.568 | 2.60 | 3.46 | $\sim 48.7$ |
| (Adjusted: Contextualized Queries $N=45$) | 0.502 | 0.278 | 0.605 | β | β | β |
π οΈ How to Use
1. Python Inference with Hugging Face & PEFT
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
BASE_MODEL_ID = "google/gemma-2-2b-it"
ADAPTER_ID = "nafis8766/Efficient_legal_model_distillation"
# 1. Load Tokenizer & Base Model
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto"
)
# 2. Load Distilled LoRA Adapter
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
# 3. Format Prompt & Generate
prompt = "What are the key constitutional safeguards against arbitrary arrest and detention in Bangladesh?"
messages = [
{"role": "user", "content": prompt}
]
formatted_prompt = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to("cuda")
with torch.no_grad():
outputs = model.generate(
formatted_prompt,
max_new_tokens=512,
temperature=0.3,
top_p=0.9
)
response = tokenizer.decode(outputs[0][formatted_prompt.shape[-1]:], skip_special_tokens=True)
print(response)
2. Edge & Local Inference via GGUF (llama.cpp)
Download the quantized model binary gemma-2-2b-legal-q4.gguf and run locally:
llama-cli \
-m gemma-2-2b-legal-q4.gguf \
-p "<start_of_turn>user\nWhat is the legal procedure for filing a writ petition under Article 102 of the Bangladesh Constitution?<end_of_turn>\n<start_of_turn>model\n" \
-n 512 \
--temp 0.3
π¬ Training & Distillation Pipeline
Bangladesh Legal Corpus (Constitution & Acts)
β
βΌ
14.5k Synthetic Queries
β
βΌ
Gemma-2 9B Teacher (3-Step CoT)
β
βΌ
Top-50 Logits Tensor (T = 4.0, ~95% Mass)
β
βΌ
Gemma-2 2B Student (Sparse KL Divergence + SFT)
β
βΌ
Distilled LoRA Adapter Weights
- LoRA Configuration: $r=64$, $\alpha=128$, target modules
[q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj]. - Precision & Hardware: bfloat16 / 4-bit NF4 quantized training on NVIDIA RTX 4080 Super (16 GB VRAM).
- Loss Function: Joint cross-entropy on teacher completions + sparse KL divergence on top-50 teacher vocabulary logits.
π Related Resources
- GitHub Codebase: https://github.com/nafis8766/Lega-LLM-model-distillation
- Progress Report & Methodology: Detailed experiment logs and analysis available in the repository
docs/report_27_4.md.
βοΈ Citation & Disclaimer
@misc{legal_llm_distillation_2026,
title={Efficient Legal AI for Bangladesh Law via Progressive Knowledge Distillation and Hybrid RAG},
author={Nafis, Md.},
year={2026},
publisher={Hugging Face},
howpublished={\url{https://huggingface.co/nafis8766/Efficient_legal_model_distillation}}
}
Disclaimer: This model is developed for academic research and assistive legal information retrieval. It does not replace professional legal consultation from a certified advocate.
- Downloads last month
- 169
We're not able to determine the quantization variants.