# slowy07/luna architecture

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

- Interactive diagram: https://gitdiagram.com/slowy07/luna
- Repository: https://github.com/slowy07/luna

## Overview

##### Project type and purpose
- This is a Python-based open-source tool and library (“luna”) that implements Stable Diffusion in TensorFlow for text-to-image, image-to-image (img2img), and web-based gradient visualization (webgrad) use cases.
- It provides both CLI scripts (text2image.py, img2img.py, webgrad.py) and a pip-installable package (`stable_diffusion_tensorflow`) exposing a `StableDiffusion` class for programmatic use.

##### File structure
- Top-level scripts:
- text2image.py – CLI entry point for text→image generation
- img2img.py – CLI entry for image→image translation
- webgrad.py – CLI for visualizing gradients or web demo
- Main package: `stable_diffusion_tensorflow/`
- clip_tokenizer/ – BPE vocabulary and tokenizer
- clip_encoder.py – CLIP model to embed text (and optionally images)
- autoencoder_kl.py – VAE encoder/decoder for latent↔image conversion
- diffusion_model.py – U-Net diffusion model implementation
- stable_diffusion.py – Orchestration class (`StableDiffusion.generate()`)
- layers.py, constants.py, util/ – supporting layers, config constants, I/O utilities

##### Key insights from README
- The library targets TensorFlow, can run on Colab (CPU/GPU or M1)
- Exposes both CLI and Python API: instantiate `StableDiffusion`, call `.generate()`
- Supports adjusting image size, guidance scale, temperature, and batch size

##### Main components
1. **CLI / Notebook Interface**
- `text2image.py`, `img2img.py`, `webgrad.py` (scripts or notebook)
2. **StableDiffusion Orchestrator** (`stable_diffusion.py`)
- Public API layer that parses flags or function args
3. **Tokenizer** (`clip_tokenizer/`)
- Byte-pair encoding + lookup → token IDs
4. **CLIP Encoder** (`clip_encoder.py`)
- Converts token IDs → text embeddings
5. **Diffusion Model** (`diffusion_model.py`)
- U-Net architecture performing iterative denoising in latent space
6. **Autoencoder KL (VAE)** (`autoencoder_kl.py`)
- Encoder: image→latent (for img2img)
- Decoder: latent→image (final image output)
7. **Utilities & Constants** (`layers.py`, `constants.py`, `util/read.py`)
8. **External Dependencies**
- TensorFlow, NumPy, PIL, other pip packages from requirements.txt

##### Component interactions and data flow
- **Text2Image path**:
CLI → Orchestrator → Tokenizer → CLIP Encoder → Text Embeddings →
Diffusion Model (iterative denoise) → Latent → VAE Decoder → PNG/JPEG → disk
- **Img2Img path**:
CLI → Orchestrator → VAE Encoder (image→latent) → add noise → Diffusion Model → VAE Decoder → output
- **WebGrad path**:
CLI → Orchestrator → partial pipeline with gradient hooks → visualization server

##### Architectural patterns & design principles
- **Modular pipeline**: each stage (tokenizer, encoder, diffusion, decoder) is a separate module/class
- **Single entry point**: `StableDiffusion` class orchestrates the full pipeline
- **Separation of concerns**: CLI scripts only handle I/O and parameter parsing, not model internals
- **Extensibility**: user can import the library and customize parameters (image size, steps, guidance scale)

##### Final advice
- Essential TensorFlow-based components.
- Don’t overcomplicate: it’s a linear pipeline, not a distributed system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Usage Interfaces"
        direction TB
        CLI1["text2image.py"]:::io
        CLI2["img2img.py"]:::io
        CLI3["webgrad.py"]:::io
    end

    CLI1 --> Orchestrator
    CLI2 --> Orchestrator
    CLI3 --> Orchestrator

    Orchestrator["StableDiffusion\n(stable_diffusion.py)"]:::io

    Orchestrator -->|"prompt"| Tokenizer
    Tokenizer["Tokenizer\n(BPE)"]:::pre
    Tokenizer -->|"token IDs"| CLIP
    CLIP["CLIP Encoder"]:::pre
    CLIP -->|"text embeddings"| Diffusion

    Orchestrator -->|"input image"| VAEEnc
    VAEEnc["VAE Encoder"]:::core
    VAEEnc -->|"latent + noise"| Diffusion

    Diffusion["Diffusion Model\n(U-Net)"]:::core
    Diffusion -->|"latent"| VAEDec
    VAEDec["VAE Decoder"]:::core
    VAEDec -->|"image file"| Output

    Orchestrator -->|"gradient hooks"| WebServer
    WebServer["WebGrad Server"]:::io
    WebServer -->|"visualization"| Output

    Output["Image Output\n(PNG/JPEG)"]:::io

    subgraph "Utilities & Constants"
        direction TB
        LAYERS["layers.py"]:::util
        CONST["constants.py"]:::util
        READ["util/read.py"]:::util
    end

    Orchestrator -.-> LAYERS
    Diffusion -.-> CONST
    Tokenizer -.-> READ

    subgraph "External Dependencies"
        direction TB
        External["TensorFlow, NumPy,\nPIL, etc."]:::external
    end

    Orchestrator -.-> External
    Tokenizer -.-> External
    CLIP -.-> External
    Diffusion -.-> External
    VAEDec -.-> External

    classDef pre fill:#BBDEFB,stroke:#333,stroke-width:1px
    classDef core fill:#FFCCBC,stroke:#333,stroke-width:1px
    classDef io fill:#C8E6C9,stroke:#333,stroke-width:1px
    classDef util fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef external fill:#FFF9C4,stroke:#333,stroke-width:1px

    click CLI1 "https://github.com/slowy07/luna/blob/main/text2image.py"
    click CLI2 "https://github.com/slowy07/luna/blob/main/img2img.py"
    click CLI3 "https://github.com/slowy07/luna/blob/main/webgrad.py"
    click Orchestrator "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/stable_diffusion.py"
    click Tokenizer "https://github.com/slowy07/luna/tree/main/stable_diffusion_tensorflow/clip_tokenizer/"
    click CLIP "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/clip_encoder.py"
    click Diffusion "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/diffusion_model.py"
    click VAEEnc "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/autoencoder_kl.py"
    click VAEDec "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/autoencoder_kl.py"
    click LAYERS "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/layers.py"
    click CONST "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/constants.py"
    click READ "https://github.com/slowy07/luna/blob/main/stable_diffusion_tensorflow/util/read.py"
    click External "https://github.com/slowy07/luna/blob/main/requirements.txt"
```
