dvir-bria commited on
Commit
02abfb5
1 Parent(s): 33349c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -111
app.py CHANGED
@@ -1,32 +1,32 @@
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='briaai/BRIA-2.2', 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
 
@@ -35,103 +35,103 @@ if __name__ == "__main__":
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)
 
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='briaai/BRIA-2.2', 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
 
 
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)