SunderAli17 commited on
Commit
d80b6f8
·
verified ·
1 Parent(s): 0a7d8a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +329 -108
app.py CHANGED
@@ -1,41 +1,126 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- from diffusers import DiffusionPipeline
5
  import torch
 
 
 
 
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
8
 
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
 
 
22
 
 
 
 
 
 
 
 
 
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  generator = torch.Generator().manual_seed(seed)
27
-
28
- image = pipe(
29
- prompt = prompt,
30
- negative_prompt = negative_prompt,
31
- guidance_scale = guidance_scale,
32
- num_inference_steps = num_inference_steps,
33
- width = width,
34
- height = height,
35
- generator = generator
36
- ).images[0]
37
-
38
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  examples = [
41
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -50,97 +135,233 @@ css="""
50
  }
51
  """
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- with gr.Blocks(css=css) as demo:
 
59
 
60
  with gr.Column(elem_id="col-container"):
61
- gr.Markdown(f"""
62
- # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
- """)
65
 
 
 
 
 
 
 
 
 
 
 
 
66
  with gr.Row():
67
-
68
- prompt = gr.Text(
69
- label="Prompt",
70
- show_label=False,
71
- max_lines=1,
72
- placeholder="Enter your prompt",
73
- container=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  )
75
-
76
- run_button = gr.Button("Run", scale=0)
77
-
78
- result = gr.Image(label="Result", show_label=False)
79
-
80
- with gr.Accordion("Advanced Settings", open=False):
81
-
82
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
  )
88
-
89
- seed = gr.Slider(
90
- label="Seed",
91
- minimum=0,
92
- maximum=MAX_SEED,
 
 
 
 
 
 
 
 
93
  step=1,
94
- value=0,
95
  )
96
-
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
-
99
- with gr.Row():
100
-
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
-
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
-
117
- with gr.Row():
118
-
119
- guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
- step=0.1,
124
- value=0.0,
125
- )
126
-
127
- num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
- minimum=1,
130
- maximum=12,
131
- step=1,
132
- value=2,
133
- )
134
-
135
- gr.Examples(
136
- examples = examples,
137
- inputs = [prompt]
138
- )
139
 
140
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  )
145
 
146
- demo.queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ from diffusers import AutoencoderKL, DiffusionPipeline
5
  import torch
6
+ from __future__ import annotations
7
+ import os
8
+ import PIL.Image
9
+ import spaces
10
 
11
+ MARKDOWN = """
12
+ The demo is based on <a href="https://huggingface.co/dataautogpt3/OpenDalleV1.1">OpenDalle V1.1</a> by @dataautogpt3
13
+ The demo is based on the fusion of different models to provide better performance, comparatively.
14
 
15
+ You can try out the prompts and check for yourself.
 
 
 
 
 
 
 
16
 
17
+ **Parts of codes are adopted from [@hysts's SD-XL demo](https://huggingface.co/spaces/hysts/SD-XL) running on A10G GPU **
 
18
 
19
+ You can check out more of my spaces. Demo by [Sunder Ali Khowaja](https://sander-ali.github.io) - [Github](https://github.com/sander-ali)
20
+ """
21
+ if not torch.cuda.is_available():
22
+ DESCRIPTION += "\n<h1>Running on CPU 🥶 This demo does not work on CPU. </h1>"
23
 
24
+ MAX_SEED = np.iinfo(np.int32).max
25
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
26
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
27
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
28
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
29
+ ENABLE_REFINER = os.getenv("ENABLE_REFINER", "0") == "1"
30
+
31
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
32
  if randomize_seed:
33
  seed = random.randint(0, MAX_SEED)
34
+ return seed
35
+
36
+ device = "cuda" if torch.cuda.is_available() else "cpu"
37
+
38
+ if torch.cuda.is_available():
39
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
40
+ pipe = DiffusionPipeline.from_pretrained("dataautogpt3/OpenDalleV1.1", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
41
+ pipe.enable_xformers_memory_efficient_attention()
42
+ if ENABLE_REFINER:
43
+ refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
44
+ if ENABLE_CPU_OFFLOAD:
45
+ pipe.enable_model_cpu_offload()
46
+ if ENABLE_REFINER:
47
+ refiner.enable_model_cpu_offload()
48
+ else:
49
+ pipe.to(device)
50
+ if ENABLE_REFINER:
51
+ refiner.to(device)
52
+ if USE_TORCH_COMPILE:
53
+ pipe.unet = torch.compile(pipe.unet, mode='reduce-overhead', fullgraph=True)
54
+ if ENABLE_REFINER:
55
+ refiner.unet = torch.compile(refiner.unet, mode="reduce_overhead", fullgraph=True)
56
+
57
+ @spaces.GPU
58
+ @spaces.GPU
59
+ def infer(
60
+ prompt: str,
61
+ seed: int = 0,
62
+ width: int = 1024,
63
+ height: int = 1024,
64
+ guidance_scale_base: float = 5.0,
65
+ guidance_scale_refiner: float = 5.0,
66
+ num_inference_steps_base: int = 25,
67
+ num_inference_steps_refiner: int = 25,
68
+ apply_refiner: bool = False,
69
+ negative_prompt: str = "",
70
+ prompt_2: str = "",
71
+ negative_prompt_2: str = "",
72
+ use_negative_prompt: bool = False,
73
+ use_prompt_2: bool = False,
74
+ use_negative_prompt_2: bool = False,
75
+ progress=gr.Progress(track_tqdm=True),
76
+ ) -> PIL.Image.Image:
77
+ print(f"** Generating image for: \"{prompt}\" **")
78
  generator = torch.Generator().manual_seed(seed)
79
+
80
+ if not use_negative_prompt:
81
+ negative_prompt = None # type: ignore
82
+ if not use_prompt_2:
83
+ prompt_2 = None # type: ignore
84
+ if not use_negative_prompt_2:
85
+ negative_prompt_2 = None # type: ignore
86
+
87
+ if not apply_refiner:
88
+ return pipe(
89
+ prompt=prompt,
90
+ negative_prompt=negative_prompt,
91
+ prompt_2=prompt_2,
92
+ negative_prompt_2=negative_prompt_2,
93
+ width=width,
94
+ height=height,
95
+ guidance_scale=guidance_scale_base,
96
+ num_inference_steps=num_inference_steps_base,
97
+ generator=generator,
98
+ output_type="pil",
99
+ ).images[0]
100
+ else:
101
+ latents = pipe(
102
+ prompt=prompt,
103
+ negative_prompt=negative_prompt,
104
+ prompt_2=prompt_2,
105
+ negative_prompt_2=negative_prompt_2,
106
+ width=width,
107
+ height=height,
108
+ guidance_scale=guidance_scale_base,
109
+ num_inference_steps=num_inference_steps_base,
110
+ generator=generator,
111
+ output_type="latent",
112
+ ).images
113
+ image = refiner(
114
+ prompt=prompt,
115
+ negative_prompt=negative_prompt,
116
+ prompt_2=prompt_2,
117
+ negative_prompt_2=negative_prompt_2,
118
+ guidance_scale=guidance_scale_refiner,
119
+ num_inference_steps=num_inference_steps_refiner,
120
+ image=latents,
121
+ generator=generator,
122
+ ).images[0]
123
+ return image
124
 
125
  examples = [
126
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
135
  }
136
  """
137
 
138
+ # if torch.cuda.is_available():
139
+ # power_device = "GPU"
140
+ # else:
141
+ # power_device = "CPU"
142
+ theme = gr.themes.Glass(
143
+ primary_hue="blue",
144
+ secondary_hue="blue",
145
+ neutral_hue="gray",
146
+ text_size="md",
147
+ spacing_size="md",
148
+ radius_size="md",
149
+ font=[gr.themes.GoogleFont('Source Sans Pro'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
150
+ ).set(
151
+ body_background_fill_dark='*background_fill_primary',
152
+ background_fill_primary_dark='*neutral_950',
153
+ background_fill_secondary='*neutral_50',
154
+ background_fill_secondary_dark='*neutral_900',
155
+ border_color_primary_dark='*neutral_700',
156
+ block_background_fill='*background_fill_primary',
157
+ block_background_fill_dark='*neutral_800',
158
+ block_border_width='1px',
159
+ block_label_background_fill='*background_fill_primary',
160
+ block_label_background_fill_dark='*background_fill_secondary',
161
+ block_label_text_color='*neutral_500',
162
+ block_label_text_size='*text_sm',
163
+ block_label_text_weight='400',
164
+ block_shadow='none',
165
+ block_shadow_dark='none',
166
+ block_title_text_color='*neutral_500',
167
+ block_title_text_weight='400',
168
+ panel_border_width='0',
169
+ panel_border_width_dark='0',
170
+ checkbox_background_color_dark='*neutral_800',
171
+ checkbox_border_width='*input_border_width',
172
+ checkbox_label_border_width='*input_border_width',
173
+ input_background_fill='*neutral_100',
174
+ input_background_fill_dark='*neutral_700',
175
+ input_border_color_focus_dark='*neutral_700',
176
+ input_border_width='0px',
177
+ input_border_width_dark='0px',
178
+ slider_color='#2563eb',
179
+ slider_color_dark='#2563eb',
180
+ table_even_background_fill_dark='*neutral_950',
181
+ table_odd_background_fill_dark='*neutral_900',
182
+ button_border_width='*input_border_width',
183
+ button_shadow_active='none',
184
+ button_primary_background_fill='*primary_200',
185
+ button_primary_background_fill_dark='*primary_700',
186
+ button_primary_background_fill_hover='*button_primary_background_fill',
187
+ button_primary_background_fill_hover_dark='*button_primary_background_fill',
188
+ button_secondary_background_fill='*neutral_200',
189
+ button_secondary_background_fill_dark='*neutral_600',
190
+ button_secondary_background_fill_hover='*button_secondary_background_fill',
191
+ button_secondary_background_fill_hover_dark='*button_secondary_background_fill',
192
+ button_cancel_background_fill='*button_secondary_background_fill',
193
+ button_cancel_background_fill_dark='*button_secondary_background_fill',
194
+ button_cancel_background_fill_hover='*button_cancel_background_fill',
195
+ button_cancel_background_fill_hover_dark='*button_cancel_background_fill'
196
+ )
197
 
198
+
199
+ with gr.Blocks(css="footer{display:none !important}", theme=theme) as demo:
200
 
201
  with gr.Column(elem_id="col-container"):
202
+ gr.Markdown(MARKDOWN)
203
+ gr.DuplicateButton()
 
 
204
 
205
+ with gr.Group():
206
+ prompt = gr.Text(
207
+ label="Prompt",
208
+ show_label=False,
209
+ max_lines=1,
210
+ container=False,
211
+ placeholder="Enter your prompt",
212
+ )
213
+ run_button = gr.Button("Generate")
214
+ result = gr.Image(label="Result", show_label=False)
215
+ with gr.Accordion("Advanced options", open=False):
216
  with gr.Row():
217
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
218
+ use_prompt_2 = gr.Checkbox(label="Use prompt 2", value=False)
219
+ use_negative_prompt_2 = gr.Checkbox(label="Use negative prompt 2", value=False)
220
+ negative_prompt = gr.Text(
221
+ label="Negative prompt",
222
+ max_lines=1,
223
+ placeholder="Enter a negative prompt",
224
+ visible=False,
225
+ )
226
+ prompt_2 = gr.Text(
227
+ label="Prompt 2",
228
+ max_lines=1,
229
+ placeholder="Enter your prompt",
230
+ visible=False,
231
+ )
232
+ negative_prompt_2 = gr.Text(
233
+ label="Negative prompt 2",
234
+ max_lines=1,
235
+ placeholder="Enter a negative prompt",
236
+ visible=False,
237
+ )
238
+
239
+ seed = gr.Slider(
240
+ label="Seed",
241
+ minimum=0,
242
+ maximum=MAX_SEED,
243
+ step=1,
244
+ value=0,
245
+ )
246
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
247
+ with gr.Row():
248
+ width = gr.Slider(
249
+ label="Width",
250
+ minimum=256,
251
+ maximum=MAX_IMAGE_SIZE,
252
+ step=32,
253
+ value=1024,
254
  )
255
+ height = gr.Slider(
256
+ label="Height",
257
+ minimum=256,
258
+ maximum=MAX_IMAGE_SIZE,
259
+ step=32,
260
+ value=1024,
 
 
 
 
 
 
261
  )
262
+ apply_refiner = gr.Checkbox(label="Apply refiner", value=False, visible=ENABLE_REFINER)
263
+ with gr.Row():
264
+ guidance_scale_base = gr.Slider(
265
+ label="Guidance scale for base",
266
+ minimum=1,
267
+ maximum=20,
268
+ step=0.1,
269
+ value=5.0,
270
+ )
271
+ num_inference_steps_base = gr.Slider(
272
+ label="Number of inference steps for base",
273
+ minimum=10,
274
+ maximum=100,
275
  step=1,
276
+ value=25,
277
  )
278
+ with gr.Row(visible=False) as refiner_params:
279
+ guidance_scale_refiner = gr.Slider(
280
+ label="Guidance scale for refiner",
281
+ minimum=1,
282
+ maximum=20,
283
+ step=0.1,
284
+ value=5.0,
285
+ )
286
+ num_inference_steps_refiner = gr.Slider(
287
+ label="Number of inference steps for refiner",
288
+ minimum=10,
289
+ maximum=100,
290
+ step=1,
291
+ value=25,
292
+ )
293
+
294
+ gr.Examples(
295
+ examples=examples,
296
+ inputs=prompt,
297
+ outputs=result,
298
+ fn=infer,
299
+ cache_examples=CACHE_EXAMPLES,
300
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
 
302
+ use_negative_prompt.change(
303
+ fn=lambda x: gr.update(visible=x),
304
+ inputs=use_negative_prompt,
305
+ outputs=negative_prompt,
306
+ queue=False,
307
+ api_name=False,
308
+ )
309
+ use_prompt_2.change(
310
+ fn=lambda x: gr.update(visible=x),
311
+ inputs=use_prompt_2,
312
+ outputs=prompt_2,
313
+ queue=False,
314
+ api_name=False,
315
+ )
316
+ use_negative_prompt_2.change(
317
+ fn=lambda x: gr.update(visible=x),
318
+ inputs=use_negative_prompt_2,
319
+ outputs=negative_prompt_2,
320
+ queue=False,
321
+ api_name=False,
322
+ )
323
+ apply_refiner.change(
324
+ fn=lambda x: gr.update(visible=x),
325
+ inputs=apply_refiner,
326
+ outputs=refiner_params,
327
+ queue=False,
328
+ api_name=False,
329
  )
330
 
331
+ gr.on(
332
+ triggers=[
333
+ prompt.submit,
334
+ negative_prompt.submit,
335
+ prompt_2.submit,
336
+ negative_prompt_2.submit,
337
+ run_button.click,
338
+ ],
339
+ fn=randomize_seed_fn,
340
+ inputs=[seed, randomize_seed],
341
+ outputs=seed,
342
+ queue=False,
343
+ api_name=False,
344
+ ).then(
345
+ fn=infer,
346
+ inputs=[
347
+ prompt,
348
+ negative_prompt,
349
+ prompt_2,
350
+ negative_prompt_2,
351
+ use_negative_prompt,
352
+ use_prompt_2,
353
+ use_negative_prompt_2,
354
+ seed,
355
+ width,
356
+ height,
357
+ guidance_scale_base,
358
+ guidance_scale_refiner,
359
+ num_inference_steps_base,
360
+ num_inference_steps_refiner,
361
+ apply_refiner,
362
+ ],
363
+ outputs=result,
364
+ api_name="run",
365
+ )
366
+ if __name__ == "__main__":
367
+ demo.queue(max_size=20, api_open=False).launch(show_api=False)