# khadija20032003/lawyerchat architecture

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

- Interactive diagram: https://gitdiagram.com/khadija20032003/lawyerchat
- Repository: https://github.com/khadija20032003/lawyerchat

## Overview

#### Purpose and Entry Points
This repository is a Python RAG application for answering natural-language questions about SEC 10-K risk-factor disclosures, optionally combined with user-uploaded PDFs. The interactive runtime starts at `chatbot/app.py`; its UI delegates model and retrieval work to the chatbot layer. Dataset refreshes run separately through `scripts/daily_update_hf.py`, scheduled by `.github/workflows/daily_update.yml`.

#### Interactive Application Boundary
`chatbot/app.py` owns the Gradio Blocks interface, chat state, file upload interaction, and application startup. `chatbot/config.py` centralizes model identifiers, retrieval/generation settings, device selection, and storage locations. The UI is an adapter: domain retrieval and generation behavior is kept in `chatbot/chains.py` rather than embedded in interface callbacks.

#### RAG Orchestration
`chatbot/chains.py` constructs the retrieval-augmented generation flow: embed a user question, retrieve relevant chunks, format grounded context, and invoke the instruction-tuned LLM. `chatbot/models.py` loads the Hugging Face LLM and Sentence-Transformer embedding model. This establishes a local/in-Space inference boundary: the application downloads/caches model weights but performs embedding and generation in its Python runtime.

#### Retrieval and Document Sources
The persistent SEC corpus is a ChromaDB vector collection, expected to be acquired from a Hugging Face dataset and stored locally. `chatbot/helpers.py` supports document ingestion and utility operations used by the UI/RAG flow, including uploaded-PDF processing. Queries can retrieve from both the persistent SEC store and a transient/per-session uploaded-document index, then combine results before prompting the model.

#### SEC Corpus Update Pipeline
The `sec_risk_factors` package is the offline data-ingestion domain. `sec_risk_factors/pipeline.py` coordinates fetching filings, extracting target content, producing artifacts, and writing searchable records. `sec_risk_factors/sec_client.py` is the external SEC access boundary; `sec_risk_factors/extractors.py` isolates 10-K risk-factor extraction from raw filing material.

#### Storage and Publishing
`sec_risk_factors/store.py` manages vector-store persistence/updating, while `sec_risk_factors/artifacts.py` handles produced dataset artifacts. `scripts/daily_update_hf.py` connects the ingestion pipeline to Hugging Face dataset publication. The GitHub Actions workflow in `.github/workflows/daily_update.yml` automates this refresh, separating scheduled corpus maintenance from the online chat-serving process.

#### Runtime and External Dependencies
The primary runtime is Python 3.8+ with LangChain, Gradio, ChromaDB, Transformers/Sentence-Transformers, and PDF extraction dependencies declared in `requirements.txt`. External services are SEC filing endpoints and Hugging Face for model and vector-dataset distribution. ChromaDB provides local SQLite/HNSW-backed semantic retrieval; CPU is supported, with optional CUDA acceleration.

Main entry points: [`sec_risk_factors/pipeline.py`](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/pipeline.py) (SEC Ingestion Pipeline), [`chatbot/chains.py`](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/chains.py) (RAG Chains), [`chatbot/app.py`](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/app.py) (Gradio App), [`scripts/daily_update_hf.py`](https://github.com/khadija20032003/lawyerchat/blob/main/scripts/daily_update_hf.py) (Dataset Update Script)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_chat["Interactive RAG"]
  node_app["Gradio App<br/>UI entry point<br/>[app.py]"]
  node_config["Runtime Config<br/>configuration<br/>[config.py]"]
  node_chains["RAG Chains<br/>orchestrator<br/>[chains.py]"]
  node_models["Local Models<br/>LLM and embedder<br/>[models.py]"]
  node_helpers["Upload Processing<br/>document ingestion<br/>[helpers.py]"]
  node_sec_store[("SEC Vector Collection<br/>ChromaDB collection")]
  node_session_store[("Session Document Index<br/>transient vector index")]
end

subgraph group_pipeline["Corpus Refresh"]
  node_workflow["Daily Update Workflow<br/>GitHub Actions workflow<br/>[daily_update.yml]"]
  node_update_script["Dataset Update Script<br/>pipeline launcher<br/>[daily_update_hf.py]"]
  node_pipeline["SEC Ingestion Pipeline<br/>pipeline orchestrator<br/>[pipeline.py]"]
  node_sec_client["SEC Client<br/>external client<br/>[sec_client.py]"]
  node_extractors["Risk Factor Extractor<br/>content extraction<br/>[extractors.py]"]
  node_store["Vector Store Writer<br/>persistence adapter<br/>[store.py]"]
  node_artifacts["Dataset Artifacts<br/>artifact manager<br/>[artifacts.py]"]
end

subgraph group_external["External Systems"]
  node_sec_api["SEC Filing Endpoints<br/>external data source"]
  node_huggingface["Hugging Face<br/>model and dataset distribution"]
end

node_requirements["Python Dependencies<br/>runtime manifest<br/>[requirements.txt]"]

node_app -->|"reads settings"| node_config
node_app -->|"answers questions"| node_chains
node_app -->|"processes uploads"| node_helpers
node_chains -->|"embeds and generates"| node_models
node_chains -->|"retrieves SEC context"| node_sec_store
node_chains -->|"retrieves upload context"| node_session_store
node_helpers -->|"indexes PDFs"| node_session_store
node_models -.->|"downloads weights"| node_huggingface
node_workflow -->|"scheduled trigger"| node_update_script
node_update_script -->|"runs ingestion"| node_pipeline
node_update_script -->|"publishes dataset"| node_huggingface
node_pipeline -->|"fetches filings"| node_sec_client
node_sec_client -->|"requests filings"| node_sec_api
node_pipeline -->|"extracts risk factors"| node_extractors
node_pipeline -->|"produces artifacts"| node_artifacts
node_pipeline -->|"writes records"| node_store
node_store -->|"persists index"| node_sec_store
node_huggingface -.->|"distributes corpus"| node_sec_store
node_requirements -.->|"runtime dependencies"| node_app

click node_app "https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/app.py"
click node_config "https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/config.py"
click node_chains "https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/chains.py"
click node_models "https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/models.py"
click node_helpers "https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/helpers.py"
click node_workflow "https://github.com/khadija20032003/lawyerchat/blob/main/.github/workflows/daily_update.yml"
click node_update_script "https://github.com/khadija20032003/lawyerchat/blob/main/scripts/daily_update_hf.py"
click node_pipeline "https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/pipeline.py"
click node_sec_client "https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/sec_client.py"
click node_extractors "https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/extractors.py"
click node_store "https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/store.py"
click node_artifacts "https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/artifacts.py"
click node_requirements "https://github.com/khadija20032003/lawyerchat/blob/main/requirements.txt"

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_app,node_config,node_chains,node_models,node_helpers,node_sec_store,node_session_store toneBlue
class node_workflow,node_update_script,node_pipeline,node_sec_client,node_extractors,node_store,node_artifacts toneAmber
class node_sec_api,node_huggingface toneMint
class node_requirements toneNeutral
```

## Components

### Interactive RAG

Online chat-serving runtime.

- [Gradio App](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/app.py): UI entry point · Owns chat state, uploads, and startup. · `chatbot/app.py`
- [Runtime Config](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/config.py): configuration · `chatbot/config.py`
- [RAG Chains](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/chains.py): orchestrator · Embeds questions, retrieves context, and generates answers. · `chatbot/chains.py`
- [Local Models](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/models.py): LLM and embedder · Loads Hugging Face generation and embedding models. · `chatbot/models.py`
- [Upload Processing](https://github.com/khadija20032003/lawyerchat/blob/main/chatbot/helpers.py): document ingestion · Processes uploaded PDFs and supports RAG utilities. · `chatbot/helpers.py`
- **SEC Vector Collection**: ChromaDB collection · Persistent local semantic index of SEC risk factors.
- **Session Document Index**: transient vector index · Per-session index for uploaded PDFs.

### Corpus Refresh

Scheduled SEC ingestion and publication.

- [Daily Update Workflow](https://github.com/khadija20032003/lawyerchat/blob/main/.github/workflows/daily_update.yml): GitHub Actions workflow · Schedules corpus refreshes. · `.github/workflows/daily_update.yml`
- [Dataset Update Script](https://github.com/khadija20032003/lawyerchat/blob/main/scripts/daily_update_hf.py): pipeline launcher · Connects ingestion with Hugging Face publication. · `scripts/daily_update_hf.py`
- [SEC Ingestion Pipeline](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/pipeline.py): pipeline orchestrator · Fetches, extracts, and writes searchable SEC records. · `sec_risk_factors/pipeline.py`
- [SEC Client](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/sec_client.py): external client · `sec_risk_factors/sec_client.py`
- [Risk Factor Extractor](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/extractors.py): content extraction · Isolates 10-K risk-factor disclosures from filings. · `sec_risk_factors/extractors.py`
- [Vector Store Writer](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/store.py): persistence adapter · Updates persisted vector-store records. · `sec_risk_factors/store.py`
- [Dataset Artifacts](https://github.com/khadija20032003/lawyerchat/blob/main/sec_risk_factors/artifacts.py): artifact manager · `sec_risk_factors/artifacts.py`

### External Systems

- **SEC Filing Endpoints**: external data source
- **Hugging Face**: model and dataset distribution · Supplies model weights and publishes the vector dataset.

### Other components

- [Python Dependencies](https://github.com/khadija20032003/lawyerchat/blob/main/requirements.txt): runtime manifest · Declares Python, LangChain, Gradio, ChromaDB, and model dependencies. · `requirements.txt`

## Connections

- Gradio App → Runtime Config: reads settings
- Gradio App → RAG Chains: answers questions
- Gradio App → Upload Processing: processes uploads
- RAG Chains → Local Models: embeds and generates
- RAG Chains → SEC Vector Collection: retrieves SEC context
- RAG Chains → Session Document Index: retrieves upload context
- Upload Processing → Session Document Index: indexes PDFs
- Local Models → Hugging Face: downloads weights
- Daily Update Workflow → Dataset Update Script: scheduled trigger
- Dataset Update Script → SEC Ingestion Pipeline: runs ingestion
- Dataset Update Script → Hugging Face: publishes dataset
- SEC Ingestion Pipeline → SEC Client: fetches filings
- SEC Client → SEC Filing Endpoints: requests filings
- SEC Ingestion Pipeline → Risk Factor Extractor: extracts risk factors
- SEC Ingestion Pipeline → Dataset Artifacts: produces artifacts
- SEC Ingestion Pipeline → Vector Store Writer: writes records
- Vector Store Writer → SEC Vector Collection: persists index
- Hugging Face → SEC Vector Collection: distributes corpus
- Python Dependencies → Gradio App: runtime dependencies
