jacktheporsche commited on
Commit
dd0b77a
·
verified ·
1 Parent(s): 6fb0bb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -110
app.py CHANGED
@@ -1,177 +1,283 @@
1
- import os
2
- import gc
3
- import gradio as gr
4
- import numpy as np
5
- import torch
6
- import json
7
- import spaces
8
- import config
9
- import utils
10
  import logging
11
  from PIL import Image, PngImagePlugin
12
  from datetime import datetime
13
- from diffusers import StableDiffusionPipeline
 
14
 
15
  logging.basicConfig(level=logging.INFO)
16
  logger = logging.getLogger(__name__)
17
 
18
- DESCRIPTION = "Stable Diffusion 3 Medium Demo"
19
  if not torch.cuda.is_available():
20
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU. </p>"
21
  IS_COLAB = utils.is_google_colab() or os.getenv("IS_COLAB") == "1"
22
- HF_TOKEN = os.getenv("HF_TOKEN")
23
- CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
24
- MIN_IMAGE_SIZE = int(os.getenv("MIN_IMAGE_SIZE", "512"))
25
- MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
26
- USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
27
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
28
  OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./outputs")
29
 
30
- repo = "stabilityai/stable-diffusion-3-medium-diffusers"
31
- device = "cuda" if torch.cuda.is_available() else "cpu"
32
- dtype = torch.float16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  @spaces.GPU
35
  def generate(
36
- prompt: str,
37
- negative_prompt: str = "",
38
- seed: int = 0,
39
- custom_width: int = 1024,
40
  custom_height: int = 1024,
41
  guidance_scale: float = 7.0,
42
  num_inference_steps: int = 30,
43
- randomize_seed: bool = True,
 
 
 
 
44
  progress=gr.Progress(track_tqdm=True),
45
  ) -> list:
46
- if randomize_seed:
47
- seed = np.random.randint(0, np.iinfo(np.int32).max)
48
- generator = torch.Generator(device=device).manual_seed(seed)
49
-
50
- images = pipe(
51
- prompt=prompt,
52
- negative_prompt=negative_prompt,
53
- guidance_scale=guidance_scale,
54
- num_inference_steps=num_inference_steps,
55
- width=custom_width,
56
- height=custom_height,
57
- generator=generator,
58
- ).images
59
-
60
- return images, seed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if torch.cuda.is_available():
63
- pipe = StableDiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
64
  logger.info("Loaded on Device!")
65
  else:
66
  pipe = None
67
 
 
 
 
68
  with gr.Blocks(css="style.css") as demo:
69
  title = gr.HTML(
70
  f"""<h1><span>{DESCRIPTION}</span></h1>""",
71
  elem_id="title",
72
  )
73
  gr.Markdown(
74
- f"""Gradio demo for [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)""",
75
  elem_id="subtitle",
76
  )
77
  gr.DuplicateButton(
78
- value="Duplicate Space for private use",
79
- elem_id="duplicate-button",
80
- visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
81
- )
82
- with gr.Group():
83
- with gr.Row():
84
- prompt = gr.Text(
85
- label="Prompt",
86
- show_label=False,
87
- max_lines=5,
88
- placeholder="Enter your prompt",
89
- container=False,
90
- )
91
- run_button = gr.Button(
92
- "Generate",
93
- variant="primary",
94
- scale=0
95
- )
96
- result = gr.Gallery(
97
- label="Result",
98
- columns=1,
99
- preview=True,
100
- show_label=False
101
- )
102
- with gr.Accordion(label="Advanced Settings", open=False):
103
- negative_prompt = gr.Text(
104
- label="Negative Prompt",
105
- max_lines=5,
106
  placeholder="Enter a negative prompt",
107
  value=""
108
  )
109
- custom_width = gr.Slider(
110
- label="Width",
111
- minimum=MIN_IMAGE_SIZE,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=64,
114
- value=1024,
115
- )
116
- custom_height = gr.Slider(
117
- label="Height",
118
- minimum=MIN_IMAGE_SIZE,
119
- maximum=MAX_IMAGE_SIZE,
120
- step=64,
121
- value=1024,
122
  )
123
- with gr.Row():
124
- guidance_scale = gr.Slider(
125
- label="Guidance scale",
126
- minimum=1,
127
- maximum=12,
128
- step=0.1,
129
- value=7.0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  )
131
- num_inference_steps = gr.Slider(
132
- label="Number of inference steps",
133
  minimum=1,
134
- maximum=100,
135
- step=1,
136
- value=30,
 
137
  )
138
- seed = gr.Slider(
139
- label="Seed", minimum=0, maximum=np.iinfo(np.int32).max, step=1, value=0
 
 
 
 
140
  )
141
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  gr.Examples(
143
- examples=[
144
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
145
- "An astronaut riding a green horse",
146
- "A delicious ceviche cheesecake slice",
147
- ],
148
  inputs=prompt,
149
- outputs=result,
 
 
150
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  inputs = [
152
  prompt,
153
  negative_prompt,
154
- seed,
155
- custom_width,
156
  custom_height,
157
  guidance_scale,
158
  num_inference_steps,
159
- randomize_seed,
 
 
 
 
160
  ]
 
161
  prompt.submit(
 
 
 
 
 
 
162
  fn=generate,
163
  inputs=inputs,
164
- outputs=[result, seed],
 
165
  )
166
  negative_prompt.submit(
 
 
 
 
 
 
167
  fn=generate,
168
  inputs=inputs,
169
- outputs=[result, seed],
 
170
  )
171
  run_button.click(
 
 
 
 
 
 
172
  fn=generate,
173
  inputs=inputs,
174
- outputs=[result, seed],
 
175
  )
176
 
177
- demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB, show_error=True)
 
 
 
 
 
 
 
 
 
 
1
  import logging
2
  from PIL import Image, PngImagePlugin
3
  from datetime import datetime
4
+ from diffusers.models import AutoencoderKL
5
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
6
 
7
  logging.basicConfig(level=logging.INFO)
8
  logger = logging.getLogger(__name__)
9
 
10
+ DESCRIPTION = "RealVis XL"
11
  if not torch.cuda.is_available():
12
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU. </p>"
13
  IS_COLAB = utils.is_google_colab() or os.getenv("IS_COLAB") == "1"
 
 
 
 
 
14
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
15
  OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./outputs")
16
 
17
+ MODEL = os.getenv(
18
+ "MODEL",
19
+ "https://huggingface.co/SG161222/RealVisXL_V4.0/blob/main/RealVisXL_V4.0.safetensors",
20
+ )
21
+
22
+ torch.backends.cudnn.deterministic = True
23
+ torch.backends.cudnn.benchmark = False
24
+
25
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
26
+
27
+ def load_pipeline(model_name):
28
+ vae = AutoencoderKL.from_pretrained(
29
+ "madebyollin/sdxl-vae-fp16-fix",
30
+ torch_dtype=torch.float16,
31
+ )
32
+ pipeline = (
33
+ StableDiffusionXLPipeline.from_single_file
34
+ if MODEL.endswith(".safetensors")
35
+ else StableDiffusionXLPipeline.from_pretrained
36
+ )
37
+
38
+ pipe = pipeline(
39
+ model_name,
40
+ vae=vae,
41
+ torch_dtype=torch.float16,
42
+ custom_pipeline="lpw_stable_diffusion_xl",
43
+ use_safetensors=True,
44
+ add_watermarker=False,
45
+ use_auth_token=HF_TOKEN,
46
+ variant="fp16",
47
+ )
48
+
49
+ pipe.to(device)
50
+ return pipe
51
 
52
  @spaces.GPU
53
  def generate(
 
 
 
 
54
  custom_height: int = 1024,
55
  guidance_scale: float = 7.0,
56
  num_inference_steps: int = 30,
57
+ sampler: str = "DPM++ 2M SDE Karras",
58
+ aspect_ratio_selector: str = "1024 x 1024",
59
+ use_upscaler: bool = False,
60
+ upscaler_strength: float = 0.55,
61
+ upscale_by: float = 1.5,
62
  progress=gr.Progress(track_tqdm=True),
63
  ) -> list:
64
+ generator = utils.seed_everything(seed)
65
+
66
+ width, height = utils.aspect_ratio_handler(
67
+ aspect_ratio_selector,
68
+ custom_width,
69
+ custom_height,
70
+ )
71
+
72
+ width, height = utils.preprocess_image_dimensions(width, height)
73
+
74
+ backup_scheduler = pipe.scheduler
75
+ pipe.scheduler = utils.get_scheduler(pipe.scheduler.config, sampler)
76
+
77
+ metadata = {
78
+ "prompt": prompt,
79
+ "negative_prompt": negative_prompt,
80
+ "resolution": f"{width} x {height}",
81
+ "guidance_scale": guidance_scale,
82
+ "num_inference_steps": num_inference_steps,
83
+ "seed": seed,
84
+ "sampler": sampler,
85
+ "use_upscaler": use_upscaler,
86
+ "upscaler_strength": upscaler_strength,
87
+ "upscale_by": upscale_by,
88
+ }
89
+ logger.info(json.dumps(metadata, indent=4))
90
+
91
+ try:
92
+ images = pipe(
93
+ prompt=prompt,
94
+ negative_prompt=negative_prompt,
95
+ width=width,
96
+ height=height,
97
+ guidance_scale=guidance_scale,
98
+ num_inference_steps=num_inference_steps,
99
+ generator=generator,
100
+ output_type="pil",
101
+ ).images
102
+
103
+ if use_upscaler:
104
+ images = [image.resize((int(width * upscale_by), int(height * upscale_by))) for image in images]
105
+
106
+ return images, metadata
107
+ except Exception as e:
108
+ logger.exception(f"An error occurred: {e}")
109
+ raise
110
+ finally:
111
+ pipe.scheduler = backup_scheduler
112
+ utils.free_memory()
113
 
114
  if torch.cuda.is_available():
115
+ pipe = load_pipeline(MODEL)
116
  logger.info("Loaded on Device!")
117
  else:
118
  pipe = None
119
 
120
+ def postprocess_images(images):
121
+ return images # No caption, just return the images
122
+
123
  with gr.Blocks(css="style.css") as demo:
124
  title = gr.HTML(
125
  f"""<h1><span>{DESCRIPTION}</span></h1>""",
126
  elem_id="title",
127
  )
128
  gr.Markdown(
129
+ f"""Gradio demo for ([RealVis XL]https://huggingface.co/SG161222/RealVisXL_V4.0/)""",
130
  elem_id="subtitle",
131
  )
132
  gr.DuplicateButton(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  placeholder="Enter a negative prompt",
134
  value=""
135
  )
136
+ aspect_ratio_selector = gr.Radio(
137
+ label="Aspect Ratio",
138
+ choices=config.aspect_ratios,
139
+ value="1024 x 1024",
140
+ container=True,
141
+
 
 
 
 
 
 
 
142
  )
143
+ with gr.Group(visible=False) as custom_resolution:
144
+ with gr.Row():
145
+ custom_width = gr.Slider(
146
+ label="Width",
147
+ minimum=MIN_IMAGE_SIZE,
148
+ maximum=MAX_IMAGE_SIZE,
149
+ step=8,
150
+ value=1024,
151
+ )
152
+ custom_height = gr.Slider(
153
+ label="Height",
154
+ minimum=MIN_IMAGE_SIZE,
155
+ maximum=MAX_IMAGE_SIZE,
156
+ step=8,
157
+ value=1024,
158
+ )
159
+ use_upscaler = gr.Checkbox(label="Use Upscaler", value=False)
160
+ with gr.Row() as upscaler_row:
161
+ upscaler_strength = gr.Slider(
162
+ label="Strength",
163
+ minimum=0,
164
+ maximum=1,
165
+ step=0.05,
166
+ value=0.55,
167
+ visible=False,
168
  )
169
+ upscale_by = gr.Slider(
170
+ label="Upscale by",
171
  minimum=1,
172
+ maximum=1.5,
173
+ step=0.1,
174
+ value=1.5,
175
+ visible=False,
176
  )
177
+
178
+ sampler = gr.Dropdown(
179
+ label="Sampler",
180
+ choices=config.sampler_list,
181
+ interactive=True,
182
+ value="DPM++ 2M SDE Karras",
183
  )
184
+ with gr.Row():
185
+ seed = gr.Slider(
186
+ label="Seed", minimum=0, maximum=utils.MAX_SEED, step=1, value=0
187
+ )
188
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
189
+ with gr.Group():
190
+ with gr.Row():
191
+ guidance_scale = gr.Slider(
192
+ label="Guidance scale",
193
+ minimum=1,
194
+ maximum=12,
195
+ step=0.1,
196
+ value=7.0,
197
+ )
198
+ num_inference_steps = gr.Slider(
199
+ label="Number of inference steps",
200
+ minimum=1,
201
+ maximum=100,
202
+ step=1,
203
+ value=28,
204
+ )
205
+ with gr.Accordion(label="Generation Parameters", open=False):
206
+ gr_metadata = gr.JSON(label="Metadata", show_label=False)
207
  gr.Examples(
208
+ examples=config.examples,
209
+
210
+
211
+
212
+
213
  inputs=prompt,
214
+ outputs=[result, gr_metadata],
215
+ fn=lambda *args, **kwargs: generate(*args, use_upscaler=True, **kwargs),
216
+ cache_examples=CACHE_EXAMPLES,
217
  )
218
+ use_upscaler.change(
219
+ fn=lambda x: [gr.update(visible=x), gr.update(visible=x)],
220
+ inputs=use_upscaler,
221
+ outputs=[upscaler_strength, upscale_by],
222
+ queue=False,
223
+ api_name=False,
224
+ )
225
+ aspect_ratio_selector.change(
226
+ fn=lambda x: gr.update(visible=x == "Custom"),
227
+ inputs=aspect_ratio_selector,
228
+ outputs=custom_resolution,
229
+ queue=False,
230
+ api_name=False,
231
+ )
232
+
233
  inputs = [
234
  prompt,
235
  negative_prompt,
 
 
236
  custom_height,
237
  guidance_scale,
238
  num_inference_steps,
239
+ sampler,
240
+ aspect_ratio_selector,
241
+ use_upscaler,
242
+ upscaler_strength,
243
+ upscale_by,
244
  ]
245
+
246
  prompt.submit(
247
+ fn=utils.randomize_seed_fn,
248
+ inputs=[seed, randomize_seed],
249
+ outputs=seed,
250
+ queue=False,
251
+ api_name=False,
252
+ ).then(
253
  fn=generate,
254
  inputs=inputs,
255
+ outputs=[result, gr_metadata],
256
+ api_name="run",
257
  )
258
  negative_prompt.submit(
259
+ fn=utils.randomize_seed_fn,
260
+ inputs=[seed, randomize_seed],
261
+ outputs=seed,
262
+ queue=False,
263
+ api_name=False,
264
+ ).then(
265
  fn=generate,
266
  inputs=inputs,
267
+ outputs=[result, gr_metadata],
268
+ api_name=False,
269
  )
270
  run_button.click(
271
+ fn=utils.randomize_seed_fn,
272
+ inputs=[seed, randomize_seed],
273
+ outputs=seed,
274
+ queue=False,
275
+ api_name=False,
276
+ ).then(
277
  fn=generate,
278
  inputs=inputs,
279
+ outputs=[result, gr_metadata],
280
+ api_name=False,
281
  )
282
 
283
+ demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB, show_error=True)