# hammad-asif/gcp-insightdocs architecture

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

- Interactive diagram: https://gitdiagram.com/hammad-asif/gcp-insightdocs
- Repository: https://github.com/hammad-asif/gcp-insightdocs

## Overview

#### Purpose and Scope
GCP InsightDocs is a Python backend for extracting structured key/value data from PDF and image documents. It is intended to support a React document viewer/sidebar client, though no frontend source is present. The implementation combines Google Cloud Document AI with OCR and document-specific post-processing.

#### Primary Entry Points
- `BackEnd-main/BackEnd-main/main.py` is the likely Flask application/API entry point and orchestration layer.
- `BackEnd-main/BackEnd-main/OCR.py` provides OCR-oriented extraction processing.
- `BackEnd-main/BackEnd-main/search.py` likely supports lookup, normalization, or extraction-result querying.
- `BackEnd-main/BackEnd-main/warehouse.py` and `BackEnd-main/BackEnd-main/warehouse sir.py` appear to implement separate document-format extraction paths.

#### Document Intake and Source Assets
Input documents are represented by checked-in PDFs: `BackEnd-main/BackEnd-main/4.pdf`, `BackEnd-main/BackEnd-main/32.pdf`, and `BackEnd-main/BackEnd-main/53.pdf`. The backend boundary is a supplied PDF/image file; it must preserve enough source context for a client-side document viewer while returning extracted fields and related details.

#### Extraction Pipeline
1. Flask receives or selects a document in `BackEnd-main/BackEnd-main/main.py`.
2. Cloud/OCR processing is invoked through logic in `BackEnd-main/BackEnd-main/OCR.py`.
3. Raw recognized text/entities are interpreted by document-specific modules, notably `BackEnd-main/BackEnd-main/warehouse.py` and `BackEnd-main/BackEnd-main/warehouse sir.py`.
4. Search or matching logic in `BackEnd-main/BackEnd-main/search.py` helps map recognized content to expected fields.
5. The service emits structured extraction output, exemplified by `BackEnd-main/BackEnd-main/testresult.json`.

#### Schema and Result Contract
`BackEnd-main/BackEnd-main/schema.json` is the central structured-data contract for extracted results or expected fields. `BackEnd-main/BackEnd-main/test.json` and `BackEnd-main/BackEnd-main/testresult.json` provide sample input/output representations. This schema boundary separates cloud OCR/DocAI’s raw response from the UI-facing key/value summary.

#### Runtime, Infrastructure, and External Services
The backend runs on Python with dependencies defined in `BackEnd-main/BackEnd-main/requirements.txt`, using Flask as the HTTP runtime. Google Cloud Document AI is the material external extraction service; OCR is a complementary recognition path. `BackEnd-main/BackEnd-main/key.json` is a Google Cloud credential artifact and should be treated as secret infrastructure configuration, not application data.

#### Architectural Considerations
The system is format-sensitive: the warehouse-specific modules indicate extraction behavior is coupled to particular document layouts. Keep API orchestration in `main.py`, cloud/OCR integration in `OCR.py`, and layout/business parsing in the warehouse modules. New document types should add isolated parsers and schema mappings rather than expanding the Flask route layer or hard-coding field logic into cloud-service calls.

Main entry points: [`BackEnd-main/BackEnd-main/main.py`](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/main.py) (Flask orchestration), [`BackEnd-main/BackEnd-main/OCR.py`](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/OCR.py) (OCR processing), [`BackEnd-main/BackEnd-main/search.py`](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/search.py) (Field matching), [`BackEnd-main/BackEnd-main/schema.json`](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/schema.json) (Extraction schema)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Backend API"]
  node_api["Flask orchestration<br/>Python API<br/>[main.py]"]
  node_result["Structured extraction result<br/>API response<br/>[testresult.json]"]
  node_dependencies["Python dependencies<br/>runtime manifest<br/>[requirements.txt]"]
end

subgraph group_pipeline["Extraction Pipeline"]
  node_ocr["OCR processing<br/>Python extraction<br/>[OCR.py]"]
  node_raw["Recognized text &amp; entities<br/>raw extraction"]
  node_warehouse["Warehouse parser<br/>layout parser<br/>[warehouse.py]"]
  node_warehouse_sir["Warehouse SIR parser<br/>layout parser<br/>[warehouse sir.py]"]
  node_search["Field matching<br/>Python lookup<br/>[search.py]"]
end

subgraph group_contracts["Contracts &amp; Samples"]
  node_samples["Checked-in PDF sample<br/>sample document<br/>[4.pdf]"]
  node_schema["Extraction schema<br/>result contract<br/>[schema.json]"]
  node_sample_input["Sample input representation<br/>sample contract<br/>[test.json]"]
end

subgraph group_external["External Boundaries"]
  node_client["Document viewer client"]
  node_input["PDF or image input<br/>document input"]
  node_docai{{"Google Cloud Document AI<br/>cloud extraction service"}}
  node_credentials["GCP credentials<br/>secret configuration<br/>[key.json]"]
end

node_client -->|"submits document / reads result"| node_api
node_samples -.->|"representative input"| node_input
node_input -->|"received or selected"| node_api
node_api -->|"starts extraction"| node_ocr
node_ocr -->|"requests recognition"| node_docai
node_credentials -.->|"authenticates access"| node_docai
node_docai -->|"returns text and entities"| node_raw
node_ocr -->|"complementary OCR output"| node_raw
node_raw -->|"warehouse layout path"| node_warehouse
node_raw -->|"SIR layout path"| node_warehouse_sir
node_warehouse -->|"parsed candidate fields"| node_search
node_warehouse_sir -->|"parsed candidate fields"| node_search
node_search -->|"maps fields to contract"| node_schema
node_schema -->|"shapes structured output"| node_result
node_api -->|"emits response"| node_result
node_sample_input -.->|"illustrates expected data"| node_schema
node_dependencies -.->|"provides Flask runtime"| node_api

click node_api "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/main.py"
click node_samples "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/4.pdf"
click node_ocr "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/OCR.py"
click node_warehouse "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/warehouse.py"
click node_warehouse_sir "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/warehouse%20sir.py"
click node_search "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/search.py"
click node_schema "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/schema.json"
click node_result "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/testresult.json"
click node_sample_input "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/test.json"
click node_credentials "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/key.json"
click node_dependencies "https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-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_api,node_result,node_dependencies toneBlue
class node_ocr,node_raw,node_warehouse,node_warehouse_sir,node_search toneAmber
class node_samples,node_schema,node_sample_input toneMint
class node_client,node_input,node_docai,node_credentials toneRose
```

## Components

### Backend API

- [Flask orchestration](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/main.py): Python API · Receives or selects documents and coordinates extraction. · `BackEnd-main/BackEnd-main/main.py`
- [Structured extraction result](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/testresult.json): API response · UI-facing key/value summary with related details. · `BackEnd-main/BackEnd-main/testresult.json`
- [Python dependencies](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/requirements.txt): runtime manifest · Defines Flask and backend runtime dependencies. · `BackEnd-main/BackEnd-main/requirements.txt`

### Extraction Pipeline

- [OCR processing](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/OCR.py): Python extraction · Invokes OCR-oriented recognition processing. · `BackEnd-main/BackEnd-main/OCR.py`
- **Recognized text & entities**: raw extraction
- [Warehouse parser](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/warehouse.py): layout parser · Document-format-specific field interpretation. · `BackEnd-main/BackEnd-main/warehouse.py`
- [Warehouse SIR parser](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/warehouse%20sir.py): layout parser · Separate format-sensitive extraction path. · `BackEnd-main/BackEnd-main/warehouse sir.py`
- [Field matching](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/search.py): Python lookup · Maps recognized content to expected fields. · `BackEnd-main/BackEnd-main/search.py`

### Contracts & Samples

- [Checked-in PDF sample](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/4.pdf): sample document · `BackEnd-main/BackEnd-main/4.pdf`
- [Extraction schema](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/schema.json): result contract · Separates raw recognition from UI-facing key/value results. · `BackEnd-main/BackEnd-main/schema.json`
- [Sample input representation](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/test.json): sample contract · `BackEnd-main/BackEnd-main/test.json`

### External Boundaries

- **Document viewer client**: client · External React-oriented consumer; source is not present.
- **PDF or image input**: document input · Source context must remain available to the viewer client.
- **Google Cloud Document AI**: cloud extraction service · Material external document-recognition service.
- [GCP credentials](https://github.com/hammad-asif/gcp-insightdocs/blob/main/BackEnd-main/BackEnd-main/key.json): secret configuration · Credential artifact for cloud-service access. · `BackEnd-main/BackEnd-main/key.json`

## Connections

- Document viewer client → Flask orchestration: submits document / reads result
- Checked-in PDF sample → PDF or image input: representative input
- PDF or image input → Flask orchestration: received or selected
- Flask orchestration → OCR processing: starts extraction
- OCR processing → Google Cloud Document AI: requests recognition
- GCP credentials → Google Cloud Document AI: authenticates access
- Google Cloud Document AI → Recognized text & entities: returns text and entities
- OCR processing → Recognized text & entities: complementary OCR output
- Recognized text & entities → Warehouse parser: warehouse layout path
- Recognized text & entities → Warehouse SIR parser: SIR layout path
- Warehouse parser → Field matching: parsed candidate fields
- Warehouse SIR parser → Field matching: parsed candidate fields
- Field matching → Extraction schema: maps fields to contract
- Extraction schema → Structured extraction result: shapes structured output
- Flask orchestration → Structured extraction result: emits response
- Sample input representation → Extraction schema: illustrates expected data
- Python dependencies → Flask orchestration: provides Flask runtime
