# kdjayakody/fooocus-ai-image-generator architecture

> Architecture diagram of the GitHub repository kdjayakody/fooocus-ai-image-generator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kdjayakody/fooocus-ai-image-generator
- Repository: https://github.com/kdjayakody/fooocus-ai-image-generator

## Overview

##### Main Components Architecture

The system should be represented with these key components:

a) Core Components:
- Web Interface Layer (Gradio-based)
- Pipeline Manager
- Model Management System
- File System Interface
- Configuration Manager

b) Processing Components:
- Prompt Processing Engine (GPT-2 based)
- Image Generation Pipeline
- Advanced Sampling System
- Model Loading/Unloading Manager
- Resource Management System

##### Key Relationships and Data Flow

**Web Interface → Pipeline Manager → Model Management**
- ↓
**Prompt Processing → Image Generation Pipeline → Output Handler**
- ↑
**Configuration Manager → Resource Management**

##### Detailed Component Breakdown

a) Model Management System:
- Checkpoint Loader
- LoRA Manager
- VAE Handler
- Controlnet Integration
- CLIP Vision Module

b) Pipeline Components:
- Base Model Pipeline
- Refiner Pipeline
- Inpainting System
- Upscaling Module
- Image Processing Units

##### Special Features Implementation

Specialized components for:
- Native Refiner Swap System
- Negative ADM Guidance
- Self-Attention Guidance
- Style Management System
- Advanced Sampling Controllers

##### Resource Management

Memory management system:
- VRAM Allocation
- CPU Memory Management
- Model Offloading System
- Cache Management

##### Directory Structure Integration

How the file structure maps to components:
- /modules → Core processing modules
- /ldm_patched → Model management
- /extras → Additional features
- /models → Model storage
- /javascript & /css → UI resources

##### Configuration Flow

Configuration system:
- Config File Parser
- Preset Manager
- Style System
- Model Path Handler

##### Resource optimization pathways

Key Technical Aspects to Highlight:
- Advanced k-diffusion sampling system
- Native refiner swap mechanism
- GPT-2 based prompt expansion
- Multiple model support architecture
- Resource optimization systems

This system design should reflect Fooocus's unique approach of combining Stable Diffusion's open-source nature with Midjourney's user-friendly approach, while highlighting its sophisticated technical implementations.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph UI
        WI[Web Interface]
        click WI "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/webui.py"
        JS[JavaScript Resources]
        click JS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/tree/main/javascript/"
        CSS[CSS Resources]
        click CSS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/tree/main/css/"
        WI --- JS & CSS
    end

    subgraph Core
        PM[Pipeline Manager]
        click PM "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/modules/default_pipeline.py"
        MMS[Model Management System]
        click MMS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/ldm_patched/modules/model_management.py"
        CM[Configuration Manager]
        click CM "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/modules/config.py"
        RMS[Resource Management System]
        click RMS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/modules/async_worker.py"
    end

    subgraph Processing
        PPE[Prompt Processing Engine]
        click PPE "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/tree/main/models/prompt_expansion/"
        IGP[Image Generation Pipeline]
        ASS[Advanced Sampling System]
        click ASS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/ldm_patched/k_diffusion/sampling.py"
    end

    subgraph Models
        subgraph ModelComponents
            CL[Checkpoint Loader]
            LM[LoRA Manager]
            click LM "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/ldm_patched/modules/lora.py"
            VH[VAE Handler]
            click VH "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/ldm_patched/ldm/models/autoencoder.py"
            CI[Controlnet Integration]
            click CI "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/tree/main/ldm_patched/controlnet/"
            CV[CLIP Vision Module]
            click CV "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/blob/main/ldm_patched/modules/clip_vision.py"
        end
    end

    subgraph Features
        SMS[Style Management System]
        click SMS "https://github.com/kdjayakody/Fooocus-AI-Image-Generator/tree/main/sdxl_styles/"
        NRS[Native Refiner Swap]
        NAG[Negative ADM Guidance]
        SAG[Self-Attention Guidance]
    end

    subgraph ResourceMgmt
        VRAM[VRAM Allocation]
        CPUMem[CPU Memory Management]
        MOS[Model Offloading System]
        Cache[Cache Management]
    end

    %% Main Flow
    WI --> PM
    PM --> MMS
    PM --> PPE
    PPE --> IGP
    IGP --> ASS
    MMS --> ModelComponents
    CM --> RMS

    %% Resource Management
    RMS --> ResourceMgmt
    ResourceMgmt --> MMS

    %% Feature Integration
    IGP --> Features
    Features --> ASS

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef core fill:#b3e0ff,stroke:#333,stroke-width:1px
    classDef processing fill:#98fb98,stroke:#333,stroke-width:1px
    classDef resource fill:#ffeb99,stroke:#333,stroke-width:1px
    classDef model fill:#dda0dd,stroke:#333,stroke-width:1px
    classDef feature fill:#ffa07a,stroke:#333,stroke-width:1px

    class WI,JS,CSS default
    class PM,MMS,CM,RMS core
    class PPE,IGP,ASS processing
    class VRAM,CPUMem,MOS,Cache resource
    class CL,LM,VH,CI,CV model
    class SMS,NRS,NAG,SAG feature
```
