Skip to content

Repository files navigation

MyPocketLawyer βš–οΈ β€” AI-Powered Legal Aid Assistant (Nepali Law)

A stateless Retrieval-Augmented Generation (RAG) assistant for Nepali law. It uses:

  • Google Gemini for embeddings and generation
  • ChromaDB for vector search
  • FastAPI for the backend
  • Vite/React and Shadcn/UI for the modern frontend user interface

This bot answers only from the ingested legal documents:

  • Constitution of Nepal 2072
  • The Criminal Offences Act 2074
  • The Labour Act 2074
  • The National Civil Act 2074
  • The National Penal Act 2074
  • Bank and Financial Institution Act 2073
  • Banking Offence and Punishment Act 2064
  • Electronic Commerce Act 2081
  • International Financial Transactions Act 2054
  • The Income Tax Act 2058

✨ Features

  • Small-talk guard and domain classification (Nepali law only)
  • Query rewriting to improve retrieval quality
  • ChromaDB retrieval with metadata-rich source display
  • Gemini 2.5 Pro/Flash for grounded answer generation
  • Streamlit chat UI with collapsible sources and search query display
  • Adaptive answer format:
    • Short Answer
    • What the Law Says (cites articles/clauses)
    • Practical Steps (only for action/procedure queries)
    • Disclaimer

πŸ—‚οΈ Repository Structure

.
β”œβ”€ backend/
β”‚  β”œβ”€ main.py                         # FastAPI stateless RAG pipeline
β”‚
β”œβ”€ chroma_db/                         # ChromaDB persistent directory (auto-generated)
β”‚
β”œβ”€ config/
β”‚  β”œβ”€ __init__.py
β”‚  └─ paths.py                        # Project path utilities (optional)
β”‚
β”œβ”€ data/
β”‚  β”œβ”€ raw/                            # Raw legal documents (if building vectors)
β”‚  β”œβ”€ processed/                       # Converted & cleaned JSON docs
β”‚  └─ evaluation/                      # Test sets, metrics, prompts
β”‚
β”œβ”€ documentation/
β”‚  β”œβ”€ LiteratureReview.pdf
β”‚  └─ Proposal.pdf
β”‚
β”œβ”€ frontend/
β”‚  β”œβ”€ index.html
β”‚  β”œβ”€ package.json
β”‚  β”œβ”€ vite.config.ts
β”‚  β”œβ”€ tailwind.config.ts
β”‚  β”œβ”€ postcss.config.js
β”‚  β”œβ”€ public/
β”‚  └─ src/
β”‚     β”œβ”€ main.tsx                      # React entry point
β”‚     β”œβ”€ App.tsx                       # Root wrapper component
β”‚     β”œβ”€ App.css                       # Global component styling
β”‚     β”œβ”€ index.css                     # Tailwind + base styles
β”‚     β”‚
β”‚     β”œβ”€ pages/
β”‚     β”‚  β”œβ”€ Index.tsx                  # Landing page (hero + CTA)
β”‚     β”‚  └─ NotFound.tsx               # 404 handler
β”‚     β”‚
β”‚     β”œβ”€ components/
β”‚     β”‚  β”œβ”€ Hero.tsx                   # Homepage hero UI
β”‚     β”‚  β”œβ”€ NavLink.tsx                # Navbar interactive link component
β”‚     β”‚  β”œβ”€ LegalAssistant.tsx         # Main chat screen
β”‚     β”‚  └─ ui/                        # shadcn component library
β”‚     β”‚
β”‚     β”œβ”€ hooks/
β”‚     β”‚  β”œβ”€ use-mobile.tsx
β”‚     β”‚  └─ use-toast.ts
β”‚     β”‚
β”‚     └─ lib/
β”‚        └─ utils.ts                   # Shared helpers (stream UI, formatting)
β”‚
β”œβ”€ notebooks/
β”‚  β”œβ”€ baseline_data_ingestion_pipeline.ipynb
β”‚  β”œβ”€ baseline_retrieval_pipeline.ipynb
β”‚  β”œβ”€ data_ingestion_multimodel.ipynb
β”‚  β”œβ”€ final_data_ingestion_pipeline.ipynb
β”‚  β”œβ”€ final_retreval_pipeline.ipynb
β”‚  β”œβ”€ final_retriever_evaluation.ipynb
β”‚  β”œβ”€ generator_evaluation.ipynb
β”‚  β”œβ”€ multimodel_evaluation.ipynb
β”‚  β”œβ”€ multimodel_split_evaluation.ipynb
β”‚  └─ retriever_evaluation.ipynb
β”‚
β”œβ”€ .env                                # GEMINI_API_KEY
β”œβ”€ .gitignore
β”œβ”€ generator_evaluation_results.csv
β”œβ”€ generator_evaluation_results.json
β”œβ”€ requirements.txt
β”œβ”€ run.py                              # Optional pipeline runner
└─ README.md


🧱 Tech Stack

  • Backend: FastAPI, Pydantic, Uvicorn
  • Vector Store: ChromaDB (persistent)
  • LLM + Embeddings: Google Gemini (via google-genai)
    • Generation: gemini-2.5-pro (fallback: gemini-2.5-flash)
    • Embeddings: models/text-embedding-004
  • Frontend: Frontend β€” Vite + React + TypeScript + Tailwind + Shadcn/UI

πŸš€ Quickstart

1) Prerequisites

  • Python 3.10+
  • A Google Gemini API key (from Google AI Studio)
  • macOS, Linux, or Windows

2) Environment

Create a .env in the repo root:

GEMINI_API_KEY=your_gemini_api_key_here

3) Install dependencies

Create a virtual environment and install:

python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux:
source .venv/bin/activate
pip install -r requirements.txt

4) Build or provide the vector store

  • If ./backend/chroma_db/ already contains a collection named legal_docs, you’re set.
  • Otherwise, build it using your ingestion notebooks:
    • Recommended: notebooks/final_data_ingestion_pipeline.ipynb
  • Place raw text/markdown under data/raw/ (or what your notebook expects).
  • Output should be a persistent Chroma collection at ./backend/../chroma_db (repo root ./chroma_db).

The backend looks for a Chroma collection named legal_docs at repo-root ./chroma_db.

5) Run the backend

From repo root:

uvicorn backend.main:app --host 0.0.0.0 --port 8000 --reload

You should see: β€œβœ… MyPocketLawyer backend (completely stateless) is live.”

6) Run the frontend 🌐

In a separate terminal, follow these steps to launch the React application:

1. Navigate to the frontend directory

cd frontend

2. Install Node.js dependencies (only needed the first time)

npm install

3. Start the development server

npm run dev

Optional: run.py

If run.py orchestrates both backend and frontend on your machine, run:

python run.py

(If not implemented to launch both, use the separate commands above.)


πŸ§ͺ Example Queries

  • What are the three organs of the Government under the Constitution of Nepal?
  • What is the overtime pay in Nepali like?
  • What happens if someone is found planting explosives?

πŸ“Š Evaluation Figures

Retriever Evaluation (Full-Chunk Splitting)

Screen Shot 2025-11-27 at 09 20 31

Retriever Evaluation (Article-wise Splitting)

Screen Shot 2025-11-27 at 09 20 01

Generator Evaluation

Quality & Efficiency Scores

Screen Shot 2025-11-27 at 09 22 24

Response Time & Cost Distribution

Screen Shot 2025-11-27 at 09 23 05

Quality Metrics Comparison

Screen Shot 2025-11-27 at 09 24 29

Performance by Legal Sources

Screen Shot 2025-11-27 at 09 25 10

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages