File size: 5,461 Bytes
d8ec882
 
 
 
 
 
 
600587b
35862c8
ee9722b
 
600587b
bda108d
600587b
 
 
35862c8
600587b
35862c8
600587b
bda108d
35862c8
 
600587b
35862c8
600587b
 
 
 
 
 
 
 
 
 
35862c8
600587b
 
 
 
 
 
35862c8
600587b
 
 
 
 
35862c8
600587b
 
 
 
 
 
35862c8
600587b
35862c8
600587b
 
35862c8
600587b
 
 
 
 
 
 
 
35862c8
600587b
 
35862c8
600587b
35862c8
600587b
35862c8
d321919
35862c8
600587b
35862c8
d321919
600587b
 
 
 
 
 
 
 
 
d321919
600587b
 
 
 
 
 
 
 
d321919
600587b
 
d321919
600587b
 
 
d321919
600587b
 
 
 
d321919
600587b
 
 
35862c8
600587b
35862c8
600587b
35862c8
600587b
35862c8
600587b
35862c8
600587b
 
35862c8
600587b
d321919
600587b
 
 
 
 
 
d321919
600587b
 
35862c8
 
 
600587b
35862c8
600587b
d321919
35862c8
 
600587b
35862c8
600587b
35862c8
d321919
 
 
35862c8
 
600587b
35862c8
d321919
35862c8
a8704d0
35862c8
 
600587b
35862c8
d321919
600587b
 
35862c8
 
 
 
600587b
35862c8
d321919
 
35862c8
 
600587b
35862c8
 
 
 
 
 
 
600587b
b2d52ea
600587b
b2d52ea
 
 
 
35862c8
600587b
d321919
600587b
d321919
b2d52ea
 
600587b
b2d52ea
600587b
 
b2d52ea
600587b
b2d52ea
600587b
 
b2d52ea
 
d321919
600587b
d321919
600587b
d321919
600587b
 
 
d321919
600587b
d321919
 
600587b
d321919
 
600587b
35862c8
d321919
35862c8
d321919
35862c8
 
d321919
35862c8
600587b
35862c8
600587b
 
35862c8
 
 
600587b
35862c8
600587b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
---
title: sd-image-gen-toolkit
app_file: src/sdgen/main.py
sdk: gradio
sdk_version: 6.0.2
---

# Stable Diffusion Image Generation Toolkit

![appdemo](https://drive.google.com/uc?export=view&id=1dO2bnYmEEj3fNU0-dV692icUPSwyP93G)

[**Live Demo**](https://huggingface.co/spaces/SanskarModi/sd-image-gen-toolkit)

---

## Overview

A modular, lightweight image generation toolkit built on **Hugging Face Diffusers**, designed for **CPU-friendly deployment**, clean architecture, and practical usability.

It supports **Text β†’ Image**, **Image β†’ Image**, and **Upscaling**, with a **preset system**, optional **LoRA adapters**, and a local **metadata history** for reproducibility.

---

## Features

### Text β†’ Image
- Stable Diffusion **1.5** and **Turbo**
- Configurable prompt parameters:
  - prompt / negative prompt
  - steps
  - guidance (CFG)
  - resolution
  - seed (optional)
- JSON metadata output
- Style presets for quick experimentation

### Image β†’ Image
- Modify existing images via the SD Img2Img pipeline
- Denoising strength control
- Full parameter configuration
- Shared preset system
- History saved for reproducibility

### Upscaling (Real-ESRGAN NCNN)
- **2Γ— and 4Γ—** upscaling
- NCNN backend (no GPU required)
- Minimal dependencies
- Fast on CPU environments (HF Spaces)

### LoRA Adapter Support
- Runtime loading of `.safetensors` adapters
- Up to **two adapters** with independent weights
- Alpha range `-2 β†’ +2` per adapter
- Automatic discovery under:
```

src/assets/loras/

```
- LoRA UI is **disabled for Turbo**, since Turbo does not benefit from LoRA injection

### Metadata History
Every generation stores:
- model id
- prompt + negative prompt
- steps, cfg, resolution
- seed
- LoRA names + weights
- timestamp

All generated data is stored in a tree structure under:
```

src/assets/history/

```

---

## Architecture

```

src/
└── sdgen/
β”œβ”€β”€ sd/                     # Stable Diffusion runtime
β”‚   β”œβ”€β”€ pipeline.py         # model loading, device config
β”‚   β”œβ”€β”€ generator.py        # text-to-image inference
β”‚   β”œβ”€β”€ img2img.py          # image-to-image inference
β”‚   β”œβ”€β”€ lora_loader.py      # LoRA discovery & injection
β”‚   └── models.py           # typed config & metadata objects
β”‚
β”œβ”€β”€ ui/                     # Gradio UI components
β”‚   β”œβ”€β”€ layout.py           # composition root for UI
β”‚   └── tabs/               # modular tabs
β”‚       β”œβ”€β”€ txt2img_tab.py
β”‚       β”œβ”€β”€ img2img_tab.py
β”‚       β”œβ”€β”€ upscaler_tab.py
β”‚       β”œβ”€β”€ presets_tab.py
β”‚       └── history_tab.py
β”‚
β”œβ”€β”€ presets/                # curated basic presets
β”‚   └── styles.py           # preset registry
β”‚
β”œβ”€β”€ upscaler/               # Real-ESRGAN NCNN backend
β”‚   β”œβ”€β”€ upscaler.py         # interface + metadata
β”‚   └── realesrgan.py       # NCNN wrapper
β”‚
β”œβ”€β”€ utils/                  # shared utilities
β”‚   β”œβ”€β”€ history.py          # atomic storage format
β”‚   β”œβ”€β”€ common.py           # PIL helpers
β”‚   └── logger.py           # structured logging
β”‚
└── config/                 # static configuration
β”œβ”€β”€ paths.py            # resolved directories
└── settings.py         # environment settings

````

---

## Presets (Included)

The project includes **four style presets**, each defining:

- prompt
- negative prompt

These presets are neutral and work with both **SD1.5** and **Turbo**:

| Name               | Style                     |
|--------------------|----------------------------|
| Realistic Photo    | 35mm, photorealistic       |
| Anime              | clean anime illustration   |
| Cinematic / Moody  | cinematic lighting/grain   |
| Oil Painting       | classical oil painting     |

Presets do **not include LoRA parameters**.  
Users may manually combine presets with LoRA adapters.

---

## Installation

### Clone
```bash
git clone https://github.com/sanskarmodi8/stable-diffusion-image-generator
cd stable-diffusion-image-generator
````

### Environment

```bash
python -m venv .venv
source .venv/bin/activate
```

### Install Dependencies (CPU)

```bash
pip install -r requirements.txt
pip install -e .
```

### GPU (optional)

```bash
pip install torch torchvision torchaudio \
  --index-url https://download.pytorch.org/whl/cu121
```

---

## Run

```bash
python src/sdgen/main.py
```

Open in browser:

```
http://127.0.0.1:7860
```

---

## Adding LoRA Models

Place `.safetensors` files here:

```
src/assets/loras/
```

They will be automatically detected and displayed in the UI (SD1.5 only).

This repository **does not include** LoRA files.

---

## Third-Party LoRA Models

The app supports optional LoRA adapters.
LoRA weights are **not included** and are **the property of their respective authors**.

If you choose to download LoRA files automatically (see `lora_urls.py`), they are fetched directly from their original sources (**Civitai**).

This project does **not** redistribute LoRA weights.
Refer to each model’s license on Civitai.

---

## Development

The repo uses `pre-commit` hooks for consistency:

```bash
pre-commit install
```

Tools:

* ruff
* black
* isort

Check formatting:

```bash
ruff check .
black .
```

---

## License

This project is licensed under the **MIT License**.
See the [`LICENSE`](LICENSE) file.

---

## Author 

[**Sanskar Modi**](https://github.com/sanskarmodi8)