# rapid292/ai-ocr architecture

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

- Interactive diagram: https://gitdiagram.com/rapid292/ai-ocr
- Repository: https://github.com/rapid292/ai-ocr

## Overview

- Input & Storage: Represent the entry point where documents (e.g., PDFs) are uploaded into Google Cloud Storage.
- OCR Processing: Use a component for Google Cloud Vision API which extracts text from documents.
- Text Analysis: Include a component for PortKey AI that takes the OCR output and applies advanced text processing with dynamic prompts and cost analysis.
- Data Validation: Highlight that Pydantic is used to ensure that extracted data matches expected formats (you might merge this with the processing section or show it as a validation layer).
- Logging & Monitoring: Represent LogFire as a component that captures real-time insights and errors.
- Output: A component that represents the Structured JSON Output generated by the pipeline.

##### Data Flow and Relationships
- The document is first uploaded to Google Cloud Storage (Input).
- The document then flows to the Google Cloud Vision API for OCR.
- The extracted text is passed on to the PortKey AI component for text analysis.
- Data validation occurs either immediately after extraction or during the AI processing (denoted by Pydantic).
- Alongside processing, LogFire intercepts events for logging and monitoring.
- Finally, the validated and processed data is output as a structured JSON.
- To indicate external dependencies and interactions, such as API calls and environment configurations obtained from the .env file.

##### Technologies and Frameworks
- Python as the runtime environment.
- Google Cloud Vision API for OCR.
- PortKey AI for advanced text processing.
- Pydantic for data validation.
- LogFire for logging and monitoring.
- Supporting tools like Makefile for command-line operations.

##### Final Steps
- Layout components in the order they’re executed. For example, you might place “Document Upload (Google Cloud Storage)” on the far left, then “Google Cloud Vision API”, “PortKey AI Processing”, and finally “Structured JSON Output” on the right.
- A side connector or overlay for “Pydantic” and “LogFire” where they interact with the processing components—Pydantic can be shown as a validation layer on the extracted text, and LogFire can be attached to all components to capture logs.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Google Cloud Storage (Input)"]:::external
    B["Google Cloud Vision API (OCR)"]:::external
    C["Pydantic Data Validation"]:::internal
    D["PortKey AI Processing"]:::external
    E["Structured JSON Output"]:::internal
    F["LogFire (Monitoring & Logging)"]:::internal
    H["Environment Configuration (.env)"]:::support
    G["Build/Deployment Support (Makefile)"]:::support

    A -->|"document_uploaded"| B
    B -->|"extracted_text"| C
    C -->|"validated_data"| D
    D -->|"analysis_results"| E

    A -.->|"logs"| F
    B -.->|"logs"| F
    C -.->|"logs"| F
    D -.->|"logs"| F
    E -.->|"logs"| F

    H -.->|"configures"| A
    G -.->|"supports"| H

    click A "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click B "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click C "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click D "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click E "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click F "https://github.com/rapid292/ai-ocr/blob/main/main.py"
    click G "https://github.com/rapid292/ai-ocr/tree/main/Makefile"

    classDef external fill:#FAD7A0,stroke:#d35400,stroke-width:2px;
    classDef internal fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef support fill:#F9E79F,stroke:#CA6F1,stroke-width:2px;
```
