# stabrise/scaledp architecture

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

- Interactive diagram: https://gitdiagram.com/stabrise/scaledp
- Repository: https://github.com/stabrise/scaledp

## Overview

##### Project type and purpose
- ScaleDP is an open-source Python library (SDK) for AI/ML–driven document processing on Apache Spark.
- It provides Spark DataFrame readers, transformers (PDF → DataFrame, binary images, text), pipeline stages (OCR, detection, extraction, NER), schema definitions, and utilities to integrate with external model engines (LLM/VLM APIs, Hugging Face, OCR engines).

##### Main components
a. User application layer
- Your Python code or notebook that builds a Spark session (`ScaleDPSession`) and a `PipelineModel` with stages.
- Optional REST API wrapper for low-latency, no-Spark deployments.
b. Apache Spark layer
- Spark driver and executors running DataFrame operations.
- Spark data sources: PDF datasource (`spark‐pdf`) and binaryFile for images.
c. ScaleDP library core
- I/O modules: `scaledp/pdf`, `scaledp/image`, `scaledp/text` — convert raw bytes to structured DataFrame columns.
- Pipeline modules: in `scaledp/pipeline/PandasPipeline.py` and stage classes under `scaledp/models/*`
- Schemas: Pydantic and Dataclasses in `scaledp/schemas` for typed outputs.
- Utils for display and template rendering (HTML, JSON).
d. Model/processing engines
- OCR engines: Tesseract, EasyOCR, Surya, DocTR (local).
- CV detectors: YOLO, DocTR text detector.
- LLM/VLM extractors and NER: calls out to OpenAI-compatible endpoints, Google Gemini, Hugging Face Hub.
- DSPy, LLMVisualExtractor, LLMExtractor, NER modules.
e. External services and resources
- RESTful API endpoints (OpenAI, Google generativelanguage API).
- Hugging Face model hub.
- Local resource files (example PDFs/images) under `scaledp/resources`.

##### Relationships and data flow
- Data ingestion: PDF/image/text files → Spark DataFrame
→ series of pipeline stages (`DataToImage` → OCR/Detector → Extractor → Ner → ImageDrawBoxes)
→ output columns (binary image, text, JSON, visualizations) in the same DataFrame
→ user consumes structured data or writes to sinks.
- Inter-stage calls: each transformer stage takes one or more inputCols, produces an outputCol; some stages invoke external APIs or local engines.
- Control plane: pipeline orchestration via Spark DAG.

##### Architectural patterns and principles
- Layered modular design: I/O → processing stages → schemas → utils.
- Plugin-style extensibility: base classes (BaseDetector, BaseExtractor, BaseOcr, BaseRecognizer, BaseNer) allow adding new engines.
- Declarative pipeline: users compose stages; engine implementations are swappable.
- Data-centric flow: everything is a Spark DataFrame column; no global state.

## Diagram (Mermaid)

```mermaid
graph TB
  %% User Layer
  subgraph "User Application" 
    U1["User Code / Notebook\n(ScaleDPSession & PipelineModel)"]:::app
    U2["Optional REST API Wrapper"]:::app
  end

  %% Spark Layer
  subgraph "Spark Runtime"
    SD["Spark Driver"]:::spark
    SE["Spark Executors"]:::spark
    DS1["PDF DataSource\n(spark-pdf)"]:::spark
    DS2["Image DataSource\n(binaryFile)"]:::spark
    DS3["Text DataSource\n(file)"]:::spark
  end

  %% ScaleDP Core
  subgraph "ScaleDP Core"
    subgraph "I/O Modules"
      IO1["PdfDataToDocument"]:::core
      IO2["PdfDataToImage"]:::core
      IO3["PdfDataToSingleImage"]:::core
      IO4["PdfDataToText"]:::core
      IO5["SingleImageToPdf"]:::core
      IO6["DataToImage"]:::core
      IO7["ImageCropBoxes"]:::core
      IO8["ImageDrawBoxes"]:::core
      IO9["TextToDocument"]:::core
    end
    PP["PandasPipeline"]:::core
    subgraph "Schemas"
      S1["Box"]:::schema
      S2["DetectorOutput"]:::schema
      S3["Document"]:::schema
      S4["Entity"]:::schema
      S5["ExtractorOutput"]:::schema
      S6["Image"]:::schema
      S7["NerOutput"]:::schema
      S8["PdfDocument"]:::schema
    end
    subgraph "Utils"
      ULM1["auxiliary"]:::util
      ULM2["dataclass"]:::util
      ULM3["pydantic_schema_utils"]:::util
      ULM4["show_utils"]:::util
      ULM5["templates"]:::util
    end
  end

  %% Model Engines
  subgraph "Detection Engines"
    D0["BaseDetector"]:::engines
    D1["DocTRTextDetector"]:::engines
    D2["YoloDetector"]:::engines
  end
  subgraph "Extraction Engines"
    E0["BaseExtractor"]:::engines
    E1["BaseVisualExtractor"]:::engines
    E2["DSPyExtractor"]:::engines
    E3["GeminiVisualExtractor"]:::engines
    E4["LLMExtractor"]:::engines
    E5["LLMVisualExtractor"]:::engines
  end
  subgraph "OCR Engines"
    O0["BaseOcr"]:::engines
    O1["DocTROcr"]:::engines
    O2["EasyOcr"]:::engines
    O3["SuryaOcr"]:::engines
    O4["TesseractOcr"]:::engines
    O5["LLMOcr"]:::engines
    O6["TesseractRecognizer"]:::engines
  end
  subgraph "NER Engines"
    N0["BaseNer"]:::engines
    N1["Ner"]:::engines
    N2["LLMNer"]:::engines
  end

  %% External Services & Resources
  subgraph "External Services"
    ES1["OpenAI / Gemini API"]:::ext
    ES2["Google GenerativeLanguage API"]:::ext
    ES3["Hugging Face Hub"]:::ext
  end
  subgraph "Local Resources"
    R1["PDFs"]:::res
    R2["Images"]:::res
    R3["Texts"]:::res
  end

  %% Data Flow & Relationships
  U1 -->|builds| SD
  U2 -->|invokes| PP
  SD --> SE
  R1 --> DS1
  R2 --> DS2
  R3 --> DS3

  DS1 --> IO1
  DS1 --> IO2
  DS1 --> IO3
  DS1 --> IO4
  DS1 --> IO5
  DS2 --> IO6
  DS2 --> IO7
  DS2 --> IO8
  DS3 --> IO9

  IO1 --> PP
  IO2 --> PP
  IO3 --> PP
  IO4 --> PP
  IO5 --> PP
  IO6 --> PP
  IO7 --> PP
  IO8 --> PP
  IO9 --> PP

  PP -->|detect| D0
  PP -->|extract| E0
  PP -->|ocr| O0
  PP -->|ner| N0

  D0 --> D1
  D0 --> D2
  E0 --> E1
  E0 --> E2
  E0 --> E3
  E0 --> E4
  E0 --> E5
  O0 --> O1
  O0 --> O2
  O0 --> O3
  O0 --> O4
  O0 --> O5
  O0 --> O6
  N0 --> N1
  N0 --> N2

  D1 -->|HTTP| ES3
  E3 -->|HTTP| ES1
  E4 -->|HTTP| ES1
  E5 -->|HTTP| ES1
  O5 -->|HTTP| ES1
  N2 -->|HTTP| ES1
  E2 -->|local| ES2

  PP -->|output DataFrame| U1

  %% Click Events
  click IO1 "https://github.com/stabrise/scaledp/blob/master/scaledp/pdf/PdfDataToDocument.py"
  click IO2 "https://github.com/stabrise/scaledp/blob/master/scaledp/pdf/PdfDataToImage.py"
  click IO3 "https://github.com/stabrise/scaledp/blob/master/scaledp/pdf/PdfDataToSingleImage.py"
  click IO4 "https://github.com/stabrise/scaledp/blob/master/scaledp/pdf/PdfDataToText.py"
  click IO5 "https://github.com/stabrise/scaledp/blob/master/scaledp/pdf/SingleImageToPdf.py"
  click IO6 "https://github.com/stabrise/scaledp/blob/master/scaledp/image/DataToImage.py"
  click IO7 "https://github.com/stabrise/scaledp/blob/master/scaledp/image/ImageCropBoxes.py"
  click IO8 "https://github.com/stabrise/scaledp/blob/master/scaledp/image/ImageDrawBoxes.py"
  click IO9 "https://github.com/stabrise/scaledp/blob/master/scaledp/text/TextToDocument.py"
  click PP "https://github.com/stabrise/scaledp/blob/master/scaledp/pipeline/PandasPipeline.py"
  click S1 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/Box.py"
  click S2 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/DetectorOutput.py"
  click S3 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/Document.py"
  click S4 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/Entity.py"
  click S5 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/ExtractorOutput.py"
  click S6 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/Image.py"
  click S7 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/NerOutput.py"
  click S8 "https://github.com/stabrise/scaledp/blob/master/scaledp/schemas/PdfDocument.py"
  click ULM1 "https://github.com/stabrise/scaledp/blob/master/scaledp/utils/auxiliary.py"
  click ULM2 "https://github.com/stabrise/scaledp/blob/master/scaledp/utils/dataclass.py"
  click ULM3 "https://github.com/stabrise/scaledp/blob/master/scaledp/utils/pydantic_shema_utils.py"
  click ULM4 "https://github.com/stabrise/scaledp/blob/master/scaledp/utils/show_utils.py"
  click ULM5 "https://github.com/stabrise/scaledp/blob/master/scaledp/utils/templates/base.html"
  click D0 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/detectors/BaseDetector.py"
  click D1 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/detectors/DocTRTextDetector.py"
  click D2 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/detectors/YoloDetector.py"
  click E0 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/BaseExtractor.py"
  click E1 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/BaseVisualExtractor.py"
  click E2 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/DSPyExtractor.py"
  click E3 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/GeminiVisualExtractor.py"
  click E4 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/LLMExtractor.py"
  click E5 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/extractors/LLMVisualExtractor.py"
  click O0 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/BaseOcr.py"
  click O1 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/DocTROcr.py"
  click O2 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/EasyOcr.py"
  click O3 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/SuryaOcr.py"
  click O4 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/TesseractOcr.py"
  click O5 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/LLMOcr.py"
  click O6 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/recognizers/TesseractRecognizer.py"
  click N0 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/ner/BaseNer.py"
  click N1 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/ner/Ner.py"
  click N2 "https://github.com/stabrise/scaledp/blob/master/scaledp/models/ner/LLMNer.py"

  %% Styles
  classDef app fill:#D6EFFF,stroke:#333,stroke-width:1px
  classDef spark fill:#CCE5FF,stroke:#333,stroke-width:1px
  classDef core fill:#DFFFE0,stroke:#333,stroke-width:1px
  classDef schema fill:#E8E8FF,stroke:#333,stroke-width:1px
  classDef util fill:#F0E0FF,stroke:#333,stroke-width:1px
  classDef engines fill:#FFF5CC,stroke:#333,stroke-width:1px
  classDef ext fill:#FFD9CC,stroke:#333,stroke-width:1px
  classDef res fill:#E0E0E0,stroke:#333,stroke-width:1px
```
