# divyansh0208/legal-ai architecture

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

- Interactive diagram: https://gitdiagram.com/divyansh0208/legal-ai
- Repository: https://github.com/divyansh0208/legal-ai

## Overview

Sahayak is a legal-assistance web app for document-grounded Q&A, PDF analysis, and voice queries. Users enter through the React home/chat/upload experience: chat calls the query API, which runs retrieval and LLM generation before returning an answer; uploaded PDFs are validated, parsed (with OCR when needed), indexed, summarized, and recorded. Voice audio is transcribed into text through a separate endpoint. The graph follows explicit calls and README-described capabilities; some internal parsing, embedding, and frontend handoffs are supported by the source index rather than shown excerpts.

Main entry points: [`app/api/routes_upload.py`](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_upload.py) (Upload endpoint), [`frontend/src/api/client.js`](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/api/client.js) (API client), [`app/api/routes_query.py`](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_query.py) (Query endpoint), [`app/rag/pipeline.py`](https://github.com/divyansh0208/legal-ai/blob/main/app/rag/pipeline.py) (Answer and summary)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web experience"]
  node_webapp["Web app routes<br/>[App.jsx]"]
  node_home["Home guidance<br/>[Home.jsx]"]
  node_chatui["Chat interface<br/>[ChatWindow.jsx]"]
  node_chatstate["Chat state<br/>[useChat.js]"]
  node_uploadui["Upload interface<br/>[FileUpload.jsx]"]
  node_voiceui["Voice input<br/>[VoiceInput.jsx]"]
  node_apiclient["API client<br/>[client.js]"]
end

subgraph group_api["API services"]
  node_backend["FastAPI app<br/>[main.py]"]
  node_queryapi["Query endpoint<br/>[routes_query.py]"]
  node_uploadapi["Upload endpoint<br/>[routes_upload.py]"]
  node_voiceapi["Voice endpoint<br/>[routes_voice.py]"]
end

subgraph group_knowledge["Document intelligence"]
  node_parser["PDF and OCR<br/>[pdf_parser.py]"]
  node_retriever["Chunking and retrieval<br/>[retriever.py]"]
  node_embedder["Text embeddings<br/>[embedder.py]"]
  node_rag["Answer and summary<br/>[pipeline.py]"]
  node_prompts["Safety prompts<br/>[prompts.py]"]
  node_transcriber["Audio transcription<br/>[transcribe.py]"]
end

subgraph group_persistence["Persistence and security"]
  node_chroma[("Chroma vector store<br/>[retriever.py]")]
  node_database[("SQL database<br/>[database.py]")]
  node_models["Chat and document records<br/>[models.py]"]
  node_security["Upload validation<br/>[security.py]"]
  node_uploads[("Uploaded PDF files<br/>[routes_upload.py]")]
end

node_visitor(("Legal help seeker"))
node_llm{{"Groq language and speech APIs"}}

node_visitor -->|"uses"| node_webapp
node_webapp -->|"routes to"| node_home
node_webapp -->|"routes to"| node_chatui
node_webapp -->|"routes to"| node_uploadui
node_chatui -->|"uses"| node_chatstate
node_chatstate -->|"calls"| node_apiclient
node_uploadui -->|"calls"| node_apiclient
node_voiceui -->|"calls"| node_apiclient
node_visitor -->|"asks"| node_chatui
node_visitor -->|"uploads PDF"| node_uploadui
node_apiclient -->|"posts query"| node_queryapi
node_apiclient -->|"posts PDF"| node_uploadapi
node_apiclient -->|"posts audio"| node_voiceapi
node_backend -->|"registers"| node_queryapi
node_backend -->|"registers"| node_uploadapi
node_backend -->|"registers"| node_voiceapi
node_queryapi -->|"calls"| node_rag
node_queryapi -->|"uses session"| node_database
node_database -->|"registers tables"| node_models
node_queryapi -->|"writes chat history"| node_models
node_uploadapi -->|"validates file"| node_security
node_uploadapi -->|"extracts text"| node_parser
node_parser -.->|"provides document text"| node_retriever
node_uploadapi -->|"indexes text"| node_retriever
node_uploadapi -->|"requests summary"| node_rag
node_uploadapi -->|"writes PDF"| node_uploads
node_uploadapi -->|"stores metadata"| node_models
node_rag -->|"retrieves context"| node_retriever
node_rag -->|"formats prompts"| node_prompts
node_rag -->|"requests generation"| node_llm
node_retriever -->|"embeds text"| node_embedder
node_retriever -->|"stores and queries"| node_chroma
node_voiceapi -->|"transcribes"| node_transcriber
node_transcriber -.->|"uses Whisper"| node_llm
node_backend -->|"installs headers"| node_security

click node_webapp "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/App.jsx"
click node_home "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/pages/Home.jsx"
click node_chatui "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/ChatWindow.jsx"
click node_chatstate "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/hooks/useChat.js"
click node_uploadui "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/FileUpload.jsx"
click node_voiceui "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/VoiceInput.jsx"
click node_apiclient "https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/api/client.js"
click node_backend "https://github.com/divyansh0208/legal-ai/blob/main/main.py"
click node_queryapi "https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_query.py"
click node_uploadapi "https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_upload.py"
click node_voiceapi "https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_voice.py"
click node_parser "https://github.com/divyansh0208/legal-ai/blob/main/app/parsing/pdf_parser.py"
click node_retriever "https://github.com/divyansh0208/legal-ai/blob/main/app/rag/retriever.py"
click node_embedder "https://github.com/divyansh0208/legal-ai/blob/main/app/embeddings/embedder.py"
click node_rag "https://github.com/divyansh0208/legal-ai/blob/main/app/rag/pipeline.py"
click node_prompts "https://github.com/divyansh0208/legal-ai/blob/main/app/rag/prompts.py"
click node_transcriber "https://github.com/divyansh0208/legal-ai/blob/main/app/voice/transcribe.py"
click node_chroma "https://github.com/divyansh0208/legal-ai/blob/main/app/rag/retriever.py"
click node_database "https://github.com/divyansh0208/legal-ai/blob/main/app/db/database.py"
click node_models "https://github.com/divyansh0208/legal-ai/blob/main/app/db/models.py"
click node_security "https://github.com/divyansh0208/legal-ai/blob/main/app/core/security.py"
click node_uploads "https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_upload.py"

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_webapp,node_home,node_chatui,node_chatstate,node_uploadui,node_voiceui,node_apiclient toneBlue
class node_backend,node_queryapi,node_uploadapi,node_voiceapi toneAmber
class node_parser,node_retriever,node_embedder,node_rag,node_prompts,node_transcriber,node_llm toneMint
class node_chroma,node_database,node_models,node_security,node_uploads toneRose
class node_visitor toneIndigo
```

## Components

### Web experience

- [Web app routes](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/App.jsx): `frontend/src/App.jsx`
- [Home guidance](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/pages/Home.jsx): `frontend/src/pages/Home.jsx`
- [Chat interface](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/ChatWindow.jsx): `frontend/src/components/ChatWindow.jsx`
- [Chat state](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/hooks/useChat.js): `frontend/src/hooks/useChat.js`
- [Upload interface](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/FileUpload.jsx): `frontend/src/components/FileUpload.jsx`
- [Voice input](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/components/VoiceInput.jsx): `frontend/src/components/VoiceInput.jsx`
- [API client](https://github.com/divyansh0208/legal-ai/blob/main/frontend/src/api/client.js): `frontend/src/api/client.js`

### API services

- [FastAPI app](https://github.com/divyansh0208/legal-ai/blob/main/main.py): `main.py`
- [Query endpoint](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_query.py): `app/api/routes_query.py`
- [Upload endpoint](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_upload.py): `app/api/routes_upload.py`
- [Voice endpoint](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_voice.py): `app/api/routes_voice.py`

### Document intelligence

- [PDF and OCR](https://github.com/divyansh0208/legal-ai/blob/main/app/parsing/pdf_parser.py): `app/parsing/pdf_parser.py`
- [Chunking and retrieval](https://github.com/divyansh0208/legal-ai/blob/main/app/rag/retriever.py): `app/rag/retriever.py`
- [Text embeddings](https://github.com/divyansh0208/legal-ai/blob/main/app/embeddings/embedder.py): `app/embeddings/embedder.py`
- [Answer and summary](https://github.com/divyansh0208/legal-ai/blob/main/app/rag/pipeline.py): `app/rag/pipeline.py`
- [Safety prompts](https://github.com/divyansh0208/legal-ai/blob/main/app/rag/prompts.py): `app/rag/prompts.py`
- [Audio transcription](https://github.com/divyansh0208/legal-ai/blob/main/app/voice/transcribe.py): `app/voice/transcribe.py`

### Persistence and security

- [Chroma vector store](https://github.com/divyansh0208/legal-ai/blob/main/app/rag/retriever.py): `app/rag/retriever.py`
- [SQL database](https://github.com/divyansh0208/legal-ai/blob/main/app/db/database.py): `app/db/database.py`
- [Chat and document records](https://github.com/divyansh0208/legal-ai/blob/main/app/db/models.py): `app/db/models.py`
- [Upload validation](https://github.com/divyansh0208/legal-ai/blob/main/app/core/security.py): `app/core/security.py`
- [Uploaded PDF files](https://github.com/divyansh0208/legal-ai/blob/main/app/api/routes_upload.py): `app/api/routes_upload.py`

### Other components

- **Legal help seeker**
- **Groq language and speech APIs**

## Connections

- Legal help seeker → Web app routes: uses (evidence: `README.md`)
- Web app routes → Home guidance: routes to (evidence: `frontend/src/App.jsx`)
- Web app routes → Chat interface: routes to (evidence: `frontend/src/App.jsx`)
- Web app routes → Upload interface: routes to (evidence: `frontend/src/App.jsx`)
- Chat interface → Chat state: uses (evidence: `frontend/src/pages/Chat.jsx`)
- Chat state → API client: calls (evidence: `frontend/src/hooks/useChat.js`)
- Upload interface → API client: calls (evidence: `frontend/src/components/FileUpload.jsx`)
- Voice input → API client: calls (evidence: `frontend/src/components/VoiceInput.jsx`)
- Legal help seeker → Chat interface: asks (evidence: `README.md`)
- Legal help seeker → Upload interface: uploads PDF (evidence: `README.md`)
- API client → Query endpoint: posts query (evidence: `frontend/src/api/client.js`)
- API client → Upload endpoint: posts PDF (evidence: `frontend/src/api/client.js`)
- API client → Voice endpoint: posts audio (evidence: `frontend/src/api/client.js`)
- FastAPI app → Query endpoint: registers (evidence: `main.py`)
- FastAPI app → Upload endpoint: registers (evidence: `main.py`)
- FastAPI app → Voice endpoint: registers (evidence: `main.py`)
- Query endpoint → Answer and summary: calls (evidence: `app/api/routes_query.py`)
- Query endpoint → SQL database: uses session (evidence: `app/api/routes_query.py`)
- SQL database → Chat and document records: registers tables (evidence: `app/db/database.py`)
- Query endpoint → Chat and document records: writes chat history (evidence: `app/api/routes_query.py`)
- Upload endpoint → Upload validation: validates file (evidence: `app/api/routes_upload.py`)
- Upload endpoint → PDF and OCR: extracts text (evidence: `app/api/routes_upload.py`)
- PDF and OCR → Chunking and retrieval: provides document text
- Upload endpoint → Chunking and retrieval: indexes text (evidence: `app/api/routes_upload.py`)
- Upload endpoint → Answer and summary: requests summary (evidence: `app/api/routes_upload.py`)
- Upload endpoint → Uploaded PDF files: writes PDF (evidence: `app/api/routes_upload.py`)
- Upload endpoint → Chat and document records: stores metadata (evidence: `app/api/routes_upload.py`)
- Answer and summary → Chunking and retrieval: retrieves context (evidence: `app/rag/pipeline.py`)
- Answer and summary → Safety prompts: formats prompts (evidence: `app/rag/pipeline.py`)
- Answer and summary → Groq language and speech APIs: requests generation (evidence: `app/rag/pipeline.py`)
- Chunking and retrieval → Text embeddings: embeds text (evidence: `app/rag/retriever.py`)
- Chunking and retrieval → Chroma vector store: stores and queries (evidence: `app/rag/retriever.py`)
- Voice endpoint → Audio transcription: transcribes (evidence: `app/api/routes_voice.py`)
- Audio transcription → Groq language and speech APIs: uses Whisper (evidence: `README.md`)
- FastAPI app → Upload validation: installs headers (evidence: `main.py`)
