# lokesh-1511/legal-doc-simplifier architecture

> Architecture diagram of the GitHub repository lokesh-1511/legal-doc-simplifier, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lokesh-1511/legal-doc-simplifier
- Repository: https://github.com/lokesh-1511/legal-doc-simplifier

## Overview

#### Purpose and Entry Points
LegalEase is a lightweight legal-document simplifier and retrieval-augmented chatbot. Users supply a PDF or pasted text, then ask questions grounded in the indexed document. The backend is a FastAPI service launched through `server.ps1`; the browser UI is static HTML/JavaScript opened from `web/index.html`. Primary application modules are `backend/main.py` and `app.py`.

#### Client Interaction Layer
The frontend manages document submission, simplification, chat history, loading states, and rendering answer source chips. It calls the FastAPI endpoints rather than hosting retrieval logic locally.
- `web/index.html`
- `web/script.js`
- `web/style.css`

#### API and Index Lifecycle
FastAPI routes coordinate PDF extraction, simplification/indexing, and chat. The API owns the document lifecycle: after ingestion, document chunks and their embeddings are retained in Python memory for subsequent chatbot requests. This makes the service stateful per running process and avoids a persistent vector database.
- `backend/main.py`
- `app.py`

#### Document Ingestion and Chunking
PDF input is extracted page by page via PyMuPDF, preserving page numbers. Pasted text follows the simplification/indexing path. The chunking stage creates overlapping text windows carrying `chunk_id`, `page`, and text metadata; page metadata becomes the basis for answer attribution.
- `backend/chunking.py`
- `backend/main.py`

#### Embedding and Retrieval Pipeline
Each indexed chunk is embedded once with the Hugging Face sentence-transformers MiniLM model. At question time, the query is embedded, compared to stored chunk embeddings using scikit-learn cosine similarity, and the highest-ranked top-k chunks are selected. Retrieval helpers and reranking modules support context selection boundaries.
- `backend/embeddings_hf.py`
- `backend/retrieval.py`
- `backend/reranker.py`

#### Grounded Generation and Responses
Retrieved chunks are formatted into a constrained chatbot prompt requiring answers only from supplied context and an “information not found” fallback. The prompt is sent to Groq’s LLaMA3 API; responses are returned with page-based source labels. Document simplification is a distinct LLM-driven flow.
- `backend/prompts.py`
- `backend/summarizer.py`
- `backend/main.py`

#### Runtime and External Dependencies
The runtime is Python/FastAPI with static browser assets. Core external services and libraries are Groq (requires `GROQ_API_KEY`), PyMuPDF, sentence-transformers, and scikit-learn. The design intentionally uses in-memory objects rather than FAISS, a vector database, Redis, Docker, or GPU infrastructure.
- `requirements.txt`
- `.env.example`
- `server.ps1`

Main entry points: [`backend/main.py`](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py) (Document and chat routes), [`backend/chunking.py`](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/chunking.py) (Overlapping chunks), [`backend/embeddings_hf.py`](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/embeddings_hf.py) (MiniLM embeddings), [`web/script.js`](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/web/script.js) (UI state and API calls)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser UI"]
  node_ui["Static document/chat UI<br/>browser client<br/>[index.html]"]
  node_ui_logic["UI state and API calls<br/>browser JavaScript<br/>[script.js]"]
end

subgraph group_api["FastAPI service"]
  node_api_routes{{"Document and chat routes<br/>FastAPI application<br/>[main.py]"}}
  node_app_module["Application module<br/>Python module<br/>[app.py]"]
  node_pdf_extract["PDF page extraction<br/>PyMuPDF ingestion<br/>[main.py]"]
  node_chunking["Overlapping chunks<br/>chunking pipeline<br/>[chunking.py]"]
  node_memory_index[("In-memory document index<br/>process state<br/>[main.py]")]
  node_simplifier["Document simplification<br/>LLM flow<br/>[summarizer.py]"]
end

subgraph group_retrieval["RAG pipeline"]
  node_embeddings["MiniLM embeddings<br/>sentence-transformers<br/>[embeddings_hf.py]"]
  node_retrieval_logic["Similarity retrieval<br/>retrieval service<br/>[retrieval.py]"]
  node_reranker["Context reranking<br/>retrieval helper<br/>[reranker.py]"]
  node_prompts["Grounded answer prompts<br/>prompt templates<br/>[prompts.py]"]
end

subgraph group_external["External dependencies"]
  node_groq{{"Groq LLaMA3 API<br/>LLM service"}}
end

node_pdf_file["User PDF or pasted text<br/>document input"]
node_runtime["Python service launcher<br/>runtime entry point<br/>[server.ps1]"]

node_ui -->|"drives"| node_ui_logic
node_ui_logic -->|"HTTP requests"| node_api_routes
node_runtime -->|"launches"| node_api_routes
node_pdf_file -->|"upload or pasted text"| node_api_routes
node_api_routes -->|"PDF ingestion"| node_pdf_extract
node_pdf_extract -->|"page text"| node_chunking
node_api_routes -->|"pasted text"| node_chunking
node_chunking -->|"chunks"| node_embeddings
node_embeddings -->|"chunk vectors"| node_memory_index
node_chunking -->|"page metadata"| node_memory_index
node_api_routes -->|"simplify request"| node_simplifier
node_simplifier -->|"LLM completion"| node_groq
node_api_routes -->|"question embedding"| node_embeddings
node_memory_index -->|"chunks and vectors"| node_retrieval_logic
node_embeddings -->|"query vector"| node_retrieval_logic
node_retrieval_logic -->|"candidate chunks"| node_reranker
node_reranker -->|"selected context"| node_prompts
node_prompts -->|"grounded prompt"| node_groq
node_groq -->|"answer"| node_api_routes
node_api_routes -->|"answer and page sources"| node_ui_logic
node_app_module -.->|"application support"| node_api_routes

click node_ui "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/web/index.html"
click node_ui_logic "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/web/script.js"
click node_api_routes "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py"
click node_app_module "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/app.py"
click node_pdf_extract "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py"
click node_chunking "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/chunking.py"
click node_memory_index "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py"
click node_simplifier "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/summarizer.py"
click node_embeddings "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/embeddings_hf.py"
click node_retrieval_logic "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/retrieval.py"
click node_reranker "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/reranker.py"
click node_prompts "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/prompts.py"
click node_runtime "https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/server.ps1"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ui,node_ui_logic toneBlue
class node_api_routes,node_app_module,node_pdf_extract,node_chunking,node_memory_index,node_simplifier toneAmber
class node_embeddings,node_retrieval_logic,node_reranker,node_prompts toneMint
class node_groq toneRose
class node_pdf_file,node_runtime toneNeutral
```

## Components

### Browser UI

- [Static document/chat UI](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/web/index.html): browser client · Submits documents and renders answers with source chips. · `web/index.html`
- [UI state and API calls](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/web/script.js): browser JavaScript · `web/script.js`

### FastAPI service

- [Document and chat routes](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py): FastAPI application · Owns the in-process document lifecycle. · `backend/main.py`
- [Application module](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/app.py): Python module · `app.py`
- [PDF page extraction](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py): PyMuPDF ingestion · Preserves page numbers for source attribution. · `backend/main.py`
- [Overlapping chunks](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/chunking.py): chunking pipeline · Produces chunk ID, page, and text metadata. · `backend/chunking.py`
- [In-memory document index](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/main.py): process state · Retains chunks and embeddings for the running service. · `backend/main.py`
- [Document simplification](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/summarizer.py): LLM flow · `backend/summarizer.py`

### RAG pipeline

- [MiniLM embeddings](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/embeddings_hf.py): sentence-transformers · Embeds indexed chunks and user queries. · `backend/embeddings_hf.py`
- [Similarity retrieval](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/retrieval.py): retrieval service · Ranks stored embeddings with cosine similarity and selects top-k context. · `backend/retrieval.py`
- [Context reranking](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/reranker.py): retrieval helper · `backend/reranker.py`
- [Grounded answer prompts](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/backend/prompts.py): prompt templates · Restricts answers to retrieved context with a not-found fallback. · `backend/prompts.py`

### External dependencies

- **Groq LLaMA3 API**: LLM service

### Other components

- **User PDF or pasted text**: document input
- [Python service launcher](https://github.com/lokesh-1511/legal-doc-simplifier/blob/main/server.ps1): runtime entry point · `server.ps1`

## Connections

- Static document/chat UI → UI state and API calls: drives
- UI state and API calls → Document and chat routes: HTTP requests
- Python service launcher → Document and chat routes: launches
- User PDF or pasted text → Document and chat routes: upload or pasted text
- Document and chat routes → PDF page extraction: PDF ingestion
- PDF page extraction → Overlapping chunks: page text
- Document and chat routes → Overlapping chunks: pasted text
- Overlapping chunks → MiniLM embeddings: chunks
- MiniLM embeddings → In-memory document index: chunk vectors
- Overlapping chunks → In-memory document index: page metadata
- Document and chat routes → Document simplification: simplify request
- Document simplification → Groq LLaMA3 API: LLM completion
- Document and chat routes → MiniLM embeddings: question embedding
- In-memory document index → Similarity retrieval: chunks and vectors
- MiniLM embeddings → Similarity retrieval: query vector
- Similarity retrieval → Context reranking: candidate chunks
- Context reranking → Grounded answer prompts: selected context
- Grounded answer prompts → Groq LLaMA3 API: grounded prompt
- Groq LLaMA3 API → Document and chat routes: answer
- Document and chat routes → UI state and API calls: answer and page sources
- Application module → Document and chat routes: application support
