elismasilva commited on
Commit
dc528d7
·
1 Parent(s): 2099bb6

support multimodel

Browse files
Files changed (1) hide show
  1. app.py +121 -13
app.py CHANGED
@@ -9,9 +9,12 @@ from pipeline.util import (
9
  create_hdr_effect,
10
  progressive_upscale,
11
  select_scheduler,
 
12
  )
13
 
14
  device = "cuda"
 
 
15
 
16
  # Initialize the models and pipeline
17
  controlnet = ControlNetUnionModel.from_pretrained(
@@ -19,19 +22,28 @@ controlnet = ControlNetUnionModel.from_pretrained(
19
  ).to(device=device)
20
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device=device)
21
 
22
- #model_id = "SG161222/RealVisXL_V5.0"
23
- model_id = "SG161222/RealVisXL_V5.0_Lightning"
24
- pipe = StableDiffusionXLControlNetTileSRPipeline.from_pretrained(
25
- model_id, controlnet=controlnet, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
26
- ).to(device)
 
 
 
 
 
 
 
 
 
 
27
 
28
- #pipe.enable_model_cpu_offload() # << Enable this if you have limited VRAM
29
- pipe.enable_vae_tiling() # << Enable this if you have limited VRAM
30
- pipe.enable_vae_slicing() # << Enable this if you have limited VRAM
31
 
32
  # region functions
33
  @spaces.GPU(duration=120)
34
  def predict(
 
35
  image,
36
  prompt,
37
  negative_prompt,
@@ -49,6 +61,9 @@ def predict(
49
  ):
50
  global pipe
51
 
 
 
 
52
  # Set selected scheduler
53
  print(f"Using scheduler: {scheduler}...")
54
  pipe.scheduler = select_scheduler(pipe, scheduler)
@@ -97,7 +112,6 @@ def predict(
97
 
98
  return image
99
 
100
-
101
  def clear_result():
102
  return gr.update(value=None)
103
 
@@ -111,6 +125,38 @@ def set_maximum_resolution(max_tile_size, current_value):
111
  def select_tile_weighting_method(tile_weighting_method):
112
  return gr.update(visible=True if tile_weighting_method=="Gaussian" else False)
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  # endregion
115
 
116
  css = """
@@ -207,6 +253,8 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
207
  result = gr.Image(
208
  label="Generated Image", show_label=True, format="png", interactive=False, scale=1, height=500, min_width=670
209
  )
 
 
210
  with gr.Row():
211
  with gr.Accordion("Input Prompt", open=False):
212
  with gr.Column():
@@ -231,6 +279,9 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
231
  with gr.Sidebar(label="Parameters", open=True):
232
  with gr.Row(elem_id="parameters_row"):
233
  gr.Markdown("### General parameters")
 
 
 
234
  tile_weighting_method = gr.Dropdown(
235
  label="Tile Weighting Method", choices=["Cosine", "Gaussian"], value="Cosine"
236
  )
@@ -260,7 +311,7 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
260
  negative_prompt.value,
261
  4096,
262
  0.0,
263
- 18,
264
  0.35,
265
  1.0,
266
  0.3,
@@ -282,7 +333,21 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
282
  4,
283
  1024,
284
  "Cosine"
285
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  [ "./examples/2.jpg",
287
  prompt.value,
288
  negative_prompt.value,
@@ -297,6 +362,20 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
297
  1024,
298
  "Cosine"
299
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  [ "./examples/3.jpg",
301
  prompt.value,
302
  negative_prompt.value,
@@ -311,6 +390,20 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
311
  1280,
312
  "Gaussian"
313
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  [ "./examples/4.jpg",
315
  prompt.value,
316
  negative_prompt.value,
@@ -325,6 +418,20 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
325
  1024,
326
  "Gaussian"
327
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  [ "./examples/5.jpg",
329
  prompt.value,
330
  negative_prompt.value,
@@ -338,7 +445,7 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
338
  4,
339
  1024,
340
  "Cosine"
341
- ],
342
  ],
343
  inputs=[
344
  input_image,
@@ -355,7 +462,7 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
355
  max_tile_size,
356
  tile_weighting_method,
357
  ],
358
- fn=predict,
359
  outputs=result,
360
  cache_examples=False,
361
  )
@@ -369,6 +476,7 @@ with gr.Blocks(css=css, theme=gr.themes.Ocean(), title="MoD ControlNet Tile Upsc
369
  ).then(
370
  fn=predict,
371
  inputs=[
 
372
  input_image,
373
  prompt,
374
  negative_prompt,
 
9
  create_hdr_effect,
10
  progressive_upscale,
11
  select_scheduler,
12
+ torch_gc,
13
  )
14
 
15
  device = "cuda"
16
+ pipe = None
17
+ last_loaded_model = None
18
 
19
  # Initialize the models and pipeline
20
  controlnet = ControlNetUnionModel.from_pretrained(
 
22
  ).to(device=device)
23
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device=device)
24
 
25
+ def load_model(model_id):
26
+ global pipe, last_loaded_model
27
+
28
+ if model_id != last_loaded_model:
29
+ pipe = None
30
+ torch_gc()
31
+
32
+ pipe = StableDiffusionXLControlNetTileSRPipeline.from_pretrained(
33
+ model_id, controlnet=controlnet, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
34
+ ).to(device)
35
+
36
+ #pipe.enable_model_cpu_offload() # << Enable this if you have limited VRAM
37
+ pipe.enable_vae_tiling() # << Enable this if you have limited VRAM
38
+ pipe.enable_vae_slicing() # << Enable this if you have limited VRAM
39
+ last_loaded_model = model_id
40
 
41
+ load_model("SG161222/RealVisXL_V5.0_Lightning")
 
 
42
 
43
  # region functions
44
  @spaces.GPU(duration=120)
45
  def predict(
46
+ model_id,
47
  image,
48
  prompt,
49
  negative_prompt,
 
61
  ):
62
  global pipe
63
 
64
+ # Load model if changed
65
+ load_model(model_id)
66
+
67
  # Set selected scheduler
68
  print(f"Using scheduler: {scheduler}...")
69
  pipe.scheduler = select_scheduler(pipe, scheduler)
 
112
 
113
  return image
114
 
 
115
  def clear_result():
116
  return gr.update(value=None)
117
 
 
125
  def select_tile_weighting_method(tile_weighting_method):
126
  return gr.update(visible=True if tile_weighting_method=="Gaussian" else False)
127
 
128
+ @spaces.GPU(duration=120)
129
+ def run_for_examples(image,
130
+ prompt,
131
+ negative_prompt,
132
+ resolution,
133
+ hdr,
134
+ num_inference_steps,
135
+ denoising_strenght,
136
+ controlnet_strength,
137
+ tile_gaussian_sigma,
138
+ scheduler,
139
+ guidance_scale,
140
+ max_tile_size,
141
+ tile_weighting_method):
142
+
143
+ predict(
144
+ model.value,
145
+ image,
146
+ prompt,
147
+ negative_prompt,
148
+ resolution,
149
+ hdr,
150
+ num_inference_steps,
151
+ denoising_strenght,
152
+ controlnet_strength,
153
+ tile_gaussian_sigma,
154
+ scheduler,
155
+ guidance_scale,
156
+ max_tile_size,
157
+ tile_weighting_method)
158
+
159
+
160
  # endregion
161
 
162
  css = """
 
253
  result = gr.Image(
254
  label="Generated Image", show_label=True, format="png", interactive=False, scale=1, height=500, min_width=670
255
  )
256
+ with gr.Row():
257
+ gr.HTML("<div style='color: red;'>If you are not a Pro account, run the LCM sampler examples on the RealVisXL_V5.0_Lightning model. For best results use the UniPC sampler and RealVisXL_V5.0 model examples.</div>")
258
  with gr.Row():
259
  with gr.Accordion("Input Prompt", open=False):
260
  with gr.Column():
 
279
  with gr.Sidebar(label="Parameters", open=True):
280
  with gr.Row(elem_id="parameters_row"):
281
  gr.Markdown("### General parameters")
282
+ model = gr.Dropdown(
283
+ label="Model", choices=["SG161222/RealVisXL_V5.0_Lightning", "SG161222/RealVisXL_V5.0"], value="SG161222/RealVisXL_V5.0_Lightning"
284
+ )
285
  tile_weighting_method = gr.Dropdown(
286
  label="Tile Weighting Method", choices=["Cosine", "Gaussian"], value="Cosine"
287
  )
 
311
  negative_prompt.value,
312
  4096,
313
  0.0,
314
+ 25,
315
  0.35,
316
  1.0,
317
  0.3,
 
333
  4,
334
  1024,
335
  "Cosine"
336
+ ],
337
+ [ "./examples/2.jpg",
338
+ prompt.value,
339
+ negative_prompt.value,
340
+ 4096,
341
+ 0.5,
342
+ 25,
343
+ 0.35,
344
+ 1.0,
345
+ 0.3,
346
+ "LCM",
347
+ 4,
348
+ 1024,
349
+ "Cosine"
350
+ ],
351
  [ "./examples/2.jpg",
352
  prompt.value,
353
  negative_prompt.value,
 
362
  1024,
363
  "Cosine"
364
  ],
365
+ [ "./examples/3.jpg",
366
+ prompt.value,
367
+ negative_prompt.value,
368
+ 5120,
369
+ 0.5,
370
+ 25,
371
+ 0.35,
372
+ 1.0,
373
+ 0.3,
374
+ "LCM",
375
+ 4,
376
+ 1280,
377
+ "Gaussian"
378
+ ],
379
  [ "./examples/3.jpg",
380
  prompt.value,
381
  negative_prompt.value,
 
390
  1280,
391
  "Gaussian"
392
  ],
393
+ [ "./examples/4.jpg",
394
+ prompt.value,
395
+ negative_prompt.value,
396
+ 8192,
397
+ 0.1,
398
+ 25,
399
+ 0.35,
400
+ 1.0,
401
+ 0.3,
402
+ "LCM",
403
+ 4,
404
+ 1024,
405
+ "Gaussian"
406
+ ],
407
  [ "./examples/4.jpg",
408
  prompt.value,
409
  negative_prompt.value,
 
418
  1024,
419
  "Gaussian"
420
  ],
421
+ [ "./examples/5.jpg",
422
+ prompt.value,
423
+ negative_prompt.value,
424
+ 8192,
425
+ 0.3,
426
+ 25,
427
+ 0.35,
428
+ 1.0,
429
+ 0.3,
430
+ "LCM",
431
+ 4,
432
+ 1024,
433
+ "Cosine"
434
+ ],
435
  [ "./examples/5.jpg",
436
  prompt.value,
437
  negative_prompt.value,
 
445
  4,
446
  1024,
447
  "Cosine"
448
+ ]
449
  ],
450
  inputs=[
451
  input_image,
 
462
  max_tile_size,
463
  tile_weighting_method,
464
  ],
465
+ fn=run_for_examples,
466
  outputs=result,
467
  cache_examples=False,
468
  )
 
476
  ).then(
477
  fn=predict,
478
  inputs=[
479
+ model,
480
  input_image,
481
  prompt,
482
  negative_prompt,