# toriving/text-classification-transformers architecture

> Architecture diagram of the GitHub repository toriving/text-classification-transformers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/toriving/text-classification-transformers
- Repository: https://github.com/toriving/text-classification-transformers

## Overview

##### Project Type and Purpose
- This is a CLI‐driven NLP pipeline for sequence classification.
- It leverages Huggingface Transformers models to train, evaluate, and predict on text classification datasets (e.g., NSMC, KoNLI, SST2, SST5).
- Key features: data preprocessing, dataset loading, model instantiation, training/evaluation loops, and experiment automation via shell scripts.

##### Main Architectural Components
a. User & Shell Scripts Layer
– Entry point for experiments (sh/*/*.sh).
– Shell scripts invoke the Python CLI with specific arguments (model choice, data path, hyperparameters).
b. CLI & Argument Parsing
– arguments.py: Defines all CLI parameters.
– main.py: Reads arguments, orchestrates the pipeline (train/eval/predict).
c. Data Layer
– data_in/: Raw and preprocessed CSV/TSV datasets.
– utils/*_preprocess.py: Converts original TSV/text files into train.csv, dev.csv, test.csv.
– datasets.py: Implements a Dataset class that wraps torch Dataset and DataLoader.
d. Model Layer
– modeling.py: Defines how to load and configure pretrained transformers via AutoModelForSequenceClassification.
– Integrates tokenization and model configuration (num_labels, max_seq_length).
e. Runner / Trainer Layer
– runner.py: Implements training, evaluation, and prediction loops using Huggingface’s Trainer or custom loops.
– Manages logging (TensorBoard), checkpoints, and metrics.
f. External Dependencies
– Huggingface Transformers library.
– PyTorch, TensorBoard for visualization.
g. Outputs & Artifacts
– output_dir/: Model checkpoints, logs, test predictions.
– cache_dir/: Downloaded transformer weights and tokenizers.

##### Relationships & Data Flow
1. User runs a shell script → invokes `python main.py` with flags
2. main.py loads & validates CLI args (arguments.py)
3. main.py calls preprocessing utilities (if needed) to ensure train/dev/test CSVs in data_in/
4. datasets.py reads CSVs → builds PyTorch DataLoaders
5. modeling.py initializes the Transformer model (AutoModelForSequenceClassification) + tokenizer
6. runner.py trains the model, evaluates on dev set, and/or makes predictions on test set
7. Checkpoints and logs are written to output_dir; metrics to TensorBoard
8. Optionally, model_name_or_path points to existing checkpoints for reload/predict

##### Architectural Patterns & Design Principles
- Pipeline pattern: clear separation of concerns (preprocess → dataset → model → runner).
- Modularity: each layer (utils, data, modeling, runner) is self‐contained.
- Config-driven: all hyperparameters and paths are injected via CLI.
- Reusability: shell scripts standardize experiments; adding a new dataset/model requires minimal changes.

##### Must-Include Elements
- Annotations for the most important CLI args (e.g., `--model`, `--task_name`, `--output_dir`).

## Diagram (Mermaid)

```mermaid
graph TD
    %% User & Shell Scripts Layer
    subgraph "User & Shell Scripts Layer"
        Shell[/Shell Scripts/]:::entry
    end

    %% CLI & Argument Parsing
    subgraph "CLI & Argument Parsing"
        Arguments["arguments.py"]:::entry
        Main["main.py"]:::entry
    end

    %% Data Layer
    subgraph "Data Layer"
        DataIn[(data_in/)]:::data
        subgraph "Preprocessing Utilities"
            Kornli["kornli_preprocess.py"]:::data
            NSMC["nsmc_preprocess.py"]:::data
            SST["sst_preprocess.py"]:::data
            Utils["utils.py"]:::data
        end
        Datasets["datasets.py"]:::data
    end

    %% Model Layer
    subgraph "Model Layer"
        Modeling["modeling.py"]:::model
    end

    %% Runner/Trainer Layer
    subgraph "Runner/Trainer Layer"
        Runner["runner.py"]:::runner
        OutputDir[(output_dir/)]:::runner
        CacheDir[(cache_dir/)]:::runner
        TensorBoard[(TensorBoard Logs)]:::external
    end

    %% External Dependencies
    subgraph "External Dependencies"
        HF[("Huggingface Transformers")]:::external
        Torch[("PyTorch")]:::external
    end

    %% Data Flow & Control Flow
    Shell -->|"invokes python main.py"| Main
    Main -->|"loads CLI args"| Arguments
    Main -->|"preprocess if needed"| Kornli
    Main -->|"preprocess if needed"| NSMC
    Main -->|"preprocess if needed"| SST
    Main -->|"preprocess if needed"| Utils
    Kornli -->|"generate CSVs"| DataIn
    NSMC -->|"generate CSVs"| DataIn
    SST -->|"generate CSVs"| DataIn
    Utils -->|"helper functions"| DataIn

    DataIn -->|"reads CSVs"| Datasets
    Datasets -->|"DataLoader"| Runner

    Main -->|"initialize model"| Modeling
    HF -->|"provides pretrained models"| Modeling
    Modeling -->|"Model + Tokenizer"| Runner
    Modeling -->|"cache weights"| CacheDir

    Torch -->|"provides tensors & training APIs"| Runner
    Runner -->|"write checkpoints & predictions"| OutputDir
    Runner -->|"write metrics"| TensorBoard

    %% Click Events
    click Shell "https://github.com/toriving/text-classification-transformers/tree/master/sh/"
    click Arguments "https://github.com/toriving/text-classification-transformers/blob/master/arguments.py"
    click Main "https://github.com/toriving/text-classification-transformers/blob/master/main.py"
    click DataIn "https://github.com/toriving/text-classification-transformers/tree/master/data_in/"
    click Datasets "https://github.com/toriving/text-classification-transformers/blob/master/datasets.py"
    click Kornli "https://github.com/toriving/text-classification-transformers/blob/master/utils/kornli_preprocess.py"
    click NSMC "https://github.com/toriving/text-classification-transformers/blob/master/utils/nsmc_preprocess.py"
    click SST "https://github.com/toriving/text-classification-transformers/blob/master/utils/sst_preprocess.py"
    click Utils "https://github.com/toriving/text-classification-transformers/blob/master/utils/utils.py"
    click Modeling "https://github.com/toriving/text-classification-transformers/blob/master/modeling.py"
    click Runner "https://github.com/toriving/text-classification-transformers/blob/master/runner.py"

    %% Styles
    classDef entry fill:#FFD966,stroke:#333
    classDef data fill:#A9D0F5,stroke:#333
    classDef model fill:#C6E0B4,stroke:#333
    classDef runner fill:#F4B183,stroke:#333
    classDef external fill:#D9D9D9,stroke:#333
```
