# electricalexis/notagen architecture

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

- Interactive diagram: https://gitdiagram.com/electricalexis/notagen
- Repository: https://github.com/electricalexis/notagen

## Overview

##### Project Type and Purpose
- **Type**: A multi-stage machine-learning pipeline for symbolic music generation built around a large language model (LLM).
- **Purpose**: Train, fine-tune, and optimize “NotaGen” to generate high-quality classical sheet music, and provide data-conversion utilities and a Gradio demo for inference.

##### File Structure & Architectural Patterns
- Top-level directories represent distinct pipeline stages or utilities:
- data/       → Data conversion (MusicXML ↔ ABC), indexing
- pretrain/   → Unsupervised pre-training on large corpus
- finetune/   → Supervised fine-tuning with composer/style prompts
- RL/         → Reinforcement Learning via CLaMP-DPO
- clamp2/     → External feature extractor (CLaMP 2)
- inference/  → Generating new samples from trained weights
- gradio/     → Local Gradio UI for demo & inference
- notebook/   → Jupyter prototype & visualization
- **Architectural Pattern**: Linear, staged pipeline with clear separation of concerns; each stage reads model weights and data indexes, produces updated weights or artifacts.

##### Key Insights from README
- Three-stage training: Pre-train → Fine-tune → RL.
- Data preprocessing/postprocessing for ABC notation and MusicXML.
- Dependency on external CLaMP-2 feature extractor (clamp2/).
- Optional Gradio UI for local/online demos.

##### Components
a. Main Components
1. Data Ingestion & Conversion (data/)
2. Pre-training Pipeline (pretrain/)
3. Fine-tuning Pipeline (finetune/)
4. CLaMP-2 Feature Extraction Service (clamp2/)
5. Reinforcement Learning Pipeline (RL/)
6. Inference Service (inference/)
7. Gradio UI & Notebook (gradio/, notebook/)
8. Model Weights Store (weights on disk or HuggingFace)
b. Relationships & Interactions
- Raw music → data/ converts to ABC/jsonl → pretrain uses this to train base LLM → outputs pretrain weights
- pretrain weights + jsonl → finetune → outputs finetuned weights
- finetuned weights → inference → generates samples (original & interleaved)
- generated samples + ground truth → clamp2 feature extractor → produces feature vectors
- features + preference rules → RL/data.py → builds preference dataset → RL/train.py → produces RL-optimized weights
- RL weights → inference → updated samples → loop for additional RL iterations
- Final weights → gradio/ & notebook/ for local demo & exploration
c. Architectural Patterns & Principles
- **Pipeline pattern**: Each stage consumes inputs (data, weights), produces outputs for next stage.
- **Modularity**: Separation of data processing, model training, feature extraction, and UI layers.
- **Reusability & Extensibility**: Config files in each module allow parameter tweaks; clamp2 and RL routines can be replaced or extended.
d. Technologies & Frameworks
- Python scripts, PyTorch (accelerate, optimum)
- JSONL for data indexing
- CLaMP-2 for feature extraction
- Gradio for UI
- HuggingFace for hosting model weights & Spaces

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Utilities
    subgraph "Data Ingestion & Conversion" 
        direction TB
        D1["1_batch_xml2abc.py"] 
        D2["2_data_preprocess.py"] 
        D3["3_batch_abc2xml.py"] 
        D4["abc2xml.py"] 
        D5["xml2abc.py"] 
        D6["README.md"] 
    end
    class D1,D2,D3,D4,D5,D6 dataBox

    %% Pre-training
    subgraph "Pre-training Module" 
        direction TB
        P1["config.py"] 
        P2["train-gen.py"] 
        P3["utils.py"] 
    end
    class P1,P2,P3 trainBox

    %% Fine-tuning
    subgraph "Fine-tuning Module" 
        direction TB
        F1["config.py"] 
        F2["train-gen.py"] 
        F3["utils.py"] 
    end
    class F1,F2,F3 trainBox

    %% Inference
    subgraph "Inference Service" 
        direction TB
        I1["config.py"] 
        I2["inference.py"] 
        I3["utils.py"] 
        subgraph Samples
            direction TB
            S1["Original Samples"] 
            S2["Interleaved Samples"] 
        end
    end
    class I1,I2,I3 trainBox
    class S1,S2 dataBox

    %% CLaMP-2 External
    subgraph "CLaMP-2 Feature Extractor" 
        direction TB
        C1["config.py"] 
        C2["extract_clamp2.py"] 
        C3["statistics.py"] 
        C4["utils.py"] 
    end
    class C1,C2,C3,C4 extBox

    %% Reinforcement Learning
    subgraph "Reinforcement Learning Pipeline" 
        direction TB
        R1["config.py"] 
        R2["data.py"] 
        R3["train.py"] 
        R4["utils.py"] 
    end
    class R1,R2,R3,R4 trainBox

    %% Demo & Notebook
    subgraph "Gradio UI" 
        direction TB
        G1["config.py"] 
        G2["demo.py"] 
        G3["inference.py"] 
        G4["abc2xml.py"] 
        G5["prompts.txt"] 
        G6["utils.py"] 
        G7["README.md"] 
    end
    class G1,G2,G3,G4,G5,G6,G7 uiBox

    subgraph "Jupyter Notebook" 
        direction TB
        N1["abc2xml.py"] 
        N2["demo.ipynb"] 
    end
    class N1,N2 uiBox

    %% Storage
    ModelWeights["Model Weights Store"] 

    %% Data Flow
    D1 -->|"outputs ABC/jsonl"| P1
    D2 --> P1
    P2 -->|"trains base LLM"| ModelWeights
    P2 --> F1
    F2 -->|"outputs fine-tuned weights"| ModelWeights
    F2 --> I1
    I2 --> S1
    I2 --> S2
    S1 --> C2
    S2 --> C2
    C2 --> R2
    R2 --> R3
    R3 -->|"outputs RL weights"| ModelWeights
    R3 --> I1
    I1 -->|"generates updated samples"| S1
    ModelWeights --> G1
    ModelWeights --> N1

    %% Click Events
    click D1 "https://github.com/electricalexis/notagen/blob/main/data/1_batch_xml2abc.py"
    click D2 "https://github.com/electricalexis/notagen/blob/main/data/2_data_preprocess.py"
    click D3 "https://github.com/electricalexis/notagen/blob/main/data/3_batch_abc2xml.py"
    click D4 "https://github.com/electricalexis/notagen/blob/main/data/abc2xml.py"
    click D5 "https://github.com/electricalexis/notagen/blob/main/data/xml2abc.py"
    click D6 "https://github.com/electricalexis/notagen/blob/main/data/README.md"
    click P1 "https://github.com/electricalexis/notagen/blob/main/pretrain/config.py"
    click P2 "https://github.com/electricalexis/notagen/blob/main/pretrain/train-gen.py"
    click P3 "https://github.com/electricalexis/notagen/blob/main/pretrain/utils.py"
    click F1 "https://github.com/electricalexis/notagen/blob/main/finetune/config.py"
    click F2 "https://github.com/electricalexis/notagen/blob/main/finetune/train-gen.py"
    click F3 "https://github.com/electricalexis/notagen/blob/main/finetune/utils.py"
    click C1 "https://github.com/electricalexis/notagen/blob/main/clamp2/config.py"
    click C2 "https://github.com/electricalexis/notagen/blob/main/clamp2/extract_clamp2.py"
    click C3 "https://github.com/electricalexis/notagen/blob/main/clamp2/statistics.py"
    click C4 "https://github.com/electricalexis/notagen/blob/main/clamp2/utils.py"
    click R1 "https://github.com/electricalexis/notagen/blob/main/RL/config.py"
    click R2 "https://github.com/electricalexis/notagen/blob/main/RL/data.py"
    click R3 "https://github.com/electricalexis/notagen/blob/main/RL/train.py"
    click R4 "https://github.com/electricalexis/notagen/blob/main/RL/utils.py"
    click I1 "https://github.com/electricalexis/notagen/blob/main/inference/config.py"
    click I2 "https://github.com/electricalexis/notagen/blob/main/inference/inference.py"
    click I3 "https://github.com/electricalexis/notagen/blob/main/inference/utils.py"
    click G1 "https://github.com/electricalexis/notagen/blob/main/gradio/config.py"
    click G2 "https://github.com/electricalexis/notagen/blob/main/gradio/demo.py"
    click G3 "https://github.com/electricalexis/notagen/blob/main/gradio/inference.py"
    click G4 "https://github.com/electricalexis/notagen/blob/main/gradio/abc2xml.py"
    click G5 "https://github.com/electricalexis/notagen/blob/main/gradio/prompts.txt"
    click G6 "https://github.com/electricalexis/notagen/blob/main/gradio/utils.py"
    click G7 "https://github.com/electricalexis/notagen/blob/main/gradio/README.md"
    click N1 "https://github.com/electricalexis/notagen/blob/main/notebook/abc2xml.py"
    click N2 "https://github.com/electricalexis/notagen/blob/main/notebook/demo.ipynb"

    %% Styles
    classDef dataBox fill:#D0E6FF,stroke:#3399FF,stroke-width:1px
    classDef trainBox fill:#DFFFD0,stroke:#339933,stroke-width:1px
    classDef extBox fill:#FFE5B4,stroke:#FF9933,stroke-width:1px
    classDef uiBox fill:#E5D0FF,stroke:#9933FF,stroke-width:1px
    classDef storageBox fill:#F0F0F0,stroke:#888888,stroke-width:1px
    class ModelWeights storageBox
```
