Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,106 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
-
# ── Monkey‐patch missing torchvision module ──
|
| 4 |
-
import sys
|
| 5 |
-
import torchvision.transforms.functional as F
|
| 6 |
-
sys.modules['torchvision.transforms.functional_tensor'] = F
|
| 7 |
-
|
| 8 |
-
import os
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 10 |
import torch
|
| 11 |
import numpy as np
|
| 12 |
-
import cv2
|
| 13 |
from PIL import Image
|
| 14 |
-
from
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
device = torch.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
def fill_and_upscale(input_img: Image.Image,
|
| 45 |
-
mask_img: Image.Image,
|
| 46 |
-
prompt: str):
|
| 47 |
-
# Inpaint
|
| 48 |
-
init = input_img.convert("RGB")
|
| 49 |
-
mask = mask_img.convert("RGB")
|
| 50 |
-
filled: Image.Image = pipe(
|
| 51 |
-
prompt=prompt, image=init, mask_image=mask
|
| 52 |
-
).images[0]
|
| 53 |
-
|
| 54 |
-
# Prepare for Real-ESRGANer (expects BGR numpy)
|
| 55 |
-
arr = np.array(filled)
|
| 56 |
-
bgr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
out_rgb = cv2.cvtColor(out_bgr, cv2.COLOR_BGR2RGB)
|
| 61 |
-
upscaled = Image.fromarray(out_rgb)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("##
|
| 68 |
with gr.Row():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import AutoencoderKL, LCMScheduler
|
| 3 |
+
from pipeline_controlnet_sd_xl import StableDiffusionXLControlNetPipeline
|
| 4 |
+
from controlnet import ControlNetModel
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
import requests
|
| 11 |
|
| 12 |
+
# Utility functions
|
| 13 |
+
def resize_image_to_retain_ratio(image):
|
| 14 |
+
pixel_number = 1024 * 1024
|
| 15 |
+
granularity_val = 8
|
| 16 |
+
ratio = image.width / image.height
|
| 17 |
+
width = int((pixel_number * ratio) ** 0.5)
|
| 18 |
+
width -= width % granularity_val
|
| 19 |
+
height = int(pixel_number / width)
|
| 20 |
+
height -= height % granularity_val
|
| 21 |
+
return image.resize((width, height))
|
| 22 |
|
| 23 |
+
def get_masked_image(image, mask):
|
| 24 |
+
image = np.array(image).astype(np.float32) / 255.0
|
| 25 |
+
mask = np.array(mask.convert("L")).astype(np.float32) / 255.0
|
| 26 |
+
masked_vis = image.copy()
|
| 27 |
+
image[mask > 0.5] = 0.5
|
| 28 |
+
masked_vis[mask > 0.5] = 0.5
|
| 29 |
+
return (Image.fromarray((image * 255).astype(np.uint8)),
|
| 30 |
+
Image.fromarray((masked_vis * 255).astype(np.uint8)),
|
| 31 |
+
mask)
|
| 32 |
|
| 33 |
+
# Load model once
|
| 34 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 36 |
+
"briaai/BRIA-2.3-ControlNet-Generative-Fill", torch_dtype=torch.float16
|
| 37 |
+
).to(device)
|
| 38 |
+
vae = AutoencoderKL.from_pretrained(
|
| 39 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 40 |
)
|
| 41 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 42 |
+
"briaai/BRIA-2.3",
|
| 43 |
+
controlnet=controlnet,
|
| 44 |
+
vae=vae,
|
| 45 |
+
torch_dtype=torch.float16
|
| 46 |
+
).to(device)
|
| 47 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 48 |
+
pipe.load_lora_weights("briaai/BRIA-2.3-FAST-LORA")
|
| 49 |
+
pipe.fuse_lora()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Image transforms
|
| 52 |
+
image_transforms = transforms.Compose([transforms.ToTensor()])
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def inference(init_img, mask_img, prompt, neg_prompt,
|
| 55 |
+
steps, guidance, control_scale, seed):
|
| 56 |
+
# Resize and prepare
|
| 57 |
+
init_img = resize_image_to_retain_ratio(init_img)
|
| 58 |
+
masked_img, vis_img, mask_arr = get_masked_image(init_img, mask_img)
|
| 59 |
+
|
| 60 |
+
# Encode masked image
|
| 61 |
+
tensor = image_transforms(masked_img).unsqueeze(0).to(device)
|
| 62 |
+
latents = vae.encode(tensor.to(vae.dtype)).latent_dist.sample() * vae.config.scaling_factor
|
| 63 |
+
|
| 64 |
+
# Prepare mask tensor
|
| 65 |
+
mask_t = torch.tensor(mask_arr)[None, None, ...].to(device)
|
| 66 |
+
mask_resized = torch.nn.functional.interpolate(mask_t, size=(latents.shape[2], latents.shape[3]), mode='nearest')
|
| 67 |
+
|
| 68 |
+
# Control image
|
| 69 |
+
control = torch.cat([latents, mask_resized], dim=1)
|
| 70 |
+
|
| 71 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 72 |
+
output = pipe(
|
| 73 |
+
prompt=prompt,
|
| 74 |
+
negative_prompt=neg_prompt,
|
| 75 |
+
controlnet_conditioning_scale=control_scale,
|
| 76 |
+
num_inference_steps=steps,
|
| 77 |
+
guidance_scale=guidance,
|
| 78 |
+
image=control,
|
| 79 |
+
init_image=init_img,
|
| 80 |
+
mask_image=mask_t[:, 0],
|
| 81 |
+
generator=generator,
|
| 82 |
+
height=init_img.height,
|
| 83 |
+
width=init_img.width,
|
| 84 |
+
).images[0]
|
| 85 |
+
return output
|
| 86 |
|
| 87 |
+
# Build Gradio interface
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("## BRIA-2.3 ControlNet Inpainting Demo")
|
| 90 |
with gr.Row():
|
| 91 |
+
inp = gr.Image(source="upload", type="pil", label="Input Image")
|
| 92 |
+
msk = gr.Image(source="upload", type="pil", label="Mask Image")
|
| 93 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe the desired content")
|
| 94 |
+
neg = gr.Textbox(label="Negative Prompt", value="blurry")
|
| 95 |
+
steps = gr.Slider(1, 50, value=12, step=1, label="Inference Steps")
|
| 96 |
+
guidance = gr.Slider(0.0, 10.0, value=1.2, step=0.1, label="Guidance Scale")
|
| 97 |
+
scale = gr.Slider(0.0, 5.0, value=1.0, step=0.1, label="ControlNet Scale")
|
| 98 |
+
seed = gr.Number(label="Seed", value=123456)
|
| 99 |
+
btn = gr.Button("Generate")
|
| 100 |
+
out = gr.Image(type="pil", label="Output")
|
| 101 |
+
btn.click(
|
| 102 |
+
fn=inference,
|
| 103 |
+
inputs=[inp, msk, prompt, neg, steps, guidance, scale, seed],
|
| 104 |
+
outputs=out,
|
| 105 |
+
)
|
| 106 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|