How lokesh-1511/legal-doc-simplifier works
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: main.py Document and chat routes · chunking.py Overlapping chunks · embeddings_hf.py MiniLM embeddings · script.js UI state and API calls
Components 15
Browser UI
- Static document/chat UI · Submits documents and renders answers with source chips.
web/index.html - UI state and API calls
web/script.js
FastAPI service
- Document and chat routes · Owns the in-process document lifecycle.
backend/main.py - Application module
app.py - PDF page extraction · Preserves page numbers for source attribution.
backend/main.py - Overlapping chunks · Produces chunk ID, page, and text metadata.
backend/chunking.py - In-memory document index · Retains chunks and embeddings for the running service.
backend/main.py - Document simplification
backend/summarizer.py
RAG pipeline
- MiniLM embeddings · Embeds indexed chunks and user queries.
backend/embeddings_hf.py - Similarity retrieval · Ranks stored embeddings with cosine similarity and selects top-k context.
backend/retrieval.py - Context reranking
backend/reranker.py - Grounded answer prompts · Restricts answers to retrieved context with a not-found fallback.
backend/prompts.py
External dependencies
- Groq LLaMA3 API
Other components
- User PDF or pasted text
- Python service launcher
server.ps1
Connections 21
- 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