# shreyasjha03/drhp_automator-s45- architecture

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

- Interactive diagram: https://gitdiagram.com/shreyasjha03/drhp_automator-s45-
- Repository: https://github.com/shreyasjha03/drhp_automator-s45-

## Overview

1. **What this repository is**
   - A **batch CLI pipeline** for extracting **authorised share capital changes** from corporate filing documents and producing DRHP-style capital structure outputs.
   - It is **not a web app**; the main entry point is `app/main.py`, which orchestrates a file-based processing flow.

2. **Primary data source and sink**
   - Inputs come from `data/sample_docs/` containing `.md` documents that represent filings such as **SH-7, board resolutions, EGM documents, and MOA amendments**.
   - Outputs are written to `data/outputs/` as:
     - `result.csv` for tabular event summaries
     - `CAPITAL_STRUCTURE.md` for detailed traceable event narratives

3. **Ingestion boundary**
   - `app/ingestion/loader.py` is the first subsystem.
   - Its job is to discover and load document files from disk into the pipeline as raw text records, preserving file identity for later traceability.

4. **Document classification**
   - `app/classification/classifier.py` assigns each loaded document a type such as **SH7, PAS3, BOARD_RESOLUTION, EGM, or MOA**.
   - The README indicates this is primarily **rule-based keyword matching**, with an **LLM fallback** when the document is ambiguous.

5. **Extraction layer**
   - `app/extraction/extractor.py` converts classified documents into structured capital-change fields.
   - `app/extraction/prompts.py` holds the prompt templates used when the system needs to ask the LLM to interpret weaker or less structured text.
   - The architecture is intentionally **hybrid**:
     - deterministic parsing for structured documents
     - Groq LLM fallback for harder cases

6. **LLM integration**
   - `app/utils/llm_client.py` encapsulates the external LLM dependency.
   - The repository uses **Groq** with `llama-3.1-8b-instant` when enabled by environment variables.
   - This is an optional dependency; the pipeline still runs without the API key, but with reduced fallback capability.

7. **Core data model**
   - `app/models/schemas.py` defines the shared structured schema used across the pipeline.
   - This is the main contract boundary between ingestion, extraction, processing, and output, keeping document-specific parsing logic separate from event-level business logic.

8. **Event reconstruction**
   - `app/processing/event_builder.py` groups extracted document facts into **capital-change events**, using the README’s event-centric approach.
   - The repository’s key merge key is effectively **(date, event_type)**, with multiple documents rolled into one event when they describe the same capital change.

9. **Validation and conflict handling**
   - `app/processing/validator.py` checks for:
     - missing fields
     - conflicting values across sources
     - confidence level assignment
   - The system explicitly avoids guessing: if sources disagree, it preserves the conflict and lowers confidence rather than silently reconciling it.

10. **Timeline and output shaping**
    - `app/processing/timeline.py` appears to organize events into a chronological capital history.
    - `app/output/generator.py` turns validated events into the two final deliverables: CSV and markdown.
    - This is the final presentation boundary of the pipeline.

11. **Repository-level flow**
    - The end-to-end flow is:
      - load documents
      - classify them
      - extract structured values
      - build events from multiple documents
      - validate/score confidence
      - generate outputs
    - The central architectural principle is **traceability**: every output row or narrative should be explainable back to specific source documents.

12. **Operational characteristics**
    - The project is a **single-process, offline analysis tool** rather than a distributed system.
    - Dependencies are managed through `requirements.txt`.
    - Environment-based config is used for the LLM backend, which means the architecture can degrade gracefully if external inference is unavailable.

13. **Testing and quality signal**
    - `tests/test_env.py` and `tests/test_llm.py` suggest the current test coverage focuses on environment setup and LLM connectivity/behavior rather than broad pipeline unit tests.
    - That matches the repository’s emphasis on integration of extraction and external model fallback.

14. **How to diagram it**
    - A good architecture diagram would show:
      - **File input layer** (`data/sample_docs`)
      - **Pipeline orchestrator** (`app/main.py`)
      - **Classification / Extraction / Processing / Output** as separate stages
      - **Groq LLM** as an optional external service used only by extraction/classification fallback
      - **File outputs** (`result.csv`, `CAPITAL_STRUCTURE.md`)
      - **Schema/model boundary** shared across stages to enforce consistent event structure

Main entry points: [`app/extraction/extractor.py`](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/extraction/extractor.py) (Extractor), [`app/main.py`](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/main.py) (Main), [`app/classification/classifier.py`](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/classification/classifier.py) (Classifier), [`app/output/generator.py`](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/output/generator.py) (Generator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inputs["Inputs"]
  node_sample_docs["Sample Docs<br/>source corpus"]
end

subgraph group_pipeline["Pipeline"]
  node_main["Main<br/>orchestrator<br/>[main.py]"]
  node_loader["Loader<br/>ingestion<br/>[loader.py]"]
  node_classifier["Classifier<br/>[classifier.py]"]
  node_extractor["Extractor<br/>[extractor.py]"]
  node_prompts["Prompts<br/>prompt templates<br/>[prompts.py]"]
  node_llm_client["LLM Client<br/>groq client<br/>[llm_client.py]"]
  node_schemas["Schemas<br/>shared model<br/>[schemas.py]"]
  node_event_builder["Event Builder<br/>event reconstruction<br/>[event_builder.py]"]
  node_validator["Validator<br/>validation<br/>[validator.py]"]
  node_timeline["Timeline<br/>chronology<br/>[timeline.py]"]
  node_generator["Generator<br/>output builder<br/>[generator.py]"]
end

subgraph group_outputs["Outputs"]
  node_result_csv["Result CSV<br/>tabular output<br/>[result.csv]"]
  node_capital_md["Capital MD<br/>narrative output"]
end

subgraph group_external["External"]
  node_groq(("Groq<br/>external model"))
end

node_sample_docs -->|"documents"| node_loader
node_loader -->|"records"| node_main
node_main -->|"route"| node_classifier
node_classifier -->|"typed docs"| node_extractor
node_classifier -.->|"fallback"| node_groq
node_extractor -->|"templates"| node_prompts
node_extractor -.->|"fallback"| node_groq
node_extractor -->|"structured fields"| node_schemas
node_schemas -->|"contract"| node_event_builder
node_event_builder -->|"events"| node_validator
node_validator -->|"validated events"| node_timeline
node_timeline -->|"history"| node_generator
node_generator -->|"write"| node_result_csv
node_generator -->|"write"| node_capital_md
node_main -->|"finalize"| node_generator
node_main -->|"shared model"| node_schemas
node_validator -->|"check"| node_schemas
node_llm_client -->|"api"| node_groq
node_classifier -.->|"model call"| node_llm_client
node_extractor -.->|"model call"| node_llm_client
node_prompts -->|"instructions"| node_extractor

click node_main "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/main.py"
click node_loader "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/ingestion/loader.py"
click node_classifier "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/classification/classifier.py"
click node_extractor "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/extraction/extractor.py"
click node_prompts "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/extraction/prompts.py"
click node_llm_client "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/utils/llm_client.py"
click node_schemas "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/models/schemas.py"
click node_event_builder "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/event_builder.py"
click node_validator "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/validator.py"
click node_timeline "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/timeline.py"
click node_generator "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/output/generator.py"
click node_sample_docs "https://github.com/shreyasjha03/drhp_automator-s45-/tree/main/data/sample_docs"
click node_result_csv "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/data/outputs/result.csv"
click node_capital_md "https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/data/outputs/CAPITAL_STRUCTURE.md"

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_sample_docs toneBlue
class node_main,node_loader,node_classifier,node_extractor,node_prompts,node_llm_client,node_schemas,node_event_builder,node_validator,node_timeline,node_generator toneAmber
class node_result_csv,node_capital_md toneMint
class node_groq toneRose
```

## Components

### Inputs

Source documents and raw filing files

- [Sample Docs](https://github.com/shreyasjha03/drhp_automator-s45-/tree/main/data/sample_docs): source corpus · Markdown filing documents used as pipeline input · `data/sample_docs`

### Pipeline

Core batch processing stages

- [Main](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/main.py): orchestrator · Entry point that runs the batch pipeline · `app/main.py`
- [Loader](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/ingestion/loader.py): ingestion · Discovers and loads filing documents from disk · `app/ingestion/loader.py`
- [Classifier](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/classification/classifier.py): document classifier · Assigns document types using rules with LLM fallback · `app/classification/classifier.py`
- [Extractor](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/extraction/extractor.py): field extractor · Derives structured capital-change fields from classified documents · `app/extraction/extractor.py`
- [Prompts](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/extraction/prompts.py): prompt templates · LLM prompt templates for ambiguous documents · `app/extraction/prompts.py`
- [LLM Client](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/utils/llm_client.py): groq client · Encapsulates optional Groq model access · `app/utils/llm_client.py`
- [Schemas](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/models/schemas.py): shared model · Common structured schema used across stages · `app/models/schemas.py`
- [Event Builder](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/event_builder.py): event reconstruction · Merges document facts into capital-change events · `app/processing/event_builder.py`
- [Validator](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/validator.py): validation · Checks missing fields, conflicts, and confidence · `app/processing/validator.py`
- [Timeline](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/processing/timeline.py): chronology · Orders validated events into capital history · `app/processing/timeline.py`
- [Generator](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/app/output/generator.py): output builder · Builds CSV and markdown deliverables · `app/output/generator.py`

### Outputs

Generated deliverables

- [Result CSV](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/data/outputs/result.csv): tabular output · Final event summary table · `data/outputs/result.csv`
- [Capital MD](https://github.com/shreyasjha03/drhp_automator-s45-/blob/main/data/outputs/CAPITAL_STRUCTURE.md): narrative output · Traceable capital-structure narrative · `data/outputs/CAPITAL_STRUCTURE.md`

### External

Optional model dependency

- **Groq**: external model · Optional external LLM used for fallback interpretation

## Connections

- Sample Docs → Loader: documents · Raw filing markdown files are discovered and loaded
- Loader → Main: records · Loaded document records enter the orchestrated flow
- Main → Classifier: route · Main pipeline sends records to document classification
- Classifier → Extractor: typed docs · Classified documents are passed to extraction
- Classifier → Groq: fallback · Ambiguous classifications may use the LLM
- Extractor → Prompts: templates · Extraction uses prompt templates for weaker documents
- Extractor → Groq: fallback · Hard cases are interpreted by the external model
- Extractor → Schemas: structured fields · Extracted values are normalized to the shared schema
- Schemas → Event Builder: contract · Shared model shapes event reconstruction
- Event Builder → Validator: events · Built events are checked for completeness and conflicts
- Validator → Timeline: validated events · Validated events are ordered into a chronology
- Timeline → Generator: history · Chronological capital history is formatted for output
- Generator → Result CSV: write · Tabular event summary is emitted to CSV
- Generator → Capital MD: write · Narrative capital structure report is emitted as markdown
- Main → Generator: finalize · The orchestrator hands validated results to output generation
- Main → Schemas: shared model · Pipeline stages share the common data contract
- Validator → Schemas: check · Validation is performed against the shared schema
- LLM Client → Groq: api · Client encapsulates optional Groq backend access
- Classifier → LLM Client: model call · Classifier can invoke the shared LLM client
- Extractor → LLM Client: model call · Extractor can invoke the shared LLM client
- Prompts → Extractor: instructions · Prompt templates support extraction logic
