# hammad-asif/tenderml architecture

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

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

## Overview

1. **Repository purpose**
   - This repo is an NLP extraction pipeline for German construction tender PDFs. Its job is to classify text segments, then extract structured fields from them in near real time.
   - The codebase is centered on two phases: **offline model preparation** and **online inference/extraction**.

2. **Core runtime boundary**
   - The main runtime path appears to start in `main.py` and use the reusable logic in `model.py`, `tokenizer.py`, `tree.py`, and `DFS.py`.
   - The runtime consumes already-extracted PDF text or text lines, not raw PDFs directly; PDF parsing is likely upstream of this repo.

3. **Text normalization and tokenization**
   - `tokenizer.py` is the preprocessing boundary for German text.
   - Its role is to normalize raw text into model-ready tokens/features so the classifier and downstream extractors see consistent input across documents with different layouts.

4. **Machine learning model layer**
   - `model.py` is the main ML abstraction layer.
   - The persisted artifacts in `models/` show the system uses trained classifiers and feature encoders: `SVM.pk`, `SGDWarm89.7.pk`, `crf.pk`, `vectorizer.pk`, and `vectorizerModel.pk`.
   - This suggests a two-step approach: vectorization / feature extraction followed by classical ML classification, with a CRF-style sequence model likely used for structured label extraction.

5. **Training and artifact generation**
   - `createCSV.py` and `make_sequence_of_bullet.py` look like offline preparation scripts for turning document text into training-ready tabular or sequence-labeled data.
   - `test.py` likely evaluates model performance and validates extraction quality.
   - The readme indicates a semi-supervised training approach and a dataset of 1000+ German documents, so the offline path is where labeling, feature engineering, and model fitting happen before artifacts are serialized.

6. **Structured extraction via tree representation**
   - `tree.py` is the main hierarchical data structure used after classification.
   - The tree represents document content in a structured way so that generic and context-aware data can be organized before final field extraction.

7. **Traversal and field discovery**
   - `DFS.py` provides depth-first traversal over that tree.
   - This is the main search mechanism for locating target values in the tree after classification and filtering, especially when relevant information may appear at varying positions in different tender layouts.

8. **Additional tree/filter logic**
   - `Tree_Extra_Code.py` likely contains supporting logic for tree construction, cleanup, filtering, or correction of extracted nodes.
   - Combined with DFS, this forms the post-classification refinement stage that turns noisy document text into structured output.

9. **Data flow across the system**
   - The practical flow is: **document text → tokenization/vectorization → ML classification → tree construction → DFS/filtering → extracted fields**.
   - The key architectural boundary is between **statistical classification** and **rule/structure-based extraction**; the first identifies what kind of content a text span is, and the second resolves where the final fields are.

10. **Deployment and operational context**
   - The readme says the solution was deployed with **FastAPI** on a **Ray cluster** running on **AWS EC2**, with autoscaling for workload variability.
   - That deployment layer is not visible in the file tree, so this repo appears to contain the core extraction engine rather than the full service wrapper.

11. **What to show in an architecture diagram**
   - Show external inputs as German tender PDFs or their extracted text.
   - Show a preprocessing/tokenization stage, a model inference stage backed by serialized artifacts in `models/`, a tree-based extraction stage, and a final structured output.
   - Mark the offline training/artifact generation scripts as a separate lane from the live inference path.

Main entry points: [`model.py`](https://github.com/hammad-asif/tenderml/blob/main/model.py) (Model layer), [`test.py`](https://github.com/hammad-asif/tenderml/blob/main/test.py) (Train and eval), [`main.py`](https://github.com/hammad-asif/tenderml/blob/main/main.py) (Main flow), [`tokenizer.py`](https://github.com/hammad-asif/tenderml/blob/main/tokenizer.py) (Tokenizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_online["Online inference"]
  node_input_text["Tender text<br/>external input"]
  node_main["Main flow<br/>entrypoint<br/>[main.py]"]
  node_tokenizer["Tokenizer<br/>preprocess<br/>[tokenizer.py]"]
  node_model["Model layer<br/>ml runtime<br/>[model.py]"]
  node_tree["Tree model<br/>structure<br/>[tree.py]"]
  node_dfs["DFS search<br/>traversal<br/>[DFS.py]"]
  node_tree_extra["Tree cleanup<br/>postprocess<br/>[Tree_Extra_Code.py]"]
  node_output_fields["Structured output<br/>extraction result"]
end

subgraph group_offline["Offline training"]
  node_csv_prep["CSV prep<br/>data prep<br/>[createCSV.py]"]
  node_bullet_seq["Sequence builder<br/>data prep"]
  node_train_eval["Train and eval<br/>offline pipeline<br/>[test.py]"]
end

subgraph group_artifacts["Model artifacts"]
  node_svm[("SVM<br/>classifier artifact<br/>[SVM.pk]")]
  node_sgd[("SGD model<br/>classifier artifact<br/>[SGDWarm89.7.pk]")]
  node_crf[("CRF<br/>sequence artifact<br/>[crf.pk]")]
  node_vectorizer[("Vectorizer<br/>feature artifact<br/>[vectorizer.pk]")]
  node_vectorizer_model[("Vectorizer model<br/>feature artifact<br/>[vectorizerModel.pk]")]
end

node_input_text -->|"feeds"| node_main
node_main -->|"normalizes"| node_tokenizer
node_tokenizer -->|"features"| node_model
node_model -.->|"loads"| node_svm
node_model -.->|"loads"| node_sgd
node_model -.->|"loads"| node_crf
node_model -.->|"uses"| node_vectorizer
node_model -.->|"uses"| node_vectorizer_model
node_model -->|"builds"| node_tree
node_tree -->|"refines"| node_tree_extra
node_tree_extra -->|"prepares"| node_dfs
node_dfs -->|"extracts"| node_output_fields
node_csv_prep -->|"prepares"| node_train_eval
node_bullet_seq -->|"prepares"| node_train_eval
node_train_eval -->|"serializes"| node_svm
node_train_eval -->|"serializes"| node_sgd
node_train_eval -->|"serializes"| node_crf
node_train_eval -->|"serializes"| node_vectorizer
node_train_eval -->|"serializes"| node_vectorizer_model

click node_main "https://github.com/hammad-asif/tenderml/blob/main/main.py"
click node_tokenizer "https://github.com/hammad-asif/tenderml/blob/main/tokenizer.py"
click node_model "https://github.com/hammad-asif/tenderml/blob/main/model.py"
click node_tree "https://github.com/hammad-asif/tenderml/blob/main/tree.py"
click node_dfs "https://github.com/hammad-asif/tenderml/blob/main/DFS.py"
click node_tree_extra "https://github.com/hammad-asif/tenderml/blob/main/Tree_Extra_Code.py"
click node_csv_prep "https://github.com/hammad-asif/tenderml/blob/main/createCSV.py"
click node_bullet_seq "https://github.com/hammad-asif/tenderml/blob/main/make_sequence_of_bullet.py"
click node_train_eval "https://github.com/hammad-asif/tenderml/blob/main/test.py"
click node_svm "https://github.com/hammad-asif/tenderml/blob/main/models/SVM.pk"
click node_sgd "https://github.com/hammad-asif/tenderml/blob/main/models/SGDWarm89.7.pk"
click node_crf "https://github.com/hammad-asif/tenderml/blob/main/models/crf.pk"
click node_vectorizer "https://github.com/hammad-asif/tenderml/blob/main/models/vectorizer.pk"
click node_vectorizer_model "https://github.com/hammad-asif/tenderml/blob/main/models/vectorizerModel.pk"

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_input_text,node_main,node_tokenizer,node_model,node_tree,node_dfs,node_tree_extra,node_output_fields toneBlue
class node_csv_prep,node_bullet_seq,node_train_eval toneAmber
class node_svm,node_sgd,node_crf,node_vectorizer,node_vectorizer_model toneMint
```

## Components

### Online inference

Live extraction path for already-parsed German tender text.

- **Tender text**: external input · German tender PDFs after upstream text extraction or line splitting.
- [Main flow](https://github.com/hammad-asif/tenderml/blob/main/main.py): entrypoint · Coordinates the end-to-end inference pipeline. · `main.py`
- [Tokenizer](https://github.com/hammad-asif/tenderml/blob/main/tokenizer.py): preprocess · Normalizes tender text into model-ready tokens and features. · `tokenizer.py`
- [Model layer](https://github.com/hammad-asif/tenderml/blob/main/model.py): ml runtime · Loads artifacts and runs classification / sequence prediction. · `model.py`
- [Tree model](https://github.com/hammad-asif/tenderml/blob/main/tree.py): structure · Represents classified document content in a hierarchical form. · `tree.py`
- [DFS search](https://github.com/hammad-asif/tenderml/blob/main/DFS.py): traversal · Traverses the tree to locate target fields and values. · `DFS.py`
- [Tree cleanup](https://github.com/hammad-asif/tenderml/blob/main/Tree_Extra_Code.py): postprocess · Supporting logic for tree construction, filtering, and correction. · `Tree_Extra_Code.py`
- **Structured output**: extraction result · Final extracted tender fields and normalized records.

### Offline training

Model preparation and artifact generation pipeline.

- [CSV prep](https://github.com/hammad-asif/tenderml/blob/main/createCSV.py): data prep · Builds training-ready tabular data from document text. · `createCSV.py`
- [Sequence builder](https://github.com/hammad-asif/tenderml/blob/main/make_sequence_of_bullet.py): data prep · Creates sequence-labeled examples for training. · `make_sequence_of_bullet.py`
- [Train and eval](https://github.com/hammad-asif/tenderml/blob/main/test.py): offline pipeline · Runs model fitting and quality checks on prepared data. · `test.py`

### Model artifacts

Serialized classifiers and feature encoders loaded at runtime.

- [SVM](https://github.com/hammad-asif/tenderml/blob/main/models/SVM.pk): classifier artifact · Serialized classical classifier used during inference. · `models/SVM.pk`
- [SGD model](https://github.com/hammad-asif/tenderml/blob/main/models/SGDWarm89.7.pk): classifier artifact · Serialized warm-start linear classifier artifact. · `models/SGDWarm89.7.pk`
- [CRF](https://github.com/hammad-asif/tenderml/blob/main/models/crf.pk): sequence artifact · Serialized sequence model for structured label extraction. · `models/crf.pk`
- [Vectorizer](https://github.com/hammad-asif/tenderml/blob/main/models/vectorizer.pk): feature artifact · Feature encoder used to transform text into model inputs. · `models/vectorizer.pk`
- [Vectorizer model](https://github.com/hammad-asif/tenderml/blob/main/models/vectorizerModel.pk): feature artifact · Additional persisted vectorization or embedding artifact. · `models/vectorizerModel.pk`

## Connections

- Tender text → Main flow: feeds · Already extracted document text enters the runtime.
- Main flow → Tokenizer: normalizes · Main flow sends text into preprocessing.
- Tokenizer → Model layer: features · Normalized tokens are converted into model inputs.
- Model layer → SVM: loads · Runtime classifier artifact.
- Model layer → SGD model: loads · Alternative linear classifier artifact.
- Model layer → CRF: loads · Sequence labeling artifact.
- Model layer → Vectorizer: uses · Feature encoding artifact.
- Model layer → Vectorizer model: uses · Additional vectorization artifact.
- Model layer → Tree model: builds · Predictions are organized into a hierarchical document tree.
- Tree model → Tree cleanup: refines · Tree is cleaned and corrected before search.
- Tree cleanup → DFS search: prepares · Refined tree is traversed for target discovery.
- DFS search → Structured output: extracts · Traversal resolves final structured fields.
- CSV prep → Train and eval: prepares · Tabular training data flows into offline fitting.
- Sequence builder → Train and eval: prepares · Sequence examples feed training and evaluation.
- Train and eval → SVM: serializes · Offline training produces classifier artifacts.
- Train and eval → SGD model: serializes · Offline training produces classifier artifacts.
- Train and eval → CRF: serializes · Offline training produces sequence artifacts.
- Train and eval → Vectorizer: serializes · Offline training produces feature encoders.
- Train and eval → Vectorizer model: serializes · Offline training produces additional vectorization artifacts.
