BNS-Classifier: The Lightweight AI That Maps a Complaint to the Right Criminal Code Section

A small Flask app uses OCR, entity extraction, semantic search, and Gemini reranking to turn raw police complaints into structured BNS 2023 suggestions without a heavy vector database.

9 min read • View on GitHub • More from Nav1709

A handwritten complaint on one side, a scanning lens in the middle, and a neat stack of legal section cards on the other. The scene explains how the app turns messy input into structured criminal code suggestions by extracting facts before it ranks legal matches.
Raw complaint in, legal triage out. The system is designed to compress the first pass of sorting into a single pipeline.
Key Takeaways

From Complaint to Code Section

The app starts with a complaint, not a conversation. A user pastes text or uploads an image, and BNS-Classifier responds with candidate BNS sections plus a short rationale. That is the whole bet: if the first pass of legal sorting is faster and clearer, everything downstream gets easier.

The project is built around a specific legal shift. Bharatiya Nyaya Sanhita 2023 replaced the IPC, which means people now need a reliable way to map messy incidents to a new codebook. This repo is not trying to decide guilt. It is trying to reduce the friction of finding the right section.

Why This Exists

That distinction matters. The tool is aimed at triage, the awkward middle ground where a complaint is still unshaped, facts are incomplete, and legal language is easy to misread. In that setting, a focused assistant beats a generic model that sounds confident but loses the plot.

The RAG-Lite Trick

The clever part is that the repo does retrieval in two passes without a vector database. First, it embeds the complaint and every row in bns_sections.csv with all-MiniLM-L6-v2, then it keeps the top semantic matches. Only after that does Gemini rerank the shortlist and explain why the winner fits the facts.

The system narrows a broad legal search in two steps. Cheap local similarity handles recall, then Gemini handles judgment.

That split is the whole design philosophy. The first stage is mathematical similarity. The second stage is legal fit. Those are not the same thing, and the repo knows it. A section can be close in embedding space and still be wrong for the facts.

ApproachStrengthWeakness
Manual legal lookupTransparent and familiarSlow, inconsistent, and hard to scale
Heavy vector database RAGScales well across large corporaMore infrastructure than this use case needs
BNS-ClassifierLightweight and explainable for a narrow legal taskDepends on local data quality and careful reranking

How the Pipeline Avoids False Matches

This is where the LLM is doing less than people expect, which is the point. Gemini is not inventing the legal answer from scratch. It is acting like a filter that rejects near misses when the embedding search gets too generous. That matters in legal text, where theft, robbery, intimidation, and assault can look adjacent but mean very different things.

# Pseudocode for the two-stage retrieval logic
candidates = semantic_search(complaint_text, bns_sections_csv, top_k=10)

prompt = f"""
You are a legal triage assistant.
Use the complaint facts to rerank these sections.
Reject sections that are semantically close but legally incorrect.
Return JSON with ranked_sections and reasoning.
Complaint: {complaint_text}
Candidates: {candidates}
"""

final_answer = gemini_rerank(prompt)
A close-up of a small drawer labeled CSV releasing several legal cards, which are narrowed by a hand-held lens into one final card while the others fall aside. The image explains how local semantic search produces candidates and Gemini reranking chooses the section that actually fits the complaint.
The repo’s real innovation is not retrieval alone. It is the second pass that turns similarity into judgment.

Extracting Facts from Messy Complaints

The fact extractor is more domain aware than a generic NER pass. It uses spaCy for people, places, and locations, then falls back to regex for patterns that standard models miss, especially times and other complaint-specific fragments. It also threads in a crime_category so extraction is not detached from the legal task.

That is a smart trade. In this kind of tool, factual slots matter more than linguistic elegance. If the system cannot reliably pull out who, where, and when, the later retrieval stage is forced to guess.

Why Gemini Vision Replaces Traditional OCR

The OCR choice tells you a lot about the repo’s priorities. Instead of classic OCR tooling, it leans on Gemini’s multimodal transcription to deal with messy handwritten complaints. That is less about novelty and more about robustness when the input is uneven and legal meaning depends on the exact words.

TaskTraditional OCRGemini Vision
Handwritten complaint transcriptionCan struggle with blur, spacing, and informal handwritingUses visual context to recover text more flexibly
Downstream legal usefulnessOften returns raw characters without judgmentProduces text that is easier to feed into extraction and retrieval
Operational footprintUsually a separate OCR stackCollapsed into the same multimodal API path

The Prototype Tells on Itself

The repo also exposes its own age. A hardcoded API key in ocr.py, root-level test artifacts, and a local CSV knowledge base all point to an active prototype, not a hardened deployment. That is not a knock. It just means the article should read the code honestly.

The infrastructure clues are useful. Procfile and runtime.txt suggest deployment intent, but the rest of the repository still behaves like a hands-on build environment. In practice, that makes the project easier to understand and harder to trust at scale.

SignalPrototypeProduction-ready
Secret handlingFast and sometimes hardcodedManaged through environment or secret store
Knowledge baseLocal CSVVersioned and governed retrieval backend
ValidationUseful for demos and small testsFormal evaluation and monitoring
DeploymentHeroku-style simplicityAudited, reproducible, and secure

What It Is Competing Against

Against manual triage, BNS-Classifier is faster and more consistent. Against a full vector database stack, it is lighter and easier to reason about. That middle position is the repo’s best argument: it takes the minimum machinery needed to make legal lookup feel structured.

OptionWhat you getWhat you pay
Manual lookupHuman judgment and total transparencyTime, inconsistency, and fatigue
Heavy RAG infrastructureLarge-scale retrieval across many documentsMore plumbing than this narrow problem needs
BNS-ClassifierA focused, explainable triage pipelineA smaller ceiling and more dependency on prompt discipline

What Would Need to Change Next

If this grows up, four things need attention. Secret management needs to be fixed. Retrieval needs stronger evaluation. The legal knowledge base needs broader coverage and governance. And the system needs clearer benchmarks for whether its rankings are actually helping people make better decisions.