mokady commited on
Commit
dc9ce74
·
verified ·
1 Parent(s): 1abdec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import spaces
2
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
3
- #from diffusers.utils import load_image
4
  from PIL import Image
5
  import torch
6
  import numpy as np
@@ -17,10 +16,10 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
17
  "briaai/BRIA-2.2",
18
  controlnet=controlnet,
19
  torch_dtype=torch.float16,
20
- #device_map='auto',
21
  low_cpu_mem_usage=True,
22
  offload_state_dict=True,
23
  ).to('cuda').to(torch.float16)
 
24
  pipe.scheduler = EulerAncestralDiscreteScheduler(
25
  beta_start=0.00085,
26
  beta_end=0.012,
@@ -28,8 +27,6 @@ pipe.scheduler = EulerAncestralDiscreteScheduler(
28
  num_train_timesteps=1000,
29
  steps_offset=1
30
  )
31
- # pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
32
- # pipe.enable_xformers_memory_efficient_attention()
33
  pipe.force_zeros_for_empty_prompt = False
34
 
35
  def resize_image(image):
@@ -42,52 +39,50 @@ def resize_image(image):
42
  resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
43
  return resized_image
44
 
45
-
46
  @spaces.GPU
47
  def generate_(prompt, negative_prompt, grayscale_image, num_steps, controlnet_conditioning_scale, seed):
48
  generator = torch.Generator("cuda").manual_seed(seed)
49
  images = pipe(
50
- prompt, negative_prompt=negative_prompt, image=grayscale_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
51
- generator=generator,
 
 
 
 
52
  ).images
53
  return images
54
 
55
  @spaces.GPU
56
  def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
57
- # resize input_image to 1024x1024
58
  input_image = resize_image(input_image)
59
-
60
  grayscale_image = input_image.convert('L').convert('RGB')
61
  images = generate_(prompt, negative_prompt, grayscale_image, num_steps, controlnet_conditioning_scale, seed)
 
62
 
63
- # Return both grayscale and output images in a list for the gallery
64
- return [grayscale_image, images[0]]
65
-
66
  block = gr.Blocks().queue()
67
 
68
  with block:
69
  gr.Markdown("## BRIA 2.2 ControlNet Recoloring")
70
  gr.HTML('''
71
  <p style="margin-bottom: 10px; font-size: 94%">
72
- This is a demo for ControlNet Recoloring that using
73
  <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
74
- Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
75
  </p>
76
  ''')
77
  with gr.Row():
78
  with gr.Column():
79
- input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
80
  prompt = gr.Textbox(label="Prompt")
81
  negative_prompt = gr.Textbox(label="Negative prompt", value="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers")
82
  num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
83
  controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
84
- seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
85
  run_button = gr.Button(value="Run")
86
-
87
-
88
  with gr.Column():
89
- result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
 
90
  ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
91
- run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
92
 
93
- block.launch(debug = True)
 
1
  import spaces
2
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
 
3
  from PIL import Image
4
  import torch
5
  import numpy as np
 
16
  "briaai/BRIA-2.2",
17
  controlnet=controlnet,
18
  torch_dtype=torch.float16,
 
19
  low_cpu_mem_usage=True,
20
  offload_state_dict=True,
21
  ).to('cuda').to(torch.float16)
22
+
23
  pipe.scheduler = EulerAncestralDiscreteScheduler(
24
  beta_start=0.00085,
25
  beta_end=0.012,
 
27
  num_train_timesteps=1000,
28
  steps_offset=1
29
  )
 
 
30
  pipe.force_zeros_for_empty_prompt = False
31
 
32
  def resize_image(image):
 
39
  resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
40
  return resized_image
41
 
 
42
  @spaces.GPU
43
  def generate_(prompt, negative_prompt, grayscale_image, num_steps, controlnet_conditioning_scale, seed):
44
  generator = torch.Generator("cuda").manual_seed(seed)
45
  images = pipe(
46
+ prompt,
47
+ negative_prompt=negative_prompt,
48
+ image=grayscale_image,
49
+ num_inference_steps=num_steps,
50
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
51
+ generator=generator,
52
  ).images
53
  return images
54
 
55
  @spaces.GPU
56
  def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
 
57
  input_image = resize_image(input_image)
 
58
  grayscale_image = input_image.convert('L').convert('RGB')
59
  images = generate_(prompt, negative_prompt, grayscale_image, num_steps, controlnet_conditioning_scale, seed)
60
+ return grayscale_image, images[0]
61
 
 
 
 
62
  block = gr.Blocks().queue()
63
 
64
  with block:
65
  gr.Markdown("## BRIA 2.2 ControlNet Recoloring")
66
  gr.HTML('''
67
  <p style="margin-bottom: 10px; font-size: 94%">
68
+ This is a demo for ControlNet Recoloring that uses
69
  <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
70
+ Trained on licensed data, BRIA 2.2 provides full legal liability coverage for copyright and privacy infringement.
71
  </p>
72
  ''')
73
  with gr.Row():
74
  with gr.Column():
75
+ input_image = gr.Image(sources=None, type="pil")
76
  prompt = gr.Textbox(label="Prompt")
77
  negative_prompt = gr.Textbox(label="Negative prompt", value="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers")
78
  num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
79
  controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
80
+ seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True)
81
  run_button = gr.Button(value="Run")
 
 
82
  with gr.Column():
83
+ preview = gr.Image(label="Control (Grayscale)", type="pil")
84
+ result = gr.Image(label="Output Image", type="pil")
85
  ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
86
+ run_button.click(fn=process, inputs=ips, outputs=[preview, result])
87
 
88
+ block.launch(debug=True)