# dhyanshah22/lexilaw architecture

> Architecture diagram of the GitHub repository dhyanshah22/lexilaw, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhyanshah22/lexilaw
- Repository: https://github.com/dhyanshah22/lexilaw

## Overview

##### Project Type and Purpose
- LexiLaw is a full‑stack, web‑based corporate legal assistant built with Streamlit. It allows users to query legal acts and case‑law PDFs in natural language using a retrieval‑augmented generation pipeline based on LangChain and Google’s Gemini API, with FAISS for vector similarity search and MongoDB for persistence.

##### Top‑Level Components
a. Frontend (Streamlit UI)
- Implements sidebar (case selection, filters, dark mode, creativity slider) and main chat interface.
- File: v5‑prod.py (formerly app.py).
b. PDF Data Store
- data/case_pdfs: raw case‑law PDF files.
- data/*.pdf: legal acts.
c. Vector Store Layer
- Prebuilt FAISS indexes under vectorstores/acts and vectorstores/cases.
- build_faiss_vectorstores.py to (re)build indexes for acts.
d. Retrieval & Generation Engine
- PyPDF2 for PDF text extraction (on‑the‑fly for selected cases).
- FAISS for similarity search.
- LangChain’s ConversationalRetrievalChain (ConversationBufferMemory + FAISS retriever).
- ChatGoogleGenerativeAI model (Gemini API) for answer generation.
- gTTS for text‑to‑speech output.
e. Database & Logging
- MongoDB (configured via MONGO_URI) to store chat logs, metadata, source documents, timestamps.
f. External Services
- Google Gemini API (requires GEMINI_API_KEY).

##### Relationships and Data Flow
1. User interacts with Streamlit UI → issues a natural‑language query or selects a case.
2. If a case is newly selected:
- PDF Extraction Module reads data/case_pdfs/<selected>.PDF via PyPDF2.
- On‑the‑fly FAISS Index Builder converts extracted text into embeddings via Gemini → stores vectors in memory (or disk).
3. Retrieval Module uses FAISS (acts index or case index) to find relevant document snippets.
4. Generation Module (ChatGoogleGenerativeAI) receives query + retrieved snippets → returns answer text.
5. If TTS enabled: gTTS converts answer to audio.
6. Logging Module writes user query, response text, case ID, sources, timestamp to MongoDB.
7. Response (text ± audio) is rendered back in Streamlit UI.

##### Architectural Patterns & Design Principles
- Monolithic service with clear separation of concerns: UI layer, data ingestion layer, vector retrieval layer, LLM orchestration layer, persistence layer.
- Retrieval‑Augmented Generation (RAG) for domain‑specific QA.
- Modular scripts: build_faiss_vectorstores.py standalone for offline vectorstore creation.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Entities
    User["User (Browser \nStreamlit Client)"]:::external
    UI["Streamlit App Server\n(v5-prod.py)\nPython, Streamlit"]:::ui
    PDFActs["PDF Data Store\n(Legal Acts .pdf)"]:::storage
    PDFCases["PDF Data Store\n(Case-Law PDFs)"]:::storage

    %% Vector Store Layer
    subgraph "Vector Store Layer"
        Builder["Offline Vector Store Builder\n(build_faiss_vectorstores.py)"]:::core
        FAISSActs["Static FAISS Vector Store\n(Acts index)"]:::storage
        FAISSCases["Dynamic FAISS Vector Store\n(Cases index)"]:::storage
    end

    %% Processing Layers
    Retrieval["Retrieval Chain\n(LangChain + FAISS)"]:::core
    Generation["Generation Chain\n(ChatGoogleGenerativeAI\nGemini API)"]:::external
    TTS["Text-to-Speech Module\ngTTS"]:::core
    Mongo["MongoDB\n(Chat logs & metadata)"]:::storage

    %% Documentation
    subgraph "Documentation"
        README["Project Overview & Setup Instructions\n(README.md)"]:::docs
        requirements["Dependency Manifest\n(requirements.txt)"]:::docs
    end

    %% Data Flows
    User -->|"query / selection"| UI
    UI -->|"if new case: PDF text"| PDFCases
    UI -->|"load prebuilt index"| FAISSActs
    UI -->|"build dynamic index"| FAISSCases
    Builder -->|"build acts index"| FAISSActs
    UI -->|"query + snippets"| Retrieval
    POI1(( )):::invisible
    PDFCases -->|"extract via PyPDF2"| UI
    Retrieval -->|"relevant snippets"| Generation
    Generation -->|"answer text"| UI
    Generation -->|"API call"| Generation
    Generation -->|"call Gemini API"| Generation
    Generation -->|"answer text"| UI
    Generation --> TTS
    TTS -->|"audio"| UI
    UI -->|"chat log"| Mongo

    %% RetrievalChain interactions
    Retrieval --> FAISSActs
    Retrieval --> FAISSCases

    %% Styles
    classDef ui fill:#CFE2F3,stroke:#036;
    classDef core fill:#D9EAD3,stroke:#274;
    classDef storage fill:#FFF2CC,stroke:#AA7;
    classDef external fill:#FCE5CD,stroke:#E67300;
    classDef docs fill:#EDEDED,stroke:#999;

    %% Click Events
    click UI "https://github.com/dhyanshah22/lexilaw/blob/main/v5-prod.py"
    click Builder "https://github.com/dhyanshah22/lexilaw/blob/main/build_faiss_vectorstores.py"
    click PDFActs "https://github.com/dhyanshah22/lexilaw/blob/main/data/*.pdf"
    click PDFCases "https://github.com/dhyanshah22/lexilaw/tree/main/data/case_pdfs/"
    click FAISSActs "https://github.com/dhyanshah22/lexilaw/blob/main/vectorstores/acts/index.faiss"
    click FAISSCases "https://github.com/dhyanshah22/lexilaw/blob/main/vectorstores/cases/index.faiss"
    click requirements "https://github.com/dhyanshah22/lexilaw/blob/main/requirements.txt"
    click README "https://github.com/dhyanshah22/lexilaw/blob/main/README.md"
```
