Updated Bria
Browse files
app.py
CHANGED
|
@@ -1,116 +1,157 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import base64
|
| 4 |
-
import requests
|
| 5 |
-
import numpy as np
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def
|
| 28 |
-
""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import requests
|
| 8 |
+
from torchvision import transforms
|
| 9 |
+
from diffusers import AutoencoderKL, LCMScheduler
|
| 10 |
+
from pipeline_controlnet_sd_xl import StableDiffusionXLControlNetPipeline
|
| 11 |
+
from controlnet import ControlNetModel
|
| 12 |
+
|
| 13 |
+
# -- Utility Functions --
|
| 14 |
+
def resize_image_to_retain_ratio(image: Image.Image) -> Image.Image:
|
| 15 |
+
pixel_number = 1024 * 1024
|
| 16 |
+
granularity = 8
|
| 17 |
+
ratio = image.width / image.height
|
| 18 |
+
width = int((pixel_number * ratio) ** 0.5)
|
| 19 |
+
width -= width % granularity
|
| 20 |
+
height = int(pixel_number / width)
|
| 21 |
+
height -= height % granularity
|
| 22 |
+
return image.resize((width, height))
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def prepare_mask(image: Image.Image, mask: Image.Image) -> Image.Image:
|
| 26 |
+
return mask.convert("L").resize(image.size)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def download_image(url: str) -> Image.Image:
|
| 30 |
+
resp = requests.get(url)
|
| 31 |
+
return Image.open(BytesIO(resp.content)).convert("RGB")
|
| 32 |
+
|
| 33 |
+
# -- Model & Pipeline Initialization --
|
| 34 |
+
|
| 35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
+
|
| 37 |
+
# Load ControlNet model
|
| 38 |
+
controlnet = (
|
| 39 |
+
ControlNetModel.from_pretrained(
|
| 40 |
+
"briaai/BRIA-2.3-ControlNet-Generative-Fill", torch_dtype=torch.float16
|
| 41 |
+
)
|
| 42 |
+
.to(device)
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Load VAE\ nvae = (
|
| 46 |
+
AutoencoderKL.from_pretrained(
|
| 47 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 48 |
+
)
|
| 49 |
+
.to(device)
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Load Stable Diffusion XL with ControlNet
|
| 53 |
+
pipe = (
|
| 54 |
+
StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 55 |
+
"briaai/BRIA-2.3",
|
| 56 |
+
controlnet=controlnet,
|
| 57 |
+
torch_dtype=torch.float16,
|
| 58 |
+
vae=vae,
|
| 59 |
+
)
|
| 60 |
+
.to(device)
|
| 61 |
+
)
|
| 62 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 63 |
+
pipe.load_lora_weights("briaai/BRIA-2.3-FAST-LORA")
|
| 64 |
+
pipe.fuse_lora()
|
| 65 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 66 |
+
|
| 67 |
+
# Tensor transform
|
| 68 |
+
to_tensor = transforms.ToTensor()
|
| 69 |
+
|
| 70 |
+
# -- Inference Function --
|
| 71 |
+
def generative_fill(
|
| 72 |
+
image: Image.Image,
|
| 73 |
+
mask: Image.Image,
|
| 74 |
+
prompt: str,
|
| 75 |
+
negative_prompt: str = "blurry",
|
| 76 |
+
num_inference_steps: int = 12,
|
| 77 |
+
controlnet_conditioning_scale: float = 1.0,
|
| 78 |
+
guidance_scale: float = 1.2,
|
| 79 |
+
seed: int = 123456,
|
| 80 |
+
) -> Image.Image:
|
| 81 |
+
# Preprocess image & mask
|
| 82 |
+
image = image.convert("RGB")
|
| 83 |
+
image = resize_image_to_retain_ratio(image)
|
| 84 |
+
width, height = image.size
|
| 85 |
+
mask = prepare_mask(image, mask)
|
| 86 |
+
|
| 87 |
+
# Create masked version
|
| 88 |
+
img_arr = np.array(image).astype(np.float32) / 255.0
|
| 89 |
+
mask_arr = (np.array(mask).astype(np.float32) / 255.0)
|
| 90 |
+
masked_arr = img_arr.copy()
|
| 91 |
+
masked_arr[mask_arr > 0.5] = 0.5
|
| 92 |
+
masked_pil = Image.fromarray((masked_arr * 255).astype(np.uint8))
|
| 93 |
+
|
| 94 |
+
# Encode latents
|
| 95 |
+
input_tensor = to_tensor(masked_pil)
|
| 96 |
+
input_tensor = (input_tensor - 0.5) / 0.5
|
| 97 |
+
input_tensor = input_tensor.unsqueeze(0).to(device)
|
| 98 |
+
latents = pipe.vae.encode(input_tensor[:, :3]).latent_dist.sample() * pipe.vae.config.scaling_factor
|
| 99 |
+
|
| 100 |
+
# Prepare mask tensor
|
| 101 |
+
mask_tensor = torch.tensor(mask_arr[None, None], dtype=torch.float32, device=device)
|
| 102 |
+
mask_resized = F.interpolate(mask_tensor, size=(latents.shape[2], latents.shape[3]), mode="nearest")
|
| 103 |
+
|
| 104 |
+
# Combine latents & mask for ControlNet
|
| 105 |
+
control_latents = latents
|
| 106 |
+
control_image = torch.cat([control_latents, mask_resized], dim=1)
|
| 107 |
+
|
| 108 |
+
# Generate
|
| 109 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 110 |
+
output = pipe(
|
| 111 |
+
prompt=prompt,
|
| 112 |
+
negative_prompt=negative_prompt,
|
| 113 |
+
num_inference_steps=num_inference_steps,
|
| 114 |
+
height=height,
|
| 115 |
+
width=width,
|
| 116 |
+
image=control_image,
|
| 117 |
+
init_image=image,
|
| 118 |
+
mask_image=mask_tensor,
|
| 119 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 120 |
+
guidance_scale=guidance_scale,
|
| 121 |
+
generator=generator,
|
| 122 |
+
).images[0]
|
| 123 |
+
return output
|
| 124 |
+
|
| 125 |
+
# -- Gradio Interface --
|
| 126 |
+
with gr.Blocks() as demo:
|
| 127 |
+
gr.Markdown("## BRIA 2.3 ControlNet Generative Fill")
|
| 128 |
with gr.Row():
|
| 129 |
+
inp_image = gr.Image(type="pil", label="Input Image")
|
| 130 |
+
inp_mask = gr.Image(type="pil", label="Mask (white = fill area)")
|
| 131 |
+
|
| 132 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Describe what to fill...")
|
| 133 |
+
neg_prompt_input = gr.Textbox(label="Negative Prompt", value="blurry")
|
| 134 |
+
steps = gr.Slider(1, 50, value=12, step=1, label="Inference Steps")
|
| 135 |
+
c_scale = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="ControlNet Scale")
|
| 136 |
+
g_scale = gr.Slider(0.0, 20.0, value=1.2, step=0.1, label="Guidance Scale")
|
| 137 |
+
seed_input = gr.Number(value=123456, label="Seed")
|
| 138 |
+
run_btn = gr.Button("Generate")
|
| 139 |
+
output_image = gr.Image(type="pil", label="Generated Image")
|
| 140 |
+
|
| 141 |
+
run_btn.click(
|
| 142 |
+
generative_fill,
|
| 143 |
+
inputs=[
|
| 144 |
+
inp_image,
|
| 145 |
+
inp_mask,
|
| 146 |
+
prompt_input,
|
| 147 |
+
neg_prompt_input,
|
| 148 |
+
steps,
|
| 149 |
+
c_scale,
|
| 150 |
+
g_scale,
|
| 151 |
+
seed_input,
|
| 152 |
+
],
|
| 153 |
+
outputs=output_image,
|
| 154 |
+
)
|
| 155 |
+
gr.Markdown("Model by BRIA AI | [Hugging Face](https://huggingface.co/briaai/BRIA-2.3-ControlNet-Generative-Fill)")
|
| 156 |
+
|
| 157 |
+
demo.launch(server_name="0.0.0.0", share=True)
|