Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,606 Bytes
95a9f0f 292ed4d 3638fca 7a86161 95a9f0f 3638fca 95a9f0f 3638fca 95a9f0f 3638fca 95a9f0f 3638fca 95a9f0f cf99e6e 95a9f0f efd6737 95a9f0f 6852b3e 95a9f0f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
from diffusers.utils import load_image
from PIL import Image
import torch
import numpy as np
import cv2
import gradio as gr
from torchvision import transforms
# from huggingface_hub import login
# login()
controlnet_conditioning_scale = 1.0
controlnet = ControlNetModel.from_pretrained(
"briaai/ControlNet-Canny",
torch_dtype=torch.float16
)
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
"briaai/BRIA-2.0",
controlnet=controlnet,
torch_dtype=torch.float16,
)
pipe.enable_model_cpu_offload()
low_threshold = 100
high_threshold = 200
def resize_image(image):
current_size = image.size
if current_size[0] > current_size[1]:
center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
else:
center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
return resized_image
def get_canny_filter(image):
if not isinstance(image, np.ndarray):
image = np.array(image)
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image)
return canny_image
def process(input_image, prompt):
# resize input_image to 1024x1024
input_image = resize_image(input_image)
canny_image = get_canny_filter(input_image)
images = pipe(
prompt,image=canny_image, controlnet_conditioning_scale=controlnet_conditioning_scale,
).images
return [canny_image,images[0]]
block = gr.Blocks().queue()
with block:
gr.Markdown("## BRIA 2.0 ControlNet Canny")
gr.HTML('''
<p style="margin-bottom: 10px; font-size: 94%">
This is a demo for BRIA 2.0 ControlNet Canny, a fully legal and safe T2I model.
</p>
''')
with gr.Row():
with gr.Column():
input_image = gr.Image(sources=None, type="numpy") # None for upload, ctrl+v and webcam
prompt = gr.Textbox(label="Prompt")
run_button = gr.Button(value="Run")
with gr.Column():
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
ips = [input_image, prompt]
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
block.launch(debug = True) |