The system developed in project implements a complete, modular pipeline for automated document classification using multiple German BERT-based models. The architecture covers all major components of a contemporary machine learning workflow:
- Data preprocessing and tokenization
- Fine-tuning and evaluation of Transformer-based language models
- Hyperparameter optimization (HPO)
- Workflow automation using Metaflow
- OCR-based document ingestion and preprocessing
- A unified inference engine for various document types
- A FastAPI-based web service for real-time predictions
The system supports both text-based documents and image-based or scanned documents (e.g., PDF scans). All architectural elements are designed to be reproducible, configurable, and extensible for research purposes.
The overall architecture consists of several loosely coupled and independently testable modules. Figure A.1 provides a high-level representation of the data and processing flow:
+------------------------------------------------------------+
| FastAPI Web Interface |
+-------------------------+----------------------------------+
|
v
+------------------------------------------------------------+
| Inference Layer |
| (OCR → Preprocessing → Tokenization → BERT Model → CLS) |
+------------------------------------------------------------+
|
v
+------------------------------------------------------------+
| Training & Evaluation Layer |
| (HF Trainer, metrics, early stop) |
+------------------------------------------------------------+
|
v
+------------------------------------------------------------+
| Hyperparameter Optimization |
| (Optuna Search) |
+------------------------------------------------------------+
|
v
+------------------------------------------------------------+
| Workflow Orchestration (Metaflow) |
+------------------------------------------------------------+
|
v
+------------------------------------------------------------+
| Data Layer (CSV input) |
+------------------------------------------------------------+
Figure A.1: High-level architectural overview of the implemented system.
Each layer exposes clearly defined interfaces and can be executed independently (training, HPO, inference, pipelines, etc.). This separation of concerns improves the reproducibility and maintainability of the system.
Module: app/core/prepare_data.py
Responsibilities:
- CSV ingestion with schema validation
- Conversion into HuggingFace
DatasetDict - Stratified splitting into train/validation/test sets
- Label encoding using
LabelEncoder - Dataset tokenization using a user-selected Transformer tokenizer
- Batch-size aware mapping for efficient preprocessing
- Removal of non-model features
This design ensures consistency between training, evaluation, and inference.
Module: app/core/train.py
The training subsystem implements:
- Automatic device detection (CUDA/MPS/CPU)
- Dynamic batch size selection based on available hardware
- Mixed-precision training (FP16 when supported)
- Configurable dropout, weight decay, warmup steps
- Gradient accumulation for memory-constrained environments
- Early stopping
- Best-model checkpointing
- Unified metric computation (accuracy, precision, recall, F1)
All training parameters are stored in a training_config.json file to ensure full experiment reproducibility.
Module: app/core/evaluate.py
The evaluation pipeline:
- Reloads model, tokenizer, and label encoder
- Recreates tokenized dataset for consistency
- Computes accuracy and class-level metrics
- Aggregates metrics into a structured dictionary
- Ensures reproducible test results (fixed random seed)
Module: app/hyperparamsearch.py
Hyperparameter optimization is based on Optuna. The implementation includes:
-
Search spaces for:
- learning rate
- dropout
- weight decay
- batch size
-
Trial directories for experiment isolation
-
Automatic cleanup policy (retain top-N trials only)
-
Structured export of metrics to:
- CSV
- JSON
-
Generation of a global HPO leaderboard across models
This subsystem ensures efficient exploration of model configurations while minimizing disk overhead.
Module: app/flow.py
The system incorporates an automated training pipeline using Metaflow, featuring:
- Parallel model training via
foreachbranches - Automatic aggregation of results
- Fault isolation between models
- A reproducible end-to-end experiment workflow
Pipeline flow structure:
start → train_each_model (foreach) → join → end
Module: app/core/predict.py
The inference engine provides a unified abstraction for document classification. It supports multiple input formats:
| File Type | Extraction Method |
|---|---|
| PDF (text-based) | PyMuPDF |
| PDF (scanned) | OCR fallback via Tesseract |
| Images (JPG/PNG/TIFF) | OCR |
| DOCX | python-docx |
| TXT | UTF-8 file reader |
Processing pipeline:
- MIME-based file type detection
- Text extraction (PDF/OCR/DOCX/Text)
- Tokenization
- BERT inference
- Softmax + label decoding
The module loads the pretrained model and the corresponding label encoder to produce final predictions with confidence scores.
Module: app/api/api.py
The web service exposes the following endpoints:
-
POST /predict- Accepts text or file uploads
- Executes the complete inference pipeline
- Returns JSON with label and confidence
-
GET /- Serves the frontend interface
-
app/static/*- Hosts all frontend assets
Additional features:
- CORS (Cross-Origin Resource Sharing)
- Asynchronous file handling
- Temporary storage with cleanup mechanisms
To ensure repeatable results, the system provides:
-
Fixed random seeds in all data splits
-
Serialization of:
- model weights
- tokenizer
- label encoder (
label_classes.npy) - training configuration
- HPO outcomes
-
Deterministic train/val/test partitioning
-
Explicit versioning of model directories
-
Device-dependent batch size logging
The developed system integrates:
- End-to-end ML pipeline engineering
- Modular and reproducible architecture
- Multi-model benchmarking
- OCR-enhanced document ingestion
- Real-time inference API
- Hyperparameter optimization and workflow automation
This appendix provides the technical foundation for interpreting and reproducing the experimental results presented in the main chapters of the project.