dvir-bria commited on
Commit
0842498
1 Parent(s): 7765fbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -89
app.py CHANGED
@@ -1,100 +1,137 @@
1
- from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
2
- from diffusers.utils import load_image
3
- from PIL import Image
4
- import torch
5
- import numpy as np
6
- import cv2
7
  import gradio as gr
8
- from torchvision import transforms
9
-
10
- controlnet = ControlNetModel.from_pretrained(
11
- "briaai/BRIA-2.2-ControlNet-Canny",
12
- torch_dtype=torch.float16
13
- ).to('cuda')
14
-
15
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
16
- "briaai/BRIA-2.2",
17
- controlnet=controlnet,
18
- torch_dtype=torch.float16,
19
- device_map='auto',
20
- low_cpu_mem_usage=True,
21
- offload_state_dict=True,
22
- ).to('cuda')
23
- pipe.scheduler = EulerAncestralDiscreteScheduler(
24
- beta_start=0.00085,
25
- beta_end=0.012,
26
- beta_schedule="scaled_linear",
27
- num_train_timesteps=1000,
28
- steps_offset=1
29
- )
30
- # pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
31
- pipe.enable_xformers_memory_efficient_attention()
32
- pipe.force_zeros_for_empty_prompt = False
33
-
34
- low_threshold = 100
35
- high_threshold = 200
36
-
37
- def resize_image(image):
38
- image = image.convert('RGB')
39
- current_size = image.size
40
- if current_size[0] > current_size[1]:
41
- center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
42
- else:
43
- center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
44
- resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
45
- return resized_image
46
-
47
- def get_canny_filter(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- if not isinstance(image, np.ndarray):
50
- image = np.array(image)
51
 
52
- image = cv2.Canny(image, low_threshold, high_threshold)
53
- image = image[:, :, None]
54
- image = np.concatenate([image, image, image], axis=2)
55
- canny_image = Image.fromarray(image)
56
- return canny_image
57
-
58
- def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
59
- generator = torch.manual_seed(seed)
60
 
61
- # resize input_image to 1024x1024
62
- input_image = resize_image(input_image)
63
 
64
- canny_image = get_canny_filter(input_image)
65
 
66
- images = pipe(
67
- prompt, negative_prompt=negative_prompt, image=canny_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
68
- generator=generator,
69
- ).images
70
 
71
- return [canny_image,images[0]]
72
 
73
- block = gr.Blocks().queue()
74
-
75
- with block:
76
- gr.Markdown("## BRIA 2.2 ControlNet Canny")
77
- gr.HTML('''
78
- <p style="margin-bottom: 10px; font-size: 94%">
79
- This is a demo for ControlNet Canny that using
80
- <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
81
- Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
82
- </p>
83
- ''')
84
- with gr.Row():
85
- with gr.Column():
86
- input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
87
- prompt = gr.Textbox(label="Prompt")
88
- 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")
89
- num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
90
- controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
91
- seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
92
- run_button = gr.Button(value="Run")
93
 
94
 
95
- with gr.Column():
96
- result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
97
- ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
98
- run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
99
 
100
- block.launch(debug = True)
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
 
 
5
  import gradio as gr
6
+ import torch
7
+
8
+ from app_canny import create_demo as create_demo_canny
9
+ # from app_depth import create_demo as create_demo_depth
10
+ # from app_recoloring import create_demo as create_demo_recoloring
11
+ from model import Model
12
+
13
+ DESCRIPTION = "# BRIA 2.2 ControlNets"
14
+
15
+ model = Model(base_model_id=DEFAULT_MODEL_ID, task_name="Canny")
16
+
17
+ with gr.Blocks(css="style.css") as demo:
18
+ gr.Markdown(DESCRIPTION)
19
+
20
+ with gr.Tabs():
21
+ with gr.TabItem("Canny"):
22
+ create_demo_canny(model.process_canny)
23
+ with gr.TabItem("Depth (Future)"):
24
+ create_demo_canny(model.process_mlsd)
25
+ with gr.TabItem("Recoloring (Future)"):
26
+ create_demo_canny(model.process_scribble)
27
+
28
+ if __name__ == "__main__":
29
+ demo.queue(max_size=20).launch()
30
+
31
+
32
+
33
+
34
+ ---------------------------------------------------------------
35
+
36
+
37
+
38
+ # from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
39
+ # from diffusers.utils import load_image
40
+ # from PIL import Image
41
+ # import torch
42
+ # import numpy as np
43
+ # import cv2
44
+ # import gradio as gr
45
+ # from torchvision import transforms
46
+
47
+ # controlnet = ControlNetModel.from_pretrained(
48
+ # "briaai/BRIA-2.2-ControlNet-Canny",
49
+ # torch_dtype=torch.float16
50
+ # ).to('cuda')
51
+
52
+ # pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
53
+ # "briaai/BRIA-2.2",
54
+ # controlnet=controlnet,
55
+ # torch_dtype=torch.float16,
56
+ # device_map='auto',
57
+ # low_cpu_mem_usage=True,
58
+ # offload_state_dict=True,
59
+ # ).to('cuda')
60
+ # pipe.scheduler = EulerAncestralDiscreteScheduler(
61
+ # beta_start=0.00085,
62
+ # beta_end=0.012,
63
+ # beta_schedule="scaled_linear",
64
+ # num_train_timesteps=1000,
65
+ # steps_offset=1
66
+ # )
67
+ # # pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
68
+ # pipe.enable_xformers_memory_efficient_attention()
69
+ # pipe.force_zeros_for_empty_prompt = False
70
+
71
+ # low_threshold = 100
72
+ # high_threshold = 200
73
+
74
+ # def resize_image(image):
75
+ # image = image.convert('RGB')
76
+ # current_size = image.size
77
+ # if current_size[0] > current_size[1]:
78
+ # center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
79
+ # else:
80
+ # center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
81
+ # resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
82
+ # return resized_image
83
+
84
+ # def get_canny_filter(image):
85
 
86
+ # if not isinstance(image, np.ndarray):
87
+ # image = np.array(image)
88
 
89
+ # image = cv2.Canny(image, low_threshold, high_threshold)
90
+ # image = image[:, :, None]
91
+ # image = np.concatenate([image, image, image], axis=2)
92
+ # canny_image = Image.fromarray(image)
93
+ # return canny_image
94
+
95
+ # def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
96
+ # generator = torch.manual_seed(seed)
97
 
98
+ # # resize input_image to 1024x1024
99
+ # input_image = resize_image(input_image)
100
 
101
+ # canny_image = get_canny_filter(input_image)
102
 
103
+ # images = pipe(
104
+ # prompt, negative_prompt=negative_prompt, image=canny_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
105
+ # generator=generator,
106
+ # ).images
107
 
108
+ # return [canny_image,images[0]]
109
 
110
+ # block = gr.Blocks().queue()
111
+
112
+ # with block:
113
+ # gr.Markdown("## BRIA 2.2 ControlNet Canny")
114
+ # gr.HTML('''
115
+ # <p style="margin-bottom: 10px; font-size: 94%">
116
+ # This is a demo for ControlNet Canny that using
117
+ # <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
118
+ # Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
119
+ # </p>
120
+ # ''')
121
+ # with gr.Row():
122
+ # with gr.Column():
123
+ # input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
124
+ # prompt = gr.Textbox(label="Prompt")
125
+ # 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")
126
+ # num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
127
+ # controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
128
+ # seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
129
+ # run_button = gr.Button(value="Run")
130
 
131
 
132
+ # with gr.Column():
133
+ # result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
134
+ # ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
135
+ # run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
136
 
137
+ # block.launch(debug = True)