Case Study — Fine-tuned LLM
Legal AI Assistant —
Pakistan Penal Code Q&A
A domain-specific legal AI: Llama 3.2 8B fine-tuned with Unsloth LoRA on all 511 sections of the Pakistan Penal Code, served as a REST API on Hugging Face Spaces, and consumed by a React chat UI that renders structured Law Reference and Punishment cards per query.
System Pipeline
Overview
Making Legal Knowledge Accessible with AI
Understanding Pakistani law requires navigating 511 sections of the Pakistan Penal Code — dense, archaic language most citizens cannot practically interpret. Consulting a lawyer for everyday legal questions is expensive and inaccessible. A general-purpose LLM gives vague or hallucinated answers on specific PPC sections. The solution was a domain-specific fine-tuned model that knows the law precisely, served through a clean chat interface.
The Problem
Legal Knowledge Gap
- 511 PPC sections in Victorian-era legal language — inaccessible to most citizens
- General LLMs hallucinate section numbers or confuse Pakistani and Indian Penal Codes
- Legal consultation is expensive and out of reach for most Pakistanis
- No conversational interface existed to query PPC sections in plain language
The Solution
Fine-Tuned Model + Structured Chat UI
Llama 3.2 8B was fine-tuned with Unsloth LoRA directly on the complete PPC — teaching it to always return a structured response with a clear Law Reference and Punishment. The model is deployed as a REST API on Hugging Face Spaces; a React chat frontend parses each response and renders two distinct cards: a blue Law Reference card and a red Punishment card.
System Architecture
Two-Part System: Fine-Tuned API + React Frontend
The system is split into two independently deployed parts: a fine-tuned model served on Hugging Face Spaces as a REST endpoint, and a React chat application on Vercel that calls that endpoint and renders structured legal responses.
Key Features
Precision Legal Q&A with Structured Output
Technical Highlights
Key Engineering Decisions
Why Fine-Tune vs RAG
Weights vs Retrieval for Legal Accuracy
RAG was evaluated but rejected: PPC section text is highly cross-referenced, and retrieval frequently fetches the wrong section when queries use colloquial language ("what happens if I steal" vs "Section 379"). Fine-tuning bakes the plain-language → section mapping directly into Llama's weights via LoRA, eliminating the retrieval error surface at the cost of a fixed training corpus. The structured output format is enforced at training time, not at prompt time.
Frontend Response Parsing
Regex Pattern Matching on Model Output
The React parseAIResponse()function uses two sequential regex patterns to extract structure from the model's free-text output. The primary pattern matches the full "Section … through … Punishment:" block. If that fails, a fallback pattern locates "Section [number]" alone. This two-tier approach handles both well-formatted and partial model outputs without crashing the UI — unstructured responses fall through to plain text display.
Technology Stack
Full Stack: Fine-Tuning to Frontend
Built By
Development Team
Mohsin Sabir
DeveloperFatima Abbas
DeveloperNeed a domain-specific AI model fine-tuned for your field?
We fine-tune LLMs on proprietary datasets — legal, medical, financial, technical — and build the frontend to make them usable. From training to deployment.


