dvir-bria commited on
Commit
91efa54
1 Parent(s): 148a299

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-Recoloring",
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
+ from transformers import DPTFeatureExtractor, DPTForDepthEstimation
35
+ depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
36
+ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
37
+
38
+ def resize_image(image):
39
+ image = image.convert('RGB')
40
+ current_size = image.size
41
+ if current_size[0] > current_size[1]:
42
+ center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
43
+ else:
44
+ center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
45
+ resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
46
+ return resized_image
47
+
48
+
49
+ def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
50
+ generator = torch.manual_seed(seed)
51
+
52
+ # resize input_image to 1024x1024
53
+ input_image = resize_image(input_image)
54
+
55
+ grayscale_image = input_image.convert('L').convert('RGB')
56
+
57
+ images = pipe(
58
+ prompt, negative_prompt=negative_prompt, image=grayscale_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
59
+ generator=generator,
60
+ ).images
61
+
62
+ return [grayscale_image, images[0]]
63
+
64
+ block = gr.Blocks().queue()
65
+
66
+ with block:
67
+ gr.Markdown("## BRIA 2.2 ControlNet Depth")
68
+ gr.HTML('''
69
+ <p style="margin-bottom: 10px; font-size: 94%">
70
+ This is a demo for ControlNet Depth that using
71
+ <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
72
+ Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
73
+ </p>
74
+ ''')
75
+ with gr.Row():
76
+ with gr.Column():
77
+ input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
78
+ prompt = gr.Textbox(label="Prompt")
79
+ 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")
80
+ num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
81
+ controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
82
+ seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
83
+ run_button = gr.Button(value="Run")
84
+
85
+
86
+ with gr.Column():
87
+ result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
88
+ ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
89
+ run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
90
+
91
+ block.launch(debug = True)