Rodneyontherock1067 commited on
Commit
87c5489
1 Parent(s): 1d9b085

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitignore +7 -0
  2. LICENSE +21 -0
  3. Readme.md +566 -0
  4. SDXL-Turbo-LICENSE.TXT +58 -0
  5. benchmark-openvino.bat +23 -0
  6. benchmark.bat +23 -0
  7. configs/lcm-lora-models.txt +4 -0
  8. configs/lcm-models.txt +8 -0
  9. configs/openvino-lcm-models.txt +8 -0
  10. configs/stable-diffusion-models.txt +7 -0
  11. controlnet_models/Readme.txt +3 -0
  12. docs/images/2steps-inference.jpg +0 -0
  13. docs/images/fastcpu-cli.png +0 -0
  14. docs/images/fastcpu-webui.png +0 -0
  15. docs/images/fastsdcpu-android-termux-pixel7.png +0 -0
  16. docs/images/fastsdcpu-api.png +0 -0
  17. docs/images/fastsdcpu-gui.jpg +0 -0
  18. docs/images/fastsdcpu-mac-gui.jpg +0 -0
  19. docs/images/fastsdcpu-screenshot.png +0 -0
  20. docs/images/fastsdcpu-webui.png +0 -0
  21. docs/images/fastsdcpu_flux_on_cpu.png +0 -0
  22. install-mac.sh +31 -0
  23. install.bat +29 -0
  24. install.sh +39 -0
  25. lora_models/Readme.txt +3 -0
  26. requirements.txt +19 -0
  27. src/__init__.py +0 -0
  28. src/app.py +534 -0
  29. src/app_settings.py +94 -0
  30. src/backend/__init__.py +0 -0
  31. src/backend/annotators/canny_control.py +15 -0
  32. src/backend/annotators/control_interface.py +12 -0
  33. src/backend/annotators/depth_control.py +15 -0
  34. src/backend/annotators/image_control_factory.py +31 -0
  35. src/backend/annotators/lineart_control.py +11 -0
  36. src/backend/annotators/mlsd_control.py +10 -0
  37. src/backend/annotators/normal_control.py +10 -0
  38. src/backend/annotators/pose_control.py +10 -0
  39. src/backend/annotators/shuffle_control.py +10 -0
  40. src/backend/annotators/softedge_control.py +10 -0
  41. src/backend/api/models/response.py +16 -0
  42. src/backend/api/web.py +103 -0
  43. src/backend/base64_image.py +21 -0
  44. src/backend/controlnet.py +90 -0
  45. src/backend/device.py +23 -0
  46. src/backend/image_saver.py +60 -0
  47. src/backend/lcm_text_to_image.py +414 -0
  48. src/backend/lora.py +136 -0
  49. src/backend/models/device.py +9 -0
  50. src/backend/models/gen_images.py +16 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ env
2
+ *.bak
3
+ *.pyc
4
+ __pycache__
5
+ results
6
+ # excluding user settings for the GUI frontend
7
+ configs/settings.yaml
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rupesh Sreeraman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
Readme.md ADDED
@@ -0,0 +1,566 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FastSD CPU :sparkles:[![Mentioned in Awesome OpenVINO](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/openvinotoolkit/awesome-openvino)
2
+
3
+ <div align="center">
4
+ <a href="https://trendshift.io/repositories/3957" target="_blank"><img src="https://trendshift.io/api/badge/repositories/3957" alt="rupeshs%2Ffastsdcpu | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
5
+ </div>
6
+
7
+ FastSD CPU is a faster version of Stable Diffusion on CPU. Based on [Latent Consistency Models](https://github.com/luosiallen/latent-consistency-model) and
8
+ [Adversarial Diffusion Distillation](https://nolowiz.com/fast-stable-diffusion-on-cpu-using-fastsd-cpu-and-openvino/).
9
+
10
+ ![FastSD CPU screenshot](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastsdcpu-webui.png)
11
+ The following interfaces are available :
12
+
13
+ - Desktop GUI, basic text to image generation (Qt,faster)
14
+ - WebUI (Advanced features,Lora,controlnet etc)
15
+ - CLI (CommandLine Interface)
16
+
17
+ 🚀 Using __OpenVINO(SDXS-512-0.9)__, it took __0.82 seconds__ (__820 milliseconds__) to create a single 512x512 image on a __Core i7-12700__.
18
+
19
+ ## Table of Contents
20
+
21
+ - [Supported&nbsp;Platforms](#Supported&nbsp;platforms)
22
+ - [Memory requirements](#memory-requirements)
23
+ - [Features](#features)
24
+ - [Benchmarks](#fast-inference-benchmarks)
25
+ - [OpenVINO Support](#openvino)
26
+ - [Installation](#installation)
27
+ - [Real-time text to image (EXPERIMENTAL)](#real-time-text-to-image)
28
+ - [Models](#models)
29
+ - [How to use Lora models](#useloramodels)
30
+ - [How to use controlnet](#usecontrolnet)
31
+ - [Android](#android)
32
+ - [Raspberry Pi 4](#raspberry)
33
+ - [Orange Pi 5](#orangepi)
34
+ - [API&nbsp;Support](#apisupport)
35
+ - [License](#license)
36
+ - [Contributors](#contributors)
37
+
38
+ ## Supported platforms⚡️
39
+
40
+ FastSD CPU works on the following platforms:
41
+
42
+ - Windows
43
+ - Linux
44
+ - Mac
45
+ - Android + Termux
46
+ - Raspberry PI 4
47
+
48
+ ## Memory requirements
49
+
50
+ Minimum system RAM requirement for FastSD CPU.
51
+
52
+ Model (LCM,OpenVINO): SD Turbo, 1 step, 512 x 512
53
+
54
+ Model (LCM-LoRA): Dreamshaper v8, 3 step, 512 x 512
55
+
56
+ | Mode | Min RAM |
57
+ | --------------------- | ------------- |
58
+ | LCM | 2 GB |
59
+ | LCM-LoRA | 4 GB |
60
+ | OpenVINO | 11 GB |
61
+
62
+ If we enable Tiny decoder(TAESD) we can save some memory(2GB approx) for example in OpenVINO mode memory usage will become 9GB.
63
+
64
+ :exclamation: Please note that guidance scale >1 increases RAM usage and slow inference speed.
65
+
66
+ ## Features
67
+
68
+ - Desktop GUI, web UI and CLI
69
+ - Supports 256,512,768,1024 image sizes
70
+ - Supports Windows,Linux,Mac
71
+ - Saves images and diffusion setting used to generate the image
72
+ - Settings to control,steps,guidance and seed
73
+ - Added safety checker setting
74
+ - Maximum inference steps increased to 25
75
+ - Added [OpenVINO](https://github.com/openvinotoolkit/openvino) support
76
+ - Fixed OpenVINO image reproducibility issue
77
+ - Fixed OpenVINO high RAM usage,thanks [deinferno](https://github.com/deinferno)
78
+ - Added multiple image generation support
79
+ - Application settings
80
+ - Added Tiny Auto Encoder for SD (TAESD) support, 1.4x speed boost (Fast,moderate quality)
81
+ - Safety checker disabled by default
82
+ - Added SDXL,SSD1B - 1B LCM models
83
+ - Added LCM-LoRA support, works well for fine-tuned Stable Diffusion model 1.5 or SDXL models
84
+ - Added negative prompt support in LCM-LoRA mode
85
+ - LCM-LoRA models can be configured using text configuration file
86
+ - Added support for custom models for OpenVINO (LCM-LoRA baked)
87
+ - OpenVINO models now supports negative prompt (Set guidance >1.0)
88
+ - Real-time inference support,generates images while you type (experimental)
89
+ - Fast 2,3 steps inference
90
+ - Lcm-Lora fused models for faster inference
91
+ - Supports integrated GPU(iGPU) using OpenVINO (export DEVICE=GPU)
92
+ - 5.7x speed using OpenVINO(steps: 2,tiny autoencoder)
93
+ - Image to Image support (Use Web UI)
94
+ - OpenVINO image to image support
95
+ - Fast 1 step inference (SDXL Turbo)
96
+ - Added SD Turbo support
97
+ - Added image to image support for Turbo models (Pytorch and OpenVINO)
98
+ - Added image variations support
99
+ - Added 2x upscaler (EDSR and Tiled SD upscale (experimental)),thanks [monstruosoft](https://github.com/monstruosoft) for SD upscale
100
+ - Works on Android + Termux + PRoot
101
+ - Added interactive CLI,thanks [monstruosoft](https://github.com/monstruosoft)
102
+ - Added basic lora support to CLI and WebUI
103
+ - ONNX EDSR 2x upscale
104
+ - Add SDXL-Lightning support
105
+ - Add SDXL-Lightning OpenVINO support (int8)
106
+ - Add multilora support,thanks [monstruosoft](https://github.com/monstruosoft)
107
+ - Add basic ControlNet v1.1 support(LCM-LoRA mode),thanks [monstruosoft](https://github.com/monstruosoft)
108
+ - Add ControlNet annotators(Canny,Depth,LineArt,MLSD,NormalBAE,Pose,SoftEdge,Shuffle)
109
+ - Add SDXS-512 0.9 support
110
+ - Add SDXS-512 0.9 OpenVINO,fast 1 step inference (0.8 seconds to generate 512x512 image)
111
+ - Default model changed to SDXS-512-0.9
112
+ - Faster realtime image generation
113
+ - Add NPU device check
114
+ - Revert default model to SDTurbo
115
+ - Update realtime UI
116
+ - Add hypersd support
117
+ - 1 step fast inference support for SDXL and SD1.5
118
+ - Experimental support for single file Safetensors SD 1.5 models(Civitai models), simply add local model path to configs/stable-diffusion-models.txt file.
119
+ - Add REST API support
120
+ - Add Aura SR (4x)/GigaGAN based upscaler support
121
+ - Add Aura SR v2 upscaler support
122
+ - Add FLUX.1 schnell OpenVINO int 4 support
123
+
124
+ <a id="fast-inference-benchmarks"></a>
125
+
126
+ ## Fast Inference Benchmarks
127
+
128
+ ### 🚀 Fast 1 step inference with Hyper-SD
129
+
130
+ #### Stable diffuion 1.5
131
+
132
+ Works with LCM-LoRA mode.
133
+ Fast 1 step inference supported on `runwayml/stable-diffusion-v1-5` model,select `rupeshs/hypersd-sd1-5-1-step-lora` lcm_lora model from the settings.
134
+
135
+ #### Stable diffuion XL
136
+
137
+ Works with LCM and LCM-OpenVINO mode.
138
+
139
+ - *Hyper-SD SDXL 1 step* - [rupeshs/hyper-sd-sdxl-1-step](https://huggingface.co/rupeshs/hyper-sd-sdxl-1-step)
140
+
141
+ - *Hyper-SD SDXL 1 step OpenVINO* - [rupeshs/hyper-sd-sdxl-1-step-openvino-int8](https://huggingface.co/rupeshs/hyper-sd-sdxl-1-step-openvino-int8)
142
+
143
+ #### Inference Speed
144
+
145
+ Tested on Core i7-12700 to generate __768x768__ image(1 step).
146
+
147
+ | Diffusion Pipeline | Latency |
148
+ | --------------------- | ------------- |
149
+ | Pytorch | 19s |
150
+ | OpenVINO | 13s |
151
+ | OpenVINO + TAESDXL | 6.3s |
152
+
153
+ ### Fastest 1 step inference (SDXS-512-0.9)
154
+
155
+ :exclamation:This is an experimental model, only text to image workflow is supported.
156
+
157
+ #### Inference Speed
158
+
159
+ Tested on Core i7-12700 to generate __512x512__ image(1 step).
160
+
161
+ __SDXS-512-0.9__
162
+
163
+ | Diffusion Pipeline | Latency |
164
+ | --------------------- | ------------- |
165
+ | Pytorch | 4.8s |
166
+ | OpenVINO | 3.8s |
167
+ | OpenVINO + TAESD | __0.82s__ |
168
+
169
+ ### 🚀 Fast 1 step inference (SD/SDXL Turbo - Adversarial Diffusion Distillation,ADD)
170
+
171
+ Added support for ultra fast 1 step inference using [sdxl-turbo](https://huggingface.co/stabilityai/sdxl-turbo) model
172
+
173
+ :exclamation: These SD turbo models are intended for research purpose only.
174
+
175
+ #### Inference Speed
176
+
177
+ Tested on Core i7-12700 to generate __512x512__ image(1 step).
178
+
179
+ __SD Turbo__
180
+
181
+ | Diffusion Pipeline | Latency |
182
+ | --------------------- | ------------- |
183
+ | Pytorch | 7.8s |
184
+ | OpenVINO | 5s |
185
+ | OpenVINO + TAESD | 1.7s |
186
+
187
+ __SDXL Turbo__
188
+
189
+ | Diffusion Pipeline | Latency |
190
+ | --------------------- | ------------- |
191
+ | Pytorch | 10s |
192
+ | OpenVINO | 5.6s |
193
+ | OpenVINO + TAESDXL | 2.5s |
194
+
195
+ ### 🚀 Fast 2 step inference (SDXL-Lightning - Adversarial Diffusion Distillation)
196
+
197
+ SDXL-Lightning works with LCM and LCM-OpenVINO mode.You can select these models from app settings.
198
+
199
+ Tested on Core i7-12700 to generate __768x768__ image(2 steps).
200
+
201
+ | Diffusion Pipeline | Latency |
202
+ | --------------------- | ------------- |
203
+ | Pytorch | 18s |
204
+ | OpenVINO | 12s |
205
+ | OpenVINO + TAESDXL | 10s |
206
+
207
+ - *SDXL-Lightning* - [rupeshs/SDXL-Lightning-2steps](https://huggingface.co/rupeshs/SDXL-Lightning-2steps)
208
+
209
+ - *SDXL-Lightning OpenVINO* - [rupeshs/SDXL-Lightning-2steps-openvino-int8](https://huggingface.co/rupeshs/SDXL-Lightning-2steps-openvino-int8)
210
+
211
+ ### 2 Steps fast inference (LCM)
212
+
213
+ FastSD CPU supports 2 to 3 steps fast inference using LCM-LoRA workflow. It works well with SD 1.5 models.
214
+
215
+ ![2 Steps inference](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/2steps-inference.jpg)
216
+
217
+ ### FLUX.1-schnell OpenVINO support
218
+
219
+ ![FLUX Schenell OpenVINO](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastsdcpu_flux_on_cpu.png)
220
+
221
+ :exclamation: Important - Please note the following points with FLUX workflow
222
+
223
+ - As of now only text to image generation mode is supported
224
+ - Use OpenVINO mode
225
+ - Use int4 model - *rupeshs/FLUX.1-schnell-openvino-int4*
226
+ - Tiny decoder will not work with FLUX
227
+ - 512x512 image generation needs around __30GB__ system RAM
228
+
229
+ Tested on Intel Core i7-12700 to generate __512x512__ image(3 steps).
230
+
231
+ | Diffusion Pipeline | Latency |
232
+ | --------------------- | ------------- |
233
+ | OpenVINO | 4 min 30sec |
234
+
235
+ ### Benchmark scripts
236
+
237
+ To benchmark run the following batch file on Windows:
238
+
239
+ - `benchmark.bat` - To benchmark Pytorch
240
+ - `benchmark-openvino.bat` - To benchmark OpenVINO
241
+
242
+ Alternatively you can run benchmarks by passing `-b` command line argument in CLI mode.
243
+ <a id="openvino"></a>
244
+
245
+ ## OpenVINO support
246
+
247
+ Fast SD CPU utilizes [OpenVINO](https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/overview.html) to speed up the inference speed.
248
+ Thanks [deinferno](https://github.com/deinferno) for the OpenVINO model contribution.
249
+ We can get 2x speed improvement when using OpenVINO.
250
+ Thanks [Disty0](https://github.com/Disty0) for the conversion script.
251
+
252
+ ### OpenVINO SDXL models
253
+
254
+ These are models converted to use directly use it with FastSD CPU. These models are compressed to int8 to reduce the file size (10GB to 4.4 GB) using [NNCF](https://github.com/openvinotoolkit/nncf)
255
+
256
+ - Hyper-SD SDXL 1 step - [rupeshs/hyper-sd-sdxl-1-step-openvino-int8](https://huggingface.co/rupeshs/hyper-sd-sdxl-1-step-openvino-int8)
257
+ - SDXL Lightning 2 steps - [rupeshs/SDXL-Lightning-2steps-openvino-int8](https://huggingface.co/rupeshs/SDXL-Lightning-2steps-openvino-int8)
258
+
259
+ ### OpenVINO SD Turbo models
260
+
261
+ We have converted SD/SDXL Turbo models to OpenVINO for fast inference on CPU. These models are intended for research purpose only. Also we converted TAESDXL MODEL to OpenVINO and
262
+
263
+ - *SD Turbo OpenVINO* - [rupeshs/sd-turbo-openvino](https://huggingface.co/rupeshs/sd-turbo-openvino)
264
+ - *SDXL Turbo OpenVINO int8* - [rupeshs/sdxl-turbo-openvino-int8](https://huggingface.co/rupeshs/sdxl-turbo-openvino-int8)
265
+ - *TAESDXL OpenVINO* - [rupeshs/taesdxl-openvino](https://huggingface.co/rupeshs/taesdxl-openvino)
266
+
267
+ You can directly use these models in FastSD CPU.
268
+
269
+ ### Convert SD 1.5 models to OpenVINO LCM-LoRA fused models
270
+
271
+ We first creates LCM-LoRA baked in model,replaces the scheduler with LCM and then converts it into OpenVINO model. For more details check [LCM OpenVINO Converter](https://github.com/rupeshs/lcm-openvino-converter), you can use this tools to convert any StableDiffusion 1.5 fine tuned models to OpenVINO.
272
+ <a id="real-time-text-to-image"></a>
273
+
274
+ ## Real-time text to image (EXPERIMENTAL)
275
+
276
+ We can generate real-time text to images using FastSD CPU.
277
+
278
+ __CPU (OpenVINO)__
279
+
280
+ Near real-time inference on CPU using OpenVINO, run the `start-realtime.bat` batch file and open the link in browser (Resolution : 512x512,Latency : 0.82s on Intel Core i7)
281
+
282
+ Watch YouTube video :
283
+
284
+ [![IMAGE_ALT](https://img.youtube.com/vi/0XMiLc_vsyI/0.jpg)](https://www.youtube.com/watch?v=0XMiLc_vsyI)
285
+
286
+ ## Models
287
+
288
+ To use single file [Safetensors](https://huggingface.co/docs/safetensors/en/index) SD 1.5 models(Civit AI) follow this [YouTube tutorial](https://www.youtube.com/watch?v=zZTfUZnXJVk). Use LCM-LoRA Mode for single file safetensors.
289
+
290
+ Fast SD supports LCM models and LCM-LoRA models.
291
+
292
+ ### LCM Models
293
+
294
+ These models can be configured in `configs/lcm-models.txt` file.
295
+
296
+ ### OpenVINO models
297
+
298
+ These are LCM-LoRA baked in models. These models can be configured in `configs/openvino-lcm-models.txt` file
299
+
300
+ ### LCM-LoRA models
301
+
302
+ These models can be configured in `configs/lcm-lora-models.txt` file.
303
+
304
+ - *lcm-lora-sdv1-5* - distilled consistency adapter for [runwayml/stable-diffusion-v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5)
305
+ - *lcm-lora-sdxl* - Distilled consistency adapter for [stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)
306
+ - *lcm-lora-ssd-1b* - Distilled consistency adapter for [segmind/SSD-1B](https://huggingface.co/segmind/SSD-1B)
307
+
308
+ These models are used with Stablediffusion base models `configs/stable-diffusion-models.txt`.
309
+
310
+ :exclamation: Currently no support for OpenVINO LCM-LoRA models.
311
+
312
+ ### How to add new LCM-LoRA models
313
+
314
+ To add new model follow the steps:
315
+ For example we will add `wavymulder/collage-diffusion`, you can give Stable diffusion 1.5 Or SDXL,SSD-1B fine tuned models.
316
+
317
+ 1. Open `configs/stable-diffusion-models.txt` file in text editor.
318
+ 2. Add the model ID `wavymulder/collage-diffusion` or locally cloned path.
319
+
320
+ Updated file as shown below :
321
+
322
+ ```Lykon/dreamshaper-8
323
+ Fictiverse/Stable_Diffusion_PaperCut_Model
324
+ stabilityai/stable-diffusion-xl-base-1.0
325
+ runwayml/stable-diffusion-v1-5
326
+ segmind/SSD-1B
327
+ stablediffusionapi/anything-v5
328
+ wavymulder/collage-diffusion
329
+ ```
330
+
331
+ Similarly we can update `configs/lcm-lora-models.txt` file with lcm-lora ID.
332
+
333
+ ### How to use LCM-LoRA models offline
334
+
335
+ Please follow the steps to run LCM-LoRA models offline :
336
+
337
+ - In the settings ensure that "Use locally cached model" setting is ticked.
338
+ - Download the model for example `latent-consistency/lcm-lora-sdv1-5`
339
+ Run the following commands:
340
+
341
+ ```
342
+ git lfs install
343
+ git clone https://huggingface.co/latent-consistency/lcm-lora-sdv1-5
344
+ ```
345
+
346
+ Copy the cloned model folder path for example "D:\demo\lcm-lora-sdv1-5" and update the `configs/lcm-lora-models.txt` file as shown below :
347
+
348
+ ```
349
+ D:\demo\lcm-lora-sdv1-5
350
+ latent-consistency/lcm-lora-sdxl
351
+ latent-consistency/lcm-lora-ssd-1b
352
+ ```
353
+
354
+ - Open the app and select the newly added local folder in the combo box menu.
355
+ - That's all!
356
+ <a id="useloramodels"></a>
357
+
358
+ ## How to use Lora models
359
+
360
+ Place your lora models in "lora_models" folder. Use LCM or LCM-Lora mode.
361
+ You can download lora model (.safetensors/Safetensor) from [Civitai](https://civitai.com/) or [Hugging Face](https://huggingface.co/)
362
+ E.g: [cutecartoonredmond](https://civitai.com/models/207984/cutecartoonredmond-15v-cute-cartoon-lora-for-liberteredmond-sd-15?modelVersionId=234192)
363
+ <a id="usecontrolnet"></a>
364
+
365
+ ## ControlNet support
366
+
367
+ We can use ControlNet in LCM-LoRA mode.
368
+
369
+ Download ControlNet models from [ControlNet-v1-1](https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/tree/main).Download and place controlnet models in "controlnet_models" folder.
370
+
371
+ Use the medium size models (723 MB)(For example : <https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/blob/main/control_v11p_sd15_canny_fp16.safetensors>)
372
+
373
+ ## Installation
374
+
375
+ ### FastSD CPU on Windows
376
+
377
+ ![FastSD CPU Desktop GUI Screenshot](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastsdcpu-gui.jpg)
378
+
379
+ :exclamation:__You must have a working Python installation.(Recommended : Python 3.10 or 3.11 )__
380
+
381
+ To install FastSD CPU on Windows run the following steps :
382
+
383
+ - Clone/download this repo or download [release](https://github.com/rupeshs/fastsdcpu/releases).
384
+ - Double click `install.bat` (It will take some time to install,depending on your internet speed.)
385
+ - You can run in desktop GUI mode or web UI mode.
386
+
387
+ #### Desktop GUI
388
+
389
+ - To start desktop GUI double click `start.bat`
390
+
391
+ #### Web UI
392
+
393
+ - To start web UI double click `start-webui.bat`
394
+
395
+ ### FastSD CPU on Linux
396
+
397
+ :exclamation:__Ensure that you have Python 3.9 or 3.10 or 3.11 version installed.__
398
+
399
+ - Clone/download this repo or download [release](https://github.com/rupeshs/fastsdcpu/releases).
400
+ - In the terminal, enter into fastsdcpu directory
401
+ - Run the following command
402
+
403
+ `chmod +x install.sh`
404
+
405
+ `./install.sh`
406
+
407
+ #### To start Desktop GUI
408
+
409
+ `./start.sh`
410
+
411
+ #### To start Web UI
412
+
413
+ `./start-webui.sh`
414
+
415
+ ### FastSD CPU on Mac
416
+
417
+ ![FastSD CPU running on Mac](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastsdcpu-mac-gui.jpg)
418
+
419
+ :exclamation:__Ensure that you have Python 3.9 or 3.10 or 3.11 version installed.__
420
+
421
+ Run the following commands to install FastSD CPU on Mac :
422
+
423
+ - Clone/download this repo or download [release](https://github.com/rupeshs/fastsdcpu/releases).
424
+ - In the terminal, enter into fastsdcpu directory
425
+ - Run the following command
426
+
427
+ `chmod +x install-mac.sh`
428
+
429
+ `./install-mac.sh`
430
+
431
+ #### To start Desktop GUI
432
+
433
+ `./start.sh`
434
+
435
+ #### To start Web UI
436
+
437
+ `./start-webui.sh`
438
+
439
+ Thanks [Autantpourmoi](https://github.com/Autantpourmoi) for Mac testing.
440
+
441
+ :exclamation:We don't support OpenVINO on Mac (M1/M2/M3 chips, but *does* work on Intel chips).
442
+
443
+ If you want to increase image generation speed on Mac(M1/M2 chip) try this:
444
+
445
+ `export DEVICE=mps` and start app `start.sh`
446
+
447
+ #### Web UI screenshot
448
+
449
+ ![FastSD CPU WebUI Screenshot](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastcpu-webui.png)
450
+
451
+ ### Google Colab
452
+
453
+ Due to the limitation of using CPU/OpenVINO inside colab, we are using GPU with colab.
454
+ [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1SuAqskB-_gjWLYNRFENAkIXZ1aoyINqL?usp=sharing)
455
+
456
+ ### CLI mode (Advanced users)
457
+
458
+ ![FastSD CPU CLI Screenshot](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastcpu-cli.png)
459
+
460
+ Open the terminal and enter into fastsdcpu folder.
461
+ Activate virtual environment using the command:
462
+
463
+ ##### Windows users
464
+
465
+ (Suppose FastSD CPU available in the directory "D:\fastsdcpu")
466
+ `D:\fastsdcpu\env\Scripts\activate.bat`
467
+
468
+ ##### Linux users
469
+
470
+ `source env/bin/activate`
471
+
472
+ Start CLI `src/app.py -h`
473
+
474
+ <a id="android"></a>
475
+
476
+ ## Android (Termux + PRoot)
477
+
478
+ FastSD CPU running on Google Pixel 7 Pro.
479
+
480
+ ![FastSD CPU Android Termux Screenshot](https://raw.githubusercontent.com/rupeshs/fastsdcpu/main/docs/images/fastsdcpu-android-termux-pixel7.png)
481
+
482
+ ### 1. Prerequisites
483
+
484
+ First you have to [install Termux](https://wiki.termux.com/wiki/Installing_from_F-Droid) and [install PRoot](https://wiki.termux.com/wiki/PRoot). Then install and login to Ubuntu in PRoot.
485
+
486
+ ### 2. Install FastSD CPU
487
+
488
+ Run the following command to install without Qt GUI.
489
+
490
+ `proot-distro login ubuntu`
491
+
492
+ `./install.sh --disable-gui`
493
+
494
+ After the installation you can use WebUi.
495
+
496
+ `./start-webui.sh`
497
+
498
+ Note : If you get `libgl.so.1` import error run `apt-get install ffmpeg`.
499
+
500
+ Thanks [patienx](https://github.com/patientx) for this guide [Step by step guide to installing FASTSDCPU on ANDROID](https://github.com/rupeshs/fastsdcpu/discussions/123)
501
+
502
+ Another step by step guide to run FastSD on Android is [here](https://nolowiz.com/how-to-install-and-run-fastsd-cpu-on-android-temux-step-by-step-guide/)
503
+
504
+ <a id="raspberry"></a>
505
+
506
+ ## Raspberry PI 4 support
507
+
508
+ Thanks [WGNW_MGM] for Raspberry PI 4 testing.FastSD CPU worked without problems.
509
+ System configuration - Raspberry Pi 4 with 4GB RAM, 8GB of SWAP memory.
510
+
511
+ <a id="orangepi"></a>
512
+
513
+ ## Orange Pi 5 support
514
+
515
+ Thanks [khanumballz](https://github.com/khanumballz) for testing FastSD CPU with Orange PI 5.
516
+ [Here is a video of FastSD CPU running on Orange Pi 5](https://www.youtube.com/watch?v=KEJiCU0aK8o).
517
+
518
+ <a id="apisupport"></a>
519
+
520
+ ## API support
521
+
522
+ ![FastSD CPU API documentation](https://raw.githubusercontent.com/rupeshs/fastsdcpu/add-basic-api-support/docs/images/fastsdcpu-api.png)
523
+
524
+ FastSD CPU supports basic API endpoints. Following API endpoints are available :
525
+
526
+ - /api/info - To get system information
527
+ - /api/config - Get configuration
528
+ - /api/models - List all available models
529
+ - /api/generate - Generate images (Text to image,image to image)
530
+
531
+ To start FastAPI in webserver mode run:
532
+ ``python src/app.py --api``
533
+
534
+ or use `start-webserver.sh` for Linux and `start-webserver.bat` for Windows.
535
+
536
+ Access API documentation locally at <http://localhost:8000/api/docs> .
537
+
538
+ Generated image is JPEG image encoded as base64 string.
539
+ In the image-to-image mode input image should be encoded as base64 string.
540
+
541
+ To generate an image a minimal request `POST /api/generate` with body :
542
+
543
+ ```
544
+ {
545
+ "prompt": "a cute cat",
546
+ "use_openvino": true
547
+ }
548
+ ```
549
+
550
+ ## Known issues
551
+
552
+ - TAESD will not work with OpenVINO image to image workflow
553
+
554
+ ## License
555
+
556
+ The fastsdcpu project is available as open source under the terms of the [MIT license](https://github.com/rupeshs/fastsdcpu/blob/main/LICENSE)
557
+
558
+ ## Disclaimer
559
+
560
+ Users are granted the freedom to create images using this tool, but they are obligated to comply with local laws and utilize it responsibly. The developers will not assume any responsibility for potential misuse by users.
561
+
562
+ ## Contributors
563
+
564
+ <a href="https://github.com/rupeshs/fastsdcpu/graphs/contributors">
565
+ <img src="https://contrib.rocks/image?repo=rupeshs/fastsdcpu" />
566
+ </a>
SDXL-Turbo-LICENSE.TXT ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ STABILITY AI NON-COMMERCIAL RESEARCH COMMUNITY LICENSE AGREEMENT
2
+ Dated: November 28, 2023
3
+
4
+
5
+ By using or distributing any portion or element of the Models, Software, Software Products or Derivative Works, you agree to be bound by this Agreement.
6
+
7
+
8
+ "Agreement" means this Stable Non-Commercial Research Community License Agreement.
9
+
10
+
11
+ “AUP” means the Stability AI Acceptable Use Policy available at https://stability.ai/use-policy, as may be updated from time to time.
12
+
13
+
14
+ "Derivative Work(s)” means (a) any derivative work of the Software Products as recognized by U.S. copyright laws and (b) any modifications to a Model, and any other model created which is based on or derived from the Model or the Model’s output. For clarity, Derivative Works do not include the output of any Model.
15
+
16
+
17
+ “Documentation” means any specifications, manuals, documentation, and other written information provided by Stability AI related to the Software.
18
+
19
+
20
+ "Licensee" or "you" means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity's behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf.
21
+
22
+
23
+ “Model(s)" means, collectively, Stability AI’s proprietary models and algorithms, including machine-learning models, trained model weights and other elements of the foregoing, made available under this Agreement.
24
+
25
+
26
+ “Non-Commercial Uses” means exercising any of the rights granted herein for the purpose of research or non-commercial purposes. Non-Commercial Uses does not include any production use of the Software Products or any Derivative Works.
27
+
28
+
29
+ "Stability AI" or "we" means Stability AI Ltd. and its affiliates.
30
+
31
+ "Software" means Stability AI’s proprietary software made available under this Agreement.
32
+
33
+
34
+ “Software Products” means the Models, Software and Documentation, individually or in any combination.
35
+
36
+
37
+
38
+ 1. License Rights and Redistribution.
39
+
40
+ a. Subject to your compliance with this Agreement, the AUP (which is hereby incorporated herein by reference), and the Documentation, Stability AI grants you a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable, royalty free and limited license under Stability AI’s intellectual property or other rights owned or controlled by Stability AI embodied in the Software Products to use, reproduce, distribute, and create Derivative Works of, the Software Products, in each case for Non-Commercial Uses only.
41
+
42
+ b. You may not use the Software Products or Derivative Works to enable third parties to use the Software Products or Derivative Works as part of your hosted service or via your APIs, whether you are adding substantial additional functionality thereto or not. Merely distributing the Software Products or Derivative Works for download online without offering any related service (ex. by distributing the Models on HuggingFace) is not a violation of this subsection. If you wish to use the Software Products or any Derivative Works for commercial or production use or you wish to make the Software Products or any Derivative Works available to third parties via your hosted service or your APIs, contact Stability AI at https://stability.ai/contact.
43
+
44
+ c. If you distribute or make the Software Products, or any Derivative Works thereof, available to a third party, the Software Products, Derivative Works, or any portion thereof, respectively, will remain subject to this Agreement and you must (i) provide a copy of this Agreement to such third party, and (ii) retain the following attribution notice within a "Notice" text file distributed as a part of such copies: "This Stability AI Model is licensed under the Stability AI Non-Commercial Research Community License, Copyright (c) Stability AI Ltd. All Rights Reserved.” If you create a Derivative Work of a Software Product, you may add your own attribution notices to the Notice file included with the Software Product, provided that you clearly indicate which attributions apply to the Software Product and you must state in the NOTICE file that you changed the Software Product and how it was modified.
45
+
46
+ 2. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE SOFTWARE PRODUCTS AND ANY OUTPUT AND RESULTS THERE FROM ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE SOFTWARE PRODUCTS, DERIVATIVE WORKS OR ANY OUTPUT OR RESULTS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE SOFTWARE PRODUCTS, DERIVATIVE WORKS AND ANY OUTPUT AND RESULTS.
47
+
48
+ 3. Limitation of Liability. IN NO EVENT WILL STABILITY AI OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF STABILITY AI OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
49
+
50
+ 4. Intellectual Property.
51
+
52
+ a. No trademark licenses are granted under this Agreement, and in connection with the Software Products or Derivative Works, neither Stability AI nor Licensee may use any name or mark owned by or associated with the other or any of its affiliates, except as required for reasonable and customary use in describing and redistributing the Software Products or Derivative Works.
53
+
54
+ b. Subject to Stability AI’s ownership of the Software Products and Derivative Works made by or for Stability AI, with respect to any Derivative Works that are made by you, as between you and Stability AI, you are and will be the owner of such Derivative Works
55
+
56
+ c. If you institute litigation or other proceedings against Stability AI (including a cross-claim or counterclaim in a lawsuit) alleging that the Software Products, Derivative Works or associated outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Stability AI from and against any claim by any third party arising out of or related to your use or distribution of the Software Products or Derivative Works in violation of this Agreement.
57
+
58
+ 5. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the Software Products and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Stability AI may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of any Software Products or Derivative Works. Sections 2-4 shall survive the termination of this Agreement.
benchmark-openvino.bat ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ setlocal
3
+
4
+ set "PYTHON_COMMAND=python"
5
+
6
+ call python --version > nul 2>&1
7
+ if %errorlevel% equ 0 (
8
+ echo Python command check :OK
9
+ ) else (
10
+ echo "Error: Python command not found, please install Python (Recommended : Python 3.10 or Python 3.11) and try again"
11
+ pause
12
+ exit /b 1
13
+
14
+ )
15
+
16
+ :check_python_version
17
+ for /f "tokens=2" %%I in ('%PYTHON_COMMAND% --version 2^>^&1') do (
18
+ set "python_version=%%I"
19
+ )
20
+
21
+ echo Python version: %python_version%
22
+
23
+ call "%~dp0env\Scripts\activate.bat" && %PYTHON_COMMAND% src/app.py -b --use_openvino --openvino_lcm_model_id "rupeshs/sd-turbo-openvino"
benchmark.bat ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ setlocal
3
+
4
+ set "PYTHON_COMMAND=python"
5
+
6
+ call python --version > nul 2>&1
7
+ if %errorlevel% equ 0 (
8
+ echo Python command check :OK
9
+ ) else (
10
+ echo "Error: Python command not found, please install Python (Recommended : Python 3.10 or Python 3.11) and try again"
11
+ pause
12
+ exit /b 1
13
+
14
+ )
15
+
16
+ :check_python_version
17
+ for /f "tokens=2" %%I in ('%PYTHON_COMMAND% --version 2^>^&1') do (
18
+ set "python_version=%%I"
19
+ )
20
+
21
+ echo Python version: %python_version%
22
+
23
+ call "%~dp0env\Scripts\activate.bat" && %PYTHON_COMMAND% src/app.py -b
configs/lcm-lora-models.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ latent-consistency/lcm-lora-sdv1-5
2
+ latent-consistency/lcm-lora-sdxl
3
+ latent-consistency/lcm-lora-ssd-1b
4
+ rupeshs/hypersd-sd1-5-1-step-lora
configs/lcm-models.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ stabilityai/sd-turbo
2
+ rupeshs/sdxs-512-0.9-orig-vae
3
+ rupeshs/hyper-sd-sdxl-1-step
4
+ rupeshs/SDXL-Lightning-2steps
5
+ stabilityai/sdxl-turbo
6
+ SimianLuo/LCM_Dreamshaper_v7
7
+ latent-consistency/lcm-sdxl
8
+ latent-consistency/lcm-ssd-1b
configs/openvino-lcm-models.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ rupeshs/sd-turbo-openvino
2
+ rupeshs/sdxs-512-0.9-openvino
3
+ rupeshs/hyper-sd-sdxl-1-step-openvino-int8
4
+ rupeshs/SDXL-Lightning-2steps-openvino-int8
5
+ rupeshs/sdxl-turbo-openvino-int8
6
+ rupeshs/LCM-dreamshaper-v7-openvino
7
+ Disty0/LCM_SoteMix
8
+ rupeshs/FLUX.1-schnell-openvino-int4
configs/stable-diffusion-models.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Lykon/dreamshaper-8
2
+ Fictiverse/Stable_Diffusion_PaperCut_Model
3
+ stabilityai/stable-diffusion-xl-base-1.0
4
+ runwayml/stable-diffusion-v1-5
5
+ segmind/SSD-1B
6
+ stablediffusionapi/anything-v5
7
+ prompthero/openjourney-v4
controlnet_models/Readme.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Place your ControlNet models in this folder.
2
+ You can download controlnet model (.safetensors) from https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/tree/main
3
+ E.g: https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/blob/main/control_v11p_sd15_canny_fp16.safetensors
docs/images/2steps-inference.jpg ADDED
docs/images/fastcpu-cli.png ADDED
docs/images/fastcpu-webui.png ADDED
docs/images/fastsdcpu-android-termux-pixel7.png ADDED
docs/images/fastsdcpu-api.png ADDED
docs/images/fastsdcpu-gui.jpg ADDED
docs/images/fastsdcpu-mac-gui.jpg ADDED
docs/images/fastsdcpu-screenshot.png ADDED
docs/images/fastsdcpu-webui.png ADDED
docs/images/fastsdcpu_flux_on_cpu.png ADDED
install-mac.sh ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ echo Starting FastSD CPU env installation...
3
+ set -e
4
+ PYTHON_COMMAND="python3"
5
+
6
+ if ! command -v python3 &>/dev/null; then
7
+ if ! command -v python &>/dev/null; then
8
+ echo "Error: Python not found, please install python 3.8 or higher and try again"
9
+ exit 1
10
+ fi
11
+ fi
12
+
13
+ if command -v python &>/dev/null; then
14
+ PYTHON_COMMAND="python"
15
+ fi
16
+
17
+ echo "Found $PYTHON_COMMAND command"
18
+
19
+ python_version=$($PYTHON_COMMAND --version 2>&1 | awk '{print $2}')
20
+ echo "Python version : $python_version"
21
+
22
+ BASEDIR=$(pwd)
23
+
24
+ $PYTHON_COMMAND -m venv "$BASEDIR/env"
25
+ # shellcheck disable=SC1091
26
+ source "$BASEDIR/env/bin/activate"
27
+ pip install torch==2.2.2
28
+ pip install -r "$BASEDIR/requirements.txt"
29
+ chmod +x "start.sh"
30
+ chmod +x "start-webui.sh"
31
+ read -n1 -r -p "FastSD CPU installation completed,press any key to continue..." key
install.bat ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ @echo off
3
+ setlocal
4
+ echo Starting FastSD CPU env installation...
5
+
6
+ set "PYTHON_COMMAND=python"
7
+
8
+ call python --version > nul 2>&1
9
+ if %errorlevel% equ 0 (
10
+ echo Python command check :OK
11
+ ) else (
12
+ echo "Error: Python command not found,please install Python(Recommended : Python 3.10 or Python 3.11) and try again."
13
+ pause
14
+ exit /b 1
15
+
16
+ )
17
+
18
+ :check_python_version
19
+ for /f "tokens=2" %%I in ('%PYTHON_COMMAND% --version 2^>^&1') do (
20
+ set "python_version=%%I"
21
+ )
22
+
23
+ echo Python version: %python_version%
24
+
25
+ %PYTHON_COMMAND% -m venv "%~dp0env"
26
+ call "%~dp0env\Scripts\activate.bat" && pip install torch==2.2.2 --index-url https://download.pytorch.org/whl/cpu
27
+ call "%~dp0env\Scripts\activate.bat" && pip install -r "%~dp0requirements.txt"
28
+ echo FastSD CPU env installation completed.
29
+ pause
install.sh ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ echo Starting FastSD CPU env installation...
3
+ set -e
4
+ PYTHON_COMMAND="python3"
5
+
6
+ if ! command -v python3 &>/dev/null; then
7
+ if ! command -v python &>/dev/null; then
8
+ echo "Error: Python not found, please install python 3.8 or higher and try again"
9
+ exit 1
10
+ fi
11
+ fi
12
+
13
+ if command -v python &>/dev/null; then
14
+ PYTHON_COMMAND="python"
15
+ fi
16
+
17
+ echo "Found $PYTHON_COMMAND command"
18
+
19
+ python_version=$($PYTHON_COMMAND --version 2>&1 | awk '{print $2}')
20
+ echo "Python version : $python_version"
21
+
22
+ BASEDIR=$(pwd)
23
+
24
+ $PYTHON_COMMAND -m venv "$BASEDIR/env"
25
+ # shellcheck disable=SC1091
26
+ source "$BASEDIR/env/bin/activate"
27
+ pip install torch==2.2.2 --index-url https://download.pytorch.org/whl/cpu
28
+ if [[ "$1" == "--disable-gui" ]]; then
29
+ #! For termux , we don't need Qt based GUI
30
+ packages="$(grep -v "^ *#\|^PyQt5" requirements.txt | grep .)"
31
+ # shellcheck disable=SC2086
32
+ pip install $packages
33
+ else
34
+ pip install -r "$BASEDIR/requirements.txt"
35
+ fi
36
+
37
+ chmod +x "start.sh"
38
+ chmod +x "start-webui.sh"
39
+ read -n1 -r -p "FastSD CPU installation completed,press any key to continue..." key
lora_models/Readme.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Place your lora models in this folder.
2
+ You can download lora model (.safetensors/Safetensor) from Civitai (https://civitai.com/) or Hugging Face(https://huggingface.co/)
3
+ E.g: https://civitai.com/models/207984/cutecartoonredmond-15v-cute-cartoon-lora-for-liberteredmond-sd-15?modelVersionId=234192
requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.33.0
2
+ diffusers==0.30.0
3
+ transformers==4.41.2
4
+ PyQt5
5
+ Pillow==9.4.0
6
+ openvino==2024.3.0
7
+ optimum-intel==1.18.2
8
+ onnx==1.16.0
9
+ onnxruntime==1.17.3
10
+ pydantic==2.4.2
11
+ typing-extensions==4.8.0
12
+ pyyaml==6.0.1
13
+ gradio==4.23.0
14
+ peft==0.6.1
15
+ opencv-python==4.8.1.78
16
+ omegaconf==2.3.0
17
+ controlnet-aux==0.0.7
18
+ mediapipe==0.10.9
19
+ tomesd==0.1.3
src/__init__.py ADDED
File without changes
src/app.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from argparse import ArgumentParser
3
+
4
+ import constants
5
+ from backend.controlnet import controlnet_settings_from_dict
6
+ from backend.models.gen_images import ImageFormat
7
+ from backend.models.lcmdiffusion_setting import DiffusionTask
8
+ from backend.upscale.tiled_upscale import generate_upscaled_image
9
+ from constants import APP_VERSION, DEVICE
10
+ from frontend.webui.image_variations_ui import generate_image_variations
11
+ from models.interface_types import InterfaceType
12
+ from paths import FastStableDiffusionPaths
13
+ from PIL import Image
14
+ from state import get_context, get_settings
15
+ from utils import show_system_info
16
+ from backend.device import get_device_name
17
+
18
+ parser = ArgumentParser(description=f"FAST SD CPU {constants.APP_VERSION}")
19
+ parser.add_argument(
20
+ "-s",
21
+ "--share",
22
+ action="store_true",
23
+ help="Create sharable link(Web UI)",
24
+ required=False,
25
+ )
26
+ group = parser.add_mutually_exclusive_group(required=False)
27
+ group.add_argument(
28
+ "-g",
29
+ "--gui",
30
+ action="store_true",
31
+ help="Start desktop GUI",
32
+ )
33
+ group.add_argument(
34
+ "-w",
35
+ "--webui",
36
+ action="store_true",
37
+ help="Start Web UI",
38
+ )
39
+ group.add_argument(
40
+ "-a",
41
+ "--api",
42
+ action="store_true",
43
+ help="Start Web API server",
44
+ )
45
+ group.add_argument(
46
+ "-r",
47
+ "--realtime",
48
+ action="store_true",
49
+ help="Start realtime inference UI(experimental)",
50
+ )
51
+ group.add_argument(
52
+ "-v",
53
+ "--version",
54
+ action="store_true",
55
+ help="Version",
56
+ )
57
+
58
+ parser.add_argument(
59
+ "-b",
60
+ "--benchmark",
61
+ action="store_true",
62
+ help="Run inference benchmark on the selected device",
63
+ )
64
+ parser.add_argument(
65
+ "--lcm_model_id",
66
+ type=str,
67
+ help="Model ID or path,Default stabilityai/sd-turbo",
68
+ default="stabilityai/sd-turbo",
69
+ )
70
+ parser.add_argument(
71
+ "--openvino_lcm_model_id",
72
+ type=str,
73
+ help="OpenVINO Model ID or path,Default rupeshs/sd-turbo-openvino",
74
+ default="rupeshs/sd-turbo-openvino",
75
+ )
76
+ parser.add_argument(
77
+ "--prompt",
78
+ type=str,
79
+ help="Describe the image you want to generate",
80
+ default="",
81
+ )
82
+ parser.add_argument(
83
+ "--negative_prompt",
84
+ type=str,
85
+ help="Describe what you want to exclude from the generation",
86
+ default="",
87
+ )
88
+ parser.add_argument(
89
+ "--image_height",
90
+ type=int,
91
+ help="Height of the image",
92
+ default=512,
93
+ )
94
+ parser.add_argument(
95
+ "--image_width",
96
+ type=int,
97
+ help="Width of the image",
98
+ default=512,
99
+ )
100
+ parser.add_argument(
101
+ "--inference_steps",
102
+ type=int,
103
+ help="Number of steps,default : 1",
104
+ default=1,
105
+ )
106
+ parser.add_argument(
107
+ "--guidance_scale",
108
+ type=float,
109
+ help="Guidance scale,default : 1.0",
110
+ default=1.0,
111
+ )
112
+
113
+ parser.add_argument(
114
+ "--number_of_images",
115
+ type=int,
116
+ help="Number of images to generate ,default : 1",
117
+ default=1,
118
+ )
119
+ parser.add_argument(
120
+ "--seed",
121
+ type=int,
122
+ help="Seed,default : -1 (disabled) ",
123
+ default=-1,
124
+ )
125
+ parser.add_argument(
126
+ "--use_openvino",
127
+ action="store_true",
128
+ help="Use OpenVINO model",
129
+ )
130
+
131
+ parser.add_argument(
132
+ "--use_offline_model",
133
+ action="store_true",
134
+ help="Use offline model",
135
+ )
136
+ parser.add_argument(
137
+ "--clip_skip",
138
+ type=int,
139
+ help="CLIP Skip (1-12), default : 1 (disabled) ",
140
+ default=1,
141
+ )
142
+ parser.add_argument(
143
+ "--token_merging",
144
+ type=float,
145
+ help="Token merging scale, 0.0 - 1.0, default : 0.0",
146
+ default=0.0,
147
+ )
148
+
149
+ parser.add_argument(
150
+ "--use_safety_checker",
151
+ action="store_true",
152
+ help="Use safety checker",
153
+ )
154
+ parser.add_argument(
155
+ "--use_lcm_lora",
156
+ action="store_true",
157
+ help="Use LCM-LoRA",
158
+ )
159
+ parser.add_argument(
160
+ "--base_model_id",
161
+ type=str,
162
+ help="LCM LoRA base model ID,Default Lykon/dreamshaper-8",
163
+ default="Lykon/dreamshaper-8",
164
+ )
165
+ parser.add_argument(
166
+ "--lcm_lora_id",
167
+ type=str,
168
+ help="LCM LoRA model ID,Default latent-consistency/lcm-lora-sdv1-5",
169
+ default="latent-consistency/lcm-lora-sdv1-5",
170
+ )
171
+ parser.add_argument(
172
+ "-i",
173
+ "--interactive",
174
+ action="store_true",
175
+ help="Interactive CLI mode",
176
+ )
177
+ parser.add_argument(
178
+ "-t",
179
+ "--use_tiny_auto_encoder",
180
+ action="store_true",
181
+ help="Use tiny auto encoder for SD (TAESD)",
182
+ )
183
+ parser.add_argument(
184
+ "-f",
185
+ "--file",
186
+ type=str,
187
+ help="Input image for img2img mode",
188
+ default="",
189
+ )
190
+ parser.add_argument(
191
+ "--img2img",
192
+ action="store_true",
193
+ help="img2img mode; requires input file via -f argument",
194
+ )
195
+ parser.add_argument(
196
+ "--batch_count",
197
+ type=int,
198
+ help="Number of sequential generations",
199
+ default=1,
200
+ )
201
+ parser.add_argument(
202
+ "--strength",
203
+ type=float,
204
+ help="Denoising strength for img2img and Image variations",
205
+ default=0.3,
206
+ )
207
+ parser.add_argument(
208
+ "--sdupscale",
209
+ action="store_true",
210
+ help="Tiled SD upscale,works only for the resolution 512x512,(2x upscale)",
211
+ )
212
+ parser.add_argument(
213
+ "--upscale",
214
+ action="store_true",
215
+ help="EDSR SD upscale ",
216
+ )
217
+ parser.add_argument(
218
+ "--custom_settings",
219
+ type=str,
220
+ help="JSON file containing custom generation settings",
221
+ default=None,
222
+ )
223
+ parser.add_argument(
224
+ "--usejpeg",
225
+ action="store_true",
226
+ help="Images will be saved as JPEG format",
227
+ )
228
+ parser.add_argument(
229
+ "--noimagesave",
230
+ action="store_true",
231
+ help="Disable image saving",
232
+ )
233
+ parser.add_argument(
234
+ "--lora",
235
+ type=str,
236
+ help="LoRA model full path e.g D:\lora_models\CuteCartoon15V-LiberteRedmodModel-Cartoon-CuteCartoonAF.safetensors",
237
+ default=None,
238
+ )
239
+ parser.add_argument(
240
+ "--lora_weight",
241
+ type=float,
242
+ help="LoRA adapter weight [0 to 1.0]",
243
+ default=0.5,
244
+ )
245
+ parser.add_argument(
246
+ "--port",
247
+ type=int,
248
+ help="Web server port",
249
+ default=8000,
250
+ )
251
+
252
+ args = parser.parse_args()
253
+
254
+ if args.version:
255
+ print(APP_VERSION)
256
+ exit()
257
+
258
+ # parser.print_help()
259
+ show_system_info()
260
+ print(f"Using device : {constants.DEVICE}")
261
+
262
+ if args.webui:
263
+ app_settings = get_settings()
264
+ else:
265
+ app_settings = get_settings()
266
+
267
+ print(f"Found {len(app_settings.lcm_models)} LCM models in config/lcm-models.txt")
268
+ print(
269
+ f"Found {len(app_settings.stable_diffsuion_models)} stable diffusion models in config/stable-diffusion-models.txt"
270
+ )
271
+ print(
272
+ f"Found {len(app_settings.lcm_lora_models)} LCM-LoRA models in config/lcm-lora-models.txt"
273
+ )
274
+ print(
275
+ f"Found {len(app_settings.openvino_lcm_models)} OpenVINO LCM models in config/openvino-lcm-models.txt"
276
+ )
277
+
278
+ if args.noimagesave:
279
+ app_settings.settings.generated_images.save_image = False
280
+ else:
281
+ app_settings.settings.generated_images.save_image = True
282
+
283
+ if not args.realtime:
284
+ # To minimize realtime mode dependencies
285
+ from backend.upscale.upscaler import upscale_image
286
+ from frontend.cli_interactive import interactive_mode
287
+
288
+ if args.gui:
289
+ from frontend.gui.ui import start_gui
290
+
291
+ print("Starting desktop GUI mode(Qt)")
292
+ start_gui(
293
+ [],
294
+ app_settings,
295
+ )
296
+ elif args.webui:
297
+ from frontend.webui.ui import start_webui
298
+
299
+ print("Starting web UI mode")
300
+ start_webui(
301
+ args.share,
302
+ )
303
+ elif args.realtime:
304
+ from frontend.webui.realtime_ui import start_realtime_text_to_image
305
+
306
+ print("Starting realtime text to image(EXPERIMENTAL)")
307
+ start_realtime_text_to_image(args.share)
308
+ elif args.api:
309
+ from backend.api.web import start_web_server
310
+
311
+ start_web_server(args.port)
312
+
313
+ else:
314
+ context = get_context(InterfaceType.CLI)
315
+ config = app_settings.settings
316
+
317
+ if args.use_openvino:
318
+ config.lcm_diffusion_setting.openvino_lcm_model_id = args.openvino_lcm_model_id
319
+ else:
320
+ config.lcm_diffusion_setting.lcm_model_id = args.lcm_model_id
321
+
322
+ config.lcm_diffusion_setting.prompt = args.prompt
323
+ config.lcm_diffusion_setting.negative_prompt = args.negative_prompt
324
+ config.lcm_diffusion_setting.image_height = args.image_height
325
+ config.lcm_diffusion_setting.image_width = args.image_width
326
+ config.lcm_diffusion_setting.guidance_scale = args.guidance_scale
327
+ config.lcm_diffusion_setting.number_of_images = args.number_of_images
328
+ config.lcm_diffusion_setting.inference_steps = args.inference_steps
329
+ config.lcm_diffusion_setting.strength = args.strength
330
+ config.lcm_diffusion_setting.seed = args.seed
331
+ config.lcm_diffusion_setting.use_openvino = args.use_openvino
332
+ config.lcm_diffusion_setting.use_tiny_auto_encoder = args.use_tiny_auto_encoder
333
+ config.lcm_diffusion_setting.use_lcm_lora = args.use_lcm_lora
334
+ config.lcm_diffusion_setting.lcm_lora.base_model_id = args.base_model_id
335
+ config.lcm_diffusion_setting.lcm_lora.lcm_lora_id = args.lcm_lora_id
336
+ config.lcm_diffusion_setting.diffusion_task = DiffusionTask.text_to_image.value
337
+ config.lcm_diffusion_setting.lora.enabled = False
338
+ config.lcm_diffusion_setting.lora.path = args.lora
339
+ config.lcm_diffusion_setting.lora.weight = args.lora_weight
340
+ config.lcm_diffusion_setting.lora.fuse = True
341
+ if config.lcm_diffusion_setting.lora.path:
342
+ config.lcm_diffusion_setting.lora.enabled = True
343
+ if args.usejpeg:
344
+ config.generated_images.format = ImageFormat.JPEG.value.upper()
345
+ if args.seed > -1:
346
+ config.lcm_diffusion_setting.use_seed = True
347
+ else:
348
+ config.lcm_diffusion_setting.use_seed = False
349
+ config.lcm_diffusion_setting.use_offline_model = args.use_offline_model
350
+ config.lcm_diffusion_setting.clip_skip = args.clip_skip
351
+ config.lcm_diffusion_setting.token_merging = args.token_merging
352
+ config.lcm_diffusion_setting.use_safety_checker = args.use_safety_checker
353
+
354
+ # Read custom settings from JSON file
355
+ custom_settings = {}
356
+ if args.custom_settings:
357
+ with open(args.custom_settings) as f:
358
+ custom_settings = json.load(f)
359
+
360
+ # Basic ControlNet settings; if ControlNet is enabled, an image is
361
+ # required even in txt2img mode
362
+ config.lcm_diffusion_setting.controlnet = None
363
+ controlnet_settings_from_dict(
364
+ config.lcm_diffusion_setting,
365
+ custom_settings,
366
+ )
367
+
368
+ # Interactive mode
369
+ if args.interactive:
370
+ # wrapper(interactive_mode, config, context)
371
+ config.lcm_diffusion_setting.lora.fuse = False
372
+ interactive_mode(config, context)
373
+
374
+ # Start of non-interactive CLI image generation
375
+ if args.img2img and args.file != "":
376
+ config.lcm_diffusion_setting.init_image = Image.open(args.file)
377
+ config.lcm_diffusion_setting.diffusion_task = DiffusionTask.image_to_image.value
378
+ elif args.img2img and args.file == "":
379
+ print("Error : You need to specify a file in img2img mode")
380
+ exit()
381
+ elif args.upscale and args.file == "" and args.custom_settings == None:
382
+ print("Error : You need to specify a file in SD upscale mode")
383
+ exit()
384
+ elif (
385
+ args.prompt == ""
386
+ and args.file == ""
387
+ and args.custom_settings == None
388
+ and not args.benchmark
389
+ ):
390
+ print("Error : You need to provide a prompt")
391
+ exit()
392
+
393
+ if args.upscale:
394
+ # image = Image.open(args.file)
395
+ output_path = FastStableDiffusionPaths.get_upscale_filepath(
396
+ args.file,
397
+ 2,
398
+ config.generated_images.format,
399
+ )
400
+ result = upscale_image(
401
+ context,
402
+ args.file,
403
+ output_path,
404
+ 2,
405
+ )
406
+ # Perform Tiled SD upscale (EXPERIMENTAL)
407
+ elif args.sdupscale:
408
+ if args.use_openvino:
409
+ config.lcm_diffusion_setting.strength = 0.3
410
+ upscale_settings = None
411
+ if custom_settings != {}:
412
+ upscale_settings = custom_settings
413
+ filepath = args.file
414
+ output_format = config.generated_images.format
415
+ if upscale_settings:
416
+ filepath = upscale_settings["source_file"]
417
+ output_format = upscale_settings["output_format"].upper()
418
+ output_path = FastStableDiffusionPaths.get_upscale_filepath(
419
+ filepath,
420
+ 2,
421
+ output_format,
422
+ )
423
+
424
+ generate_upscaled_image(
425
+ config,
426
+ filepath,
427
+ config.lcm_diffusion_setting.strength,
428
+ upscale_settings=upscale_settings,
429
+ context=context,
430
+ tile_overlap=32 if config.lcm_diffusion_setting.use_openvino else 16,
431
+ output_path=output_path,
432
+ image_format=output_format,
433
+ )
434
+ exit()
435
+ # If img2img argument is set and prompt is empty, use image variations mode
436
+ elif args.img2img and args.prompt == "":
437
+ for i in range(0, args.batch_count):
438
+ generate_image_variations(
439
+ config.lcm_diffusion_setting.init_image, args.strength
440
+ )
441
+ else:
442
+
443
+ if args.benchmark:
444
+ print("Initializing benchmark...")
445
+ bench_lcm_setting = config.lcm_diffusion_setting
446
+ bench_lcm_setting.prompt = "a cat"
447
+ bench_lcm_setting.use_tiny_auto_encoder = False
448
+ context.generate_text_to_image(
449
+ settings=config,
450
+ device=DEVICE,
451
+ )
452
+ latencies = []
453
+
454
+ print("Starting benchmark please wait...")
455
+ for _ in range(3):
456
+ context.generate_text_to_image(
457
+ settings=config,
458
+ device=DEVICE,
459
+ )
460
+ latencies.append(context.latency)
461
+
462
+ avg_latency = sum(latencies) / 3
463
+
464
+ bench_lcm_setting.use_tiny_auto_encoder = True
465
+
466
+ context.generate_text_to_image(
467
+ settings=config,
468
+ device=DEVICE,
469
+ )
470
+ latencies = []
471
+ for _ in range(3):
472
+ context.generate_text_to_image(
473
+ settings=config,
474
+ device=DEVICE,
475
+ )
476
+ latencies.append(context.latency)
477
+
478
+ avg_latency_taesd = sum(latencies) / 3
479
+
480
+ benchmark_name = ""
481
+
482
+ if config.lcm_diffusion_setting.use_openvino:
483
+ benchmark_name = "OpenVINO"
484
+ else:
485
+ benchmark_name = "PyTorch"
486
+
487
+ bench_model_id = ""
488
+ if bench_lcm_setting.use_openvino:
489
+ bench_model_id = bench_lcm_setting.openvino_lcm_model_id
490
+ elif bench_lcm_setting.use_lcm_lora:
491
+ bench_model_id = bench_lcm_setting.lcm_lora.base_model_id
492
+ else:
493
+ bench_model_id = bench_lcm_setting.lcm_model_id
494
+
495
+ benchmark_result = [
496
+ ["Device", f"{DEVICE.upper()},{get_device_name()}"],
497
+ ["Stable Diffusion Model", bench_model_id],
498
+ [
499
+ "Image Size ",
500
+ f"{bench_lcm_setting.image_width}x{bench_lcm_setting.image_height}",
501
+ ],
502
+ [
503
+ "Inference Steps",
504
+ f"{bench_lcm_setting.inference_steps}",
505
+ ],
506
+ [
507
+ "Benchmark Passes",
508
+ 3,
509
+ ],
510
+ [
511
+ "Average Latency",
512
+ f"{round(avg_latency,3)} sec",
513
+ ],
514
+ [
515
+ "Average Latency(TAESD* enabled)",
516
+ f"{round(avg_latency_taesd,3)} sec",
517
+ ],
518
+ ]
519
+ print()
520
+ print(
521
+ f" FastSD Benchmark - {benchmark_name:8} "
522
+ )
523
+ print(f"-" * 80)
524
+ for benchmark in benchmark_result:
525
+ print(f"{benchmark[0]:35} - {benchmark[1]}")
526
+ print(f"-" * 80)
527
+ print("*TAESD - Tiny AutoEncoder for Stable Diffusion")
528
+
529
+ else:
530
+ for i in range(0, args.batch_count):
531
+ context.generate_text_to_image(
532
+ settings=config,
533
+ device=DEVICE,
534
+ )
src/app_settings.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yaml
2
+ from os import path, makedirs
3
+ from models.settings import Settings
4
+ from paths import FastStableDiffusionPaths
5
+ from utils import get_models_from_text_file
6
+ from constants import (
7
+ OPENVINO_LCM_MODELS_FILE,
8
+ LCM_LORA_MODELS_FILE,
9
+ SD_MODELS_FILE,
10
+ LCM_MODELS_FILE,
11
+ )
12
+ from copy import deepcopy
13
+
14
+
15
+ class AppSettings:
16
+ def __init__(self):
17
+ self.config_path = FastStableDiffusionPaths().get_app_settings_path()
18
+ self._stable_diffsuion_models = get_models_from_text_file(
19
+ FastStableDiffusionPaths().get_models_config_path(SD_MODELS_FILE)
20
+ )
21
+ self._lcm_lora_models = get_models_from_text_file(
22
+ FastStableDiffusionPaths().get_models_config_path(LCM_LORA_MODELS_FILE)
23
+ )
24
+ self._openvino_lcm_models = get_models_from_text_file(
25
+ FastStableDiffusionPaths().get_models_config_path(OPENVINO_LCM_MODELS_FILE)
26
+ )
27
+ self._lcm_models = get_models_from_text_file(
28
+ FastStableDiffusionPaths().get_models_config_path(LCM_MODELS_FILE)
29
+ )
30
+ self._config = None
31
+
32
+ @property
33
+ def settings(self):
34
+ return self._config
35
+
36
+ @property
37
+ def stable_diffsuion_models(self):
38
+ return self._stable_diffsuion_models
39
+
40
+ @property
41
+ def openvino_lcm_models(self):
42
+ return self._openvino_lcm_models
43
+
44
+ @property
45
+ def lcm_models(self):
46
+ return self._lcm_models
47
+
48
+ @property
49
+ def lcm_lora_models(self):
50
+ return self._lcm_lora_models
51
+
52
+ def load(self, skip_file=False):
53
+ if skip_file:
54
+ print("Skipping config file")
55
+ settings_dict = self._load_default()
56
+ self._config = Settings.model_validate(settings_dict)
57
+ else:
58
+ if not path.exists(self.config_path):
59
+ base_dir = path.dirname(self.config_path)
60
+ if not path.exists(base_dir):
61
+ makedirs(base_dir)
62
+ try:
63
+ print("Settings not found creating default settings")
64
+ with open(self.config_path, "w") as file:
65
+ yaml.dump(
66
+ self._load_default(),
67
+ file,
68
+ )
69
+ except Exception as ex:
70
+ print(f"Error in creating settings : {ex}")
71
+ exit()
72
+ try:
73
+ with open(self.config_path) as file:
74
+ settings_dict = yaml.safe_load(file)
75
+ self._config = Settings.model_validate(settings_dict)
76
+ except Exception as ex:
77
+ print(f"Error in loading settings : {ex}")
78
+
79
+ def save(self):
80
+ try:
81
+ with open(self.config_path, "w") as file:
82
+ tmp_cfg = deepcopy(self._config)
83
+ tmp_cfg.lcm_diffusion_setting.init_image = None
84
+ configurations = tmp_cfg.model_dump(
85
+ exclude=["init_image"],
86
+ )
87
+ if configurations:
88
+ yaml.dump(configurations, file)
89
+ except Exception as ex:
90
+ print(f"Error in saving settings : {ex}")
91
+
92
+ def _load_default(self) -> dict:
93
+ default_config = Settings()
94
+ return default_config.model_dump()
src/backend/__init__.py ADDED
File without changes
src/backend/annotators/canny_control.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from backend.annotators.control_interface import ControlInterface
3
+ from cv2 import Canny
4
+ from PIL import Image
5
+
6
+
7
+ class CannyControl(ControlInterface):
8
+ def get_control_image(self, image: Image) -> Image:
9
+ low_threshold = 100
10
+ high_threshold = 200
11
+ image = np.array(image)
12
+ image = Canny(image, low_threshold, high_threshold)
13
+ image = image[:, :, None]
14
+ image = np.concatenate([image, image, image], axis=2)
15
+ return Image.fromarray(image)
src/backend/annotators/control_interface.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC, abstractmethod
2
+
3
+ from PIL import Image
4
+
5
+
6
+ class ControlInterface(ABC):
7
+ @abstractmethod
8
+ def get_control_image(
9
+ self,
10
+ image: Image,
11
+ ) -> Image:
12
+ pass
src/backend/annotators/depth_control.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from backend.annotators.control_interface import ControlInterface
3
+ from PIL import Image
4
+ from transformers import pipeline
5
+
6
+
7
+ class DepthControl(ControlInterface):
8
+ def get_control_image(self, image: Image) -> Image:
9
+ depth_estimator = pipeline("depth-estimation")
10
+ image = depth_estimator(image)["depth"]
11
+ image = np.array(image)
12
+ image = image[:, :, None]
13
+ image = np.concatenate([image, image, image], axis=2)
14
+ image = Image.fromarray(image)
15
+ return image
src/backend/annotators/image_control_factory.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.canny_control import CannyControl
2
+ from backend.annotators.depth_control import DepthControl
3
+ from backend.annotators.lineart_control import LineArtControl
4
+ from backend.annotators.mlsd_control import MlsdControl
5
+ from backend.annotators.normal_control import NormalControl
6
+ from backend.annotators.pose_control import PoseControl
7
+ from backend.annotators.shuffle_control import ShuffleControl
8
+ from backend.annotators.softedge_control import SoftEdgeControl
9
+
10
+
11
+ class ImageControlFactory:
12
+ def create_control(self, controlnet_type: str):
13
+ if controlnet_type == "Canny":
14
+ return CannyControl()
15
+ elif controlnet_type == "Pose":
16
+ return PoseControl()
17
+ elif controlnet_type == "MLSD":
18
+ return MlsdControl()
19
+ elif controlnet_type == "Depth":
20
+ return DepthControl()
21
+ elif controlnet_type == "LineArt":
22
+ return LineArtControl()
23
+ elif controlnet_type == "Shuffle":
24
+ return ShuffleControl()
25
+ elif controlnet_type == "NormalBAE":
26
+ return NormalControl()
27
+ elif controlnet_type == "SoftEdge":
28
+ return SoftEdgeControl()
29
+ else:
30
+ print("Error: Control type not implemented!")
31
+ raise Exception("Error: Control type not implemented!")
src/backend/annotators/lineart_control.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from backend.annotators.control_interface import ControlInterface
3
+ from controlnet_aux import LineartDetector
4
+ from PIL import Image
5
+
6
+
7
+ class LineArtControl(ControlInterface):
8
+ def get_control_image(self, image: Image) -> Image:
9
+ processor = LineartDetector.from_pretrained("lllyasviel/Annotators")
10
+ control_image = processor(image)
11
+ return control_image
src/backend/annotators/mlsd_control.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.control_interface import ControlInterface
2
+ from controlnet_aux import MLSDdetector
3
+ from PIL import Image
4
+
5
+
6
+ class MlsdControl(ControlInterface):
7
+ def get_control_image(self, image: Image) -> Image:
8
+ mlsd = MLSDdetector.from_pretrained("lllyasviel/ControlNet")
9
+ image = mlsd(image)
10
+ return image
src/backend/annotators/normal_control.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.control_interface import ControlInterface
2
+ from controlnet_aux import NormalBaeDetector
3
+ from PIL import Image
4
+
5
+
6
+ class NormalControl(ControlInterface):
7
+ def get_control_image(self, image: Image) -> Image:
8
+ processor = NormalBaeDetector.from_pretrained("lllyasviel/Annotators")
9
+ control_image = processor(image)
10
+ return control_image
src/backend/annotators/pose_control.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.control_interface import ControlInterface
2
+ from controlnet_aux import OpenposeDetector
3
+ from PIL import Image
4
+
5
+
6
+ class PoseControl(ControlInterface):
7
+ def get_control_image(self, image: Image) -> Image:
8
+ openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
9
+ image = openpose(image)
10
+ return image
src/backend/annotators/shuffle_control.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.control_interface import ControlInterface
2
+ from controlnet_aux import ContentShuffleDetector
3
+ from PIL import Image
4
+
5
+
6
+ class ShuffleControl(ControlInterface):
7
+ def get_control_image(self, image: Image) -> Image:
8
+ shuffle_processor = ContentShuffleDetector()
9
+ image = shuffle_processor(image)
10
+ return image
src/backend/annotators/softedge_control.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from backend.annotators.control_interface import ControlInterface
2
+ from controlnet_aux import PidiNetDetector
3
+ from PIL import Image
4
+
5
+
6
+ class SoftEdgeControl(ControlInterface):
7
+ def get_control_image(self, image: Image) -> Image:
8
+ processor = PidiNetDetector.from_pretrained("lllyasviel/Annotators")
9
+ control_image = processor(image)
10
+ return control_image
src/backend/api/models/response.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from pydantic import BaseModel
4
+
5
+
6
+ class StableDiffusionResponse(BaseModel):
7
+ """
8
+ Stable diffusion response model
9
+
10
+ Attributes:
11
+ images (List[str]): List of JPEG image as base64 encoded
12
+ latency (float): Latency in seconds
13
+ """
14
+
15
+ images: List[str]
16
+ latency: float
src/backend/api/web.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import platform
2
+
3
+ import uvicorn
4
+ from backend.api.models.response import StableDiffusionResponse
5
+ from backend.models.device import DeviceInfo
6
+ from backend.base64_image import base64_image_to_pil, pil_image_to_base64_str
7
+ from backend.device import get_device_name
8
+ from backend.models.lcmdiffusion_setting import DiffusionTask, LCMDiffusionSetting
9
+ from constants import APP_VERSION, DEVICE
10
+ from context import Context
11
+ from fastapi import FastAPI
12
+ from models.interface_types import InterfaceType
13
+ from state import get_settings
14
+
15
+ app_settings = get_settings()
16
+ app = FastAPI(
17
+ title="FastSD CPU",
18
+ description="Fast stable diffusion on CPU",
19
+ version=APP_VERSION,
20
+ license_info={
21
+ "name": "MIT",
22
+ "identifier": "MIT",
23
+ },
24
+ docs_url="/api/docs",
25
+ redoc_url="/api/redoc",
26
+ openapi_url="/api/openapi.json",
27
+ )
28
+ print(app_settings.settings.lcm_diffusion_setting)
29
+
30
+ context = Context(InterfaceType.API_SERVER)
31
+
32
+
33
+ @app.get("/api/")
34
+ async def root():
35
+ return {"message": "Welcome to FastSD CPU API"}
36
+
37
+
38
+ @app.get(
39
+ "/api/info",
40
+ description="Get system information",
41
+ summary="Get system information",
42
+ )
43
+ async def info():
44
+ device_info = DeviceInfo(
45
+ device_type=DEVICE,
46
+ device_name=get_device_name(),
47
+ os=platform.system(),
48
+ platform=platform.platform(),
49
+ processor=platform.processor(),
50
+ )
51
+ return device_info.model_dump()
52
+
53
+
54
+ @app.get(
55
+ "/api/config",
56
+ description="Get current configuration",
57
+ summary="Get configurations",
58
+ )
59
+ async def config():
60
+ return app_settings.settings
61
+
62
+
63
+ @app.get(
64
+ "/api/models",
65
+ description="Get available models",
66
+ summary="Get available models",
67
+ )
68
+ async def models():
69
+ return {
70
+ "lcm_lora_models": app_settings.lcm_lora_models,
71
+ "stable_diffusion": app_settings.stable_diffsuion_models,
72
+ "openvino_models": app_settings.openvino_lcm_models,
73
+ "lcm_models": app_settings.lcm_models,
74
+ }
75
+
76
+
77
+ @app.post(
78
+ "/api/generate",
79
+ description="Generate image(Text to image,Image to Image)",
80
+ summary="Generate image(Text to image,Image to Image)",
81
+ )
82
+ async def generate(diffusion_config: LCMDiffusionSetting) -> StableDiffusionResponse:
83
+ app_settings.settings.lcm_diffusion_setting = diffusion_config
84
+ if diffusion_config.diffusion_task == DiffusionTask.image_to_image:
85
+ app_settings.settings.lcm_diffusion_setting.init_image = base64_image_to_pil(
86
+ diffusion_config.init_image
87
+ )
88
+
89
+ images = context.generate_text_to_image(app_settings.settings)
90
+
91
+ images_base64 = [pil_image_to_base64_str(img) for img in images]
92
+ return StableDiffusionResponse(
93
+ latency=round(context.latency, 2),
94
+ images=images_base64,
95
+ )
96
+
97
+
98
+ def start_web_server(port: int = 8000):
99
+ uvicorn.run(
100
+ app,
101
+ host="0.0.0.0",
102
+ port=port,
103
+ )
src/backend/base64_image.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from base64 import b64encode, b64decode
3
+ from PIL import Image
4
+
5
+
6
+ def pil_image_to_base64_str(
7
+ image: Image,
8
+ format: str = "JPEG",
9
+ ) -> str:
10
+ buffer = BytesIO()
11
+ image.save(buffer, format=format)
12
+ buffer.seek(0)
13
+ img_base64 = b64encode(buffer.getvalue()).decode("utf-8")
14
+ return img_base64
15
+
16
+
17
+ def base64_image_to_pil(base64_str) -> Image:
18
+ image_data = b64decode(base64_str)
19
+ image_buffer = BytesIO(image_data)
20
+ image = Image.open(image_buffer)
21
+ return image
src/backend/controlnet.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from PIL import Image
3
+ from diffusers import ControlNetModel
4
+ from backend.models.lcmdiffusion_setting import (
5
+ DiffusionTask,
6
+ ControlNetSetting,
7
+ )
8
+
9
+
10
+ # Prepares ControlNet adapters for use with FastSD CPU
11
+ #
12
+ # This function loads the ControlNet adapters defined by the
13
+ # _lcm_diffusion_setting.controlnet_ object and returns a dictionary
14
+ # with the pipeline arguments required to use the loaded adapters
15
+ def load_controlnet_adapters(lcm_diffusion_setting) -> dict:
16
+ controlnet_args = {}
17
+ if (
18
+ lcm_diffusion_setting.controlnet is None
19
+ or not lcm_diffusion_setting.controlnet.enabled
20
+ ):
21
+ return controlnet_args
22
+
23
+ logging.info("Loading ControlNet adapter")
24
+ controlnet_adapter = ControlNetModel.from_single_file(
25
+ lcm_diffusion_setting.controlnet.adapter_path,
26
+ # local_files_only=True,
27
+ use_safetensors=True,
28
+ )
29
+ controlnet_args["controlnet"] = controlnet_adapter
30
+ return controlnet_args
31
+
32
+
33
+ # Updates the ControlNet pipeline arguments to use for image generation
34
+ #
35
+ # This function uses the contents of the _lcm_diffusion_setting.controlnet_
36
+ # object to generate a dictionary with the corresponding pipeline arguments
37
+ # to be used for image generation; in particular, it sets the ControlNet control
38
+ # image and conditioning scale
39
+ def update_controlnet_arguments(lcm_diffusion_setting) -> dict:
40
+ controlnet_args = {}
41
+ if (
42
+ lcm_diffusion_setting.controlnet is None
43
+ or not lcm_diffusion_setting.controlnet.enabled
44
+ ):
45
+ return controlnet_args
46
+
47
+ controlnet_args["controlnet_conditioning_scale"] = (
48
+ lcm_diffusion_setting.controlnet.conditioning_scale
49
+ )
50
+ if lcm_diffusion_setting.diffusion_task == DiffusionTask.text_to_image.value:
51
+ controlnet_args["image"] = lcm_diffusion_setting.controlnet._control_image
52
+ elif lcm_diffusion_setting.diffusion_task == DiffusionTask.image_to_image.value:
53
+ controlnet_args["control_image"] = (
54
+ lcm_diffusion_setting.controlnet._control_image
55
+ )
56
+ return controlnet_args
57
+
58
+
59
+ # Helper function to adjust ControlNet settings from a dictionary
60
+ def controlnet_settings_from_dict(
61
+ lcm_diffusion_setting,
62
+ dictionary,
63
+ ) -> None:
64
+ if lcm_diffusion_setting is None or dictionary is None:
65
+ logging.error("Invalid arguments!")
66
+ return
67
+ if (
68
+ "controlnet" not in dictionary
69
+ or dictionary["controlnet"] is None
70
+ or len(dictionary["controlnet"]) == 0
71
+ ):
72
+ logging.warning("ControlNet settings not found, ControlNet will be disabled")
73
+ lcm_diffusion_setting.controlnet = None
74
+ return
75
+
76
+ controlnet = ControlNetSetting()
77
+ controlnet.enabled = dictionary["controlnet"][0]["enabled"]
78
+ controlnet.conditioning_scale = dictionary["controlnet"][0]["conditioning_scale"]
79
+ controlnet.adapter_path = dictionary["controlnet"][0]["adapter_path"]
80
+ controlnet._control_image = None
81
+ image_path = dictionary["controlnet"][0]["control_image"]
82
+ if controlnet.enabled:
83
+ try:
84
+ controlnet._control_image = Image.open(image_path)
85
+ except (AttributeError, FileNotFoundError) as err:
86
+ print(err)
87
+ if controlnet._control_image is None:
88
+ logging.error("Wrong ControlNet control image! Disabling ControlNet")
89
+ controlnet.enabled = False
90
+ lcm_diffusion_setting.controlnet = controlnet
src/backend/device.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import platform
2
+ from constants import DEVICE
3
+ import torch
4
+ import openvino as ov
5
+
6
+ core = ov.Core()
7
+
8
+
9
+ def is_openvino_device() -> bool:
10
+ if DEVICE.lower() == "cpu" or DEVICE.lower()[0] == "g" or DEVICE.lower()[0] == "n":
11
+ return True
12
+ else:
13
+ return False
14
+
15
+
16
+ def get_device_name() -> str:
17
+ if DEVICE == "cuda" or DEVICE == "mps":
18
+ default_gpu_index = torch.cuda.current_device()
19
+ return torch.cuda.get_device_name(default_gpu_index)
20
+ elif platform.system().lower() == "darwin":
21
+ return platform.processor()
22
+ elif is_openvino_device():
23
+ return core.get_property(DEVICE.upper(), "FULL_DEVICE_NAME")
src/backend/image_saver.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from os import path, mkdir
3
+ from typing import Any
4
+ from uuid import uuid4
5
+ from backend.models.lcmdiffusion_setting import LCMDiffusionSetting
6
+ from utils import get_image_file_extension
7
+
8
+
9
+ def get_exclude_keys():
10
+ exclude_keys = {
11
+ "init_image": True,
12
+ "generated_images": True,
13
+ "lora": {
14
+ "models_dir": True,
15
+ "path": True,
16
+ },
17
+ "dirs": True,
18
+ "controlnet": {
19
+ "adapter_path": True,
20
+ },
21
+ }
22
+ return exclude_keys
23
+
24
+
25
+ class ImageSaver:
26
+ @staticmethod
27
+ def save_images(
28
+ output_path: str,
29
+ images: Any,
30
+ folder_name: str = "",
31
+ format: str = "PNG",
32
+ lcm_diffusion_setting: LCMDiffusionSetting = None,
33
+ ) -> None:
34
+ gen_id = uuid4()
35
+
36
+ for index, image in enumerate(images):
37
+ if not path.exists(output_path):
38
+ mkdir(output_path)
39
+
40
+ if folder_name:
41
+ out_path = path.join(
42
+ output_path,
43
+ folder_name,
44
+ )
45
+ else:
46
+ out_path = output_path
47
+
48
+ if not path.exists(out_path):
49
+ mkdir(out_path)
50
+ image_extension = get_image_file_extension(format)
51
+ image.save(path.join(out_path, f"{gen_id}-{index+1}{image_extension}"))
52
+ if lcm_diffusion_setting:
53
+ with open(path.join(out_path, f"{gen_id}.json"), "w") as json_file:
54
+ json.dump(
55
+ lcm_diffusion_setting.model_dump(
56
+ exclude=get_exclude_keys(),
57
+ ),
58
+ json_file,
59
+ indent=4,
60
+ )
src/backend/lcm_text_to_image.py ADDED
@@ -0,0 +1,414 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gc
2
+ from math import ceil
3
+ from typing import Any
4
+
5
+ import numpy as np
6
+ import torch
7
+ import logging
8
+ from backend.device import is_openvino_device
9
+ from backend.lora import load_lora_weight
10
+ from backend.controlnet import (
11
+ load_controlnet_adapters,
12
+ update_controlnet_arguments,
13
+ )
14
+ from backend.models.lcmdiffusion_setting import (
15
+ DiffusionTask,
16
+ LCMDiffusionSetting,
17
+ LCMLora,
18
+ )
19
+ from backend.openvino.pipelines import (
20
+ get_ov_image_to_image_pipeline,
21
+ get_ov_text_to_image_pipeline,
22
+ ov_load_taesd,
23
+ )
24
+ from backend.pipelines.lcm import (
25
+ get_image_to_image_pipeline,
26
+ get_lcm_model_pipeline,
27
+ load_taesd,
28
+ )
29
+ from backend.pipelines.lcm_lora import get_lcm_lora_pipeline
30
+ from constants import DEVICE
31
+ from diffusers import LCMScheduler
32
+ from image_ops import resize_pil_image
33
+ from backend.openvino.flux_pipeline import get_flux_pipeline
34
+
35
+ try:
36
+ # support for token merging; keeping it optional for now
37
+ import tomesd
38
+ except ImportError:
39
+ print("tomesd library unavailable; disabling token merging support")
40
+ tomesd = None
41
+
42
+ class LCMTextToImage:
43
+ def __init__(
44
+ self,
45
+ device: str = "cpu",
46
+ ) -> None:
47
+ self.pipeline = None
48
+ self.use_openvino = False
49
+ self.device = ""
50
+ self.previous_model_id = None
51
+ self.previous_use_tae_sd = False
52
+ self.previous_use_lcm_lora = False
53
+ self.previous_ov_model_id = ""
54
+ self.previous_token_merging = 0.0
55
+ self.previous_safety_checker = False
56
+ self.previous_use_openvino = False
57
+ self.img_to_img_pipeline = None
58
+ self.is_openvino_init = False
59
+ self.previous_lora = None
60
+ self.task_type = DiffusionTask.text_to_image
61
+ self.torch_data_type = (
62
+ torch.float32 if is_openvino_device() or DEVICE == "mps" else torch.float16
63
+ )
64
+ print(f"Torch datatype : {self.torch_data_type}")
65
+
66
+ def _pipeline_to_device(self):
67
+ print(f"Pipeline device : {DEVICE}")
68
+ print(f"Pipeline dtype : {self.torch_data_type}")
69
+ self.pipeline.to(
70
+ torch_device=DEVICE,
71
+ torch_dtype=self.torch_data_type,
72
+ )
73
+
74
+ def _add_freeu(self):
75
+ pipeline_class = self.pipeline.__class__.__name__
76
+ if isinstance(self.pipeline.scheduler, LCMScheduler):
77
+ if pipeline_class == "StableDiffusionPipeline":
78
+ print("Add FreeU - SD")
79
+ self.pipeline.enable_freeu(
80
+ s1=0.9,
81
+ s2=0.2,
82
+ b1=1.2,
83
+ b2=1.4,
84
+ )
85
+ elif pipeline_class == "StableDiffusionXLPipeline":
86
+ print("Add FreeU - SDXL")
87
+ self.pipeline.enable_freeu(
88
+ s1=0.6,
89
+ s2=0.4,
90
+ b1=1.1,
91
+ b2=1.2,
92
+ )
93
+
94
+ def _enable_vae_tiling(self):
95
+ self.pipeline.vae.enable_tiling()
96
+
97
+ def _update_lcm_scheduler_params(self):
98
+ if isinstance(self.pipeline.scheduler, LCMScheduler):
99
+ self.pipeline.scheduler = LCMScheduler.from_config(
100
+ self.pipeline.scheduler.config,
101
+ beta_start=0.001,
102
+ beta_end=0.01,
103
+ )
104
+
105
+ def init(
106
+ self,
107
+ device: str = "cpu",
108
+ lcm_diffusion_setting: LCMDiffusionSetting = LCMDiffusionSetting(),
109
+ ) -> None:
110
+ self.device = device
111
+ self.use_openvino = lcm_diffusion_setting.use_openvino
112
+ model_id = lcm_diffusion_setting.lcm_model_id
113
+ use_local_model = lcm_diffusion_setting.use_offline_model
114
+ use_tiny_auto_encoder = lcm_diffusion_setting.use_tiny_auto_encoder
115
+ use_lora = lcm_diffusion_setting.use_lcm_lora
116
+ lcm_lora: LCMLora = lcm_diffusion_setting.lcm_lora
117
+ token_merging = lcm_diffusion_setting.token_merging
118
+ ov_model_id = lcm_diffusion_setting.openvino_lcm_model_id
119
+
120
+ if lcm_diffusion_setting.diffusion_task == DiffusionTask.image_to_image.value:
121
+ lcm_diffusion_setting.init_image = resize_pil_image(
122
+ lcm_diffusion_setting.init_image,
123
+ lcm_diffusion_setting.image_width,
124
+ lcm_diffusion_setting.image_height,
125
+ )
126
+
127
+ if (
128
+ self.pipeline is None
129
+ or self.previous_model_id != model_id
130
+ or self.previous_use_tae_sd != use_tiny_auto_encoder
131
+ or self.previous_lcm_lora_base_id != lcm_lora.base_model_id
132
+ or self.previous_lcm_lora_id != lcm_lora.lcm_lora_id
133
+ or self.previous_use_lcm_lora != use_lora
134
+ or self.previous_ov_model_id != ov_model_id
135
+ or self.previous_token_merging != token_merging
136
+ or self.previous_safety_checker != lcm_diffusion_setting.use_safety_checker
137
+ or self.previous_use_openvino != lcm_diffusion_setting.use_openvino
138
+ or (
139
+ self.use_openvino
140
+ and (
141
+ self.previous_task_type != lcm_diffusion_setting.diffusion_task
142
+ or self.previous_lora != lcm_diffusion_setting.lora
143
+ )
144
+ )
145
+ or lcm_diffusion_setting.rebuild_pipeline
146
+ ):
147
+ if self.use_openvino and is_openvino_device():
148
+ if self.pipeline:
149
+ del self.pipeline
150
+ self.pipeline = None
151
+ gc.collect()
152
+ self.is_openvino_init = True
153
+ if (
154
+ lcm_diffusion_setting.diffusion_task
155
+ == DiffusionTask.text_to_image.value
156
+ ):
157
+ print(f"***** Init Text to image (OpenVINO) - {ov_model_id} *****")
158
+ if "flux" in ov_model_id.lower():
159
+ print("Loading OpenVINO Flux pipeline")
160
+ self.pipeline = get_flux_pipeline(ov_model_id)
161
+ else:
162
+ self.pipeline = get_ov_text_to_image_pipeline(
163
+ ov_model_id,
164
+ use_local_model,
165
+ )
166
+ elif (
167
+ lcm_diffusion_setting.diffusion_task
168
+ == DiffusionTask.image_to_image.value
169
+ ):
170
+ print(f"***** Image to image (OpenVINO) - {ov_model_id} *****")
171
+ self.pipeline = get_ov_image_to_image_pipeline(
172
+ ov_model_id,
173
+ use_local_model,
174
+ )
175
+ else:
176
+ if self.pipeline:
177
+ del self.pipeline
178
+ self.pipeline = None
179
+ if self.img_to_img_pipeline:
180
+ del self.img_to_img_pipeline
181
+ self.img_to_img_pipeline = None
182
+
183
+ controlnet_args = load_controlnet_adapters(lcm_diffusion_setting)
184
+ if use_lora:
185
+ print(
186
+ f"***** Init LCM-LoRA pipeline - {lcm_lora.base_model_id} *****"
187
+ )
188
+ self.pipeline = get_lcm_lora_pipeline(
189
+ lcm_lora.base_model_id,
190
+ lcm_lora.lcm_lora_id,
191
+ use_local_model,
192
+ torch_data_type=self.torch_data_type,
193
+ pipeline_args=controlnet_args,
194
+ )
195
+
196
+ else:
197
+ print(f"***** Init LCM Model pipeline - {model_id} *****")
198
+ self.pipeline = get_lcm_model_pipeline(
199
+ model_id,
200
+ use_local_model,
201
+ controlnet_args,
202
+ )
203
+
204
+ self.img_to_img_pipeline = get_image_to_image_pipeline(self.pipeline)
205
+
206
+ if tomesd and token_merging > 0.001:
207
+ print(f"***** Token Merging: {token_merging} *****")
208
+ tomesd.apply_patch(self.pipeline, ratio=token_merging)
209
+ tomesd.apply_patch(self.img_to_img_pipeline, ratio=token_merging)
210
+
211
+ if use_tiny_auto_encoder:
212
+ if self.use_openvino and is_openvino_device():
213
+ print("Using Tiny Auto Encoder (OpenVINO)")
214
+ ov_load_taesd(
215
+ self.pipeline,
216
+ use_local_model,
217
+ )
218
+ else:
219
+ print("Using Tiny Auto Encoder")
220
+ load_taesd(
221
+ self.pipeline,
222
+ use_local_model,
223
+ self.torch_data_type,
224
+ )
225
+ load_taesd(
226
+ self.img_to_img_pipeline,
227
+ use_local_model,
228
+ self.torch_data_type,
229
+ )
230
+
231
+ if not self.use_openvino and not is_openvino_device():
232
+ self._pipeline_to_device()
233
+
234
+ if (
235
+ lcm_diffusion_setting.diffusion_task
236
+ == DiffusionTask.image_to_image.value
237
+ and lcm_diffusion_setting.use_openvino
238
+ ):
239
+ self.pipeline.scheduler = LCMScheduler.from_config(
240
+ self.pipeline.scheduler.config,
241
+ )
242
+ else:
243
+ self._update_lcm_scheduler_params()
244
+
245
+ if use_lora:
246
+ self._add_freeu()
247
+
248
+ self.previous_model_id = model_id
249
+ self.previous_ov_model_id = ov_model_id
250
+ self.previous_use_tae_sd = use_tiny_auto_encoder
251
+ self.previous_lcm_lora_base_id = lcm_lora.base_model_id
252
+ self.previous_lcm_lora_id = lcm_lora.lcm_lora_id
253
+ self.previous_use_lcm_lora = use_lora
254
+ self.previous_token_merging = lcm_diffusion_setting.token_merging
255
+ self.previous_safety_checker = lcm_diffusion_setting.use_safety_checker
256
+ self.previous_use_openvino = lcm_diffusion_setting.use_openvino
257
+ self.previous_task_type = lcm_diffusion_setting.diffusion_task
258
+ self.previous_lora = lcm_diffusion_setting.lora.model_copy(deep=True)
259
+ lcm_diffusion_setting.rebuild_pipeline = False
260
+ if (
261
+ lcm_diffusion_setting.diffusion_task
262
+ == DiffusionTask.text_to_image.value
263
+ ):
264
+ print(f"Pipeline : {self.pipeline}")
265
+ elif (
266
+ lcm_diffusion_setting.diffusion_task
267
+ == DiffusionTask.image_to_image.value
268
+ ):
269
+ if self.use_openvino and is_openvino_device():
270
+ print(f"Pipeline : {self.pipeline}")
271
+ else:
272
+ print(f"Pipeline : {self.img_to_img_pipeline}")
273
+ if self.use_openvino:
274
+ if lcm_diffusion_setting.lora.enabled:
275
+ print("Warning: Lora models not supported on OpenVINO mode")
276
+ else:
277
+ adapters = self.pipeline.get_active_adapters()
278
+ print(f"Active adapters : {adapters}")
279
+
280
+ def _get_timesteps(self):
281
+ time_steps = self.pipeline.scheduler.config.get("timesteps")
282
+ time_steps_value = [int(time_steps)] if time_steps else None
283
+ return time_steps_value
284
+
285
+ def generate(
286
+ self,
287
+ lcm_diffusion_setting: LCMDiffusionSetting,
288
+ reshape: bool = False,
289
+ ) -> Any:
290
+ guidance_scale = lcm_diffusion_setting.guidance_scale
291
+ img_to_img_inference_steps = lcm_diffusion_setting.inference_steps
292
+ check_step_value = int(
293
+ lcm_diffusion_setting.inference_steps * lcm_diffusion_setting.strength
294
+ )
295
+ if (
296
+ lcm_diffusion_setting.diffusion_task == DiffusionTask.image_to_image.value
297
+ and check_step_value < 1
298
+ ):
299
+ img_to_img_inference_steps = ceil(1 / lcm_diffusion_setting.strength)
300
+ print(
301
+ f"Strength: {lcm_diffusion_setting.strength},{img_to_img_inference_steps}"
302
+ )
303
+
304
+ if lcm_diffusion_setting.use_seed:
305
+ cur_seed = lcm_diffusion_setting.seed
306
+ if self.use_openvino:
307
+ np.random.seed(cur_seed)
308
+ else:
309
+ torch.manual_seed(cur_seed)
310
+
311
+ is_openvino_pipe = lcm_diffusion_setting.use_openvino and is_openvino_device()
312
+ if is_openvino_pipe:
313
+ print("Using OpenVINO")
314
+ if reshape and not self.is_openvino_init:
315
+ print("Reshape and compile")
316
+ self.pipeline.reshape(
317
+ batch_size=-1,
318
+ height=lcm_diffusion_setting.image_height,
319
+ width=lcm_diffusion_setting.image_width,
320
+ num_images_per_prompt=lcm_diffusion_setting.number_of_images,
321
+ )
322
+ self.pipeline.compile()
323
+
324
+ if self.is_openvino_init:
325
+ self.is_openvino_init = False
326
+
327
+ pipeline_extra_args = {}
328
+ if lcm_diffusion_setting.clip_skip > 1:
329
+ # We follow the convention that "CLIP Skip == 2" means "skip
330
+ # the last layer", so "CLIP Skip == 1" means "no skipping"
331
+ pipeline_extra_args['clip_skip'] = lcm_diffusion_setting.clip_skip - 1
332
+
333
+ if not lcm_diffusion_setting.use_safety_checker:
334
+ self.pipeline.safety_checker = None
335
+ if (
336
+ lcm_diffusion_setting.diffusion_task
337
+ == DiffusionTask.image_to_image.value
338
+ and not is_openvino_pipe
339
+ ):
340
+ self.img_to_img_pipeline.safety_checker = None
341
+
342
+ if (
343
+ not lcm_diffusion_setting.use_lcm_lora
344
+ and not lcm_diffusion_setting.use_openvino
345
+ and lcm_diffusion_setting.guidance_scale != 1.0
346
+ ):
347
+ print("Not using LCM-LoRA so setting guidance_scale 1.0")
348
+ guidance_scale = 1.0
349
+
350
+ controlnet_args = update_controlnet_arguments(lcm_diffusion_setting)
351
+ if lcm_diffusion_setting.use_openvino:
352
+ if (
353
+ lcm_diffusion_setting.diffusion_task
354
+ == DiffusionTask.text_to_image.value
355
+ ):
356
+ result_images = self.pipeline(
357
+ prompt=lcm_diffusion_setting.prompt,
358
+ negative_prompt=lcm_diffusion_setting.negative_prompt,
359
+ num_inference_steps=lcm_diffusion_setting.inference_steps,
360
+ guidance_scale=guidance_scale,
361
+ width=lcm_diffusion_setting.image_width,
362
+ height=lcm_diffusion_setting.image_height,
363
+ num_images_per_prompt=lcm_diffusion_setting.number_of_images,
364
+ ).images
365
+ elif (
366
+ lcm_diffusion_setting.diffusion_task
367
+ == DiffusionTask.image_to_image.value
368
+ ):
369
+ result_images = self.pipeline(
370
+ image=lcm_diffusion_setting.init_image,
371
+ strength=lcm_diffusion_setting.strength,
372
+ prompt=lcm_diffusion_setting.prompt,
373
+ negative_prompt=lcm_diffusion_setting.negative_prompt,
374
+ num_inference_steps=img_to_img_inference_steps * 3,
375
+ guidance_scale=guidance_scale,
376
+ num_images_per_prompt=lcm_diffusion_setting.number_of_images,
377
+ ).images
378
+
379
+ else:
380
+ if (
381
+ lcm_diffusion_setting.diffusion_task
382
+ == DiffusionTask.text_to_image.value
383
+ ):
384
+ result_images = self.pipeline(
385
+ prompt=lcm_diffusion_setting.prompt,
386
+ negative_prompt=lcm_diffusion_setting.negative_prompt,
387
+ num_inference_steps=lcm_diffusion_setting.inference_steps,
388
+ guidance_scale=guidance_scale,
389
+ width=lcm_diffusion_setting.image_width,
390
+ height=lcm_diffusion_setting.image_height,
391
+ num_images_per_prompt=lcm_diffusion_setting.number_of_images,
392
+ timesteps=self._get_timesteps(),
393
+ **pipeline_extra_args,
394
+ **controlnet_args,
395
+ ).images
396
+
397
+ elif (
398
+ lcm_diffusion_setting.diffusion_task
399
+ == DiffusionTask.image_to_image.value
400
+ ):
401
+ result_images = self.img_to_img_pipeline(
402
+ image=lcm_diffusion_setting.init_image,
403
+ strength=lcm_diffusion_setting.strength,
404
+ prompt=lcm_diffusion_setting.prompt,
405
+ negative_prompt=lcm_diffusion_setting.negative_prompt,
406
+ num_inference_steps=img_to_img_inference_steps,
407
+ guidance_scale=guidance_scale,
408
+ width=lcm_diffusion_setting.image_width,
409
+ height=lcm_diffusion_setting.image_height,
410
+ num_images_per_prompt=lcm_diffusion_setting.number_of_images,
411
+ **pipeline_extra_args,
412
+ **controlnet_args,
413
+ ).images
414
+ return result_images
src/backend/lora.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ from os import path
3
+ from paths import get_file_name, FastStableDiffusionPaths
4
+ from pathlib import Path
5
+
6
+
7
+ # A basic class to keep track of the currently loaded LoRAs and
8
+ # their weights; the diffusers function \c get_active_adapters()
9
+ # returns a list of adapter names but not their weights so we need
10
+ # a way to keep track of the current LoRA weights to set whenever
11
+ # a new LoRA is loaded
12
+ class _lora_info:
13
+ def __init__(
14
+ self,
15
+ path: str,
16
+ weight: float,
17
+ ):
18
+ self.path = path
19
+ self.adapter_name = get_file_name(path)
20
+ self.weight = weight
21
+
22
+ def __del__(self):
23
+ self.path = None
24
+ self.adapter_name = None
25
+
26
+
27
+ _loaded_loras = []
28
+ _current_pipeline = None
29
+
30
+
31
+ # This function loads a LoRA from the LoRA path setting, so it's
32
+ # possible to load multiple LoRAs by calling this function more than
33
+ # once with a different LoRA path setting; note that if you plan to
34
+ # load multiple LoRAs and dynamically change their weights, you
35
+ # might want to set the LoRA fuse option to False
36
+ def load_lora_weight(
37
+ pipeline,
38
+ lcm_diffusion_setting,
39
+ ):
40
+ if not lcm_diffusion_setting.lora.path:
41
+ raise Exception("Empty lora model path")
42
+
43
+ if not path.exists(lcm_diffusion_setting.lora.path):
44
+ raise Exception("Lora model path is invalid")
45
+
46
+ # If the pipeline has been rebuilt since the last call, remove all
47
+ # references to previously loaded LoRAs and store the new pipeline
48
+ global _loaded_loras
49
+ global _current_pipeline
50
+ if pipeline != _current_pipeline:
51
+ for lora in _loaded_loras:
52
+ del lora
53
+ del _loaded_loras
54
+ _loaded_loras = []
55
+ _current_pipeline = pipeline
56
+
57
+ current_lora = _lora_info(
58
+ lcm_diffusion_setting.lora.path,
59
+ lcm_diffusion_setting.lora.weight,
60
+ )
61
+ _loaded_loras.append(current_lora)
62
+
63
+ if lcm_diffusion_setting.lora.enabled:
64
+ print(f"LoRA adapter name : {current_lora.adapter_name}")
65
+ pipeline.load_lora_weights(
66
+ FastStableDiffusionPaths.get_lora_models_path(),
67
+ weight_name=Path(lcm_diffusion_setting.lora.path).name,
68
+ local_files_only=True,
69
+ adapter_name=current_lora.adapter_name,
70
+ )
71
+ update_lora_weights(
72
+ pipeline,
73
+ lcm_diffusion_setting,
74
+ )
75
+
76
+ if lcm_diffusion_setting.lora.fuse:
77
+ pipeline.fuse_lora()
78
+
79
+
80
+ def get_lora_models(root_dir: str):
81
+ lora_models = glob.glob(f"{root_dir}/**/*.safetensors", recursive=True)
82
+ lora_models_map = {}
83
+ for file_path in lora_models:
84
+ lora_name = get_file_name(file_path)
85
+ if lora_name is not None:
86
+ lora_models_map[lora_name] = file_path
87
+ return lora_models_map
88
+
89
+
90
+ # This function returns a list of (adapter_name, weight) tuples for the
91
+ # currently loaded LoRAs
92
+ def get_active_lora_weights():
93
+ active_loras = []
94
+ for lora_info in _loaded_loras:
95
+ active_loras.append(
96
+ (
97
+ lora_info.adapter_name,
98
+ lora_info.weight,
99
+ )
100
+ )
101
+ return active_loras
102
+
103
+
104
+ # This function receives a pipeline, an lcm_diffusion_setting object and
105
+ # an optional list of updated (adapter_name, weight) tuples
106
+ def update_lora_weights(
107
+ pipeline,
108
+ lcm_diffusion_setting,
109
+ lora_weights=None,
110
+ ):
111
+ global _loaded_loras
112
+ global _current_pipeline
113
+ if pipeline != _current_pipeline:
114
+ print("Wrong pipeline when trying to update LoRA weights")
115
+ return
116
+ if lora_weights:
117
+ for idx, lora in enumerate(lora_weights):
118
+ if _loaded_loras[idx].adapter_name != lora[0]:
119
+ print("Wrong adapter name in LoRA enumeration!")
120
+ continue
121
+ _loaded_loras[idx].weight = lora[1]
122
+
123
+ adapter_names = []
124
+ adapter_weights = []
125
+ if lcm_diffusion_setting.use_lcm_lora:
126
+ adapter_names.append("lcm")
127
+ adapter_weights.append(1.0)
128
+ for lora in _loaded_loras:
129
+ adapter_names.append(lora.adapter_name)
130
+ adapter_weights.append(lora.weight)
131
+ pipeline.set_adapters(
132
+ adapter_names,
133
+ adapter_weights=adapter_weights,
134
+ )
135
+ adapter_weights = zip(adapter_names, adapter_weights)
136
+ print(f"Adapters: {list(adapter_weights)}")
src/backend/models/device.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class DeviceInfo(BaseModel):
5
+ device_type: str
6
+ device_name: str
7
+ os: str
8
+ platform: str
9
+ processor: str
src/backend/models/gen_images.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from enum import Enum, auto
3
+ from paths import FastStableDiffusionPaths
4
+
5
+
6
+ class ImageFormat(str, Enum):
7
+ """Image format"""
8
+
9
+ JPEG = "jpeg"
10
+ PNG = "png"
11
+
12
+
13
+ class GeneratedImages(BaseModel):
14
+ path: str = FastStableDiffusionPaths.get_results_path()
15
+ format: str = ImageFormat.PNG.value.upper()
16
+ save_image: bool = True