eyal.benaroche commited on
Commit
f88f754
β€’
1 Parent(s): 55c0372
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +305 -0
  3. sdxl_lora.json +131 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Flash Lora
3
- emoji: πŸ‘
4
  colorFrom: blue
5
  colorTo: pink
6
  sdk: gradio
 
1
  ---
2
  title: Flash Lora
3
+ emoji: 🎨
4
  colorFrom: blue
5
  colorTo: pink
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+
4
+ import gradio as gr
5
+ import numpy as np
6
+ import spaces
7
+ import torch
8
+ from diffusers import DiffusionPipeline, LCMScheduler
9
+
10
+ with open("sdxl_lora.json", "r") as file:
11
+ data = json.load(file)
12
+ sdxl_loras_raw = [
13
+ {
14
+ "image": item["image"],
15
+ "title": item["title"],
16
+ "repo": item["repo"],
17
+ "trigger_word": item["trigger_word"],
18
+ "weights": item["weights"],
19
+ "is_pivotal": item.get("is_pivotal", False),
20
+ "text_embedding_weights": item.get("text_embedding_weights", None),
21
+ "likes": item.get("likes", 0),
22
+ }
23
+ for item in data
24
+ ]
25
+
26
+ # Sort the loras by likes
27
+ sdxl_loras_raw = sorted(sdxl_loras_raw, key=lambda x: x["likes"], reverse=True)
28
+
29
+ device = "cuda" if torch.cuda.is_available() else "cpu"
30
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
31
+
32
+ if gr.NO_RELOAD:
33
+ torch.cuda.max_memory_allocated(device=device)
34
+ pipe = DiffusionPipeline.from_pretrained(model_id, variant="fp16")
35
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
36
+ pipe.load_lora_weights("jasperai/flash-sdxl", adapter_name="lora")
37
+ pipe.to(device="cuda", dtype=torch.float16)
38
+
39
+
40
+ MAX_SEED = np.iinfo(np.int32).max
41
+ MAX_IMAGE_SIZE = 1024
42
+
43
+
44
+ def check_and_load_lora_user(user_lora_selector, user_lora_weight, gr_lora_loaded):
45
+ flash_sdxl_id = "jasperai/flash-sdxl"
46
+
47
+ global pipe
48
+ if user_lora_selector == "" or user_lora_selector == "":
49
+ raise gr.Error("Please select a LoRA before running the inference.")
50
+
51
+ if gr_lora_loaded != user_lora_selector:
52
+ gr.Info("Loading LoRA")
53
+ pipe.unload_lora_weights()
54
+ pipe.load_lora_weights(flash_sdxl_id, adapter_name="lora")
55
+ pipe.load_lora_weights(user_lora_selector, adapter_name="user")
56
+ pipe.set_adapters(["lora", "user"], adapter_weights=[1.0, user_lora_weight])
57
+ gr.Info("LoRA Loaded")
58
+
59
+ gr_lora_loaded = user_lora_selector
60
+
61
+ return gr_lora_loaded
62
+
63
+
64
+ def rescale_lora(user_lora_weight):
65
+
66
+ global pipe
67
+ pipe.set_adapters(["lora", "user"], adapter_weights=[1.0, user_lora_weight])
68
+
69
+
70
+ def update_selection(
71
+ selected_state: gr.SelectData,
72
+ gr_sdxl_loras,
73
+ ):
74
+
75
+ lora_id = gr_sdxl_loras[selected_state.index]["repo"]
76
+ trigger_word = gr_sdxl_loras[selected_state.index]["trigger_word"]
77
+
78
+ return lora_id, trigger_word
79
+
80
+
81
+ @spaces.GPU
82
+ def infer(
83
+ pre_prompt,
84
+ prompt,
85
+ seed,
86
+ randomize_seed,
87
+ num_inference_steps,
88
+ negative_prompt,
89
+ guidance_scale,
90
+ ):
91
+
92
+ if randomize_seed:
93
+ seed = random.randint(0, MAX_SEED)
94
+
95
+ generator = torch.Generator().manual_seed(seed)
96
+
97
+ if pre_prompt != "":
98
+ prompt = f"{pre_prompt} {prompt}"
99
+
100
+ image = pipe(
101
+ prompt=prompt,
102
+ negative_prompt=negative_prompt,
103
+ guidance_scale=guidance_scale,
104
+ num_inference_steps=num_inference_steps,
105
+ generator=generator,
106
+ ).images[0]
107
+
108
+ return image
109
+
110
+
111
+ css = """
112
+
113
+ h1 {
114
+ text-align: center;
115
+ display:block;
116
+ }
117
+
118
+ p {
119
+ text-align: justify;
120
+ display:block;
121
+ }
122
+
123
+ """
124
+
125
+ if torch.cuda.is_available():
126
+ power_device = "GPU"
127
+ else:
128
+ power_device = "CPU"
129
+
130
+ with gr.Blocks(css=css) as demo:
131
+
132
+ gr.Markdown(
133
+ f"""
134
+ # ⚑ FlashDiffusion: FlashLoRA ⚑
135
+
136
+ This is an interactive demo of [Flash Diffusion](https://gojasper.github.io/flash-diffusion-project/) **on top of** existing LoRAs.
137
+
138
+ The distillation method proposed in [Flash Diffusion: Accelerating Any Conditional Diffusion Model for Few Steps Image Generation](http://arxiv.org/abs/2406.02347) *by ClΓ©ment Chadebec, Onur Tasar, Eyal Benaroche and Benjamin Aubin* from Jasper Research.
139
+ The LoRAs can be added **without** any retraining for similar results in most cases. Feel free to tweak the parameters and use your own LoRAs by giving a look at the [Github Repo](https://github.com/gojasper/flash-diffusion)
140
+ """
141
+ )
142
+ gr.Markdown(
143
+ "If you enjoy the space, please also promote *open-source* by giving a ⭐ to our repo [![GitHub Stars](https://img.shields.io/github/stars/gojasper/flash-diffusion?style=social)](https://github.com/gojasper/flash-diffusion)"
144
+ )
145
+
146
+ # Index of selected LoRA
147
+ gr_sdxl_loras = gr.State(value=sdxl_loras_raw)
148
+ # Serve as memory for currently loaded lora in pipe
149
+ gr_lora_loaded = gr.State(value="")
150
+ gr_lora_id = gr.State(value="")
151
+
152
+ with gr.Row():
153
+
154
+ with gr.Blocks():
155
+
156
+ with gr.Column():
157
+
158
+ user_lora_selector = gr.Textbox(
159
+ label="Current Selected LoRA",
160
+ max_lines=1,
161
+ interactive=False,
162
+ )
163
+
164
+ user_lora_weight = gr.Slider(
165
+ label="Selected LoRA Weight",
166
+ minimum=0.5,
167
+ maximum=3,
168
+ step=0.1,
169
+ value=1,
170
+ )
171
+
172
+ gallery = gr.Gallery(
173
+ value=[(item["image"], item["title"]) for item in sdxl_loras_raw],
174
+ label="SDXL LoRA Gallery",
175
+ allow_preview=False,
176
+ columns=3,
177
+ elem_id="gallery",
178
+ show_share_button=False,
179
+ )
180
+
181
+ with gr.Column():
182
+
183
+ with gr.Row():
184
+
185
+ prompt = gr.Text(
186
+ label="Prompt",
187
+ show_label=False,
188
+ max_lines=1,
189
+ placeholder="Enter your prompt",
190
+ container=False,
191
+ scale=5,
192
+ )
193
+
194
+ run_button = gr.Button("Run", scale=1)
195
+
196
+ result = gr.Image(label="Result", show_label=False)
197
+
198
+ with gr.Accordion("Advanced Settings", open=False):
199
+
200
+ pre_prompt = gr.Text(
201
+ label="Pre-Prompt",
202
+ show_label=True,
203
+ max_lines=1,
204
+ placeholder="Pre Prompt from the LoRA config",
205
+ container=True,
206
+ scale=5,
207
+ )
208
+
209
+ seed = gr.Slider(
210
+ label="Seed",
211
+ minimum=0,
212
+ maximum=MAX_SEED,
213
+ step=1,
214
+ value=0,
215
+ )
216
+
217
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
218
+
219
+ with gr.Row():
220
+
221
+ num_inference_steps = gr.Slider(
222
+ label="Number of inference steps",
223
+ minimum=4,
224
+ maximum=8,
225
+ step=1,
226
+ value=4,
227
+ )
228
+
229
+ with gr.Row():
230
+
231
+ guidance_scale = gr.Slider(
232
+ label="Guidance Scale",
233
+ minimum=1,
234
+ maximum=6,
235
+ step=0.5,
236
+ value=1,
237
+ )
238
+
239
+ hint_negative = gr.Markdown(
240
+ """πŸ’‘ _Hint : Negative Prompt will only work with Guidance > 1 but the model was
241
+ trained to be used with guidance = 1 (ie. without guidance).
242
+ Can degrade the results, use cautiously._"""
243
+ )
244
+
245
+ negative_prompt = gr.Text(
246
+ label="Negative Prompt",
247
+ show_label=False,
248
+ max_lines=1,
249
+ placeholder="Enter a negative Prompt",
250
+ container=False,
251
+ )
252
+
253
+ gr.on(
254
+ [
255
+ run_button.click,
256
+ seed.change,
257
+ randomize_seed.change,
258
+ # prompt.change,
259
+ prompt.submit,
260
+ negative_prompt.change,
261
+ negative_prompt.submit,
262
+ guidance_scale.change,
263
+ ],
264
+ fn=check_and_load_lora_user,
265
+ inputs=[user_lora_selector, user_lora_weight, gr_lora_loaded],
266
+ outputs=[gr_lora_loaded],
267
+ ).success(
268
+ fn=infer,
269
+ inputs=[
270
+ pre_prompt,
271
+ prompt,
272
+ seed,
273
+ randomize_seed,
274
+ num_inference_steps,
275
+ negative_prompt,
276
+ guidance_scale,
277
+ ],
278
+ outputs=[result],
279
+ show_progress=True,
280
+ )
281
+
282
+ user_lora_weight.change(
283
+ fn=rescale_lora,
284
+ inputs=[user_lora_weight],
285
+ outputs=[],
286
+ show_progress="hidden",
287
+ )
288
+
289
+ gallery.select(
290
+ fn=update_selection,
291
+ inputs=[gr_sdxl_loras],
292
+ outputs=[
293
+ user_lora_selector,
294
+ pre_prompt,
295
+ ],
296
+ show_progress=False,
297
+ )
298
+
299
+ gr.Markdown("**Disclaimer:**")
300
+ gr.Markdown(
301
+ "This demo is only for research purpose. Users are solely responsible for any content they create, and it is their obligation to ensure that it adheres to appropriate and ethical standards."
302
+ )
303
+
304
+
305
+ demo.queue().launch()
sdxl_lora.json ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "image": "https://cdn-uploads.huggingface.co/production/uploads/6303f37c3926de1f7ec42d3e/SSOQ9lfB1PVhXVWJiL7Mx.jpeg",
4
+ "title": "Pixel Art XL",
5
+ "repo": "nerijs/pixel-art-xl",
6
+ "trigger_word": "pixel art",
7
+ "weights": "pixel-art-xl.safetensors",
8
+ "likes": 341
9
+ },
10
+ {
11
+ "image": "https://huggingface.co/ostris/crayon_style_lora_sdxl/resolve/main/samples/02939-3715890861-a%20dog%20tripping%20balls%20on%20mushrooms%20%20%20_lora_crayons_v1_sdxl_1_.jpeg",
12
+ "title": "Crayon Style",
13
+ "repo": "ostris/crayon_style_lora_sdxl",
14
+ "trigger_word": "",
15
+ "weights": "crayons_v1_sdxl.safetensors",
16
+ "likes": 27
17
+ },
18
+ {
19
+ "image": "https://huggingface.co/TheLastBen/Papercut_SDXL/resolve/main/images/pc%20(13).webp",
20
+ "title": "Papercut SDXL",
21
+ "repo": "TheLastBen/Papercut_SDXL",
22
+ "trigger_word": "papercut",
23
+ "weights": "papercut.safetensors",
24
+ "likes": 46
25
+ },
26
+ {
27
+ "image": "https://huggingface.co/artificialguybr/ColoringBookRedmond-V2/resolve/main/00493-1759595235.png",
28
+ "title": "ColoringBook.Redmond V2",
29
+ "repo": "artificialguybr/ColoringBookRedmond-V2",
30
+ "trigger_word": "ColoringBookAF",
31
+ "weights": "ColoringBookRedmond-ColoringBook-ColoringBookAF.safetensors",
32
+ "likes": 18
33
+ },
34
+ {
35
+ "image": "https://huggingface.co/goofyai/3d_render_style_xl/resolve/main/3d_style_2.jpeg",
36
+ "title": "3D Render Style",
37
+ "repo": "goofyai/3d_render_style_xl",
38
+ "trigger_word": "3d style",
39
+ "weights": "3d_render_style_xl.safetensors",
40
+ "likes": 103
41
+ },
42
+ {
43
+ "image": "https://huggingface.co/ostris/watercolor_style_lora_sdxl/resolve/main/samples/03014-3386049647-marty%20mcfly%20%20_lora_watercolor_v1_sdxl_1_.jpeg",
44
+ "title": "Watercolor Style",
45
+ "repo": "ostris/watercolor_style_lora_sdxl",
46
+ "trigger_word": "",
47
+ "weights": "watercolor_v1_sdxl.safetensors",
48
+ "likes": 22
49
+ },
50
+ {
51
+ "image": "https://cdn-uploads.huggingface.co/production/uploads/635749860725c2f190a76e88/nD-or57fKyKY-HdYsDAC0.png",
52
+ "title": "Voxel XL",
53
+ "repo": "Fictiverse/Voxel_XL_Lora",
54
+ "trigger_word": "voxel style",
55
+ "weights": "VoxelXL_v1.safetensors",
56
+ "likes": 12,
57
+ "downloads": 0
58
+ },
59
+ {
60
+ "image": "https://huggingface.co/alvdansen/BandW-Manga/resolve/main/images/BW_e000014_02_20240610214209.jpeg",
61
+ "title": "B&W Manga",
62
+ "repo": "alvdansen/BandW-Manga",
63
+ "weights": "Little_Tinies.safetensors",
64
+ "trigger_word": "",
65
+ "likes": 98
66
+ },
67
+ {
68
+ "image": "https://huggingface.co/alvdansen/littletinies/resolve/main/images/227DE29148BC8798591C0EF99A41B71C44C0CAB5A16B976EFCC387C08D748DC0.jpeg",
69
+ "title": "Little Tinies",
70
+ "repo": "alvdansen/littletinies",
71
+ "weights": "Little_Tinies.safetensors",
72
+ "trigger_word": "",
73
+ "likes": 75
74
+ },
75
+ {
76
+ "image": "https://huggingface.co/alvdansen/sketchedoutmanga/resolve/main/images/Sketched_Out_Manga_e000007_01_20240605225456%20(1).jpeg",
77
+ "title": "Sketched Out Manga",
78
+ "repo": "alvdansen/sketchedoutmanga",
79
+ "weights": "Sketched_Out_Manga.safetensors",
80
+ "trigger_word": "daiton",
81
+ "likes": 13
82
+ },
83
+ {
84
+ "image": "https://huggingface.co/goofyai/Leonardo_Ai_Style_Illustration/resolve/main/leo-2.png",
85
+ "title": "Leonardo Style",
86
+ "repo": "goofyai/Leonardo_Ai_Style_Illustration",
87
+ "trigger_word": "leonardo style",
88
+ "weights": "leonardo_illustration.safetensors",
89
+ "likes": 4
90
+ },
91
+ {
92
+ "image": "https://huggingface.co/artificialguybr/TshirtDesignRedmond/resolve/main/00097-1339429505.png",
93
+ "repo": "artificialguybr/TshirtDesignRedmond-V2",
94
+ "title": "T-Shirt.Design.Redmond V2",
95
+ "trigger_word": "TshirtDesignAF",
96
+ "weights": "TShirtDesignRedmondV2-Tshirtdesign-TshirtDesignAF.safetensors",
97
+ "likes": 13
98
+ },
99
+ {
100
+ "image": "https://huggingface.co/artificialguybr/CuteCartoonRedmond-V2/resolve/main/00418-1769284661.png",
101
+ "title": "Cute Cartoon Redmond V2",
102
+ "repo": "artificialguybr/CuteCartoonRedmond-V2",
103
+ "trigger_word": "CuteCartoonAF, Cute Cartoon",
104
+ "weights": "CuteCartoonRedmond-CuteCartoon-CuteCartoonAF.safetensors",
105
+ "likes": 6
106
+ },
107
+ {
108
+ "image": "https://huggingface.co/Norod78/SDXL-YarnArtStyle-LoRA/resolve/main/images/00016-20240102204306-7790-Rick%20Sanchez%20Yarn%20art%20style%20%20_lora_SDXL_Yarn_Art_Style_1.0_.jpg",
109
+ "title": "Yarn Art style",
110
+ "repo": "Norod78/SDXL-YarnArtStyle-LoRA",
111
+ "trigger_word": "Yarn art style",
112
+ "weights": "SDXL_Yarn_Art_Style.safetensors",
113
+ "likes": 33
114
+ },
115
+ {
116
+ "image": "https://huggingface.co/blink7630/storyboard-sketch/resolve/main/2947992.jpeg",
117
+ "title": "Storyboard Sketch",
118
+ "repo": "blink7630/storyboard-sketch",
119
+ "trigger_word": "storyboard sketch of",
120
+ "weights": "Storyboard_sketch.safetensors",
121
+ "likes": 43
122
+ },
123
+ {
124
+ "image": "https://huggingface.co/e-n-v-y/envy-send-noodles-xl-01/resolve/main/2879347.jpeg",
125
+ "title": "Envy Send Noodles XL",
126
+ "repo": "e-n-v-y/envy-send-noodles-xl-01",
127
+ "trigger_word": "noodles",
128
+ "weights": "EnvySendNoodlesXL01.safetensors",
129
+ "likes": 4
130
+ }
131
+ ]