Views
No views yet
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.google/gemma-2-2b-it (2.61B parameters)google/gemma-2-9b-it (via Ollama & Hugging Face)<thought> ... </thought>) structured legal reasoning.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 | — | — | — |
1import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5BASE_MODEL_ID = "google/gemma-2-2b-it"
6ADAPTER_ID = "nafis8766/Efficient_legal_model_distillation"
7
8# 1. Load Tokenizer & Base Model
9tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
10base_model = AutoModelForCausalLM.from_pretrained(
11 BASE_MODEL_ID,
12 torch_dtype=torch.bfloat16,
13 device_map="auto"
14)
15
16# 2. Load Distilled LoRA Adapter
17model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
18
19# 3. Format Prompt & Generate
20prompt = "What are the key constitutional safeguards against arbitrary arrest and detention in Bangladesh?"
21messages = [
22 {"role": "user", "content": prompt}
23]
24
25formatted_prompt = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to("cuda")
26
27with torch.no_grad():
28 outputs = model.generate(
29 formatted_prompt,
30 max_new_tokens=512,
31 temperature=0.3,
32 top_p=0.9
33 )
34
35response = tokenizer.decode(outputs[0][formatted_prompt.shape[-1]:], skip_special_tokens=True)
36print(response)gemma-2-2b-legal-q4.gguf and run locally:1llama-cli \
2 -m gemma-2-2b-legal-q4.gguf \
3 -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" \
4 -n 512 \
5 --temp 0.3 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[q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj].docs/report_27_4.md.1@misc{legal_llm_distillation_2026,
2 title={Efficient Legal AI for Bangladesh Law via Progressive Knowledge Distillation and Hybrid RAG},
3 author={Nafis, Md.},
4 year={2026},
5 publisher={Hugging Face},
6 howpublished={\url{https://huggingface.co/nafis8766/Efficient_legal_model_distillation}}
7}