# ddalko/svgagent architecture

> Architecture diagram of the GitHub repository ddalko/svgagent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ddalko/svgagent
- Repository: https://github.com/ddalko/svgagent

## Overview

1. **What this repo is**
   SVGAgent is a pipeline for turning a raster image into an SVG by combining:
   - a local SVG generation model (OmniSVG),
   - a simple image decomposition step,
   - and a judge that decides whether a candidate SVG is good enough or should be refined.

2. **Top-level orchestration**
   The architectural center is `pipeline.py`, with CLI entrypoints in `run_pipeline.py` and `run_benchmark.py`. The pipeline is not a monolith: it is a controller that routes an input image through generation, evaluation, optional decomposition, recursive refinement, and final composition.

3. **Primary data flow**
   The main flow is:
   input image → full-image SVG generation → judge/accept-or-refine → optional decomposition into subregions → per-region generation and selection → recursive verification → SVG composition → final SVG and rendered artifacts.
   The pipeline is optimized for early exit when the first full-image SVG is judged acceptable.

4. **Generation subsystem**
   `agents/generate_agent.py` wraps the SVG-producing model. The README indicates it can call either:
   - `OmniSvgClient` for local in-process inference, or
   - a Gemini-based generation path if configured that way.
   The generated output is an SVG string, not just tokens or tensors, so generation is the boundary between ML inference and the rest of the pipeline.

5. **Local OmniSVG inference stack**
   The `omnisvg_inference/` directory is a vendored model runtime. It contains tokenization, decoding, SVG libraries, transformer/model code, and supporting utilities from OmniSVG/DeepSVG. This is the low-level ML subsystem that converts model outputs into SVG syntax and supports local GPU inference via PyTorch/CUDA.

6. **Decomposition subsystem**
   `agents/decompose_agent.py` splits an input image into smaller regions using Otsu thresholding, connected components, and transparency-aware filtering. This creates leaf regions that can be handled independently. Architecturally, this is the “branching” point that turns one difficult image into multiple smaller generation tasks.

7. **Judging subsystem**
   There are two judge implementations:
   - `agents/judge_agent.py` uses Gemini VLM calls for semantic verification/picking the best candidate.
   - `agents/mse_judge.py` uses a local pixel-MSE comparison against the source image, avoiding API calls.
   The judge is the quality gate: it determines whether a candidate SVG is accepted, which candidate among several is best, and whether recursive refinement should continue.

8. **Composition subsystem**
   `compositor.py` implements the final merge of leaf SVGs using a painter’s-algorithm-style z-order concatenation under a single SVG root. This is important because the pipeline is not stitching pixels; it is assembling vector subtrees into one coherent SVG document.

9. **Supporting image/SVG utilities**
   The `utils/` package contains helper code for rendering SVGs, image handling, visualization, prompt templates, and client wrappers. These utilities sit at the boundary between model output and human-readable artifacts: they render SVGs to PNGs, create comparison grids, and support debugging/inspection.

10. **Configuration boundary**
    Runtime behavior is controlled by YAML config files:
    - `config/default.yaml` provides defaults,
    - `config.yaml.example` is the user-facing template.
    The key architectural toggle is the judge backend (`mse` vs `gemini`), which changes whether the pipeline stays local or depends on an external VLM API.

11. **Benchmarking and dataset integration**
    `scripts/extract_mmsvgbench.py` converts the MMSVGBench parquet into images plus a manifest. `run_benchmark.py` then runs the pipeline across the benchmark and emits per-sample outputs, summary JSON, and an HTML gallery. This is a separate evaluation path from single-image inference, but it uses the same core pipeline.

12. **Visualization and observability**
    Files like `visualize_decompose.py`, `utils/decompose_viz.py`, `utils/visualizer.py`, and the `runs/` artifacts described in the README indicate a strong inspection layer. The repository is designed to save intermediate images, SVGs, rendered PNGs, and comparison grids so the decomposition and generation decisions can be audited.

13. **Main architectural boundary**
    The key boundary in this repo is between:
    - **model-facing code**: OmniSVG inference, Gemini calls, tokenization/decoding,
    - and **pipeline/control code**: decomposition, judging, composition, benchmark orchestration.
    That separation makes it possible to swap judge backends or generation backends without changing the overall control flow.

14. **Expected deployment/runtime profile**
    The repo is primarily a Python inference/evaluation system, with one path requiring local GPU-backed PyTorch and another optionally depending on external Gemini API access. It is not a web service; its natural runtime is CLI-based batch evaluation or single-image processing with saved artifacts.

Main entry points: [`pipeline.py`](https://github.com/ddalko/svgagent/blob/main/pipeline.py) (Pipeline), [`agents/generate_agent.py`](https://github.com/ddalko/svgagent/blob/main/agents/generate_agent.py) (Generate), [`omnisvg_inference/decoder.py`](https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/decoder.py) (Decoder), [`utils/omnisvg_client.py`](https://github.com/ddalko/svgagent/blob/main/utils/omnisvg_client.py) (OmniSVG Client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Orchestration"]
  node_pipeline["Pipeline<br/>controller<br/>[pipeline.py]"]
  node_run_pipeline["Run Pipe<br/>cli<br/>[run_pipeline.py]"]
  node_run_benchmark["Run Bench<br/>cli<br/>[run_benchmark.py]"]
  node_compositor["Compositor<br/>svg merge<br/>[compositor.py]"]
end

subgraph group_agents["Agents"]
  node_generate_agent["Generate<br/>agent<br/>[generate_agent.py]"]
  node_decompose_agent["Decompose<br/>agent<br/>[decompose_agent.py]"]
  node_judge_agent["Judge<br/>agent<br/>[judge_agent.py]"]
  node_mse_judge["MSE Judge<br/>[mse_judge.py]"]
end

subgraph group_inference["OmniSVG Runtime"]
  node_tokenizer["Tokenizer<br/>tokenization<br/>[tokenizer.py]"]
  node_decoder["Decoder<br/>svg decode<br/>[decoder.py]"]
  node_model["Transformer<br/>svg model<br/>[model.py]"]
  node_svglib["SVG Lib<br/>geometry/svg<br/>[svg.py]"]
end

subgraph group_utils["Utilities"]
  node_omnisvg_client["OmniSVG Client<br/>runtime client<br/>[omnisvg_client.py]"]
  node_gemini_client["Gemini Client<br/>api client<br/>[gemini_client.py]"]
  node_svg_renderer["SVG Renderer<br/>rendering<br/>[svg_renderer.py]"]
  node_image_utils["Image Utils<br/>image ops<br/>[image_utils.py]"]
  node_visualizer["Visualizer<br/>inspection<br/>[visualizer.py]"]
  node_decompose_viz["Decompose Viz<br/>inspection<br/>[decompose_viz.py]"]
end

subgraph group_benchmarking["Benchmarking"]
  node_extract_mmsvgbench["Bench Extract<br/>dataset prep"]
  node_visualize_decompose["Viz Decompose<br/>cli"]
end

node_run_pipeline -->|"invokes"| node_pipeline
node_run_benchmark -->|"drives"| node_pipeline
node_run_benchmark -.->|"prepares"| node_extract_mmsvgbench
node_pipeline -->|"generate"| node_generate_agent
node_pipeline -->|"judge"| node_judge_agent
node_pipeline -.->|"judge"| node_mse_judge
node_pipeline -->|"decompose"| node_decompose_agent
node_pipeline -->|"compose"| node_compositor
node_pipeline -.->|"render"| node_svg_renderer
node_pipeline -.->|"inspect"| node_visualizer
node_pipeline -.->|"inspect"| node_decompose_viz
node_generate_agent -->|"local path"| node_omnisvg_client
node_generate_agent -.->|"alt path"| node_gemini_client
node_omnisvg_client -->|"uses"| node_tokenizer
node_omnisvg_client -->|"decodes"| node_decoder
node_decoder -->|"runs on"| node_model
node_decoder -->|"builds"| node_svglib
node_judge_agent -->|"calls"| node_gemini_client
node_mse_judge -->|"compares"| node_image_utils
node_decompose_agent -->|"segments"| node_image_utils
node_compositor -->|"merges"| node_svglib
node_visualizer -.->|"renders"| node_svg_renderer
node_visualizer -.->|"formats"| node_image_utils
node_decompose_viz -.->|"renders"| node_svg_renderer
node_visualize_decompose -->|"uses"| node_decompose_agent
node_visualize_decompose -->|"shows"| node_decompose_viz

click node_pipeline "https://github.com/ddalko/svgagent/blob/main/pipeline.py"
click node_run_pipeline "https://github.com/ddalko/svgagent/blob/main/run_pipeline.py"
click node_run_benchmark "https://github.com/ddalko/svgagent/blob/main/run_benchmark.py"
click node_generate_agent "https://github.com/ddalko/svgagent/blob/main/agents/generate_agent.py"
click node_decompose_agent "https://github.com/ddalko/svgagent/blob/main/agents/decompose_agent.py"
click node_judge_agent "https://github.com/ddalko/svgagent/blob/main/agents/judge_agent.py"
click node_mse_judge "https://github.com/ddalko/svgagent/blob/main/agents/mse_judge.py"
click node_compositor "https://github.com/ddalko/svgagent/blob/main/compositor.py"
click node_omnisvg_client "https://github.com/ddalko/svgagent/blob/main/utils/omnisvg_client.py"
click node_gemini_client "https://github.com/ddalko/svgagent/blob/main/utils/gemini_client.py"
click node_svg_renderer "https://github.com/ddalko/svgagent/blob/main/utils/svg_renderer.py"
click node_image_utils "https://github.com/ddalko/svgagent/blob/main/utils/image_utils.py"
click node_visualizer "https://github.com/ddalko/svgagent/blob/main/utils/visualizer.py"
click node_decompose_viz "https://github.com/ddalko/svgagent/blob/main/utils/decompose_viz.py"
click node_tokenizer "https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/tokenizer.py"
click node_decoder "https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/decoder.py"
click node_model "https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/deepsvg/model/model.py"
click node_svglib "https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/deepsvg/svglib/svg.py"
click node_extract_mmsvgbench "https://github.com/ddalko/svgagent/blob/main/scripts/extract_mmsvgbench.py"
click node_visualize_decompose "https://github.com/ddalko/svgagent/blob/main/visualize_decompose.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pipeline,node_run_pipeline,node_run_benchmark,node_compositor toneBlue
class node_generate_agent,node_decompose_agent,node_judge_agent,node_mse_judge toneAmber
class node_tokenizer,node_decoder,node_model,node_svglib toneMint
class node_omnisvg_client,node_gemini_client,node_svg_renderer,node_image_utils,node_visualizer,node_decompose_viz toneRose
class node_extract_mmsvgbench,node_visualize_decompose toneIndigo
```

## Components

### Orchestration

CLI entrypoints and the central pipeline controller.

- [Pipeline](https://github.com/ddalko/svgagent/blob/main/pipeline.py): controller · Main control flow for generation, judge/accept-or-refine, decomposition, recursion, and composition. · `pipeline.py`
- [Run Pipe](https://github.com/ddalko/svgagent/blob/main/run_pipeline.py): cli · Single-image CLI entrypoint. · `run_pipeline.py`
- [Run Bench](https://github.com/ddalko/svgagent/blob/main/run_benchmark.py): cli · Benchmark CLI entrypoint that drives batch evaluation. · `run_benchmark.py`
- [Compositor](https://github.com/ddalko/svgagent/blob/main/compositor.py): svg merge · Merges leaf SVGs into one document using z-order composition. · `compositor.py`

### Agents

Generation, decomposition, and judging subsystems.

- [Generate](https://github.com/ddalko/svgagent/blob/main/agents/generate_agent.py): agent · Wraps SVG generation backends and returns SVG strings. · `agents/generate_agent.py`
- [Decompose](https://github.com/ddalko/svgagent/blob/main/agents/decompose_agent.py): agent · Splits images into regions via thresholding and connected components. · `agents/decompose_agent.py`
- [Judge](https://github.com/ddalko/svgagent/blob/main/agents/judge_agent.py): agent · External VLM-based semantic judging and candidate selection. · `agents/judge_agent.py`
- [MSE Judge](https://github.com/ddalko/svgagent/blob/main/agents/mse_judge.py): judge · Local pixel-distance judge for candidate comparison. · `agents/mse_judge.py`

### OmniSVG Runtime

Vendored local SVG model stack and token-to-SVG machinery.

- [Tokenizer](https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/tokenizer.py): tokenization · Converts SVG/model symbols for the OmniSVG runtime. · `omnisvg_inference/tokenizer.py`
- [Decoder](https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/decoder.py): svg decode · Decodes model outputs into SVG structure. · `omnisvg_inference/decoder.py`
- [Transformer](https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/deepsvg/model/model.py): svg model · Core OmniSVG/DeepSVG model implementation. · `omnisvg_inference/deepsvg/model/model.py`
- [SVG Lib](https://github.com/ddalko/svgagent/blob/main/omnisvg_inference/deepsvg/svglib/svg.py): geometry/svg · Low-level SVG primitives and path utilities. · `omnisvg_inference/deepsvg/svglib/svg.py`

### Utilities

Rendering, image handling, clients, and inspection helpers.

- [OmniSVG Client](https://github.com/ddalko/svgagent/blob/main/utils/omnisvg_client.py): runtime client · Local inference wrapper for the vendored OmniSVG stack. · `utils/omnisvg_client.py`
- [Gemini Client](https://github.com/ddalko/svgagent/blob/main/utils/gemini_client.py): api client · External VLM client used for generation or judging when configured. · `utils/gemini_client.py`
- [SVG Renderer](https://github.com/ddalko/svgagent/blob/main/utils/svg_renderer.py): rendering · Renders SVG artifacts for inspection and comparison. · `utils/svg_renderer.py`
- [Image Utils](https://github.com/ddalko/svgagent/blob/main/utils/image_utils.py): image ops · Image loading, conversion, and comparison helpers. · `utils/image_utils.py`
- [Visualizer](https://github.com/ddalko/svgagent/blob/main/utils/visualizer.py): inspection · Builds comparison grids and debug visuals. · `utils/visualizer.py`
- [Decompose Viz](https://github.com/ddalko/svgagent/blob/main/utils/decompose_viz.py): inspection · Visualization helpers for decomposition output. · `utils/decompose_viz.py`

### Benchmarking

Benchmark ingestion and evaluation workflows.

- [Bench Extract](https://github.com/ddalko/svgagent/blob/main/scripts/extract_mmsvgbench.py): dataset prep · Converts MMSVGBench parquet into images and a manifest. · `scripts/extract_mmsvgbench.py`
- [Viz Decompose](https://github.com/ddalko/svgagent/blob/main/visualize_decompose.py): cli · Standalone decomposition visualization entrypoint. · `visualize_decompose.py`

## Connections

- Run Pipe → Pipeline: invokes · CLI enters the main controller.
- Run Bench → Pipeline: drives · Batch evaluation uses the same core pipeline.
- Run Bench → Bench Extract: prepares · Benchmark flow relies on dataset extraction and manifests.
- Pipeline → Generate: generate · Creates an initial full-image SVG candidate.
- Pipeline → Judge: judge · Uses semantic judging to accept or refine candidates.
- Pipeline → MSE Judge: judge · Optional local quality gate for candidate comparison.
- Pipeline → Decompose: decompose · Branches into region-level subtasks when needed.
- Pipeline → Compositor: compose · Assembles accepted leaf SVGs into final output.
- Pipeline → SVG Renderer: render · Produces rendered artifacts for inspection.
- Pipeline → Visualizer: inspect · Generates comparison grids and debug outputs.
- Pipeline → Decompose Viz: inspect · Supports decomposition auditing.
- Generate → OmniSVG Client: local path · Primary local SVG generation backend.
- Generate → Gemini Client: alt path · Optional external generation backend.
- OmniSVG Client → Tokenizer: uses · Tokenization is part of local model inference.
- OmniSVG Client → Decoder: decodes · Converts model outputs into SVG syntax.
- Decoder → Transformer: runs on · Decoding depends on the underlying OmniSVG transformer model.
- Decoder → SVG Lib: builds · Uses SVG primitives and path utilities to form output.
- Judge → Gemini Client: calls · Judge semantics are handled through Gemini VLM calls.
- MSE Judge → Image Utils: compares · Uses image conversion/comparison helpers for pixel scoring.
- Decompose → Image Utils: segments · Uses image preprocessing and region extraction utilities.
- Compositor → SVG Lib: merges · Composes leaf SVG structures under a single root.
- Visualizer → SVG Renderer: renders · Builds visual comparisons from rendered SVGs.
- Visualizer → Image Utils: formats · Prepares image grids and side-by-side views.
- Decompose Viz → SVG Renderer: renders · Shows decomposition results as viewable artifacts.
- Viz Decompose → Decompose: uses · Standalone decomposition visualization workflow.
- Viz Decompose → Decompose Viz: shows · Generates inspection outputs for decomposition.
