Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -14,17 +14,28 @@ import random
|
|
14 |
|
15 |
os.system("pip install -e ./controlnet_aux")
|
16 |
|
17 |
-
from controlnet_aux import OpenposeDetector
|
18 |
from depth_anything_v2.dpt import DepthAnythingV2
|
19 |
|
20 |
from huggingface_hub import hf_hub_download
|
21 |
|
22 |
from huggingface_hub import login
|
23 |
-
hf_token = os.environ.get("
|
24 |
login(token=hf_token)
|
25 |
|
26 |
MAX_SEED = np.iinfo(np.int32).max
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
@@ -38,6 +49,20 @@ model_configs = {
|
|
38 |
'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
|
39 |
}
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
encoder = 'vitl'
|
42 |
model = DepthAnythingV2(**model_configs[encoder])
|
43 |
filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-Large", filename=f"depth_anything_v2_vitl.pth", repo_type="model")
|
@@ -47,25 +72,35 @@ model = model.to(DEVICE).eval()
|
|
47 |
|
48 |
import torch
|
49 |
from diffusers.utils import load_image
|
50 |
-
from
|
51 |
-
from
|
|
|
52 |
|
53 |
-
base_model = '
|
54 |
-
controlnet_model = '
|
55 |
-
controlnet =
|
56 |
-
|
57 |
-
pipe =
|
58 |
-
pipe.to("cuda")
|
59 |
|
60 |
-
mode_mapping = {
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
open_pose = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
|
65 |
|
66 |
torch.backends.cuda.matmul.allow_tf32 = True
|
67 |
-
pipe.vae.enable_tiling()
|
68 |
-
pipe.vae.enable_slicing()
|
69 |
pipe.enable_model_cpu_offload() # for saving memory
|
70 |
|
71 |
def convert_from_image_to_cv2(img: Image) -> np.ndarray:
|
@@ -84,102 +119,69 @@ def extract_depth(image):
|
|
84 |
|
85 |
def extract_openpose(img):
|
86 |
processed_image_open_pose = open_pose(img, hand_and_face=True)
|
|
|
87 |
return processed_image_open_pose
|
88 |
|
89 |
-
def extract_canny(
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
def apply_gaussian_blur(image, kernel_size=(21, 21)):
|
94 |
-
image = convert_from_image_to_cv2(image)
|
95 |
-
blurred_image = convert_from_cv2_to_image(cv2.GaussianBlur(image, kernel_size, 0))
|
96 |
-
return blurred_image
|
97 |
|
98 |
def convert_to_grayscale(image):
|
99 |
-
|
100 |
-
gray_image = convert_from_cv2_to_image(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))
|
101 |
return gray_image
|
102 |
|
103 |
-
def
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
k = float(resolution) / min(H, W)
|
115 |
-
H *= k
|
116 |
-
W *= k
|
117 |
-
H = int(np.round(H / 64.0)) * 64
|
118 |
-
W = int(np.round(W / 64.0)) * 64
|
119 |
-
img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA)
|
120 |
-
img = convert_from_cv2_to_image(img)
|
121 |
-
return img
|
122 |
-
|
123 |
-
def resize_img(input_image, max_side=768, min_side=512, size=None,
|
124 |
-
pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
|
125 |
-
|
126 |
-
w, h = input_image.size
|
127 |
-
if size is not None:
|
128 |
-
w_resize_new, h_resize_new = size
|
129 |
-
else:
|
130 |
-
ratio = min_side / min(h, w)
|
131 |
-
w, h = round(ratio*w), round(ratio*h)
|
132 |
-
ratio = max_side / max(h, w)
|
133 |
-
input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
|
134 |
-
w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
|
135 |
-
h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
|
136 |
-
input_image = input_image.resize([w_resize_new, h_resize_new], mode)
|
137 |
-
|
138 |
-
if pad_to_max_side:
|
139 |
-
res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
|
140 |
-
offset_x = (max_side - w_resize_new) // 2
|
141 |
-
offset_y = (max_side - h_resize_new) // 2
|
142 |
-
res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
|
143 |
-
input_image = Image.fromarray(res)
|
144 |
-
return input_image
|
145 |
|
146 |
@spaces.GPU(duration=180)
|
147 |
-
def infer(
|
148 |
-
|
149 |
control_mode_num = mode_mapping[control_mode]
|
150 |
|
151 |
-
if
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
control_image = tile(image_in)
|
168 |
-
else:
|
169 |
-
control_image = resize_img(load_image(cond_in))
|
170 |
|
171 |
width, height = control_image.size
|
172 |
|
173 |
image = pipe(
|
174 |
prompt,
|
175 |
-
control_image=
|
176 |
-
control_mode=
|
177 |
width=width,
|
178 |
height=height,
|
179 |
-
controlnet_conditioning_scale=
|
180 |
num_inference_steps=inference_steps,
|
181 |
guidance_scale=guidance_scale,
|
182 |
generator=torch.manual_seed(seed),
|
|
|
|
|
183 |
).images[0]
|
184 |
|
185 |
torch.cuda.empty_cache()
|
@@ -196,9 +198,8 @@ css="""
|
|
196 |
with gr.Blocks(css=css) as demo:
|
197 |
with gr.Column(elem_id="col-container"):
|
198 |
gr.Markdown("""
|
199 |
-
#
|
200 |
-
A unified ControlNet for
|
201 |
-
The recommended strength: {"canny":0.65, "tile":0.45, "depth":0.55, "blur":0.45, "openpose":0.55, "gray":0.45, "low quality": 0.4}. Long prompt is preferred by FLUX.1.
|
202 |
""")
|
203 |
|
204 |
with gr.Column():
|
@@ -206,15 +207,15 @@ with gr.Blocks(css=css) as demo:
|
|
206 |
with gr.Row():
|
207 |
with gr.Column():
|
208 |
|
209 |
-
with gr.Row(equal_height=True):
|
210 |
-
cond_in = gr.Image(label="Upload a processed control image", sources=["upload"], type="filepath")
|
211 |
-
|
212 |
|
213 |
prompt = gr.Textbox(label="Prompt", value="best quality")
|
214 |
|
215 |
with gr.Accordion("Controlnet"):
|
216 |
control_mode = gr.Radio(
|
217 |
-
["
|
218 |
info="select the control mode, one for all"
|
219 |
)
|
220 |
|
@@ -223,7 +224,7 @@ with gr.Blocks(css=css) as demo:
|
|
223 |
minimum=0,
|
224 |
maximum=1.0,
|
225 |
step=0.05,
|
226 |
-
value=0.
|
227 |
)
|
228 |
|
229 |
seed = gr.Slider(
|
@@ -231,15 +232,15 @@ with gr.Blocks(css=css) as demo:
|
|
231 |
minimum=0,
|
232 |
maximum=MAX_SEED,
|
233 |
step=1,
|
234 |
-
value=
|
235 |
)
|
236 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
237 |
|
238 |
with gr.Accordion("Advanced settings", open=False):
|
239 |
with gr.Column():
|
240 |
with gr.Row():
|
241 |
-
inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=
|
242 |
-
guidance_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=10.0, step=0.1, value=
|
243 |
|
244 |
submit_btn = gr.Button("Submit")
|
245 |
|
@@ -255,7 +256,7 @@ with gr.Blocks(css=css) as demo:
|
|
255 |
api_name=False
|
256 |
).then(
|
257 |
fn = infer,
|
258 |
-
inputs = [
|
259 |
outputs = [result, processed_cond],
|
260 |
show_api=False
|
261 |
)
|
|
|
14 |
|
15 |
os.system("pip install -e ./controlnet_aux")
|
16 |
|
17 |
+
from controlnet_aux import OpenposeDetector #, CannyDetector
|
18 |
from depth_anything_v2.dpt import DepthAnythingV2
|
19 |
|
20 |
from huggingface_hub import hf_hub_download
|
21 |
|
22 |
from huggingface_hub import login
|
23 |
+
hf_token = os.environ.get("HF_TOKEN")
|
24 |
login(token=hf_token)
|
25 |
|
26 |
MAX_SEED = np.iinfo(np.int32).max
|
27 |
|
28 |
+
try:
|
29 |
+
local_dir = os.path.dirname(__file__)
|
30 |
+
except:
|
31 |
+
local_dir = '.'
|
32 |
+
|
33 |
+
hf_hub_download(repo_id="briaai/BRIA-3.2", filename='pipeline_bria.py', local_dir=local_dir)
|
34 |
+
hf_hub_download(repo_id="briaai/BRIA-3.2", filename='transformer_bria.py', local_dir=local_dir)
|
35 |
+
hf_hub_download(repo_id="briaai/BRIA-3.2", filename='bria_utils.py', local_dir=local_dir)
|
36 |
+
hf_hub_download(repo_id="briaai/BRIA-3.2-ControlNet-Union", filename='pipeline_bria_controlnet.py', local_dir=local_dir)
|
37 |
+
hf_hub_download(repo_id="briaai/BRIA-3.2-ControlNet-Union", filename='controlnet_bria.py', local_dir=local_dir)
|
38 |
+
|
39 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
40 |
if randomize_seed:
|
41 |
seed = random.randint(0, MAX_SEED)
|
|
|
49 |
'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
|
50 |
}
|
51 |
|
52 |
+
RATIO_CONFIGS_1024 = {
|
53 |
+
0.6666666666666666: {"width": 832, "height": 1248},
|
54 |
+
0.7432432432432432: {"width": 880, "height": 1184},
|
55 |
+
0.8028169014084507: {"width": 912, "height": 1136},
|
56 |
+
1.0: {"width": 1024, "height": 1024},
|
57 |
+
1.2456140350877194: {"width": 1136, "height": 912},
|
58 |
+
1.3454545454545455: {"width": 1184, "height": 880},
|
59 |
+
1.4339622641509433: {"width": 1216, "height": 848},
|
60 |
+
1.5: {"width": 1248, "height": 832},
|
61 |
+
1.5490196078431373: {"width": 1264, "height": 816},
|
62 |
+
1.62: {"width": 1296, "height": 800},
|
63 |
+
1.7708333333333333: {"width": 1360, "height": 768},
|
64 |
+
}
|
65 |
+
|
66 |
encoder = 'vitl'
|
67 |
model = DepthAnythingV2(**model_configs[encoder])
|
68 |
filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-Large", filename=f"depth_anything_v2_vitl.pth", repo_type="model")
|
|
|
72 |
|
73 |
import torch
|
74 |
from diffusers.utils import load_image
|
75 |
+
from controlnet_bria import BriaControlNetModel, BriaMultiControlNetModel
|
76 |
+
from pipeline_bria_controlnet import BriaControlNetPipeline
|
77 |
+
import PIL.Image as Image
|
78 |
|
79 |
+
base_model = 'briaai/BRIA-3.2'
|
80 |
+
controlnet_model = 'briaai/BRIA-3.2-ControlNet-Union'
|
81 |
+
controlnet = BriaControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
|
82 |
+
pipe = BriaControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16, trust_remote_code=True)
|
83 |
+
pipe = pipe.to(device="cuda", dtype=torch.bfloat16)
|
|
|
84 |
|
85 |
+
mode_mapping = {
|
86 |
+
"depth": 0,
|
87 |
+
"canny": 1,
|
88 |
+
"colorgrid": 2,
|
89 |
+
"recolor": 3,
|
90 |
+
"tile": 4,
|
91 |
+
"pose": 5,
|
92 |
+
}
|
93 |
+
strength_mapping = {
|
94 |
+
"depth": 1.0,
|
95 |
+
"canny": 1.0,
|
96 |
+
"colorgrid": 1.0,
|
97 |
+
"recolor": 1.0,
|
98 |
+
"tile": 1.0,
|
99 |
+
"pose": 1.0,
|
100 |
+
}
|
101 |
open_pose = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
|
102 |
|
103 |
torch.backends.cuda.matmul.allow_tf32 = True
|
|
|
|
|
104 |
pipe.enable_model_cpu_offload() # for saving memory
|
105 |
|
106 |
def convert_from_image_to_cv2(img: Image) -> np.ndarray:
|
|
|
119 |
|
120 |
def extract_openpose(img):
|
121 |
processed_image_open_pose = open_pose(img, hand_and_face=True)
|
122 |
+
processed_image_open_pose = processed_image_open_pose.resize(img.size)
|
123 |
return processed_image_open_pose
|
124 |
|
125 |
+
def extract_canny(input_image):
|
126 |
+
image = np.array(input_image)
|
127 |
+
image = cv2.Canny(image, 100, 200)
|
128 |
+
image = image[:, :, None]
|
129 |
+
image = np.concatenate([image, image, image], axis=2)
|
130 |
+
canny_image = Image.fromarray(image)
|
131 |
+
return canny_image
|
132 |
|
|
|
|
|
|
|
|
|
133 |
|
134 |
def convert_to_grayscale(image):
|
135 |
+
gray_image = image.convert('L').convert('RGB')
|
|
|
136 |
return gray_image
|
137 |
|
138 |
+
def tile(downscale_factor, input_image):
|
139 |
+
control_image = input_image.resize((input_image.size[0] // downscale_factor, input_image.size[1] // downscale_factor)).resize(input_image.size, Image.NEAREST)
|
140 |
+
return control_image
|
141 |
+
|
142 |
+
def resize_img(control_image):
|
143 |
+
image_ratio = control_image.width / control_image.height
|
144 |
+
ratio = min(RATIO_CONFIGS_1024.keys(), key=lambda k: abs(k - image_ratio))
|
145 |
+
to_height = RATIO_CONFIGS_1024[ratio]["height"]
|
146 |
+
to_width = RATIO_CONFIGS_1024[ratio]["width"]
|
147 |
+
resized_image = control_image.resize((to_width, to_height), resample=Image.Resampling.LANCZOS)
|
148 |
+
return resized_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
@spaces.GPU(duration=180)
|
151 |
+
def infer(image_in, prompt, inference_steps, guidance_scale, control_mode, control_strength, seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
152 |
control_mode_num = mode_mapping[control_mode]
|
153 |
|
154 |
+
if image_in is not None:
|
155 |
+
image_in = resize_img(load_image(image_in))
|
156 |
+
if control_mode == "canny":
|
157 |
+
control_image = extract_canny(image_in)
|
158 |
+
elif control_mode == "depth":
|
159 |
+
control_image = extract_depth(image_in)
|
160 |
+
elif control_mode == "pose":
|
161 |
+
control_image = extract_openpose(image_in)
|
162 |
+
elif control_mode == "colorgrid":
|
163 |
+
control_image = tile(64, image_in)
|
164 |
+
elif control_mode == "recolor":
|
165 |
+
control_image = convert_to_grayscale(image_in)
|
166 |
+
elif control_mode == "tile":
|
167 |
+
control_image = tile(16, image_in)
|
168 |
+
|
169 |
+
control_image = resize_img(control_image)
|
|
|
|
|
|
|
170 |
|
171 |
width, height = control_image.size
|
172 |
|
173 |
image = pipe(
|
174 |
prompt,
|
175 |
+
control_image=control_image,
|
176 |
+
control_mode=control_mode_num,
|
177 |
width=width,
|
178 |
height=height,
|
179 |
+
controlnet_conditioning_scale=control_strength,
|
180 |
num_inference_steps=inference_steps,
|
181 |
guidance_scale=guidance_scale,
|
182 |
generator=torch.manual_seed(seed),
|
183 |
+
max_sequence_length=128,
|
184 |
+
negative_prompt="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate"
|
185 |
).images[0]
|
186 |
|
187 |
torch.cuda.empty_cache()
|
|
|
198 |
with gr.Blocks(css=css) as demo:
|
199 |
with gr.Column(elem_id="col-container"):
|
200 |
gr.Markdown("""
|
201 |
+
# BRIA-3.2-ControlNet-Union
|
202 |
+
A unified ControlNet for BRIA-3.2 model from Bria.ai.<br />
|
|
|
203 |
""")
|
204 |
|
205 |
with gr.Column():
|
|
|
207 |
with gr.Row():
|
208 |
with gr.Column():
|
209 |
|
210 |
+
# with gr.Row(equal_height=True):
|
211 |
+
# cond_in = gr.Image(label="Upload a processed control image", sources=["upload"], type="filepath")
|
212 |
+
image_in = gr.Image(label="Extract condition from a reference image (Optional)", sources=["upload"], type="filepath")
|
213 |
|
214 |
prompt = gr.Textbox(label="Prompt", value="best quality")
|
215 |
|
216 |
with gr.Accordion("Controlnet"):
|
217 |
control_mode = gr.Radio(
|
218 |
+
["depth", "canny", "colorgrid", "recolor", "tile", "pose"], label="Mode", value="canny",
|
219 |
info="select the control mode, one for all"
|
220 |
)
|
221 |
|
|
|
224 |
minimum=0,
|
225 |
maximum=1.0,
|
226 |
step=0.05,
|
227 |
+
value=0.9,
|
228 |
)
|
229 |
|
230 |
seed = gr.Slider(
|
|
|
232 |
minimum=0,
|
233 |
maximum=MAX_SEED,
|
234 |
step=1,
|
235 |
+
value=555,
|
236 |
)
|
237 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
238 |
|
239 |
with gr.Accordion("Advanced settings", open=False):
|
240 |
with gr.Column():
|
241 |
with gr.Row():
|
242 |
+
inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=50)
|
243 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=10.0, step=0.1, value=5.0)
|
244 |
|
245 |
submit_btn = gr.Button("Submit")
|
246 |
|
|
|
256 |
api_name=False
|
257 |
).then(
|
258 |
fn = infer,
|
259 |
+
inputs = [image_in, prompt, inference_steps, guidance_scale, control_mode, control_strength, seed],
|
260 |
outputs = [result, processed_cond],
|
261 |
show_api=False
|
262 |
)
|