How slowy07/luna works
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.