Yjiggfghhjnjj commited on
Commit
440061e
1 Parent(s): 8e3d74a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -16
app.py CHANGED
@@ -5,6 +5,18 @@ from huggingface_hub import hf_hub_download
5
  from safetensors.torch import load_file
6
  import spaces
7
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
9
 
10
  ### RealVisXL V3 ###
@@ -75,16 +87,85 @@ pipe_hyper.to("cuda")
75
  del unet
76
 
77
  @spaces.GPU
78
- def run_comparison(prompt, progress=gr.Progress(track_tqdm=True)):
79
- image_turbo=pipe_turbo(prompt=prompt, num_inference_steps=1, guidance_scale=0).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  yield image_turbo, None, None, None, None
81
- image_lightning=pipe_lightning(prompt=prompt, num_inference_steps=1, guidance_scale=0).images[0]
 
 
 
 
 
 
 
 
 
 
 
82
  yield image_turbo, image_lightning, None, None, None
83
- image_hyper=pipe_hyper(prompt=prompt, num_inference_steps=1, guidance_scale=0, timesteps=[800]).images[0]
 
 
 
 
 
 
 
 
 
 
 
84
  yield image_turbo, image_lightning, image_hyper, None, None
85
- image_r3=RealVisXLv3_pipe(prompt=prompt, num_inference_steps=8, guidance_scale=0).images[0]
 
 
 
 
 
 
 
 
 
 
 
86
  yield image_turbo, image_lightning, image_hyper,image_r3, None
87
- image_r4=RealVisXLv4_pipe(prompt=prompt, num_inference_steps=8, guidance_scale=0).images[0]
 
 
 
 
 
 
 
 
 
 
 
88
  yield image_turbo, image_lightning, image_hyper,image_r3, image_r4
89
 
90
  examples = ["A dignified beaver wearing glasses, a vest, and colorful neck tie.",
@@ -100,6 +181,7 @@ with gr.Blocks() as demo:
100
  gr.Markdown("## One step SDXL comparison 🦶")
101
  gr.Markdown('Compare SDXL variants and distillations able to generate images in a single diffusion step')
102
  prompt = gr.Textbox(label="Prompt")
 
103
  with gr.Accordion("Advanced options", open=False):
104
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
105
  negative_prompt = gr.Text(
@@ -159,35 +241,56 @@ with gr.Blocks() as demo:
159
  value=6,
160
  )
161
 
162
- run = gr.Button("Run")
163
  with gr.Row():
164
  with gr.Column():
165
- image_turbo = gr.Image(label="SDXL Turbo")
166
  gr.Markdown("## [SDXL Turbo](https://huggingface.co/stabilityai/sdxl-turbo)")
167
  with gr.Column():
168
- image_lightning = gr.Image(label="SDXL Lightning")
169
  gr.Markdown("## [SDXL Lightning](https://huggingface.co/ByteDance/SDXL-Lightning)")
170
  with gr.Column():
171
- image_hyper = gr.Image(label="Hyper SDXL")
172
  gr.Markdown("## [Hyper SDXL](https://huggingface.co/ByteDance/Hyper-SD)")
173
  with gr.Column():
174
- image_r3 = gr.Image(label="RealVisXL V3")
175
  gr.Markdown("## [RealVisXL V3](https://huggingface.co)")
176
  with gr.Column():
177
- image_r4 = gr.Image(label="RealVisXL V4")
178
  gr.Markdown("## [RealVisXL V3](https://huggingface.co)")
179
  image_outputs = [image_turbo, image_lightning, image_hyper, image_r3, image_r4]
180
  gr.on(
181
- triggers=[prompt.submit, run.click],
 
 
 
 
182
  fn=run_comparison,
183
- inputs=prompt,
184
- outputs=image_outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  )
186
  gr.Examples(
187
  examples=examples,
188
  fn=run_comparison,
189
  inputs=prompt,
190
- outputs=image_outputs,
191
  cache_examples=False,
192
  run_on_click=True
193
  )
 
5
  from safetensors.torch import load_file
6
  import spaces
7
 
8
+
9
+ def save_image(img):
10
+ unique_name = str(uuid.uuid4()) + ".png"
11
+ img.save(unique_name)
12
+ return unique_name
13
+
14
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
15
+ if randomize_seed:
16
+ seed = random.randint(0, MAX_SEED)
17
+ return seed
18
+
19
+ MAX_SEED = np.iinfo(np.int32).max
20
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
21
 
22
  ### RealVisXL V3 ###
 
87
  del unet
88
 
89
  @spaces.GPU
90
+ def run_comparison(prompt: str,
91
+ negative_prompt: str = "",
92
+ use_negative_prompt: bool = False,
93
+ num_inference_steps: int = 30,
94
+ num_images_per_prompt: int = 2,
95
+ seed: int = 0,
96
+ width: int = 1024,
97
+ height: int = 1024,
98
+ guidance_scale: float = 3,
99
+ randomize_seed: bool = False,
100
+ progress=gr.Progress(track_tqdm=True),
101
+ ):
102
+ seed = int(randomize_seed_fn(seed, randomize_seed))
103
+ if not use_negative_prompt:
104
+ negative_prompt = ""
105
+ image_turbo=pipe_turbo(prompt=prompt,
106
+ negative_prompt=negative_prompt,
107
+ width=width,
108
+ height=height,
109
+ guidance_scale=guidance_scale,
110
+ num_inference_steps=num_inference_steps,
111
+ num_images_per_prompt=num_images_per_prompt,
112
+ cross_attention_kwargs={"scale": 0.65},
113
+ output_type="pil",
114
+ ).images
115
+ image_paths = [save_image(img) for img in images]
116
+ return image_paths, seed
117
  yield image_turbo, None, None, None, None
118
+ image_lightning=pipe_lightning(prompt=prompt,
119
+ negative_prompt=negative_prompt,
120
+ width=width,
121
+ height=height,
122
+ guidance_scale=guidance_scale,
123
+ num_inference_steps=num_inference_steps,
124
+ num_images_per_prompt=num_images_per_prompt,
125
+ cross_attention_kwargs={"scale": 0.65},
126
+ output_type="pil",
127
+ ).images
128
+ image_paths = [save_image(img) for img in images]
129
+ return image_paths, seed
130
  yield image_turbo, image_lightning, None, None, None
131
+ image_hyper=pipe_hyper(prompt=prompt,
132
+ negative_prompt=negative_prompt,
133
+ width=width,
134
+ height=height,
135
+ guidance_scale=guidance_scale,
136
+ num_inference_steps=num_inference_steps,
137
+ num_images_per_prompt=num_images_per_prompt,
138
+ cross_attention_kwargs={"scale": 0.65},
139
+ output_type="pil",
140
+ ).images
141
+ image_paths = [save_image(img) for img in images]
142
+ return image_paths, seed
143
  yield image_turbo, image_lightning, image_hyper, None, None
144
+ image_r3=RealVisXLv3_pipe(prompt=prompt,
145
+ negative_prompt=negative_prompt,
146
+ width=width,
147
+ height=height,
148
+ guidance_scale=guidance_scale,
149
+ num_inference_steps=num_inference_steps,
150
+ num_images_per_prompt=num_images_per_prompt,
151
+ cross_attention_kwargs={"scale": 0.65},
152
+ output_type="pil",
153
+ ).images
154
+ image_paths = [save_image(img) for img in images]
155
+ return image_paths, seed
156
  yield image_turbo, image_lightning, image_hyper,image_r3, None
157
+ image_r4=RealVisXLv4_pipe(prompt=prompt,
158
+ negative_prompt=negative_prompt,
159
+ width=width,
160
+ height=height,
161
+ guidance_scale=guidance_scale,
162
+ num_inference_steps=num_inference_steps,
163
+ num_images_per_prompt=num_images_per_prompt,
164
+ cross_attention_kwargs={"scale": 0.65},
165
+ output_type="pil",
166
+ ).images
167
+ image_paths = [save_image(img) for img in images]
168
+ return image_paths, seed
169
  yield image_turbo, image_lightning, image_hyper,image_r3, image_r4
170
 
171
  examples = ["A dignified beaver wearing glasses, a vest, and colorful neck tie.",
 
181
  gr.Markdown("## One step SDXL comparison 🦶")
182
  gr.Markdown('Compare SDXL variants and distillations able to generate images in a single diffusion step')
183
  prompt = gr.Textbox(label="Prompt")
184
+ run = gr.Button("Run")
185
  with gr.Accordion("Advanced options", open=False):
186
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
187
  negative_prompt = gr.Text(
 
241
  value=6,
242
  )
243
 
 
244
  with gr.Row():
245
  with gr.Column():
246
+ image_turbo = gr.Gallery(label="SDXL Turbo",columns=1, preview=True,)
247
  gr.Markdown("## [SDXL Turbo](https://huggingface.co/stabilityai/sdxl-turbo)")
248
  with gr.Column():
249
+ image_lightning = gr.Gallery(label="SDXL Lightning",columns=1, preview=True,)
250
  gr.Markdown("## [SDXL Lightning](https://huggingface.co/ByteDance/SDXL-Lightning)")
251
  with gr.Column():
252
+ image_hyper = gr.Gallery(label="Hyper SDXL",columns=1, preview=True,)
253
  gr.Markdown("## [Hyper SDXL](https://huggingface.co/ByteDance/Hyper-SD)")
254
  with gr.Column():
255
+ image_r3 = gr.Gallery(label="RealVisXL V3",columns=1, preview=True,)
256
  gr.Markdown("## [RealVisXL V3](https://huggingface.co)")
257
  with gr.Column():
258
+ image_r4 = gr.Gallery(label="RealVisXL V4",columns=1, preview=True,)
259
  gr.Markdown("## [RealVisXL V3](https://huggingface.co)")
260
  image_outputs = [image_turbo, image_lightning, image_hyper, image_r3, image_r4]
261
  gr.on(
262
+ triggers=[
263
+ prompt.submit,
264
+ negative_prompt.submit,
265
+ run_button.click,
266
+ ],
267
  fn=run_comparison,
268
+ inputs=[
269
+ prompt,
270
+ negative_prompt,
271
+ use_negative_prompt,
272
+ num_inference_steps,
273
+ num_images_per_prompt,
274
+ seed,
275
+ width,
276
+ height,
277
+ guidance_scale,
278
+ randomize_seed,
279
+ ],
280
+ outputs=[image_outputs, seed],
281
+ api_name="run",
282
+ )
283
+ use_negative_prompt.change(
284
+ fn=lambda x: gr.update(visible=x),
285
+ inputs=use_negative_prompt,
286
+ outputs=negative_prompt,
287
+ api_name=False,
288
  )
289
  gr.Examples(
290
  examples=examples,
291
  fn=run_comparison,
292
  inputs=prompt,
293
+ outputs=[image_outputs, seed],
294
  cache_examples=False,
295
  run_on_click=True
296
  )