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.
- BNS-Classifier is interesting because it turns legal triage into a pipeline, not a chatbot, by chaining OCR, entity extraction, retrieval, and reranking.
- Its most distinctive move is a RAG-lite design that searches a local CSV with embeddings first and asks Gemini to judge the shortlist second.
- The project is deliberately practical, because it solves a narrow mapping problem for a new criminal code instead of pretending to be a general legal assistant.
- The repo also reads like an early prototype, which makes its security gaps and deployment rough edges part of the story rather than hidden footnotes.
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.
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.
| Approach | Strength | Weakness |
|---|---|---|
| Manual legal lookup | Transparent and familiar | Slow, inconsistent, and hard to scale |
| Heavy vector database RAG | Scales well across large corpora | More infrastructure than this use case needs |
| BNS-Classifier | Lightweight and explainable for a narrow legal task | Depends 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)
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.
| Task | Traditional OCR | Gemini Vision |
|---|---|---|
| Handwritten complaint transcription | Can struggle with blur, spacing, and informal handwriting | Uses visual context to recover text more flexibly |
| Downstream legal usefulness | Often returns raw characters without judgment | Produces text that is easier to feed into extraction and retrieval |
| Operational footprint | Usually a separate OCR stack | Collapsed 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.
| Signal | Prototype | Production-ready |
|---|---|---|
| Secret handling | Fast and sometimes hardcoded | Managed through environment or secret store |
| Knowledge base | Local CSV | Versioned and governed retrieval backend |
| Validation | Useful for demos and small tests | Formal evaluation and monitoring |
| Deployment | Heroku-style simplicity | Audited, 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.
| Option | What you get | What you pay |
|---|---|---|
| Manual lookup | Human judgment and total transparency | Time, inconsistency, and fatigue |
| Heavy RAG infrastructure | Large-scale retrieval across many documents | More plumbing than this narrow problem needs |
| BNS-Classifier | A focused, explainable triage pipeline | A 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.
- Move API keys out of source files and into proper secret management.
- Expand from a local CSV to a governed retrieval layer with versioning.
- Build an evaluation set for false positives, near misses, and OCR errors.
- Separate prototype convenience from anything that could touch real legal workflows.





